source: Sophya/trunk/SophyaLib/NTools/simplesort.cc@ 521

Last change on this file since 521 was 244, checked in by ansari, 26 years ago

beaucoup modifs rz+cmv 22/4/99

File size: 1.1 KB
Line 
1//
2// $Id: simplesort.cc,v 1.2 1999-04-22 16:18:50 ansari Exp $
3//
4
5#include "machdefs.h"
6#include "simplesort.h"
7#include <stdlib.h>
8
9//++
10// Class SimpleSort
11// Lib Outils++
12// include simplesort.h
13//
14// Classe pour trier. Utiliser plutôt la STL.
15//--
16
17
18SimpleSort::SimpleSort(int nElts)
19: numElts(nElts), index(new TRIPAIRE[nElts])
20{END_CONSTRUCTOR}
21
22SimpleSort::~SimpleSort()
23{
24 delete[] index;
25}
26
27int SimpleSort::Compare(const void* tp1, const void* tp2)
28{ float x=((TRIPAIRE*)tp1)->value - ((TRIPAIRE*)tp2)->value;
29 if (x>0) return(1);
30 else if (x<0) return(-1);
31 else return(0);
32}
33
34#if !ARG_FUNC_CPP_C
35extern "C" int SimpleSortCompare(const void* tp1, const void* tp2);
36
37int SimpleSortCompare(const void* tp1, const void* tp2)
38{ return SimpleSort::Compare(tp1, tp2);
39}
40#endif
41
42void SimpleSort::Sort()
43{
44#if ARG_FUNC_CPP_C
45 qsort(index, numElts, sizeof(TRIPAIRE), SimpleSort::Compare);
46#else
47 qsort(index, numElts, sizeof(TRIPAIRE), SimpleSortCompare);
48#endif
49}
50
51int SimpleSort::Lookup(float val)
52{
53 int a = 0;
54 int b = numElts-1;
55 while (b>a+1) {
56 int c = (b+a)/2;
57 ((val >= index[c].value) ? a : b) = c;
58 }
59 return a;
60}
Note: See TracBrowser for help on using the repository browser.