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

Last change on this file since 3836 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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