00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 #if !defined (octave_sort_h)
00083 #define octave_sort_h 1
00084
00085 #include "lo-traits.h"
00086
00087
00088
00089
00090
00091
00092 #define MAX_MERGE_PENDING 85
00093
00094
00095
00096 #define MIN_GALLOP 7
00097
00098
00099 #define MERGESTATE_TEMP_SIZE 1024
00100
00101
00102 enum sortmode { UNSORTED = 0, ASCENDING, DESCENDING };
00103
00104 template <class T>
00105 class
00106 octave_sort
00107 {
00108 public:
00109
00110 typedef bool (*compare_fcn_type) (typename ref_param<T>::type,
00111 typename ref_param<T>::type);
00112
00113 octave_sort (void);
00114
00115 octave_sort (compare_fcn_type);
00116
00117 ~octave_sort (void);
00118
00119 void set_compare (compare_fcn_type comp) { compare = comp; }
00120
00121 void set_compare (sortmode mode);
00122
00123
00124 void sort (T *data, octave_idx_type nel);
00125
00126
00127 void sort (T *data, octave_idx_type *idx, octave_idx_type nel);
00128
00129
00130 bool is_sorted (const T *data, octave_idx_type nel);
00131
00132
00133
00134 void sort_rows (const T *data, octave_idx_type *idx,
00135 octave_idx_type rows, octave_idx_type cols);
00136
00137
00138 bool is_sorted_rows (const T *data,
00139 octave_idx_type rows, octave_idx_type cols);
00140
00141
00142 octave_idx_type lookup (const T *data, octave_idx_type nel,
00143 const T& value);
00144
00145
00146
00147 void lookup (const T *data, octave_idx_type nel,
00148 const T* values, octave_idx_type nvalues,
00149 octave_idx_type *idx, octave_idx_type offset = 0);
00150
00151
00152
00153 void lookupm (const T *data, octave_idx_type nel,
00154 const T* values, octave_idx_type nvalues,
00155 octave_idx_type *idx);
00156
00157
00158 void lookupb (const T *data, octave_idx_type nel,
00159 const T* values, octave_idx_type nvalues,
00160 bool *match);
00161
00162
00163
00164 void nth_element (T *data, octave_idx_type nel,
00165 octave_idx_type lo, octave_idx_type up = -1);
00166
00167 static bool ascending_compare (typename ref_param<T>::type,
00168 typename ref_param<T>::type);
00169
00170 static bool descending_compare (typename ref_param<T>::type,
00171 typename ref_param<T>::type);
00172
00173 private:
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 struct s_slice
00184 {
00185 octave_idx_type base, len;
00186 };
00187
00188 struct MergeState
00189 {
00190 MergeState (void)
00191 : a (0), ia (0), alloced (0)
00192 { reset (); }
00193
00194 ~MergeState (void)
00195 { delete [] a; delete [] ia; }
00196
00197 void reset (void)
00198 { min_gallop = MIN_GALLOP; n = 0; }
00199
00200 void getmem (octave_idx_type need);
00201
00202 void getmemi (octave_idx_type need);
00203
00204
00205
00206
00207
00208 octave_idx_type min_gallop;
00209
00210
00211
00212 T *a;
00213 octave_idx_type *ia;
00214 octave_idx_type alloced;
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 octave_idx_type n;
00225 struct s_slice pending[MAX_MERGE_PENDING];
00226 };
00227
00228 compare_fcn_type compare;
00229
00230 MergeState *ms;
00231
00232
00233 template <class Comp>
00234 void binarysort (T *data, octave_idx_type nel,
00235 octave_idx_type start, Comp comp);
00236
00237 template <class Comp>
00238 void binarysort (T *data, octave_idx_type *idx, octave_idx_type nel,
00239 octave_idx_type start, Comp comp);
00240
00241 template <class Comp>
00242 octave_idx_type count_run (T *lo, octave_idx_type n, bool& descending, Comp comp);
00243
00244 template <class Comp>
00245 octave_idx_type gallop_left (T key, T *a, octave_idx_type n, octave_idx_type hint,
00246 Comp comp);
00247
00248 template <class Comp>
00249 octave_idx_type gallop_right (T key, T *a, octave_idx_type n, octave_idx_type hint,
00250 Comp comp);
00251
00252 template <class Comp>
00253 int merge_lo (T *pa, octave_idx_type na,
00254 T *pb, octave_idx_type nb,
00255 Comp comp);
00256
00257 template <class Comp>
00258 int merge_lo (T *pa, octave_idx_type *ipa, octave_idx_type na,
00259 T *pb, octave_idx_type *ipb, octave_idx_type nb,
00260 Comp comp);
00261
00262 template <class Comp>
00263 int merge_hi (T *pa, octave_idx_type na,
00264 T *pb, octave_idx_type nb,
00265 Comp comp);
00266
00267 template <class Comp>
00268 int merge_hi (T *pa, octave_idx_type *ipa, octave_idx_type na,
00269 T *pb, octave_idx_type *ipb, octave_idx_type nb,
00270 Comp comp);
00271
00272 template <class Comp>
00273 int merge_at (octave_idx_type i, T *data,
00274 Comp comp);
00275
00276 template <class Comp>
00277 int merge_at (octave_idx_type i, T *data, octave_idx_type *idx,
00278 Comp comp);
00279
00280 template <class Comp>
00281 int merge_collapse (T *data, Comp comp);
00282
00283 template <class Comp>
00284 int merge_collapse (T *data, octave_idx_type *idx, Comp comp);
00285
00286 template <class Comp>
00287 int merge_force_collapse (T *data, Comp comp);
00288
00289 template <class Comp>
00290 int merge_force_collapse (T *data, octave_idx_type *idx, Comp comp);
00291
00292 octave_idx_type merge_compute_minrun (octave_idx_type n);
00293
00294 template <class Comp>
00295 void sort (T *data, octave_idx_type nel, Comp comp);
00296
00297 template <class Comp>
00298 void sort (T *data, octave_idx_type *idx, octave_idx_type nel, Comp comp);
00299
00300 template <class Comp>
00301 bool is_sorted (const T *data, octave_idx_type nel, Comp comp);
00302
00303 template <class Comp>
00304 void sort_rows (const T *data, octave_idx_type *idx,
00305 octave_idx_type rows, octave_idx_type cols,
00306 Comp comp);
00307
00308 template <class Comp>
00309 bool is_sorted_rows (const T *data, octave_idx_type rows,
00310 octave_idx_type cols, Comp comp);
00311
00312 template <class Comp>
00313 octave_idx_type lookup (const T *data, octave_idx_type nel,
00314 const T& value, Comp comp);
00315
00316 template <class Comp>
00317 void lookup (const T *data, octave_idx_type nel,
00318 const T* values, octave_idx_type nvalues,
00319 octave_idx_type *idx, octave_idx_type offset, Comp comp);
00320
00321 template <class Comp>
00322 void lookupm (const T *data, octave_idx_type nel,
00323 const T* values, octave_idx_type nvalues,
00324 octave_idx_type *idx, Comp comp);
00325
00326 template <class Comp>
00327 void lookupb (const T *data, octave_idx_type nel,
00328 const T* values, octave_idx_type nvalues,
00329 bool *match, Comp comp);
00330
00331 template <class Comp>
00332 void nth_element (T *data, octave_idx_type nel,
00333 octave_idx_type lo, octave_idx_type up,
00334 Comp comp);
00335 };
00336
00337 template <class T>
00338 class
00339 vec_index
00340 {
00341 public:
00342 T vec;
00343 octave_idx_type indx;
00344 };
00345 #endif
00346
00347
00348
00349
00350
00351