Mercurial > hg > octave-lyh
comparison liboctave/oct-sort.h @ 8725:d5af326a3ede
[mq]: sort-traits
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 12 Feb 2009 02:49:14 -0500 |
parents | e9cb742df9eb |
children | de16ebeef93d |
comparison
equal
deleted
inserted
replaced
8724:a50228129dba | 8725:d5af326a3ede |
---|---|
80 */ | 80 */ |
81 | 81 |
82 #if !defined (octave_sort_h) | 82 #if !defined (octave_sort_h) |
83 #define octave_sort_h 1 | 83 #define octave_sort_h 1 |
84 | 84 |
85 #include "lo-traits.h" | |
86 | |
85 // The maximum number of entries in a MergeState's pending-runs stack. | 87 // The maximum number of entries in a MergeState's pending-runs stack. |
86 // This is enough to sort arrays of size up to about | 88 // This is enough to sort arrays of size up to about |
87 // 32 * phi ** MAX_MERGE_PENDING | 89 // 32 * phi ** MAX_MERGE_PENDING |
88 // where phi ~= 1.618. 85 is ridiculously large enough, good for an array | 90 // where phi ~= 1.618. 85 is ridiculously large enough, good for an array |
89 // with 2**64 elements. | 91 // with 2**64 elements. |
103 class | 105 class |
104 octave_sort | 106 octave_sort |
105 { | 107 { |
106 public: | 108 public: |
107 | 109 |
110 typedef bool (*compare_fcn_type) (typename ref_param<T>::type, | |
111 typename ref_param<T>::type); | |
112 | |
108 octave_sort (void); | 113 octave_sort (void); |
109 | 114 |
110 octave_sort (bool (*comp) (T, T)); | 115 octave_sort (compare_fcn_type); |
111 | 116 |
112 ~octave_sort (void) { merge_freemem (); } | 117 ~octave_sort (void) { merge_freemem (); } |
113 | 118 |
114 void set_compare (bool (*comp) (T, T)) { compare = comp; } | 119 void set_compare (compare_fcn_type comp) { compare = comp; } |
115 | 120 |
116 void set_compare (sortmode mode); | 121 void set_compare (sortmode mode); |
117 | 122 |
118 // Sort an array in-place. | 123 // Sort an array in-place. |
119 void sort (T *data, octave_idx_type nel); | 124 void sort (T *data, octave_idx_type nel); |
131 | 136 |
132 // Determine whether a matrix (as a contiguous block) is sorted by rows. | 137 // Determine whether a matrix (as a contiguous block) is sorted by rows. |
133 bool is_sorted_rows (const T *data, | 138 bool is_sorted_rows (const T *data, |
134 octave_idx_type rows, octave_idx_type cols); | 139 octave_idx_type rows, octave_idx_type cols); |
135 | 140 |
136 static bool ascending_compare (T, T); | 141 static bool ascending_compare (typename ref_param<T>::type, |
137 static bool descending_compare (T, T); | 142 typename ref_param<T>::type); |
143 | |
144 static bool descending_compare (typename ref_param<T>::type, | |
145 typename ref_param<T>::type); | |
138 | 146 |
139 private: | 147 private: |
140 | 148 |
141 // One MergeState exists on the stack per invocation of mergesort. | 149 // One MergeState exists on the stack per invocation of mergesort. |
142 // It's just a convenient way to pass state around among the helper | 150 // It's just a convenient way to pass state around among the helper |
175 // and keeping all the info explicit simplifies the code. | 183 // and keeping all the info explicit simplifies the code. |
176 octave_idx_type n; | 184 octave_idx_type n; |
177 struct s_slice pending[MAX_MERGE_PENDING]; | 185 struct s_slice pending[MAX_MERGE_PENDING]; |
178 }; | 186 }; |
179 | 187 |
180 bool (*compare) (T, T); | 188 compare_fcn_type compare; |
181 | 189 |
182 MergeState ms; | 190 MergeState ms; |
183 | 191 |
184 | 192 |
185 template <class Comp> | 193 template <class Comp> |