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