comparison liboctave/oct-sort.h @ 4851:047ff938b0d9

[project @ 2004-04-06 17:12:14 by jwe]
author jwe
date Tue, 06 Apr 2004 17:14:17 +0000
parents
children 14027e0bafa4
comparison
equal deleted inserted replaced
4850:8cc4818a0de0 4851:047ff938b0d9
1 /*
2 Copyright (C) 2003 David Bateman
3
4 This file is part of Octave.
5
6 Octave is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 Octave is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Octave; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 Code stolen in large part from Python's, listobject.c, which itself had
21 no license header. However, thanks to Tim Peters for the parts of the
22 code I ripped-off.
23
24 As required in the Python license the short description of the changes
25 made are
26
27 * convert the sorting code in listobject.cc into a generic class,
28 replacing PyObject* with the type of the class T.
29
30 The Python license is
31
32 PSF LICENSE AGREEMENT FOR PYTHON 2.3
33 --------------------------------------
34
35 1. This LICENSE AGREEMENT is between the Python Software Foundation
36 ("PSF"), and the Individual or Organization ("Licensee") accessing and
37 otherwise using Python 2.3 software in source or binary form and its
38 associated documentation.
39
40 2. Subject to the terms and conditions of this License Agreement, PSF
41 hereby grants Licensee a nonexclusive, royalty-free, world-wide
42 license to reproduce, analyze, test, perform and/or display publicly,
43 prepare derivative works, distribute, and otherwise use Python 2.3
44 alone or in any derivative version, provided, however, that PSF's
45 License Agreement and PSF's notice of copyright, i.e., "Copyright (c)
46 2001, 2002, 2003 Python Software Foundation; All Rights Reserved" are
47 retained in Python 2.3 alone or in any derivative version prepared by
48 Licensee.
49
50 3. In the event Licensee prepares a derivative work that is based on
51 or incorporates Python 2.3 or any part thereof, and wants to make
52 the derivative work available to others as provided herein, then
53 Licensee hereby agrees to include in any such work a brief summary of
54 the changes made to Python 2.3.
55
56 4. PSF is making Python 2.3 available to Licensee on an "AS IS"
57 basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
58 IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
59 DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
60 FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 2.3 WILL NOT
61 INFRINGE ANY THIRD PARTY RIGHTS.
62
63 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
64 2.3 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS
65 A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 2.3,
66 OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
67
68 6. This License Agreement will automatically terminate upon a material
69 breach of its terms and conditions.
70
71 7. Nothing in this License Agreement shall be deemed to create any
72 relationship of agency, partnership, or joint venture between PSF and
73 Licensee. This License Agreement does not grant permission to use PSF
74 trademarks or trade name in a trademark sense to endorse or promote
75 products or services of Licensee, or any third party.
76
77 8. By copying, installing or otherwise using Python 2.3, Licensee
78 agrees to be bound by the terms and conditions of this License
79 Agreement.
80 */
81
82 #if !defined (octave_sort_h)
83 #define octave_sort_h 1
84
85 #ifdef HAVE_CONFIG_H
86 #include <config.h>
87 #endif
88
89 #include "oct-obj.h"
90 #include "lo-mappers.h"
91 #include "quit.h"
92
93 /* The maximum number of entries in a MergeState's pending-runs stack.
94 * This is enough to sort arrays of size up to about
95 * 32 * phi ** MAX_MERGE_PENDING
96 * where phi ~= 1.618. 85 is ridiculously large enough, good for an array
97 * with 2**64 elements.
98 */
99 #define MAX_MERGE_PENDING 85
100
101 /* When we get into galloping mode, we stay there until both runs win less
102 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
103 */
104 #define MIN_GALLOP 7
105
106 /* Avoid malloc for small temp arrays. */
107 #define MERGESTATE_TEMP_SIZE 1024
108
109 template <class T>
110 class
111 octave_sort
112 {
113 public:
114 octave_sort (void);
115
116 octave_sort (bool (*comp) (T, T));
117
118 ~octave_sort (void) { merge_freemem ( ); }
119
120 void sort (T *v, int elements);
121
122 private:
123 /* One MergeState exists on the stack per invocation of mergesort. It's just
124 * a convenient way to pass state around among the helper functions.
125 *
126 * DGB: This isn't needed with mergesort in a class, but it doesn't slow
127 * things up, and it is likely to make my life easier for any potential
128 * backporting of changes in the Python code.
129 */
130
131 struct s_slice
132 {
133 T *base;
134 int len;
135 };
136
137 typedef struct s_MergeState
138 {
139 /* This controls when we get *into* galloping mode. It's initialized
140 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
141 * random data, and lower for highly structured data.
142 */
143 int min_gallop;
144
145 /* 'a' is temp storage to help with merges. It contains room for
146 * alloced entries.
147 */
148 T *a; /* may point to temparray below */
149 int alloced;
150
151 /* A stack of n pending runs yet to be merged. Run #i starts at
152 * address base[i] and extends for len[i] elements. It's always
153 * true (so long as the indices are in bounds) that
154 *
155 * pending[i].base + pending[i].len == pending[i+1].base
156 *
157 * so we could cut the storage for this, but it's a minor amount,
158 * and keeping all the info explicit simplifies the code.
159 */
160 int n;
161 struct s_slice pending[MAX_MERGE_PENDING];
162 } MergeState;
163
164 bool (*compare)(T, T);
165
166 MergeState ms;
167
168 void reverse_slice (T *lo, T *hi);
169
170 void binarysort (T *lo, T *hi, T *start);
171
172 int count_run(T *lo, T *hi, int *descending);
173
174 int gallop_left(T key, T *a, int n, int hint);
175
176 int gallop_right(T key, T *a, int n, int hint);
177
178 void merge_init(void);
179
180 void merge_freemem(void);
181
182 int merge_getmem(int need);
183
184 int merge_lo(T *pa, int na, T *pb, int nb);
185
186 int merge_hi(T *pa, int na, T *pb, int nb);
187
188 int merge_at(int i);
189
190 int merge_collapse(void);
191
192 int merge_force_collapse(void);
193
194 int merge_compute_minrun(int n);
195 };
196
197 #endif
198
199 /*
200 ;;; Local Variables: ***
201 ;;; mode: C++ ***
202 ;;; End: ***
203 */