Last change
on this file since 2526 was 2506, checked in by ansari, 22 years ago |
Remplacement THROW par throw - Reza 15/03/2004
|
File size:
1.1 KB
|
Rev | Line | |
---|
[220] | 1 | //
|
---|
[2506] | 2 | // $Id: simplesort.cc,v 1.3 2004-03-15 16:46:56 ansari Exp $
|
---|
[220] | 3 | //
|
---|
| 4 |
|
---|
[244] | 5 | #include "machdefs.h"
|
---|
[220] | 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 |
|
---|
| 18 | SimpleSort::SimpleSort(int nElts)
|
---|
| 19 | : numElts(nElts), index(new TRIPAIRE[nElts])
|
---|
[2506] | 20 | {
|
---|
| 21 | }
|
---|
[220] | 22 |
|
---|
| 23 | SimpleSort::~SimpleSort()
|
---|
| 24 | {
|
---|
| 25 | delete[] index;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | int SimpleSort::Compare(const void* tp1, const void* tp2)
|
---|
| 29 | { float x=((TRIPAIRE*)tp1)->value - ((TRIPAIRE*)tp2)->value;
|
---|
| 30 | if (x>0) return(1);
|
---|
| 31 | else if (x<0) return(-1);
|
---|
| 32 | else return(0);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | #if !ARG_FUNC_CPP_C
|
---|
| 36 | extern "C" int SimpleSortCompare(const void* tp1, const void* tp2);
|
---|
| 37 |
|
---|
| 38 | int SimpleSortCompare(const void* tp1, const void* tp2)
|
---|
| 39 | { return SimpleSort::Compare(tp1, tp2);
|
---|
| 40 | }
|
---|
| 41 | #endif
|
---|
| 42 |
|
---|
| 43 | void SimpleSort::Sort()
|
---|
| 44 | {
|
---|
| 45 | #if ARG_FUNC_CPP_C
|
---|
| 46 | qsort(index, numElts, sizeof(TRIPAIRE), SimpleSort::Compare);
|
---|
| 47 | #else
|
---|
| 48 | qsort(index, numElts, sizeof(TRIPAIRE), SimpleSortCompare);
|
---|
| 49 | #endif
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | int SimpleSort::Lookup(float val)
|
---|
| 53 | {
|
---|
| 54 | int a = 0;
|
---|
| 55 | int b = numElts-1;
|
---|
| 56 | while (b>a+1) {
|
---|
| 57 | int c = (b+a)/2;
|
---|
| 58 | ((val >= index[c].value) ? a : b) = c;
|
---|
| 59 | }
|
---|
| 60 | return a;
|
---|
| 61 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.