| Line |  | 
|---|
| 1 | // | 
|---|
| 2 | // $Id: simplesort.cc,v 1.1.1.1 1999-11-26 16:37:12 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 |  | 
|---|
| 18 | SimpleSort::SimpleSort(int nElts) | 
|---|
| 19 | : numElts(nElts), index(new TRIPAIRE[nElts]) | 
|---|
| 20 | {END_CONSTRUCTOR} | 
|---|
| 21 |  | 
|---|
| 22 | SimpleSort::~SimpleSort() | 
|---|
| 23 | { | 
|---|
| 24 | delete[] index; | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | int 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 | 
|---|
| 35 | extern "C" int SimpleSortCompare(const void* tp1, const void* tp2); | 
|---|
| 36 |  | 
|---|
| 37 | int SimpleSortCompare(const void* tp1, const void* tp2) | 
|---|
| 38 | { return SimpleSort::Compare(tp1, tp2); | 
|---|
| 39 | } | 
|---|
| 40 | #endif | 
|---|
| 41 |  | 
|---|
| 42 | void 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 |  | 
|---|
| 51 | int 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.