GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-sort.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2003-2025 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24// Code stolen in large part from Python's, listobject.c, which itself had
25// no license header. However, thanks to Tim Peters for the parts of the
26// code I ripped-off.
27//
28// As required in the Python license the short description of the changes
29// made are
30//
31// * convert the sorting code in listobject.cc into a generic class,
32// replacing PyObject* with the type of the class T.
33//
34// The Python license is
35//
36// PSF LICENSE AGREEMENT FOR PYTHON 2.3
37// --------------------------------------
38//
39// 1. This LICENSE AGREEMENT is between the Python Software Foundation
40// ("PSF"), and the Individual or Organization ("Licensee") accessing and
41// otherwise using Python 2.3 software in source or binary form and its
42// associated documentation.
43//
44// 2. Subject to the terms and conditions of this License Agreement, PSF
45// hereby grants Licensee a nonexclusive, royalty-free, world-wide
46// license to reproduce, analyze, test, perform and/or display publicly,
47// prepare derivative works, distribute, and otherwise use Python 2.3
48// alone or in any derivative version, provided, however, that PSF's
49// License Agreement and PSF's notice of copyright, i.e., "Copyright (c)
50// 2001, 2002, 2003 Python Software Foundation; All Rights Reserved" are
51// retained in Python 2.3 alone or in any derivative version prepared by
52// Licensee.
53//
54// 3. In the event Licensee prepares a derivative work that is based on
55// or incorporates Python 2.3 or any part thereof, and wants to make
56// the derivative work available to others as provided herein, then
57// Licensee hereby agrees to include in any such work a brief summary of
58// the changes made to Python 2.3.
59//
60// 4. PSF is making Python 2.3 available to Licensee on an "AS IS"
61// basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
62// IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
63// DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
64// FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 2.3 WILL NOT
65// INFRINGE ANY THIRD PARTY RIGHTS.
66//
67// 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
68// 2.3 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS
69// A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 2.3,
70// OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
71//
72// 6. This License Agreement will automatically terminate upon a material
73// breach of its terms and conditions.
74//
75// 7. Nothing in this License Agreement shall be deemed to create any
76// relationship of agency, partnership, or joint venture between PSF and
77// Licensee. This License Agreement does not grant permission to use PSF
78// trademarks or trade name in a trademark sense to endorse or promote
79// products or services of Licensee, or any third party.
80//
81// 8. By copying, installing or otherwise using Python 2.3, Licensee
82// agrees to be bound by the terms and conditions of this License
83// Agreement.
84//
85////////////////////////////////////////////////////////////////////////
86
87#if ! defined (octave_oct_sort_h)
88#define octave_oct_sort_h 1
89
90#include "octave-config.h"
91
92#include <functional>
93
94#include "lo-traits.h"
95
96// Enum for type of sort function
98
99template <typename T>
100class OCTAVE_TEMPLATE_API octave_sort
101{
102public:
103
104 typedef std::function<bool (typename ref_param<T>::type,
106
107 octave_sort ();
108
110
111 OCTAVE_DISABLE_COPY_MOVE (octave_sort)
112
113 ~octave_sort ();
114
115 void set_compare (const compare_fcn_type& comp) { m_compare = comp; }
116
117 void set_compare (sortmode mode);
118
119 // Sort an array in-place.
120 void sort (T *data, octave_idx_type nel);
121
122 // Ditto, but also permute the passed indices (may not be valid indices).
123 void sort (T *data, octave_idx_type *idx, octave_idx_type nel);
124
125 // Check whether an array is sorted.
126 bool issorted (const T *data, octave_idx_type nel);
127
128 // Sort a matrix by rows, return a permutation
129 // vector.
130 void sort_rows (const T *data, octave_idx_type *idx,
132
133 // Determine whether a matrix (as a contiguous block) is sorted by rows.
134 bool is_sorted_rows (const T *data,
136
137 // Do a binary lookup in a sorted array.
138 octave_idx_type lookup (const T *data, octave_idx_type nel,
139 const T& value);
140
141 // Ditto, but for an array.
142 void lookup (const T *data, octave_idx_type nel,
143 const T *values, octave_idx_type nvalues,
144 octave_idx_type *idx);
145
146 // A linear merge of two sorted tables. rev indicates the second table is
147 // in reverse order.
148 void lookup_sorted (const T *data, octave_idx_type nel,
149 const T *values, octave_idx_type nvalues,
150 octave_idx_type *idx, bool rev = false);
151
152 // Rearranges the array so that the elements with indices
153 // lo..up-1 are in their correct place.
154 void nth_element (T *data, octave_idx_type nel,
155 octave_idx_type lo, octave_idx_type up = -1);
156
157 static bool ascending_compare (typename ref_param<T>::type,
158 typename ref_param<T>::type);
159
160 static bool descending_compare (typename ref_param<T>::type,
161 typename ref_param<T>::type);
162
163private:
164
165 // The maximum number of entries in a MergeState's pending-runs stack.
166 // This is enough to sort arrays of size up to about
167 // 32 * phi ** MAX_MERGE_PENDING
168 // where phi ~= 1.618. 85 is ridiculously large enough, good for an array
169 // with 2^64 elements.
170 static const int MAX_MERGE_PENDING = 85;
171
172 // When we get into galloping mode, we stay there until both runs win less
173 // often than MIN_GALLOP consecutive times. See listsort.txt for more info.
174 static const int MIN_GALLOP = 7;
175
176 // Avoid malloc for small temp arrays.
177 static const int MERGESTATE_TEMP_SIZE = 1024;
178
179 // One MergeState exists on the stack per invocation of mergesort.
180 // It's just a convenient way to pass state around among the helper
181 // functions.
182 //
183 // DGB: This isn't needed with mergesort in a class, but it doesn't
184 // slow things up, and it is likely to make my life easier for any
185 // potential backporting of changes in the Python code.
186
187 struct s_slice
188 {
189 octave_idx_type m_base, m_len;
190 };
191
192 struct MergeState
193 {
194 public:
195
196 MergeState ()
197 : m_min_gallop (), m_a (nullptr), m_ia (nullptr), m_alloced (0), m_n (0)
198 { reset (); }
199
200 OCTAVE_DISABLE_COPY_MOVE (MergeState)
201
202 ~MergeState ()
203 { delete [] m_a; delete [] m_ia; }
204
205 void reset ()
206 { m_min_gallop = MIN_GALLOP; m_n = 0; }
207
208 void getmem (octave_idx_type need);
209
210 void getmemi (octave_idx_type need);
211
212 //--------
213
214 // This controls when we get *into* galloping mode. It's initialized to
215 // MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for random
216 // data, and lower for highly structured data.
217 octave_idx_type m_min_gallop;
218
219 // 'a' is temp storage to help with merges. It contains room for
220 // alloced entries.
221 T *m_a; // may point to temparray below
222 octave_idx_type *m_ia;
223 octave_idx_type m_alloced;
224
225 // A stack of n pending runs yet to be merged. Run #i starts at address
226 // base[i] and extends for len[i] elements. It's always true (so long as
227 // the indices are in bounds) that
228 //
229 // pending[i].base + pending[i].len == pending[i+1].base
230 //
231 // so we could cut the storage for this, but it's a minor amount,
232 // and keeping all the info explicit simplifies the code.
233 octave_idx_type m_n;
234 struct s_slice m_pending[MAX_MERGE_PENDING];
235 };
236
237 compare_fcn_type m_compare;
238
239 MergeState *m_ms;
240
241 template <typename Comp>
242 void binarysort (T *data, octave_idx_type nel,
243 octave_idx_type start, Comp comp);
244
245 template <typename Comp>
246 void binarysort (T *data, octave_idx_type *idx, octave_idx_type nel,
247 octave_idx_type start, Comp comp);
248
249 template <typename Comp>
250 octave_idx_type count_run (T *lo, octave_idx_type n, bool& descending,
251 Comp comp);
252
253 template <typename Comp>
254 octave_idx_type gallop_left (T key, T *a, octave_idx_type n,
255 octave_idx_type hint, Comp comp);
256
257 template <typename Comp>
258 octave_idx_type gallop_right (T key, T *a, octave_idx_type n,
259 octave_idx_type hint, Comp comp);
260
261 template <typename Comp>
262 int merge_lo (T *pa, octave_idx_type na,
263 T *pb, octave_idx_type nb,
264 Comp comp);
265
266 template <typename Comp>
267 int merge_lo (T *pa, octave_idx_type *ipa, octave_idx_type na,
268 T *pb, octave_idx_type *ipb, octave_idx_type nb,
269 Comp comp);
270
271 template <typename Comp>
272 int merge_hi (T *pa, octave_idx_type na,
273 T *pb, octave_idx_type nb,
274 Comp comp);
275
276 template <typename Comp>
277 int merge_hi (T *pa, octave_idx_type *ipa, octave_idx_type na,
278 T *pb, octave_idx_type *ipb, octave_idx_type nb,
279 Comp comp);
280
281 template <typename Comp>
282 int merge_at (octave_idx_type i, T *data, Comp comp);
283
284 template <typename Comp>
285 int merge_at (octave_idx_type i, T *data, octave_idx_type *idx, Comp comp);
286
287 template <typename Comp>
288 int merge_collapse (T *data, Comp comp);
289
290 template <typename Comp>
291 int merge_collapse (T *data, octave_idx_type *idx, Comp comp);
292
293 template <typename Comp>
294 int merge_force_collapse (T *data, Comp comp);
295
296 template <typename Comp>
297 int merge_force_collapse (T *data, octave_idx_type *idx, Comp comp);
298
299 octave_idx_type merge_compute_minrun (octave_idx_type n);
300
301 template <typename Comp>
302 void sort (T *data, octave_idx_type nel, Comp comp);
303
304 template <typename Comp>
305 void sort (T *data, octave_idx_type *idx, octave_idx_type nel, Comp comp);
306
307 template <typename Comp>
308 bool issorted (const T *data, octave_idx_type nel, Comp comp);
309
310 template <typename Comp>
311 void sort_rows (const T *data, octave_idx_type *idx,
313 Comp comp);
314
315 template <typename Comp>
316 bool is_sorted_rows (const T *data, octave_idx_type rows,
317 octave_idx_type cols, Comp comp);
318
319 template <typename Comp>
320 octave_idx_type lookup (const T *data, octave_idx_type nel,
321 const T& value, Comp comp);
322
323 template <typename Comp>
324 void lookup (const T *data, octave_idx_type nel,
325 const T *values, octave_idx_type nvalues,
326 octave_idx_type *idx, Comp comp);
327
328 template <typename Comp>
329 void lookup_sorted (const T *data, octave_idx_type nel,
330 const T *values, octave_idx_type nvalues,
331 octave_idx_type *idx, bool rev, Comp comp);
332
333 template <typename Comp>
334 void nth_element (T *data, octave_idx_type nel,
336 Comp comp);
337};
338
339template <typename T>
340class OCTAVE_TEMPLATE_API vec_index
341{
342public:
345};
346
347#endif
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
void set_compare(const compare_fcn_type &comp)
Definition oct-sort.h:115
std::function< bool(typename ref_param< T >::type, typename ref_param< T >::type)> compare_fcn_type
Definition oct-sort.h:105
if_then_else< is_class_type< T >::no, T, Tconst & >::result type
Definition lo-traits.h:121
octave_idx_type m_indx
Definition oct-sort.h:344
sortmode
Definition oct-sort.h:97
@ UNSORTED
Definition oct-sort.h:97
@ ASCENDING
Definition oct-sort.h:97
@ DESCENDING
Definition oct-sort.h:97