| 1 | // This may look like C code, but it is really -*- C++ -*-
 | 
|---|
| 2 | //
 | 
|---|
| 3 | // $Id: simplesort.h,v 1.1.1.1 1999-11-26 16:37:12 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 | 
 | 
|---|
| 64 | 
 | 
|---|
| 65 | // On a un vecteur de donnees et un vecteur d'index, on trie les index
 | 
|---|
| 66 | // en comparant les donnees.
 | 
|---|
| 67 | // L'objet-fonction de type dataCompare doit comparer deux objets de type data.
 | 
|---|
| 68 | 
 | 
|---|
| 69 | // Comparateur qui agit sur l'index en comparant les donnees avec dataCompare
 | 
|---|
| 70 | template <class data, class index, class dataCompare>
 | 
|---|
| 71 | struct IndexComp : public binary_function<index,index,bool>
 | 
|---|
| 72 | {
 | 
|---|
| 73 |   IndexComp(data* begin_data, index* begin_index, dataCompare comp)
 | 
|---|
| 74 |     : bdata(begin_data), bindex(begin_index), datacomp(comp) {}
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   bool operator()(index const& i1, index const& i2)
 | 
|---|
| 77 |     { return datacomp(*(bdata + (&i1 - bindex)), *(bdata + (&i2 - bindex)));}
 | 
|---|
| 78 |     
 | 
|---|
| 79 |   data* bdata;
 | 
|---|
| 80 |   index* bindex;
 | 
|---|
| 81 |   dataCompare datacomp;
 | 
|---|
| 82 | };
 | 
|---|
| 83 | 
 | 
|---|
| 84 | // Fonction de tri qui trie un index en comparant des donnees avec dataComp
 | 
|---|
| 85 | template <class data, class index, class dataCompare>
 | 
|---|
| 86 | void TriIndex(data* begin_data, data* end_data, index* begin_index, dataCompare comp)
 | 
|---|
| 87 | {
 | 
|---|
| 88 |   sort(begin_index, begin_index + (end_data-begin_data),
 | 
|---|
| 89 |         IndexComp<data, index, dataCompare>(begin_data, begin_index, comp));
 | 
|---|
| 90 | }  
 | 
|---|
| 91 | 
 | 
|---|
| 92 | 
 | 
|---|
| 93 | 
 | 
|---|
| 94 | 
 | 
|---|
| 95 | 
 | 
|---|
| 96 | #endif // SIMPLESORT_SEEN
 | 
|---|