Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

Sort.hpp

Go to the documentation of this file.
00001 
00027 // *************************************************************
00028 //
00029 //  Class Sort<C,n> for sorting
00030 //  06/05/2002 
00031 //
00032 // *************************************************************
00033 
00034 
00035 #ifndef __sort__
00036 #  define __sort__
00037 
00038 
00039 #include "Memory.hpp"
00040 
00041 
00042 // ******************* Class CList *******************************
00043 
00044 
00045 template <class C>
00046 class Sort
00047 {
00048  public:
00049 
00050   inline
00051   Sort (int length) 
00052     :
00053     _elems ( new C[length] ),
00054     _size (length),
00055     _length (0)
00056     {
00057       if (! _elems) {
00058         NO_MEMORY;
00059       }
00060     }
00061 
00062   ~Sort () 
00063     {
00064       delete [] _elems;
00065     }
00066 
00067   inline int length () const 
00068     { return _length; }
00069 
00070   // note that this operator can only return values
00071   inline C operator [] (int n) const
00072     { 
00073       ASS(n < _length);
00074       return _elems[n]; 
00075     } // Sort::operator []
00076 
00077   inline void add (C c)
00078     {
00079       ASS(_length < _size);
00080 
00081       _elems[_length++] = c;
00082     } // Sort::add
00083 
00084   // remove duplicates from sorted list
00085   void removeDuplicates ()
00086     {
00087       int cur = 0;
00088 
00089       for (int next = 1; next < _length; next++) {
00090         if ( _elems[cur] != _elems[next] ) {
00091           cur++;
00092         }
00093       }
00094 
00095       // reset length
00096       _length = cur + 1;
00097     } // Sort::removeDuplicates
00098 
00099   inline void sort ()
00100     { sort (0,_length-1); }
00101 
00102   // sort using a comparison function compare on C
00103   inline void sortF ()
00104     { sortF (0,_length-1); }
00105 
00106   // sort using a global comparison function compare on C
00107   // inline void sortGF ()
00108   //  { sortGF (0,_length-1); }
00109 
00110   // check membership in the sorted list
00111   inline bool member (const C c) const
00112     { return member (c,0,_length-1); }
00113 
00114  protected:  // structure
00115 
00116   C* _elems;
00117   int _size;
00118   int _length;
00119 
00120   // Quicksort, copied from Cormen et.al's "Introduction to Algorithms"
00121   void sort ( int p,int r ) 
00122     {
00123       ASS(r < _length);
00124 
00125       if (p < r) {
00126         int q = partition(p,r);
00127         sort(p,q);
00128         sort(q+1,r);    
00129       }
00130     } // Sort::sort
00131 
00132   // Quicksort, copied from Cormen et.al's "Introduction to Algorithms"
00133   void sortF ( int p,int r ) 
00134     {
00135       ASS(r < _length);
00136 
00137       if (p < r) {
00138         int q = partitionF(p,r);
00139         sortF(p,q);
00140         sortF(q+1,r);    
00141       }
00142     } // Sort::sort
00143 
00144   // Quicksort, copied from Cormen et.al's "Introduction to Algorithms"
00145   void sortGF ( int p,int r ) 
00146     {
00147       ASS(r < _length);
00148 
00149       if (p < r) {
00150         int q = partitionGF(p,r);
00151         sortGF(p,q);
00152         sortGF(q+1,r);    
00153       }
00154     } // Sort::sort
00155 
00156   int partition( int p,int r ) 
00157   {
00158     C x = _elems[p];
00159     int i = p-1;
00160     int j = r+1;
00161 
00162     for (;;) {
00163       do --j;
00164       while (x < _elems[j]);
00165       do ++i;
00166       while (_elems[i] < x);
00167       if (i < j) {
00168         // swap [i] and [j]
00169         C tmp = _elems[i];
00170         _elems[i] = _elems[j];
00171         _elems[j] = tmp;
00172       }
00173       else
00174         return j;
00175     }
00176   } // Sort::partitition
00177 
00178   int partitionF( int p,int r ) 
00179   {
00180     C x = _elems[p];
00181     int i = p-1;
00182     int j = r+1;
00183 
00184     for (;;) {
00185       do --j;
00186       while (x.isless(_elems[j]));
00187 
00188       do ++i;
00189       while (_elems[i].isless(x));
00190 
00191       if (i < j) {
00192         // swap [i] and [j]
00193         C tmp = _elems[i];
00194         _elems[i] = _elems[j];
00195         _elems[j] = tmp;
00196       }
00197       else
00198         return j;
00199     }
00200   } // Sort::partititionF
00201 
00202   int partitionGF( int p,int r ) 
00203   {
00204     C x = _elems[p];
00205     int i = p-1;
00206     int j = r+1;
00207 
00208     for (;;) {
00209       do --j;
00210       while ( isless(_elems[j],x) );
00211 
00212       do ++i;
00213       while ( isless(x,_elems[i]) );
00214 
00215       if (i < j) {
00216         // swap [i] and [j]
00217         C tmp = _elems[i];
00218         _elems[i] = _elems[j];
00219         _elems[j] = tmp;
00220       }
00221       else
00222         return j;
00223     }
00224   } // Sort::partititionGF
00225 
00226   bool member (const C c, int fst, int lst) const
00227     { 
00228       for (;;) {
00229         if (fst > lst) {
00230           return false;
00231         }
00232 
00233         int mid = (fst + lst) / 2;
00234       
00235         if (_elems[mid] == c) {
00236           return true;
00237         }
00238 
00239         if (_elems[mid] < c) {
00240           lst = mid-1;
00241         }
00242         else { // _elems[mid] > c
00243           fst = mid+1;
00244         }
00245       }
00246     } // Sort::member
00247 };  // class Sort
00248 
00249 
00250 #endif
00251 
00252 

Generated on Sat Jun 28 15:08:58 2003 for Vampire by doxygen 1.3.2