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

Last change on this file since 3842 was 1381, checked in by ansari, 25 years ago

Deplacement de .h de std ds simplesort.h - Reza 12/1/2001

File size: 2.4 KB
RevLine 
[220]1// This may look like C code, but it is really -*- C++ -*-
2//
[1381]3// $Id: simplesort.h,v 1.5 2001-01-12 15:47:31 ansari Exp $
[220]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
[1381]14// Pour la variante avec la STL, a preferer
15#include <functional>
16#include <algorithm>
17
[1371]18namespace SOPHYA {
19
20class SimpleSort {
[220]21public:
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
42private:
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
57inline
58void SimpleSort::Set(int i, float value) // Check range ?
59{
60 index[i].num = i;
61 index[i].value = value;
62}
63
64
[1381]65// ----- Une variante avec la STL, a preferer
[220]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
73template <class data, class index, class dataCompare>
[1380]74struct IndexComp : public std::binary_function<index,index,bool>
[220]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
88template <class data, class index, class dataCompare>
89void 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
[1371]95} // namespace SOPHYA
[220]96
97#endif // SIMPLESORT_SEEN
Note: See TracBrowser for help on using the repository browser.