forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered_vector.T
More file actions
379 lines (329 loc) · 12.5 KB
/
ordered_vector.T
File metadata and controls
379 lines (329 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file ordered_vector.T
* @author drose
* @date 2002-02-02
*/
/**
* Inserts the indicated key into the ordered vector. The iterator is a hint
* to the expected position; if this is correct, the insert operation is
* likely to be faster. The return value is the iterator referencing the new
* element.
*
* This flavor of insert does not allow multiple copies of the same key to be
* inserted. If the key is already present, it is not inserted, and the
* iterator referencing the original value is returned.
*/
template<class Key, class Compare, class Vector>
typename ordered_vector<Key, Compare, Vector>::ITERATOR ordered_vector<Key, Compare, Vector>::
insert_unique(typename ordered_vector<Key, Compare, Vector>::ITERATOR position,
const typename ordered_vector<Key, Compare, Vector>::VALUE_TYPE &key) {
TAU_PROFILE("ordered_vector::insert_unique(iterator, const value_type &)", " ", TAU_USER);
if (position != end()) {
// If we're not inserting at the end, the element we're
// inserting before should not lexicographically precede this one.
if (_compare(*position, key)) {
return insert_unique(key).first;
} else if (!_compare(key, *position)) {
// Oops, !(*position < key) and !(key < *position). That means
// they're equivalent, and we shouldn't insert a new one.
return position;
}
}
if (position != begin()) {
// If we're not inserting at the beginning, this element should
// not lexicographically precede the one we're inserting after.
if (_compare(key, *(position - 1))) {
return insert_unique(key).first;
} else if (!_compare(*(position - 1), key)) {
// Once again, they're equivalent.
return position - 1;
}
}
// Otherwise, we may insert where the caller requested.
ITERATOR result = _vector.insert(position, key);
return result;
}
/**
* Inserts the indicated key into the ordered vector. The iterator is a hint
* to the expected position; if this is correct, the insert operation is
* likely to be faster. The return value is the iterator referencing the new
* element.
*
* This flavor of insert allows multiple copies of the
* same key to be inserted.
*/
template<class Key, class Compare, class Vector>
typename ordered_vector<Key, Compare, Vector>::ITERATOR ordered_vector<Key, Compare, Vector>::
insert_nonunique(typename ordered_vector<Key, Compare, Vector>::ITERATOR position,
const typename ordered_vector<Key, Compare, Vector>::VALUE_TYPE &key) {
TAU_PROFILE("ordered_vector::insert_nonunique(iterator, const value_type &)", " ", TAU_USER);
if (position != end()) {
// If we're not inserting at the end, the element we're
// inserting before should not lexicographically precede this one.
if (_compare(*position, key)) {
return insert_nonunique(key);
}
}
if (position != begin()) {
// If we're not inserting at the beginning, this element should
// not lexicographically precede the one we're inserting after.
if (_compare(key, *(position - 1))) {
return insert_nonunique(key);
}
}
// Otherwise, we may insert where the caller requested.
ITERATOR result = _vector.insert(position, key);
return result;
}
/**
* Ensures that the indicated range of elements is sorted correctly. Returns
* true if this is the case; otherwise, returns false.
*/
template<class Key, class Compare, class Vector>
bool ordered_vector<Key, Compare, Vector>::
verify_list_unique() const {
TAU_PROFILE("ordered_vector::verify_list_unique()", " ", TAU_USER);
if (!empty()) {
CONST_ITERATOR prev = begin();
CONST_ITERATOR i = begin();
++i;
while (i < end()) {
bool ordered_correctly = _compare(*prev, *i);
if (!ordered_correctly) {
return true;
}
prev = i;
++i;
}
}
return true;
}
/**
* Ensures that the indicated range of elements is sorted correctly. Returns
* true if this is the case; otherwise, returns false.
*/
template<class Key, class Compare, class Vector>
bool ordered_vector<Key, Compare, Vector>::
verify_list_nonunique() const {
TAU_PROFILE("ordered_vector::verify_list_nonunique()", " ", TAU_USER);
if (!empty()) {
CONST_ITERATOR prev = begin();
CONST_ITERATOR i = begin();
++i;
while (i < end()) {
bool ordered_correctly = !_compare(*i, *prev);
if (!ordered_correctly) {
return true;
}
prev = i;
++i;
}
}
return true;
}
/**
* The recursive implementation of find_insert_position().
*/
template<class Key, class Compare, class Vector>
typename ordered_vector<Key, Compare, Vector>::ITERATOR ordered_vector<Key, Compare, Vector>::
r_find_insert_position(typename ordered_vector<Key, Compare, Vector>::ITERATOR first,
typename ordered_vector<Key, Compare, Vector>::ITERATOR last,
const typename ordered_vector<Key, Compare, Vector>::KEY_TYPE &key) {
if (first == last) {
// The list is empty; the insert position is the last of the list.
return last;
}
ITERATOR center = first + (last - first) / 2;
nassertr(center < last, last);
if (_compare(key, *center)) {
// Insert before the center.
return r_find_insert_position(first, center, key);
} else {
// Insert after the center.
return r_find_insert_position(center + 1, last, key);
}
}
/**
* The recursive implementation of find().
*/
template<class Key, class Compare, class Vector>
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR ordered_vector<Key, Compare, Vector>::
r_find(typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR first,
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR last,
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR not_found,
const typename ordered_vector<Key, Compare, Vector>::KEY_TYPE &key) const {
if (first == last) {
// The list is empty; the key is not on the list.
return not_found;
}
CONST_ITERATOR center = first + (last - first) / 2;
nassertr(center < last, last);
if (_compare(key, *center)) {
// It must be before the center.
return r_find(first, center, not_found, key);
} else if (_compare(*center, key)) {
// It must be after the center.
return r_find(center + 1, last, not_found, key);
} else {
// Here it is!
return center;
}
}
/**
* The recursive implementation of find_particular().
*/
template<class Key, class Compare, class Vector>
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR ordered_vector<Key, Compare, Vector>::
r_find_particular(typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR first,
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR last,
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR not_found,
const typename ordered_vector<Key, Compare, Vector>::KEY_TYPE &key) const {
if (first == last) {
// The list is empty; the key is not on the list.
return not_found;
}
CONST_ITERATOR center = first + (last - first) / 2;
nassertr(center < last, last);
if (_compare(key, *center)) {
// It must be before the center.
return r_find_particular(first, center, not_found, key);
} else if (_compare(*center, key)) {
// It must be after the center.
return r_find_particular(center + 1, last, not_found, key);
} else {
// The center's sort matches the key's sort. It could be either
// before or after the center. First try after.
CONST_ITERATOR i = center;
while (i < last && !_compare(key, *i)) {
if ((*i) == key) {
return i;
}
++i;
}
// No, try before.
i = center;
--i;
while (i >= first && !_compare(key, *i)) {
if ((*i) == key) {
return i;
}
--i;
}
// No such key!
return not_found;
}
}
/**
* The recursive implementation of count().
*/
template<class Key, class Compare, class Vector>
typename ordered_vector<Key, Compare, Vector>::SIZE_TYPE ordered_vector<Key, Compare, Vector>::
r_count(typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR first,
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR last,
const typename ordered_vector<Key, Compare, Vector>::KEY_TYPE &key) const {
if (first == last) {
// The list is empty; the key is not on the list.
return 0;
}
CONST_ITERATOR center = first + (last - first) / 2;
nassertr(center < last, 0);
if (_compare(key, *center)) {
// It must be before the center.
return r_count(first, center, key);
} else if (_compare(*center, key)) {
// It must be after the center.
return r_count(center + 1, last, key);
} else {
// The center matches the key; the range is here.
size_type lower = r_count(first, center, key);
size_type upper = r_count(center + 1, last, key);
return lower + 1 + upper;
}
}
/**
* The recursive implementation of lower_bound().
*/
template<class Key, class Compare, class Vector>
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR ordered_vector<Key, Compare, Vector>::
r_lower_bound(typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR first,
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR last,
const typename ordered_vector<Key, Compare, Vector>::KEY_TYPE &key) const {
if (first == last) {
// The list is empty; the key is not on the list.
return last;
}
CONST_ITERATOR center = first + (last - first) / 2;
nassertr(center < last, last);
if (_compare(key, *center)) {
// It must be before the center.
return r_lower_bound(first, center, key);
} else if (_compare(*center, key)) {
// It must be after the center.
return r_lower_bound(center + 1, last, key);
} else {
// The center matches the key; thus, the first element not less
// than key is at or before the center.
return r_lower_bound(first, center, key);
}
}
/**
* The recursive implementation of upper_bound().
*/
template<class Key, class Compare, class Vector>
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR ordered_vector<Key, Compare, Vector>::
r_upper_bound(typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR first,
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR last,
const typename ordered_vector<Key, Compare, Vector>::KEY_TYPE &key) const {
if (first == last) {
// The list is empty; the key is not on the list.
return last;
}
const_iterator center = first + (last - first) / 2;
nassertr(center < last, last);
if (_compare(key, *center)) {
// It must be before the center.
return r_upper_bound(first, center, key);
} else if (_compare(*center, key)) {
// It must be after the center.
return r_upper_bound(center + 1, last, key);
} else {
// The center matches the key; thus, the first element greater
// than key is after the center.
return r_upper_bound(center + 1, last, key);
}
}
/**
* The recursive implementation of equal_range().
*/
template<class Key, class Compare, class Vector>
std::pair<typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR, typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR> ordered_vector<Key, Compare, Vector>::
r_equal_range(typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR first,
typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR last,
const typename ordered_vector<Key, Compare, Vector>::KEY_TYPE &key) const {
typedef std::pair<typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR, typename ordered_vector<Key, Compare, Vector>::CONST_ITERATOR> pair_type;
if (first == last) {
// The list is empty; the key is not on the list.
return pair_type(last, last);
}
CONST_ITERATOR center = first + (last - first) / 2;
nassertr(center < last, pair_type(last, last));
if (_compare(key, *center)) {
// It must be before the center.
return r_equal_range(first, center, key);
} else if (_compare(*center, key)) {
// It must be after the center.
return r_equal_range(center + 1, last, key);
} else {
// The center matches the key; the range is here.
CONST_ITERATOR lower = r_lower_bound(first, center, key);
CONST_ITERATOR upper = r_upper_bound(center + 1, last, key);
return pair_type(lower, upper);
}
}