Debian Codegraph @

sys/Collection.h

raw symbols SCIP
1 #ifndef _Collection_h_
2 #define _Collection_h_
3 /* Collection.h
4 *
5 * Copyright (C) 1992-2005,2007,2008,2011,2012,2014-2022 Paul Boersma
6 *
7 * This code is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
11 *
12 * This code is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this work. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 * Collection objects contain a number of items whose class is a subclass of Thing or Daata.
23 */
24
25 #include "Simple.h"
26
27
28 #pragma mark - class Collection
29
30 /**
31 An object of type Collection is a collection of objects of any class T*
32 that is a subclass of Thing or Daata.
33 These elements ("items") are either owned by the Collection, or they are references.
34 You can access the items as `at [1]` through `at [size]`.
35 There can be no null items.
36
37 @param size the current number of items
38 @param at[1..size] the items
39 */
40 template <typename T> struct CollectionOf;
41
42 typedef CollectionOf<structDaata> _CollectionOfDaata;
43 extern void _CollectionOfDaata_v1_copy (const _CollectionOfDaata* me, _CollectionOfDaata* thee);
44 extern bool _CollectionOfDaata_v1_equal (_CollectionOfDaata* me, _CollectionOfDaata* thee);
45 extern bool _CollectionOfDaata_v1_canWriteAsEncoding (_CollectionOfDaata* me, int outputEncoding);
46 extern void _CollectionOfDaata_v1_writeText (_CollectionOfDaata* me, MelderFile openFile);
47 extern void _CollectionOfDaata_v1_readText (_CollectionOfDaata* me, MelderReadText text, int formatVersion);
48 extern void _CollectionOfDaata_v1_writeBinary (_CollectionOfDaata* me, FILE *f);
49 extern void _CollectionOfDaata_v1_readBinary (_CollectionOfDaata* me, FILE *f, int formatVersion);
50
51 template <typename T /*Melder_ENABLE_IF_ISA (T, structThing)*/>
52 struct ArrayOf {
53 T** _elements { nullptr };
54 T*& operator[] (integer i) const {
55 return our _elements [i];
56 }
57 };
58
59 /*
60 The following has to be global, not a static field,
61 because not all static fields in the same template have the same address,
62 and equality of address is needed by the recursive showMembers function in DataEditor.
63 We cannot even declare this inline, because of ordering problems.
64 */
65 extern structData_Description theCollectionOfDaata_v_description [];
66
67 template <typename T /*Melder_ENABLE_IF_ISA (T, structThing)*/>
68 struct CollectionOf : structDaata {
69 ArrayOf <T> at;
70 integer size { 0 };
71 integer _capacity { 0 };
72 bool _ownItems { true };
73 bool _ownershipInitialized { false };
74
75 CollectionOf () {
76 extern ClassInfo classCollection;
77 our classInfo = classCollection;
78 our name. reset();
79 }
80 virtual ~ CollectionOf () {
81 /*
82 We cannot simply do
83 //our v9_destroy ();
84 because C++ will implicitly call the destructor for structDaata,
85 whereas structCollection::v9_destroy explicitly calls structDaata::v9_destroy;
86 calling v9_destroy here would therefore free structThing::name twice,
87 which may not crash Praat (assuming that `name` is nulled the first time)
88 but which is not how destruction should be organized.
89 */
90 if (our at._elements) {
91 if (our _ownItems)
92 for (integer i = 1; i <= our size; i ++)
93 _Thing_forget (our at [i]);
94 our at._elements ++; // convert from base-1 to base-0
95 Melder_free (our at._elements);
96 }
97 }
98 //explicit operator bool () const noexcept {
99 // return !! our item;
100 //}
101 CollectionOf<T>& operator= (const CollectionOf<T>&) = delete; // disable copy assignment from an l-value of class T*
102 template <class Y> CollectionOf<T>& operator= (const CollectionOf<Y>&) = delete; // disable copy assignment from an l-value of a descendant class of T*
103 CollectionOf<T> (CollectionOf<T>&& other) noexcept :
104 at (other.at),
105 size (other.size),
106 _capacity (other. _capacity),
107 _ownItems (other. _ownItems),
108 _ownershipInitialized (other. _ownershipInitialized)
109 {
110 other.at._elements = nullptr;
111 other.size = 0;
112 other. _capacity = 0;
113 other. _ownItems = false;
114 other. _ownershipInitialized = false;
115 }
116 template <class Y> CollectionOf<T> (CollectionOf<Y>&& other) noexcept :
117 at (other.at),
118 size (other.size),
119 _capacity (other. _capacity),
120 _ownItems (other. _ownItems),
121 _ownershipInitialized (other. _ownershipInitialized)
122 {
123 other.at._elements = nullptr;
124 other.size = 0;
125 other. _capacity = 0;
126 other. _ownItems = false;
127 other. _ownershipInitialized = false;
128 }
129 CollectionOf<T>& operator= (CollectionOf<T>&& other) noexcept {
130 if (other.at._elements != our at._elements) {
131 if (our at._elements) {
132 if (our _ownItems)
133 for (integer i = 1; i <= our size; i ++)
134 _Thing_forget (our at [i]);
135 our at._elements ++; // convert from base-1 to base-0
136 Melder_free (our at._elements);
137 }
138 }
139 our at = other.at;
140 our size = other.size;
141 our _capacity = other. _capacity;
142 our _ownItems = other. _ownItems;
143 our _ownershipInitialized = other. _ownershipInitialized;
144 other.at._elements = nullptr;
145 other.size = 0;
146 other. _capacity = 0;
147 other. _ownItems = false;
148 other. _ownershipInitialized = false;
149 return *this;
150 }
151 template <class Y> CollectionOf<T>& operator= (CollectionOf<Y>&& other) noexcept {
152 if (other.at._elements != our at._elements) {
153 if (our at._elements) {
154 if (our _ownItems)
155 for (integer i = 1; i <= our size; i ++)
156 _Thing_forget (our at [i]);
157 our at._elements ++; // convert from base-1 to base-0
158 Melder_free (our at._elements);
159 }
160 }
161 our at = other.at;
162 our size = other.size;
163 our _capacity = other. _capacity;
164 our _ownItems = other. _ownItems;
165 our _ownershipInitialized = other. _ownershipInitialized;
166 other.at._elements = nullptr;
167 other.size = 0;
168 other. _capacity = 0;
169 other. _ownItems = false;
170 other. _ownershipInitialized = false;
171 return *this;
172 }
173 CollectionOf<T>&& move () noexcept { return static_cast <CollectionOf<T>&&> (*this); }
174
175 void _initializeOwnership (bool ownItems) {
176 if (our _ownershipInitialized) {
177 Melder_assert (our _ownItems == ownItems);
178 } else {
179 our _ownItems = ownItems;
180 our _ownershipInitialized = true;
181 }
182 }
183 void _grow (integer newCapacity) {
184 if (newCapacity <= our _capacity)
185 return;
186 T** oldItem_base0 = ( our at._elements ? our at._elements + 1 : nullptr ); // convert from base-1 to base-0
187 T** newItem_base0 = (T**) Melder_realloc (oldItem_base0, newCapacity * (int64) sizeof (T*));
188 our at._elements = newItem_base0 - 1; // convert from base-0 to base-1
189 our _capacity = newCapacity;
190 }
191 void _makeRoomForOneMoreItem (integer pos) {
192 if (our size >= our _capacity) {
193 const integer newCapacity = 2 * our _capacity + 30; // enough room to guarantee space for one more item, if _capacity >= 0
194 T** oldItem_base0 = ( our at._elements ? our at._elements + 1 : nullptr ); // convert from base-1 to base-0
195 T** newItem_base0 = (T**) Melder_realloc (oldItem_base0, newCapacity * (int64) sizeof (T*));
196 our at._elements = newItem_base0 - 1; // convert from base-0 to base-1
197 our _capacity = newCapacity;
198 }
199 our size ++;
200 for (integer i = our size; i > pos; i --)
201 our at [i] = our at [i - 1];
202 }
203 T* _insertItem_move (autoSomeThing <T> data, integer pos) {
204 our _initializeOwnership (true);
205 our _makeRoomForOneMoreItem (pos);
206 return our at [pos] = data.releaseToAmbiguousOwner();
207 }
208 void _insertItem_ref (T* data, integer pos) {
209 our _initializeOwnership (false);
210 our _makeRoomForOneMoreItem (pos);
211 our at [pos] = data;
212 }
213
214 /**
215 Add `thing` to the collection.
216 @pre
217 !! thing;
218 @post
219 my size >= my old size
220
221 You don't transfer ownership of `thing` to the Collection.
222
223 You cannot call both addItem_move() and addItem_ref() on the same Collection.
224 */
225 void addItem_ref (T* thing) {
226 Melder_assert (thing);
227 const integer index = our _v_position (thing);
228 if (index != 0)
229 our _insertItem_ref (thing, index);
230 else
231 our _initializeOwnership (false);
232 }
233
234 /**
235 Add 'thing' to the collection.
236 @pre
237 !! thing;
238 @post
239 my size >= my old size
240
241 You transfer ownership of 'thing' to the Collection.
242
243 You cannot call both addItem_move() and addItem_ref() on the same Collection.
244 For a SortedSet, this may mean that the Collection immediately disposes of 'item',
245 if that item already occurred in the Collection.
246 */
247 T* addItem_move (autoSomeThing<T> thing) {
248 T* thingRef = thing.get();
249 const integer index = our _v_position (thingRef);
250 if (index != 0) {
251 return our _insertItem_move (thing.move(), index);
252 } else {
253 our _initializeOwnership (true);
254 thing. reset(); // could not insert; I am the owner, so I must dispose of the data
255 return nullptr;
256 }
257 }
258
259 /**
260 Remove the item from the collection, without destroying it.
261 @post
262 item not found || my size == my old size - 1;
263
264 Usage:
265 this is the way in which an item can detach itself from a list;
266 often used just before the item is destroyed, hence the name of this procedure.
267 */
268 void undangleItem (Thing thing) {
269 for (integer i = our size; i > 0; i --) {
270 if (our at [i] == thing) {
271 for (integer j = i; j < our size; j ++)
272 our at [j] = our at [j + 1];
273 our size --;
274 }
275 }
276 }
277
278 /**
279 For subtractItem_move(): FIXME
280 Remove the item at 'position' from the collection and transfer ownership to the caller.
281 Return value:
282 the subtracted item; the caller is responsible for eventually removing it.
283 Preconditions:
284 1 <= position <= my size;
285 Postconditions:
286 my size == my old size - 1;
287 my _capacity not changed;
288 */
289 autoSomeThing<T> subtractItem_move (integer pos) {
290 Melder_assert (pos >= 1 && pos <= our size);
291 Melder_assert (our _ownItems);
292 autoSomeThing<T> result;
293 result. adoptFromAmbiguousOwner (our at [pos]);
294 for (integer i = pos; i < our size; i ++)
295 our at [i] = our at [i + 1];
296 our size --;
297 return result;
298 }
299 T* subtractItem_ref (integer pos) {
300 Melder_assert (pos >= 1 && pos <= our size);
301 Melder_assert (! our _ownItems);
302 T* result = our at [pos];
303 for (integer i = pos; i < our size; i ++)
304 our at [i] = our at [i + 1];
305 our size --;
306 return result;
307 }
308 void replaceItem_ref (T* data, integer pos) {
309 Melder_assert (pos >= 1 && pos <= our size);
310 Melder_assert (! our _ownItems);
311 our at [pos] = data;
312 }
313 T* replaceItem_move (autoSomeThing <T> data, integer pos) {
314 Melder_assert (pos >= 1 && pos <= our size);
315 Melder_assert (our _ownItems);
316 _Thing_forget (our at [pos]);
317 return our at [pos] = data.releaseToAmbiguousOwner();
318 }
319
320 /**
321 Remove the item at 'position' from the collection and from memory.
322 @pre
323 1 <= position <= my size;
324 @post
325 my size == my old size - 1;
326 my _capacity not changed;
327 */
328 void removeItem (integer pos) {
329 Melder_assert (pos >= 1 && pos <= our size);
330 if (our _ownItems)
331 _Thing_forget (our at [pos]);
332 for (integer i = pos; i < our size; i ++)
333 our at [i] = our at [i + 1];
334 our size --;
335 }
336
337 /**
338 Remove all items from the collection and from memory.
339 @post
340 my size == 0;
341 my _capacity not changed;
342 */
343 void removeAllItems () {
344 if (our _ownItems)
345 for (integer i = 1; i <= our size; i ++)
346 _Thing_forget (our at [i]);
347 our size = 0;
348 }
349
350 /**
351 Release as much memory as possible without affecting the items.
352 @post
353 my _capacity == max (my size, 1);
354 */
355 void shrinkToFit () {
356 our _capacity = ( our size > 0 ? our size : 1 );
357 our at ++;
358 our at = (T**) Melder_realloc (our at, our _capacity * (int64) sizeof (Thing));
359 our at --;
360 }
361 void sort (int (*compare) (T*, T*)) {
362 if (our size < 2)
363 return;
364 T** a = our at._elements;
365 integer l = (our size >> 1) + 1, r = our size, i, j;
366 T* k;
367 for (;;) {
368 if (l > 1) {
369 l --;
370 k = a [l];
371 } else {
372 k = a [r];
373 a [r] = a [1];
374 r --;
375 if (r == 1) {
376 a [1] = k;
377 return;
378 }
379 }
380 j = l;
381 for (;;) {
382 i = j;
383 j = j << 1;
384 if (j > r)
385 break;
386 if (j < r && compare (a [j], a [j + 1]) < 0)
387 j ++;
388 if (compare (k, a [j]) >= 0)
389 break;
390 a [i] = a [j];
391 }
392 a [i] = k;
393 }
394 }
395 /*
396 Function:
397 merge two Collections into a new one. The class is the same as the type of `me`.
398 Postconditions:
399 result->size >= my size;
400 result->size >= thy size;
401 */
402 void merge (CollectionOf<T>* thee) {
403 try {
404 if (our classInfo != thy classInfo)
405 Melder_throw (U"The two collections are of different classes.");
406 if (our _ownershipInitialized && thy _ownershipInitialized && our _ownItems != thy _ownItems)
407 Melder_throw (U"Cannot mix data and references.");
408 if (! our _ownershipInitialized && ! thy _ownershipInitialized) {
409 Melder_assert (our size == 0 && thy size == 0);
410 return;
411 }
412 our _ownItems = ( our _ownershipInitialized ? our _ownItems : thy _ownItems );
413 for (integer i = 1; i <= thy size; i ++) {
414 T* item = thy at [i];
415 if (our _ownItems) {
416 if (! Thing_isa (item, classDaata))
417 Melder_throw (U"Cannot copy item of class ", Thing_className (item), U".");
418 our addItem_move (Data_copy (item));
419 } else {
420 our addItem_ref (item);
421 }
422 }
423 } catch (MelderError) {
424 Melder_throw (this, U" and ", thee, U" not merged." );
425 }
426 }
427
428 /*
429 CollectionOf overrides all virtual methods of Daata.
430 They are defined in Collection.cpp (if not in this header file).
431 These methods are needed only when a Collection object is used
432 as an independent pointer-to-object created with XXX_create ().
433 */
434
435 void v1_info () override {
436 MelderInfo_writeLine (our size, U" items");
437 }
438
439 void v9_destroy () noexcept override {
440 /*
441 The items are destroyed automatically by the destructor,
442 which is called by delete, which is called by forget().
443 So we only have to destroy the members of Daata,
444 many of which are not destroyed automatically.
445 */
446 structDaata::v9_destroy ();
447 }
448
449
450 void v1_copy (Daata data_to) const override { // copies all the items
451 _CollectionOfDaata_v1_copy (reinterpret_cast <const _CollectionOfDaata*> (this), reinterpret_cast<_CollectionOfDaata*> (data_to));
452 }
453 bool v1_equal (Daata data2) override { // compares 'my item [i]' with 'thy item [i]', i = 1..size
454 return _CollectionOfDaata_v1_equal (reinterpret_cast <_CollectionOfDaata*> (this), reinterpret_cast<_CollectionOfDaata*> (data2));
455 }
456 bool v1_canWriteAsEncoding (int outputEncoding) override {
457 return _CollectionOfDaata_v1_canWriteAsEncoding (reinterpret_cast <_CollectionOfDaata*> (this), outputEncoding);
458 }
459 void v1_writeText (MelderFile openFile) override {
460 _CollectionOfDaata_v1_writeText (reinterpret_cast <_CollectionOfDaata*> (this), openFile);
461 }
462 void v1_readText (MelderReadText text, int formatVersion) override {
463 _CollectionOfDaata_v1_readText (reinterpret_cast <_CollectionOfDaata*> (this), text, formatVersion);
464 }
465 void v1_writeBinary (FILE *f) override {
466 _CollectionOfDaata_v1_writeBinary (reinterpret_cast <_CollectionOfDaata*> (this), f);
467 }
468 void v1_readBinary (FILE *f, int formatVersion) override {
469 _CollectionOfDaata_v1_readBinary (reinterpret_cast <_CollectionOfDaata*> (this), f, formatVersion);
470 }
471 Data_Description v_description () override {
472 return & theCollectionOfDaata_v_description [0];
473 }
474
475 /*
476 CollectionOf<> introduces one virtual method of its own (not counting the destructor).
477 */
478 virtual integer _v_position (T* /* data */) {
479 return our size + 1; // at end
480 };
481 T **begin () const { return & our at [1]; }
482 T **end () const { return & our at [our size + 1]; }
483 };
484
485
486 #define _Collection_declare(klas,genericClass,itemClass) \
487 typedef genericClass<struct##itemClass> struct##klas; \
488 typedef genericClass<struct##itemClass> *klas; \
489 typedef autoSomeThing <genericClass<struct##itemClass>> auto##klas; \
490 extern struct structClassInfo theClassInfo_##klas; \
491 extern ClassInfo class##klas; \
492 static inline auto##klas klas##_create () { \
493 auto##klas me; \
494 me. adoptFromAmbiguousOwner (new genericClass<struct##itemClass>); \
495 theTotalNumberOfThings += 1; \
496 return me; \
497 }
498
499 _Collection_declare (Collection, CollectionOf, Thing);
500
501 #pragma mark - class Ordered
502 /*
503 An Ordered is a Collection in which the order of the elements is crucial.
504 It could also be called a List.
505 */
506
507 template <typename T>
508 struct OrderedOf : CollectionOf <T /*Melder_ENABLE_IF_ISA (T, structDaata)*/> {
509
510 OrderedOf () {
511 extern ClassInfo classOrdered;
512 our classInfo = classOrdered;
513 }
514
515 /**
516 Function:
517 insert an item at 'position'.
518 If 'position' is less than 1 or greater than the current 'size',
519 insert the item at the end.
520 */
521 T* addItemAtPosition_move (autoSomeThing <T> data, integer position) {
522 Melder_assert (data);
523 if (position < 1 || position > our size)
524 position = our size + 1;
525 return our _insertItem_move (data.move(), position);
526 }
527
528 OrderedOf<T>&& move () noexcept { return static_cast <OrderedOf<T>&&> (*this); }
529 };
530
531 _Collection_declare (Ordered, OrderedOf, Daata);
532
533 #pragma mark - class Sorted
534 /*
535 A Sorted is a sorted Collection.
536 Behaviour:
537 Sorted::addItem inserts an item at such a position that the collection stays sorted.
538 Sorted::merge yields a Sorted.
539 */
540
541 template <typename T /*Melder_ENABLE_IF_ISA (T, structDaata)*/>
542 struct SortedOf : CollectionOf <T> {
543 SortedOf () {
544 extern ClassInfo classSorted;
545 our classInfo = classSorted;
546 }
547 SortedOf<T>&& move () noexcept { return static_cast <SortedOf<T>&&> (*this); }
548
549 /***** Two routines for optimization. ******/
550 /* If you want to add a large group of items,
551 it is best to call addItem_unsorted () repeatedly,
552 and finish with Sorted::sort(), which uses the fast 'heapsort' algorithm,
553 and, when appropriate, also SortedSet::unicize(), which has linear complexity.
554 Calling addItem_move () repeatedly would be slower,
555 because on average half the collection is moved in memory
556 with every insertion.
557 */
558 /*
559 Function:
560 add an item to the collection, quickly at the end.
561 Warning:
562 this leaves the collection unsorted; follow by Sorted::sort ().
563 */
564 void addItem_unsorted_move (autoSomeThing <T> data) {
565 our _insertItem_move (data.move(), our size + 1);
566 }
567 /* Call this after a number of calls to Sorted_addItem_unsorted (). */
568 /* The procedure used is 'heapsort'. */
569 void sort () {
570 our CollectionOf<T>::sort (our v_getCompareHook ());
571 }
572
573 integer _v_position (T* data) override {
574 typename SortedOf<T>::CompareHook compare = our v_getCompareHook ();
575 if (our size == 0 || compare (data, our at [our size]) >= 0)
576 return our size + 1;
577 if (compare (data, our at [1]) < 0)
578 return 1;
579 /*
580 Binary search.
581 */
582 integer left = 1, right = our size;
583 while (left < right - 1) {
584 const integer mid = (left + right) / 2;
585 if (compare (data, our at [mid]) >= 0)
586 left = mid;
587 else
588 right = mid;
589 }
590 Melder_assert (right == left + 1);
591 return right;
592 }
593
594 static int s_compareHook (T* /* data1 */, T* /* data2 */) noexcept { return 0; }
595 typedef int (*CompareHook) (T*, T*);
596 virtual CompareHook v_getCompareHook () { return s_compareHook; }
597 // should compare the keys of two items; returns negative if me < thee, 0 if me == thee, and positive if me > thee
598 };
599
600 _Collection_declare (Sorted, SortedOf, Daata);
601
602
603 #pragma mark - class SortedSet
604 /*
605 In a SortedSet, every item must be unique (by key).
606 */
607
608 template <typename T /*Melder_ENABLE_IF_ISA (T, structDaata)*/>
609 struct SortedSetOf : SortedOf <T> {
610
611 SortedSetOf () {
612 extern ClassInfo classSortedSet;
613 our classInfo = classSortedSet;
614 }
615
616 SortedSetOf<T>&& move () noexcept { return static_cast <SortedSetOf<T>&&> (*this); }
617
618 /**
619 @return
620 0 (refusal) if the key of `data` already occurs
621 */
622 integer _v_position (T* data) override {
623 typename SortedOf<T>::CompareHook compare = our v_getCompareHook ();
624 if (our size == 0)
625 return 1; // empty set? then 'data' is going to be the first item
626 const int where = compare (data, our at [our size]); // compare with last item
627 if (where > 0)
628 return our size + 1; // insert at end
629 if (where == 0)
630 return 0;
631 if (compare (data, our at [1]) < 0)
632 return 1; // compare with first item
633 integer left = 1, right = our size;
634 while (left < right - 1) {
635 const integer mid = (left + right) / 2;
636 if (compare (data, our at [mid]) >= 0)
637 left = mid;
638 else
639 right = mid;
640 }
641 Melder_assert (right == left + 1);
642 if (! compare (data, our at [left]) || ! compare (data, our at [right]))
643 return 0;
644 return right;
645 }
646
647 bool hasItem (T* item) {
648 return our _v_position (item) == 0;
649 }
650
651 /**
652 See the comments at Sorted::sort().
653 @pre Must have been sorted beforehand.
654 @post All elements are different (by key).
655 */
656 void unicize () {
657 typename SortedOf<T>::CompareHook compare = our v_getCompareHook ();
658 integer n = 0, ifrom = 1;
659 for (integer i = 1; i <= our size; i ++) {
660 if (i == our size || compare (our at [i], our at [i + 1])) {
661 /*
662 Detected a change.
663 */
664 n ++;
665 integer ito = i;
666 /*
667 Move item 'ifrom' to 'n'.
668 */
669 Melder_assert (ifrom >= n);
670 if (ifrom != n) {
671 our at [n] = our at [ifrom]; // surface copy
672 our at [ifrom] = nullptr; // undangle
673 }
674 /*
675 Purge items from 'ifrom'+1 to 'ito'.
676 */
677 if (our _ownItems)
678 for (integer j = ifrom + 1; j <= ito; j ++)
679 _Thing_forget (our at [j]);
680 ifrom = ito + 1;
681 }
682 }
683 our size = n;
684 }
685
686 };
687
688 /* Behaviour:
689 Collection_addItem (SortedSet) refuses to insert an item if this item already occurs.
690 Equality is there when the compare routine returns 0.
691 Collections_merge (SortedSet) yields a SortedSet that is the union of the two sources.
692 */
693
694 _Collection_declare (SortedSet, SortedSetOf, Daata);
695
696
697 #pragma mark - class SortedSetOfInteger
698
699 template <typename T Melder_ENABLE_IF_ISA (T, structSimpleInteger)>
700 struct SortedSetOfIntegerOf : SortedSetOf <T> {
701 SortedSetOfIntegerOf () {
702 }
703 SortedSetOfIntegerOf<T>&& move () noexcept { return static_cast <SortedSetOfIntegerOf<T>&&> (*this); }
704 static int s_compareHook (SimpleInteger me, SimpleInteger thee) noexcept {
705 if (my number < thy number)
706 return -1;
707 if (my number > thy number)
708 return +1;
709 return 0;
710 }
711 typename SortedOf<T>::CompareHook v_getCompareHook ()
712 override { return (typename SortedOf<T>::CompareHook) our s_compareHook; }
713 };
714
715
716 #pragma mark - class SortedSetOfDouble
717
718 template <typename T /*Melder_ENABLE_IF_ISA (T, structSimpleDouble)*/>
719 struct SortedSetOfDoubleOf : SortedSetOf <T> {
720 SortedSetOfDoubleOf () {
721 }
722 SortedSetOfDoubleOf<T>&& move () noexcept { return static_cast <SortedSetOfDoubleOf<T>&&> (*this); }
723 static int s_compareHook (SimpleDouble me, SimpleDouble thee) noexcept {
724 if (my number < thy number)
725 return -1;
726 if (my number > thy number)
727 return +1;
728 return 0;
729 }
730 typename SortedOf<T>::CompareHook v_getCompareHook ()
731 override { return (typename SortedOf<T>::CompareHook) our s_compareHook; }
732 };
733
734
735 #pragma mark - class SortedSetOfString
736
737 template <typename T Melder_ENABLE_IF_ISA (T, structSimpleString)>
738 struct SortedSetOfStringOf : SortedSetOf <T> {
739 SortedSetOfStringOf () {
740 }
741 SortedSetOfStringOf<T>&& move () noexcept { return static_cast <SortedSetOfStringOf<T>&&> (*this); }
742 static int s_compareHook (SimpleString me, SimpleString thee) noexcept {
743 return str32cmp (my string.get(), thy string.get());
744 }
745 typename SortedOf<T>::CompareHook v_getCompareHook ()
746 override { return (typename SortedOf<T>::CompareHook) our s_compareHook; }
747
748 integer lookUp (conststring32 string) {
749 if (our size == 0)
750 return 0;
751
752 const int atEnd = str32cmp (string, our at [our size] -> string.get());
753 if (atEnd > 0)
754 return 0;
755 if (atEnd == 0)
756 return our size;
757
758 const int atStart = str32cmp (string, our at [1] -> string.get());
759 if (atStart < 0)
760 return 0;
761 if (atStart == 0)
762 return 1;
763
764 integer left = 1, right = our size;
765 while (left < right - 1) {
766 const integer mid = (left + right) / 2;
767 const int here = str32cmp (string, our at [mid] -> string.get());
768 if (here == 0)
769 return mid;
770 if (here > 0)
771 left = mid;
772 else
773 right = mid;
774 }
775 Melder_assert (right == left + 1);
776 return 0;
777 }
778
779 };
780
781 _Collection_declare (SortedSetOfString, SortedSetOfStringOf, SimpleString);
782
783
784 #pragma mark - Collections of specific types
785
786 #define Collection_define(klas,genericClass,itemClass) \
787 Thing_declare (klas); \
788 static inline auto##klas klas##_create () { return Thing_new (klas); } \
789 struct struct##klas : genericClass<struct##itemClass>
790
791 #pragma mark class DaataList
792
793 Collection_define (DaataList, OrderedOf, /* generic */ Daata) {
794 };
795
796 #pragma mark class StringList
797
798 Collection_define (StringList, OrderedOf, /* final */ SimpleString) {
799 };
800
801 #pragma mark class StringSet
802
803 Collection_define (StringSet, SortedSetOfStringOf, /* final */ SimpleString) {
804 void addString_copy (conststring32 string) {
805 autoSimpleString newSimp = SimpleString_create (string);
806 our addItem_move (newSimp.move());
807 }
808 };
809
810 /* End of file Collection.h */
811 #endif