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