source: Sophya/trunk/SophyaLib/NTools/simplesort.h@ 1380

Last change on this file since 1380 was 1380, checked in by yvon, 25 years ago

binary_finction castee en std::

File size: 2.4 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2//
3// $Id: simplesort.h,v 1.4 2001-01-10 16:55:53 yvon 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
14namespace SOPHYA {
15
16class SimpleSort {
17public:
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
38private:
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
53inline
54void 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
72template <class data, class index, class dataCompare>
73struct IndexComp : public std::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
87template <class data, class index, class dataCompare>
88void 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
Note: See TracBrowser for help on using the repository browser.