Mercurial > octave
view liboctave/util/oct-sort.h @ 31605:e88a07dec498 stable
maint: Use macros to begin/end C++ namespaces.
* oct-conf-post-public.in.h: Define two macros (OCTAVE_BEGIN_NAMESPACE,
OCTAVE_END_NAMESPACE) that can be used to start/end a namespace.
* mk-opts.pl, build-env.h, build-env.in.cc, __betainc__.cc, __contourc__.cc,
__dsearchn__.cc, __eigs__.cc, __expint__.cc, __ftp__.cc, __gammainc__.cc,
__ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __lin_interpn__.cc,
__magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, amd.cc, auto-shlib.cc,
auto-shlib.h, balance.cc, base-text-renderer.cc, base-text-renderer.h,
besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h,
call-stack.cc, call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc,
colloc.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, data.h, debug.cc,
defaults.cc, defaults.h, defun-int.h, defun.cc, det.cc, dirfns.cc, display.cc,
display.h, dlmread.cc, dmperm.cc, dot.cc, dynamic-ld.cc, dynamic-ld.h, eig.cc,
ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h,
event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc,
fcn-info.h, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc,
ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, getgrent.cc, getpwent.cc,
getrusage.cc, givens.cc, gl-render.cc, gl-render.h, gl2ps-print.cc,
gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc,
graphics.in.h, gsvd.cc, gtk-manager.cc, gtk-manager.h, hash.cc, help.cc,
help.h, hess.cc, hex2num.cc, hook-fcn.cc, hook-fcn.h, input.cc, input.h,
interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h,
inv.cc, jsondecode.cc, jsonencode.cc, kron.cc, latex-text-renderer.cc,
latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h,
lookup.cc, ls-ascii-helper.cc, ls-ascii-helper.h, ls-oct-text.cc, ls-utils.cc,
ls-utils.h, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex-private.h,
mex.cc, mgorth.cc, nproc.cc, oct-fstrm.cc, oct-fstrm.h, oct-hdf5-types.cc,
oct-hdf5-types.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h,
oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h,
oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h,
oct-strstrm.cc, oct-strstrm.h, oct-tex-lexer.in.ll, oct-tex-parser.yy,
ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-flt-fmt.cc,
pr-output.cc, procstream.cc, procstream.h, psi.cc, qr.cc, quad.cc, quadcc.cc,
qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, settings.cc, settings.h,
sighandlers.cc, sighandlers.h, sparse-xdiv.cc, sparse-xdiv.h, sparse-xpow.cc,
sparse-xpow.h, sparse.cc, spparms.cc, sqrtm.cc, stack-frame.cc, stack-frame.h,
stream-euler.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc,
symbfact.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h,
symscope.cc, symscope.h, symtab.cc, symtab.h, syscalls.cc, sysdep.cc, sysdep.h,
text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc,
toplev.cc, tril.cc, tsearch.cc, typecast.cc, url-handle-manager.cc,
url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc,
variables.h, xdiv.cc, xdiv.h, xnorm.cc, xnorm.h, xpow.cc, xpow.h,
__delaunayn__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc,
__init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audiodevinfo.cc,
audioread.cc, convhulln.cc, fftw.cc, gzip.cc, mk-build-env-features.sh,
mk-builtins.pl, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc,
cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h,
cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h,
cdef-utils.cc, cdef-utils.h, ov-base.cc, ov-base.h, ov-bool-mat.cc,
ov-builtin.h, ov-cell.cc, ov-class.cc, ov-class.h, ov-classdef.cc,
ov-classdef.h, ov-complex.cc, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h,
ov-java.cc, ov-java.h, ov-mex-fcn.h, ov-null-mat.cc, ov-oncleanup.cc,
ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h,
ov.cc, ov.h, octave.cc, octave.h, mk-ops.sh, op-b-b.cc, op-b-bm.cc,
op-b-sbm.cc, op-bm-b.cc, op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc,
op-chm.cc, op-class.cc, op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-s.cc,
op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc,
op-cs-scm.cc, op-cs-sm.cc, op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc,
op-dm-template.cc, op-dms-template.cc, op-fcdm-fcdm.cc, op-fcm-fcm.cc,
op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc, op-fcn.cc, op-fcs-fcm.cc,
op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc, op-fdm-fdm.cc, op-fm-fcm.cc,
op-fm-fcs.cc, op-fm-fm.cc, op-fm-fs.cc, op-fs-fcm.cc, op-fs-fcs.cc,
op-fs-fm.cc, op-fs-fs.cc, op-i16-i16.cc, op-i32-i32.cc, op-i64-i64.cc,
op-i8-i8.cc, op-int-concat.cc, op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc,
op-m-scm.cc, op-m-sm.cc, op-mi.cc, op-pm-pm.cc, op-pm-scm.cc, op-pm-sm.cc,
op-pm-template.cc, op-range.cc, op-s-cm.cc, op-s-cs.cc, op-s-m.cc, op-s-s.cc,
op-s-scm.cc, op-s-sm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-sbm-sbm.cc,
op-scm-cm.cc, op-scm-cs.cc, op-scm-m.cc, op-scm-s.cc, op-scm-scm.cc,
op-scm-sm.cc, op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc, op-sm-s.cc, op-sm-scm.cc,
op-sm-sm.cc, op-str-m.cc, op-str-s.cc, op-str-str.cc, op-struct.cc,
op-ui16-ui16.cc, op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc, ops.h,
anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h,
comment-list.cc, comment-list.h, filepos.h, lex.h, lex.ll, oct-lvalue.cc,
oct-lvalue.h, oct-parse.yy, parse.h, profiler.cc, profiler.h,
pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h,
pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h,
pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h,
pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h,
pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc,
pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc,
pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc,
pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc,
pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc,
pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc,
pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-vm-eval.cc, pt-walk.cc, pt-walk.h,
pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h,
range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc,
chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h,
lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h,
oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h,
oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc,
qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc,
randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc,
sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc,
child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc,
file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h,
mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h,
oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc,
oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h,
base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h,
file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc,
lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h,
oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h,
oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h,
oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h,
pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h,
url-transfer.cc, url-transfer.h : Use new macros to begin/end C++ namespaces.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 01 Dec 2022 14:23:45 -0800 |
parents | e7fc6251b698 |
children | 597f3ee61a48 |
line wrap: on
line source
//////////////////////////////////////////////////////////////////////// // // Copyright (C) 2003-2022 The Octave Project Developers // // See the file COPYRIGHT.md in the top-level directory of this // distribution or <https://octave.org/copyright/>. // // This file is part of Octave. // // Octave is free software: you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Octave is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Octave; see the file COPYING. If not, see // <https://www.gnu.org/licenses/>. // // Code stolen in large part from Python's, listobject.c, which itself had // no license header. However, thanks to Tim Peters for the parts of the // code I ripped-off. // // As required in the Python license the short description of the changes // made are // // * convert the sorting code in listobject.cc into a generic class, // replacing PyObject* with the type of the class T. // // The Python license is // // PSF LICENSE AGREEMENT FOR PYTHON 2.3 // -------------------------------------- // // 1. This LICENSE AGREEMENT is between the Python Software Foundation // ("PSF"), and the Individual or Organization ("Licensee") accessing and // otherwise using Python 2.3 software in source or binary form and its // associated documentation. // // 2. Subject to the terms and conditions of this License Agreement, PSF // hereby grants Licensee a nonexclusive, royalty-free, world-wide // license to reproduce, analyze, test, perform and/or display publicly, // prepare derivative works, distribute, and otherwise use Python 2.3 // alone or in any derivative version, provided, however, that PSF's // License Agreement and PSF's notice of copyright, i.e., "Copyright (c) // 2001, 2002, 2003 Python Software Foundation; All Rights Reserved" are // retained in Python 2.3 alone or in any derivative version prepared by // Licensee. // // 3. In the event Licensee prepares a derivative work that is based on // or incorporates Python 2.3 or any part thereof, and wants to make // the derivative work available to others as provided herein, then // Licensee hereby agrees to include in any such work a brief summary of // the changes made to Python 2.3. // // 4. PSF is making Python 2.3 available to Licensee on an "AS IS" // basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR // IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND // DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS // FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 2.3 WILL NOT // INFRINGE ANY THIRD PARTY RIGHTS. // // 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON // 2.3 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS // A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 2.3, // OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. // // 6. This License Agreement will automatically terminate upon a material // breach of its terms and conditions. // // 7. Nothing in this License Agreement shall be deemed to create any // relationship of agency, partnership, or joint venture between PSF and // Licensee. This License Agreement does not grant permission to use PSF // trademarks or trade name in a trademark sense to endorse or promote // products or services of Licensee, or any third party. // // 8. By copying, installing or otherwise using Python 2.3, Licensee // agrees to be bound by the terms and conditions of this License // Agreement. // //////////////////////////////////////////////////////////////////////// #if ! defined (octave_oct_sort_h) #define octave_oct_sort_h 1 #include "octave-config.h" #include <functional> #include "lo-traits.h" // Enum for type of sort function enum sortmode { UNSORTED = 0, ASCENDING, DESCENDING }; template <typename T> class OCTAVE_TEMPLATE_API octave_sort { public: typedef std::function<bool (typename ref_param<T>::type, typename ref_param<T>::type)> compare_fcn_type; octave_sort (void); octave_sort (const compare_fcn_type&); // No copying! octave_sort (const octave_sort&) = delete; octave_sort& operator = (const octave_sort&) = delete; ~octave_sort (void); void set_compare (const compare_fcn_type& comp) { m_compare = comp; } void set_compare (sortmode mode); // Sort an array in-place. void sort (T *data, octave_idx_type nel); // Ditto, but also permute the passed indices (may not be valid indices). void sort (T *data, octave_idx_type *idx, octave_idx_type nel); // Check whether an array is sorted. bool issorted (const T *data, octave_idx_type nel); // Sort a matrix by rows, return a permutation // vector. void sort_rows (const T *data, octave_idx_type *idx, octave_idx_type rows, octave_idx_type cols); // Determine whether a matrix (as a contiguous block) is sorted by rows. bool is_sorted_rows (const T *data, octave_idx_type rows, octave_idx_type cols); // Do a binary lookup in a sorted array. octave_idx_type lookup (const T *data, octave_idx_type nel, const T& value); // Ditto, but for an array. void lookup (const T *data, octave_idx_type nel, const T *values, octave_idx_type nvalues, octave_idx_type *idx); // A linear merge of two sorted tables. rev indicates the second table is // in reverse order. void lookup_sorted (const T *data, octave_idx_type nel, const T *values, octave_idx_type nvalues, octave_idx_type *idx, bool rev = false); // Rearranges the array so that the elements with indices // lo..up-1 are in their correct place. void nth_element (T *data, octave_idx_type nel, octave_idx_type lo, octave_idx_type up = -1); static bool ascending_compare (typename ref_param<T>::type, typename ref_param<T>::type); static bool descending_compare (typename ref_param<T>::type, typename ref_param<T>::type); private: // The maximum number of entries in a MergeState's pending-runs stack. // This is enough to sort arrays of size up to about // 32 * phi ** MAX_MERGE_PENDING // where phi ~= 1.618. 85 is ridiculously large enough, good for an array // with 2^64 elements. static const int MAX_MERGE_PENDING = 85; // When we get into galloping mode, we stay there until both runs win less // often than MIN_GALLOP consecutive times. See listsort.txt for more info. static const int MIN_GALLOP = 7; // Avoid malloc for small temp arrays. static const int MERGESTATE_TEMP_SIZE = 1024; // One MergeState exists on the stack per invocation of mergesort. // It's just a convenient way to pass state around among the helper // functions. // // DGB: This isn't needed with mergesort in a class, but it doesn't // slow things up, and it is likely to make my life easier for any // potential backporting of changes in the Python code. struct s_slice { octave_idx_type m_base, m_len; }; struct MergeState { public: MergeState (void) : m_min_gallop (), m_a (nullptr), m_ia (nullptr), m_alloced (0), m_n (0) { reset (); } // No copying! MergeState (const MergeState&) = delete; MergeState& operator = (const MergeState&) = delete; ~MergeState (void) { delete [] m_a; delete [] m_ia; } void reset (void) { m_min_gallop = MIN_GALLOP; m_n = 0; } void getmem (octave_idx_type need); void getmemi (octave_idx_type need); //-------- // This controls when we get *into* galloping mode. It's initialized to // MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for random // data, and lower for highly structured data. octave_idx_type m_min_gallop; // 'a' is temp storage to help with merges. It contains room for // alloced entries. T *m_a; // may point to temparray below octave_idx_type *m_ia; octave_idx_type m_alloced; // A stack of n pending runs yet to be merged. Run #i starts at address // base[i] and extends for len[i] elements. It's always true (so long as // the indices are in bounds) that // // pending[i].base + pending[i].len == pending[i+1].base // // so we could cut the storage for this, but it's a minor amount, // and keeping all the info explicit simplifies the code. octave_idx_type m_n; struct s_slice m_pending[MAX_MERGE_PENDING]; }; compare_fcn_type m_compare; MergeState *m_ms; template <typename Comp> void binarysort (T *data, octave_idx_type nel, octave_idx_type start, Comp comp); template <typename Comp> void binarysort (T *data, octave_idx_type *idx, octave_idx_type nel, octave_idx_type start, Comp comp); template <typename Comp> octave_idx_type count_run (T *lo, octave_idx_type n, bool& descending, Comp comp); template <typename Comp> octave_idx_type gallop_left (T key, T *a, octave_idx_type n, octave_idx_type hint, Comp comp); template <typename Comp> octave_idx_type gallop_right (T key, T *a, octave_idx_type n, octave_idx_type hint, Comp comp); template <typename Comp> int merge_lo (T *pa, octave_idx_type na, T *pb, octave_idx_type nb, Comp comp); template <typename Comp> int merge_lo (T *pa, octave_idx_type *ipa, octave_idx_type na, T *pb, octave_idx_type *ipb, octave_idx_type nb, Comp comp); template <typename Comp> int merge_hi (T *pa, octave_idx_type na, T *pb, octave_idx_type nb, Comp comp); template <typename Comp> int merge_hi (T *pa, octave_idx_type *ipa, octave_idx_type na, T *pb, octave_idx_type *ipb, octave_idx_type nb, Comp comp); template <typename Comp> int merge_at (octave_idx_type i, T *data, Comp comp); template <typename Comp> int merge_at (octave_idx_type i, T *data, octave_idx_type *idx, Comp comp); template <typename Comp> int merge_collapse (T *data, Comp comp); template <typename Comp> int merge_collapse (T *data, octave_idx_type *idx, Comp comp); template <typename Comp> int merge_force_collapse (T *data, Comp comp); template <typename Comp> int merge_force_collapse (T *data, octave_idx_type *idx, Comp comp); octave_idx_type merge_compute_minrun (octave_idx_type n); template <typename Comp> void sort (T *data, octave_idx_type nel, Comp comp); template <typename Comp> void sort (T *data, octave_idx_type *idx, octave_idx_type nel, Comp comp); template <typename Comp> bool issorted (const T *data, octave_idx_type nel, Comp comp); template <typename Comp> void sort_rows (const T *data, octave_idx_type *idx, octave_idx_type rows, octave_idx_type cols, Comp comp); template <typename Comp> bool is_sorted_rows (const T *data, octave_idx_type rows, octave_idx_type cols, Comp comp); template <typename Comp> octave_idx_type lookup (const T *data, octave_idx_type nel, const T& value, Comp comp); template <typename Comp> void lookup (const T *data, octave_idx_type nel, const T *values, octave_idx_type nvalues, octave_idx_type *idx, Comp comp); template <typename Comp> void lookup_sorted (const T *data, octave_idx_type nel, const T *values, octave_idx_type nvalues, octave_idx_type *idx, bool rev, Comp comp); template <typename Comp> void nth_element (T *data, octave_idx_type nel, octave_idx_type lo, octave_idx_type up, Comp comp); }; template <typename T> class OCTAVE_TEMPLATE_API vec_index { public: T m_vec; octave_idx_type m_indx; }; #endif