GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-sort.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2003-2021 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 "lo-traits.h"
93 
94 // Enum for type of sort function
96 
97 template <typename T>
98 class
100 {
101 public:
102 
103  typedef bool (*compare_fcn_type) (typename ref_param<T>::type,
104  typename ref_param<T>::type);
105 
106  octave_sort (void);
107 
108  octave_sort (compare_fcn_type);
109 
110  // No copying!
111 
112  octave_sort (const octave_sort&) = delete;
113 
114  octave_sort& operator = (const octave_sort&) = delete;
115 
116  ~octave_sort (void);
117 
118  void set_compare (compare_fcn_type comp) { m_compare = comp; }
119 
120  void set_compare (sortmode mode);
121 
122  // Sort an array in-place.
123  void sort (T *data, octave_idx_type nel);
124 
125  // Ditto, but also permute the passed indices (may not be valid indices).
126  void sort (T *data, octave_idx_type *idx, octave_idx_type nel);
127 
128  // Check whether an array is sorted.
129  bool issorted (const T *data, octave_idx_type nel);
130 
131  // Sort a matrix by rows, return a permutation
132  // vector.
133  void sort_rows (const T *data, octave_idx_type *idx,
134  octave_idx_type rows, octave_idx_type cols);
135 
136  // Determine whether a matrix (as a contiguous block) is sorted by rows.
137  bool is_sorted_rows (const T *data,
138  octave_idx_type rows, octave_idx_type cols);
139 
140  // Do a binary lookup in a sorted array.
141  octave_idx_type lookup (const T *data, octave_idx_type nel,
142  const T& value);
143 
144  // Ditto, but for an array.
145  void lookup (const T *data, octave_idx_type nel,
146  const T *values, octave_idx_type nvalues,
147  octave_idx_type *idx);
148 
149  // A linear merge of two sorted tables. rev indicates the second table is
150  // in reverse order.
151  void lookup_sorted (const T *data, octave_idx_type nel,
152  const T *values, octave_idx_type nvalues,
153  octave_idx_type *idx, bool rev = false);
154 
155  // Rearranges the array so that the elements with indices
156  // lo..up-1 are in their correct place.
157  void nth_element (T *data, octave_idx_type nel,
158  octave_idx_type lo, octave_idx_type up = -1);
159 
160  static bool ascending_compare (typename ref_param<T>::type,
161  typename ref_param<T>::type);
162 
163  static bool descending_compare (typename ref_param<T>::type,
164  typename ref_param<T>::type);
165 
166 private:
167 
168  // The maximum number of entries in a MergeState's pending-runs stack.
169  // This is enough to sort arrays of size up to about
170  // 32 * phi ** MAX_MERGE_PENDING
171  // where phi ~= 1.618. 85 is ridiculously large enough, good for an array
172  // with 2**64 elements.
173  static const int MAX_MERGE_PENDING = 85;
174 
175  // When we get into galloping mode, we stay there until both runs win less
176  // often than MIN_GALLOP consecutive times. See listsort.txt for more info.
177  static const int MIN_GALLOP = 7;
178 
179  // Avoid malloc for small temp arrays.
180  static const int MERGESTATE_TEMP_SIZE = 1024;
181 
182  // One MergeState exists on the stack per invocation of mergesort.
183  // It's just a convenient way to pass state around among the helper
184  // functions.
185  //
186  // DGB: This isn't needed with mergesort in a class, but it doesn't
187  // slow things up, and it is likely to make my life easier for any
188  // potential backporting of changes in the Python code.
189 
190  struct s_slice
191  {
193  };
194 
195  struct MergeState
196  {
197  MergeState (void)
198  : m_min_gallop (), m_a (nullptr), m_ia (nullptr), m_alloced (0), m_n (0)
199  { reset (); }
200 
201  // No copying!
202 
203  MergeState (const MergeState&) = delete;
204 
205  MergeState& operator = (const MergeState&) = delete;
206 
207  ~MergeState (void)
208  { delete [] m_a; delete [] m_ia; }
209 
210  void reset (void)
211  { m_min_gallop = MIN_GALLOP; m_n = 0; }
212 
213  void getmem (octave_idx_type need);
214 
215  void getmemi (octave_idx_type need);
216 
217  // This controls when we get *into* galloping mode. It's
218  // initialized to MIN_GALLOP. merge_lo and merge_hi tend to nudge
219  // it higher for random data, and lower for highly structured
220  // data.
222 
223  // 'a' is temp storage to help with merges. It contains room for
224  // alloced entries.
225  T *m_a; // may point to temparray below
228 
229  // A stack of n pending runs yet to be merged. Run #i starts at
230  // address base[i] and extends for len[i] elements. It's always
231  // true (so long as the indices are in bounds) that
232  //
233  // pending[i].base + pending[i].len == pending[i+1].base
234  //
235  // so we could cut the storage for this, but it's a minor amount,
236  // and keeping all the info explicit simplifies the code.
238  struct s_slice m_pending[MAX_MERGE_PENDING];
239  };
240 
241  compare_fcn_type m_compare;
242 
244 
245  template <typename Comp>
246  void binarysort (T *data, octave_idx_type nel,
247  octave_idx_type start, Comp comp);
248 
249  template <typename Comp>
250  void binarysort (T *data, octave_idx_type *idx, octave_idx_type nel,
251  octave_idx_type start, Comp comp);
252 
253  template <typename Comp>
254  octave_idx_type count_run (T *lo, octave_idx_type n, bool& descending,
255  Comp comp);
256 
257  template <typename Comp>
258  octave_idx_type gallop_left (T key, T *a, octave_idx_type n,
259  octave_idx_type hint, Comp comp);
260 
261  template <typename Comp>
262  octave_idx_type gallop_right (T key, T *a, octave_idx_type n,
263  octave_idx_type hint, Comp comp);
264 
265  template <typename Comp>
266  int merge_lo (T *pa, octave_idx_type na,
267  T *pb, octave_idx_type nb,
268  Comp comp);
269 
270  template <typename Comp>
271  int merge_lo (T *pa, octave_idx_type *ipa, octave_idx_type na,
272  T *pb, octave_idx_type *ipb, octave_idx_type nb,
273  Comp comp);
274 
275  template <typename Comp>
276  int merge_hi (T *pa, octave_idx_type na,
277  T *pb, octave_idx_type nb,
278  Comp comp);
279 
280  template <typename Comp>
281  int merge_hi (T *pa, octave_idx_type *ipa, octave_idx_type na,
282  T *pb, octave_idx_type *ipb, octave_idx_type nb,
283  Comp comp);
284 
285  template <typename Comp>
286  int merge_at (octave_idx_type i, T *data, Comp comp);
287 
288  template <typename Comp>
289  int merge_at (octave_idx_type i, T *data, octave_idx_type *idx, Comp comp);
290 
291  template <typename Comp>
292  int merge_collapse (T *data, Comp comp);
293 
294  template <typename Comp>
295  int merge_collapse (T *data, octave_idx_type *idx, Comp comp);
296 
297  template <typename Comp>
298  int merge_force_collapse (T *data, Comp comp);
299 
300  template <typename Comp>
301  int merge_force_collapse (T *data, octave_idx_type *idx, Comp comp);
302 
303  octave_idx_type merge_compute_minrun (octave_idx_type n);
304 
305  template <typename Comp>
306  void sort (T *data, octave_idx_type nel, Comp comp);
307 
308  template <typename Comp>
309  void sort (T *data, octave_idx_type *idx, octave_idx_type nel, Comp comp);
310 
311  template <typename Comp>
312  bool issorted (const T *data, octave_idx_type nel, Comp comp);
313 
314  template <typename Comp>
315  void sort_rows (const T *data, octave_idx_type *idx,
316  octave_idx_type rows, octave_idx_type cols,
317  Comp comp);
318 
319  template <typename Comp>
320  bool is_sorted_rows (const T *data, octave_idx_type rows,
321  octave_idx_type cols, Comp comp);
322 
323  template <typename Comp>
324  octave_idx_type lookup (const T *data, octave_idx_type nel,
325  const T& value, Comp comp);
326 
327  template <typename Comp>
328  void lookup (const T *data, octave_idx_type nel,
329  const T *values, octave_idx_type nvalues,
330  octave_idx_type *idx, Comp comp);
331 
332  template <typename Comp>
333  void lookup_sorted (const T *data, octave_idx_type nel,
334  const T *values, octave_idx_type nvalues,
335  octave_idx_type *idx, bool rev, Comp comp);
336 
337  template <typename Comp>
338  void nth_element (T *data, octave_idx_type nel,
340  Comp comp);
341 };
342 
343 template <typename T>
344 class
345 vec_index
346 {
347 public:
348  T m_vec;
350 };
351 #endif
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
void set_compare(compare_fcn_type comp)
Definition: oct-sort.h:118
octave_sort(const octave_sort &)=delete
compare_fcn_type m_compare
Definition: oct-sort.h:241
MergeState * m_ms
Definition: oct-sort.h:243
if_then_else< is_class_type< T >::no, T, T const & >::result type
Definition: lo-traits.h:121
octave_idx_type m_indx
Definition: oct-sort.h:349
octave_idx_type n
Definition: mx-inlines.cc:753
sortmode
Definition: oct-sort.h:95
@ UNSORTED
Definition: oct-sort.h:95
@ ASCENDING
Definition: oct-sort.h:95
@ DESCENDING
Definition: oct-sort.h:95
octave_idx_type m_n
Definition: oct-sort.h:237
MergeState(const MergeState &)=delete
octave_idx_type m_alloced
Definition: oct-sort.h:227
octave_idx_type * m_ia
Definition: oct-sort.h:226
octave_idx_type m_min_gallop
Definition: oct-sort.h:221
octave_idx_type m_base
Definition: oct-sort.h:192