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
00083 #if !defined (octave_sort_h)
00084 #define octave_sort_h 1
00085
00086 #include "lo-traits.h"
00087
00088
00089
00090
00091
00092
00093 #define MAX_MERGE_PENDING 85
00094
00095
00096
00097 #define MIN_GALLOP 7
00098
00099
00100 #define MERGESTATE_TEMP_SIZE 1024
00101
00102
00103 enum sortmode { UNSORTED = 0, ASCENDING, DESCENDING };
00104
00105 template <class T>
00106 class
00107 octave_sort
00108 {
00109 public:
00110
00111 typedef bool (*compare_fcn_type) (typename ref_param<T>::type,
00112 typename ref_param<T>::type);
00113
00114 octave_sort (void);
00115
00116 octave_sort (compare_fcn_type);
00117
00118 ~octave_sort (void);
00119
00120 void set_compare (compare_fcn_type comp) { compare = comp; }
00121
00122 void set_compare (sortmode mode);
00123
00124
00125 void sort (T *data, octave_idx_type nel);
00126
00127
00128 void sort (T *data, octave_idx_type *idx, octave_idx_type nel);
00129
00130
00131 bool is_sorted (const T *data, octave_idx_type nel);
00132
00133
00134
00135 void sort_rows (const T *data, octave_idx_type *idx,
00136 octave_idx_type rows, octave_idx_type cols);
00137
00138
00139 bool is_sorted_rows (const T *data,
00140 octave_idx_type rows, octave_idx_type cols);
00141
00142
00143 octave_idx_type lookup (const T *data, octave_idx_type nel,
00144 const T& value);
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);
00150
00151
00152
00153 void lookup_sorted (const T *data, octave_idx_type nel,
00154 const T* values, octave_idx_type nvalues,
00155 octave_idx_type *idx, bool rev = false);
00156
00157
00158
00159 void nth_element (T *data, octave_idx_type nel,
00160 octave_idx_type lo, octave_idx_type up = -1);
00161
00162 static bool ascending_compare (typename ref_param<T>::type,
00163 typename ref_param<T>::type);
00164
00165 static bool descending_compare (typename ref_param<T>::type,
00166 typename ref_param<T>::type);
00167
00168 private:
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 struct s_slice
00179 {
00180 octave_idx_type base, len;
00181 };
00182
00183 struct MergeState
00184 {
00185 MergeState (void)
00186 : min_gallop (), a (0), ia (0), alloced (0), n (0)
00187 { reset (); }
00188
00189 ~MergeState (void)
00190 { delete [] a; delete [] ia; }
00191
00192 void reset (void)
00193 { min_gallop = MIN_GALLOP; n = 0; }
00194
00195 void getmem (octave_idx_type need);
00196
00197 void getmemi (octave_idx_type need);
00198
00199
00200
00201
00202
00203 octave_idx_type min_gallop;
00204
00205
00206
00207 T *a;
00208 octave_idx_type *ia;
00209 octave_idx_type alloced;
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 octave_idx_type n;
00220 struct s_slice pending[MAX_MERGE_PENDING];
00221
00222
00223
00224 MergeState (const MergeState&);
00225
00226 MergeState& operator = (const MergeState&);
00227 };
00228
00229 compare_fcn_type compare;
00230
00231 MergeState *ms;
00232
00233
00234 template <class Comp>
00235 void binarysort (T *data, octave_idx_type nel,
00236 octave_idx_type start, Comp comp);
00237
00238 template <class Comp>
00239 void binarysort (T *data, octave_idx_type *idx, octave_idx_type nel,
00240 octave_idx_type start, Comp comp);
00241
00242 template <class Comp>
00243 octave_idx_type count_run (T *lo, octave_idx_type n, bool& descending, Comp comp);
00244
00245 template <class Comp>
00246 octave_idx_type gallop_left (T key, T *a, octave_idx_type n, octave_idx_type hint,
00247 Comp comp);
00248
00249 template <class Comp>
00250 octave_idx_type gallop_right (T key, T *a, octave_idx_type n, octave_idx_type hint,
00251 Comp comp);
00252
00253 template <class Comp>
00254 int merge_lo (T *pa, octave_idx_type na,
00255 T *pb, octave_idx_type nb,
00256 Comp comp);
00257
00258 template <class Comp>
00259 int merge_lo (T *pa, octave_idx_type *ipa, octave_idx_type na,
00260 T *pb, octave_idx_type *ipb, octave_idx_type nb,
00261 Comp comp);
00262
00263 template <class Comp>
00264 int merge_hi (T *pa, octave_idx_type na,
00265 T *pb, octave_idx_type nb,
00266 Comp comp);
00267
00268 template <class Comp>
00269 int merge_hi (T *pa, octave_idx_type *ipa, octave_idx_type na,
00270 T *pb, octave_idx_type *ipb, octave_idx_type nb,
00271 Comp comp);
00272
00273 template <class Comp>
00274 int merge_at (octave_idx_type i, T *data,
00275 Comp comp);
00276
00277 template <class Comp>
00278 int merge_at (octave_idx_type i, T *data, octave_idx_type *idx,
00279 Comp comp);
00280
00281 template <class Comp>
00282 int merge_collapse (T *data, Comp comp);
00283
00284 template <class Comp>
00285 int merge_collapse (T *data, octave_idx_type *idx, Comp comp);
00286
00287 template <class Comp>
00288 int merge_force_collapse (T *data, Comp comp);
00289
00290 template <class Comp>
00291 int merge_force_collapse (T *data, octave_idx_type *idx, Comp comp);
00292
00293 octave_idx_type merge_compute_minrun (octave_idx_type n);
00294
00295 template <class Comp>
00296 void sort (T *data, octave_idx_type nel, Comp comp);
00297
00298 template <class Comp>
00299 void sort (T *data, octave_idx_type *idx, octave_idx_type nel, Comp comp);
00300
00301 template <class Comp>
00302 bool is_sorted (const T *data, octave_idx_type nel, Comp comp);
00303
00304 template <class Comp>
00305 void sort_rows (const T *data, octave_idx_type *idx,
00306 octave_idx_type rows, octave_idx_type cols,
00307 Comp comp);
00308
00309 template <class Comp>
00310 bool is_sorted_rows (const T *data, octave_idx_type rows,
00311 octave_idx_type cols, Comp comp);
00312
00313 template <class Comp>
00314 octave_idx_type lookup (const T *data, octave_idx_type nel,
00315 const T& value, Comp comp);
00316
00317 template <class Comp>
00318 void lookup (const T *data, octave_idx_type nel,
00319 const T* values, octave_idx_type nvalues,
00320 octave_idx_type *idx, Comp comp);
00321
00322 template <class Comp>
00323 void lookup_sorted (const T *data, octave_idx_type nel,
00324 const T* values, octave_idx_type nvalues,
00325 octave_idx_type *idx, bool rev, Comp comp);
00326
00327 template <class Comp>
00328 void nth_element (T *data, octave_idx_type nel,
00329 octave_idx_type lo, octave_idx_type up,
00330 Comp comp);
00331
00332
00333
00334 octave_sort (const octave_sort&);
00335
00336 octave_sort& operator = (const octave_sort&);
00337 };
00338
00339 template <class T>
00340 class
00341 vec_index
00342 {
00343 public:
00344 T vec;
00345 octave_idx_type indx;
00346 };
00347 #endif