1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 | //
|
---|
3 | // $Id: simplesort.h,v 1.1.1.1 1999-04-09 17:57:58 ansari Exp $
|
---|
4 | //
|
---|
5 |
|
---|
6 | // Un tri tout bete sur des reels. On donne un tableau de reels,
|
---|
7 | // et on recupere un index trie, et on peut faire des recherches.
|
---|
8 |
|
---|
9 | #ifndef SIMPLESORT_SEEN
|
---|
10 | #define SIMPLESORT_SEEN
|
---|
11 |
|
---|
12 | #include "peida.h"
|
---|
13 |
|
---|
14 | class SimpleSort EXC_AWARE {
|
---|
15 | public:
|
---|
16 | SimpleSort(int nElts);
|
---|
17 | // On donne au constructeur le nombre d'elements a trier.
|
---|
18 | virtual ~SimpleSort();
|
---|
19 |
|
---|
20 | void Set(int i, float value);
|
---|
21 | // Donne la valeur de l'element i
|
---|
22 |
|
---|
23 | void Sort();
|
---|
24 | // Trie. A faire avant d'appeler la suite.
|
---|
25 |
|
---|
26 | float Value(int i) const {return index[i].value;}
|
---|
27 | // La valeur numero i une fois trie.
|
---|
28 |
|
---|
29 | int Num(int i) const {return index[i].num;}
|
---|
30 | // L'ancien indice qui est le numero i une fois trie
|
---|
31 |
|
---|
32 | int Lookup(float val);
|
---|
33 | // L'indice - trie - le plus proche de val.
|
---|
34 |
|
---|
35 | #if ARG_FUNC_CPP_C
|
---|
36 | private:
|
---|
37 | #endif
|
---|
38 | typedef struct {
|
---|
39 | int num;
|
---|
40 | float value;
|
---|
41 | } TRIPAIRE;
|
---|
42 |
|
---|
43 | static int Compare(const void* tp1, const void* tp2);
|
---|
44 |
|
---|
45 | int numElts;
|
---|
46 | TRIPAIRE* index;
|
---|
47 | };
|
---|
48 |
|
---|
49 | // ************ INLINES
|
---|
50 |
|
---|
51 | inline
|
---|
52 | void SimpleSort::Set(int i, float value) // Check range ?
|
---|
53 | {
|
---|
54 | index[i].num = i;
|
---|
55 | index[i].value = value;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | // Une variante avec la STL, a preferer
|
---|
61 | #include <functional>
|
---|
62 | #include <algorithm>
|
---|
63 | #if defined(__KCC__)
|
---|
64 | #include <functional.h>
|
---|
65 | #include <algorithm.h>
|
---|
66 | #endif
|
---|
67 |
|
---|
68 |
|
---|
69 | // On a un vecteur de donnees et un vecteur d'index, on trie les index
|
---|
70 | // en comparant les donnees.
|
---|
71 | // L'objet-fonction de type dataCompare doit comparer deux objets de type data.
|
---|
72 |
|
---|
73 | // Comparateur qui agit sur l'index en comparant les donnees avec dataCompare
|
---|
74 | template <class data, class index, class dataCompare>
|
---|
75 | struct IndexComp : public binary_function<index,index,bool>
|
---|
76 | {
|
---|
77 | IndexComp(data* begin_data, index* begin_index, dataCompare comp)
|
---|
78 | : bdata(begin_data), bindex(begin_index), datacomp(comp) {}
|
---|
79 |
|
---|
80 | bool operator()(index const& i1, index const& i2)
|
---|
81 | { return datacomp(*(bdata + (&i1 - bindex)), *(bdata + (&i2 - bindex)));}
|
---|
82 |
|
---|
83 | data* bdata;
|
---|
84 | index* bindex;
|
---|
85 | dataCompare datacomp;
|
---|
86 | };
|
---|
87 |
|
---|
88 | // Fonction de tri qui trie un index en comparant des donnees avec dataComp
|
---|
89 | template <class data, class index, class dataCompare>
|
---|
90 | void TriIndex(data* begin_data, data* end_data, index* begin_index, dataCompare comp)
|
---|
91 | {
|
---|
92 | sort(begin_index, begin_index + (end_data-begin_data),
|
---|
93 | IndexComp<data, index, dataCompare>(begin_data, begin_index, comp));
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 |
|
---|
100 | #endif // SIMPLESORT_SEEN
|
---|