-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Expand file tree
/
Copy pathelst.h
More file actions
1128 lines (1022 loc) · 38 KB
/
elst.h
File metadata and controls
1128 lines (1022 loc) · 38 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
* File: elst.h (Formerly elist.h)
* Description: Embedded list module include file.
* Author: Phil Cheatle
*
* (C) Copyright 1991, Hewlett-Packard Ltd.
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
** http://www.apache.org/licenses/LICENSE-2.0
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*
**********************************************************************/
#ifndef ELST_H
#define ELST_H
#include "lsterr.h"
#include "serialis.h"
#include <algorithm>
#include <cstdio>
namespace tesseract {
/**********************************************************************
This module implements list classes and iterators.
The following list types and iterators are provided:
List type List Class Iterator Class Element Class
--------- ---------- -------------- -------------
Embedded list ELIST
ELIST_ITERATOR
ELIST_LINK
(Single linked)
Embedded list ELIST2
ELIST2_ITERATOR
ELIST2_LINK
(Double linked)
Cons List CLIST
CLIST_ITERATOR
CLIST_LINK
(Single linked)
An embedded list is where the list pointers are provided by a generic class.
Data types to be listed inherit from the generic class. Data is thus linked
in only ONE list at any one time.
A cons list has a separate structure for a "cons cell". This contains the
list pointer(s) AND a pointer to the data structure held on the list. A
structure can be on many cons lists at the same time, and the structure does
not need to inherit from any generic class in order to be on the list.
The implementation of lists is very careful about space and speed overheads.
This is why many embedded lists are provided. The same concerns mean that
in-line type coercion is done, rather than use virtual functions. This is
cumbersome in that each data type to be listed requires its own iterator and
list class - though macros can generate these. It also prevents heterogeneous
lists.
**********************************************************************/
/**********************************************************************
* CLASS - ELIST
*
* Generic list class for singly linked lists with embedded links
**********************************************************************/
template <typename T>
class IntrusiveForwardList {
public:
/**********************************************************************
* CLASS - ELIST_LINK
*
* Generic link class for singly linked lists with
*embedded links
*
* Note: No destructor - elements are assumed to be destroyed EITHER after
* they have been extracted from a list OR by the IntrusiveForwardList destructor which
* walks the list.
**********************************************************************/
class Link {
friend class Iterator;
friend class IntrusiveForwardList;
T *next;
public:
Link() {
next = nullptr;
}
// constructor
// The special copy constructor is used by lots of classes.
Link(const Link &) {
next = nullptr;
}
// The special assignment operator is used by lots of classes.
void operator=(const Link &) {
next = nullptr;
}
};
using LINK = Link; // compat
/***********************************************************************
* CLASS - ELIST_ITERATOR
*
* Generic iterator class for singly linked lists with
*embedded links
**********************************************************************/
class Iterator {
friend void IntrusiveForwardList::assign_to_sublist(Iterator *, Iterator *);
IntrusiveForwardList *list; // List being iterated
T *prev; // prev element
T *current; // current element
T *next; // next element
T *cycle_pt; // point we are cycling the list to.
bool ex_current_was_last; // current extracted was end of list
bool ex_current_was_cycle_pt; // current extracted was cycle point
bool started_cycling; // Have we moved off the start?
/***********************************************************************
* Iterator::extract_sublist()
*
* This is a private member, used only by IntrusiveForwardList::assign_to_sublist.
* Given another iterator for the same list, extract the links from THIS to
* OTHER inclusive, link them into a new circular list, and return a
* pointer to the last element.
* (Can't inline this function because it contains a loop)
**********************************************************************/
T *extract_sublist( // from this current...
Iterator *other_it) { // to other current
#ifndef NDEBUG
constexpr ERRCODE BAD_EXTRACTION_PTS("Can't extract sublist from points on different lists");
constexpr ERRCODE DONT_EXTRACT_DELETED("Can't extract a sublist marked by deleted points");
#endif
constexpr ERRCODE BAD_SUBLIST("Can't find sublist end point in original list");
Iterator temp_it = *this;
T *end_of_new_list;
#ifndef NDEBUG
if (!other_it)
BAD_PARAMETER.error("ELIST_ITERATOR::extract_sublist", ABORT, "other_it nullptr");
if (!list)
NO_LIST.error("ELIST_ITERATOR::extract_sublist", ABORT);
if (list != other_it->list)
BAD_EXTRACTION_PTS.error("ELIST_ITERATOR.extract_sublist", ABORT);
if (list->empty())
EMPTY_LIST.error("ELIST_ITERATOR::extract_sublist", ABORT);
if (!current || !other_it->current)
DONT_EXTRACT_DELETED.error("ELIST_ITERATOR.extract_sublist", ABORT);
#endif
ex_current_was_last = other_it->ex_current_was_last = false;
ex_current_was_cycle_pt = false;
other_it->ex_current_was_cycle_pt = false;
temp_it.mark_cycle_pt();
do { // walk sublist
if (temp_it.cycled_list()) { // can't find end pt
BAD_SUBLIST.error("Iterator.extract_sublist", ABORT);
}
if (temp_it.at_last()) {
list->last = prev;
ex_current_was_last = other_it->ex_current_was_last = true;
}
if (temp_it.current == cycle_pt) {
ex_current_was_cycle_pt = true;
}
if (temp_it.current == other_it->cycle_pt) {
other_it->ex_current_was_cycle_pt = true;
}
temp_it.forward();
} while (temp_it.prev != other_it->current);
// circularise sublist
other_it->current->next = current;
end_of_new_list = other_it->current;
// sublist = whole list
if (prev == other_it->current) {
list->last = nullptr;
prev = current = next = nullptr;
other_it->prev = other_it->current = other_it->next = nullptr;
} else {
prev->next = other_it->next;
current = other_it->current = nullptr;
next = other_it->next;
other_it->prev = prev;
}
return end_of_new_list;
} // to other current
public:
Iterator() { // constructor
list = nullptr;
} // unassigned list
/***********************************************************************
* ELIST_ITERATOR::ELIST_ITERATOR
*
* CONSTRUCTOR - set iterator to specified list;
**********************************************************************/
Iterator(IntrusiveForwardList *list_to_iterate) {
set_to_list(list_to_iterate);
}
/***********************************************************************
* ELIST_ITERATOR::set_to_list
*
* (Re-)initialise the iterator to point to the start of the list_to_iterate
* over.
**********************************************************************/
void set_to_list( // change list
IntrusiveForwardList *list_to_iterate) {
#ifndef NDEBUG
if (!list_to_iterate) {
BAD_PARAMETER.error("ELIST_ITERATOR::set_to_list", ABORT, "list_to_iterate is nullptr");
}
#endif
list = list_to_iterate;
prev = list->last;
current = list->First();
next = current ? current->next : nullptr;
cycle_pt = nullptr; // await explicit set
started_cycling = false;
ex_current_was_last = false;
ex_current_was_cycle_pt = false;
}
/***********************************************************************
* ELIST_ITERATOR::add_after_then_move
*
* Add a new element to the list after the current element and move the
* iterator to the new element.
**********************************************************************/
void add_after_then_move( // add after current &
T *new_element) {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::add_after_then_move", ABORT);
}
if (!new_element) {
BAD_PARAMETER.error("ELIST_ITERATOR::add_after_then_move", ABORT, "new_element is nullptr");
}
if (new_element->next) {
STILL_LINKED.error("ELIST_ITERATOR::add_after_then_move", ABORT);
}
#endif
if (list->empty()) {
new_element->next = new_element;
list->last = new_element;
prev = next = new_element;
} else {
new_element->next = next;
if (current) { // not extracted
current->next = new_element;
prev = current;
if (current == list->last) {
list->last = new_element;
}
} else { // current extracted
prev->next = new_element;
if (ex_current_was_last) {
list->last = new_element;
}
if (ex_current_was_cycle_pt) {
cycle_pt = new_element;
}
}
}
current = new_element;
} // move to new
/***********************************************************************
* ELIST_ITERATOR::add_after_stay_put
*
* Add a new element to the list after the current element but do not move
* the iterator to the new element.
**********************************************************************/
void add_after_stay_put( // add after current &
T *new_element) {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::add_after_stay_put", ABORT);
}
if (!new_element) {
BAD_PARAMETER.error("ELIST_ITERATOR::add_after_stay_put", ABORT, "new_element is nullptr");
}
if (new_element->next) {
STILL_LINKED.error("ELIST_ITERATOR::add_after_stay_put", ABORT);
}
#endif
if (list->empty()) {
new_element->next = new_element;
list->last = new_element;
prev = next = new_element;
ex_current_was_last = false;
current = nullptr;
} else {
new_element->next = next;
if (current) { // not extracted
current->next = new_element;
if (prev == current) {
prev = new_element;
}
if (current == list->last) {
list->last = new_element;
}
} else { // current extracted
prev->next = new_element;
if (ex_current_was_last) {
list->last = new_element;
ex_current_was_last = false;
}
}
next = new_element;
}
} // stay at current
/***********************************************************************
* ELIST_ITERATOR::add_before_then_move
*
* Add a new element to the list before the current element and move the
* iterator to the new element.
**********************************************************************/
void add_before_then_move( // add before current &
T *new_element) {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::add_before_then_move", ABORT);
}
if (!new_element) {
BAD_PARAMETER.error("ELIST_ITERATOR::add_before_then_move", ABORT, "new_element is nullptr");
}
if (new_element->next) {
STILL_LINKED.error("ELIST_ITERATOR::add_before_then_move", ABORT);
}
#endif
if (list->empty()) {
new_element->next = new_element;
list->last = new_element;
prev = next = new_element;
} else {
prev->next = new_element;
if (current) { // not extracted
new_element->next = current;
next = current;
} else { // current extracted
new_element->next = next;
if (ex_current_was_last) {
list->last = new_element;
}
if (ex_current_was_cycle_pt) {
cycle_pt = new_element;
}
}
}
current = new_element;
} // move to new
/***********************************************************************
* ELIST_ITERATOR::add_before_stay_put
*
* Add a new element to the list before the current element but don't move the
* iterator to the new element.
**********************************************************************/
void add_before_stay_put( // add before current &
T *new_element) {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::add_before_stay_put", ABORT);
}
if (!new_element) {
BAD_PARAMETER.error("ELIST_ITERATOR::add_before_stay_put", ABORT, "new_element is nullptr");
}
if (new_element->next) {
STILL_LINKED.error("ELIST_ITERATOR::add_before_stay_put", ABORT);
}
#endif
if (list->empty()) {
new_element->next = new_element;
list->last = new_element;
prev = next = new_element;
ex_current_was_last = true;
current = nullptr;
} else {
prev->next = new_element;
if (current) { // not extracted
new_element->next = current;
if (next == current) {
next = new_element;
}
} else { // current extracted
new_element->next = next;
if (ex_current_was_last) {
list->last = new_element;
}
}
prev = new_element;
}
} // stay at current
/***********************************************************************
* ELIST_ITERATOR::add_list_after
*
* Insert another list to this list after the current element but don't move
*the
* iterator.
**********************************************************************/
void add_list_after( // add a list &
IntrusiveForwardList *list_to_add) {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::add_list_after", ABORT);
}
if (!list_to_add) {
BAD_PARAMETER.error("ELIST_ITERATOR::add_list_after", ABORT, "list_to_add is nullptr");
}
#endif
if (!list_to_add->empty()) {
if (list->empty()) {
list->last = list_to_add->last;
prev = list->last;
next = list->First();
ex_current_was_last = true;
current = nullptr;
} else {
if (current) { // not extracted
current->next = list_to_add->First();
if (current == list->last) {
list->last = list_to_add->last;
}
list_to_add->last->next = next;
next = current->next;
} else { // current extracted
prev->next = list_to_add->First();
if (ex_current_was_last) {
list->last = list_to_add->last;
ex_current_was_last = false;
}
list_to_add->last->next = next;
next = prev->next;
}
}
list_to_add->last = nullptr;
}
} // stay at current
/***********************************************************************
* ELIST_ITERATOR::add_list_before
*
* Insert another list to this list before the current element. Move the
* iterator to the start of the inserted elements
* iterator.
**********************************************************************/
void add_list_before( // add a list &
IntrusiveForwardList *list_to_add) {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::add_list_before", ABORT);
}
if (!list_to_add) {
BAD_PARAMETER.error("ELIST_ITERATOR::add_list_before", ABORT, "list_to_add is nullptr");
}
#endif
if (!list_to_add->empty()) {
if (list->empty()) {
list->last = list_to_add->last;
prev = list->last;
current = list->First();
next = current->next;
ex_current_was_last = false;
} else {
prev->next = list_to_add->First();
if (current) { // not extracted
list_to_add->last->next = current;
} else { // current extracted
list_to_add->last->next = next;
if (ex_current_was_last) {
list->last = list_to_add->last;
}
if (ex_current_was_cycle_pt) {
cycle_pt = prev->next;
}
}
current = prev->next;
next = current->next;
}
list_to_add->last = nullptr;
}
} // move to it 1st item
T *data() { // get current data
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::data", ABORT);
}
if (!current) {
NULL_DATA.error("ELIST_ITERATOR::data", ABORT);
}
#endif
return current;
}
/***********************************************************************
* ELIST_ITERATOR::data_relative
*
* Return the data pointer to the element "offset" elements from current.
* "offset" must not be less than -1.
* (This function can't be INLINEd because it contains a loop)
**********************************************************************/
T *data_relative( // get data + or - ...
int8_t offset) { // offset from current
T *ptr;
#ifndef NDEBUG
if (!list)
NO_LIST.error("ELIST_ITERATOR::data_relative", ABORT);
if (list->empty())
EMPTY_LIST.error("ELIST_ITERATOR::data_relative", ABORT);
if (offset < -1)
BAD_PARAMETER.error("ELIST_ITERATOR::data_relative", ABORT, "offset < -l");
#endif
if (offset == -1) {
ptr = prev;
} else {
for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next) {
;
}
}
#ifndef NDEBUG
if (!ptr)
NULL_DATA.error("ELIST_ITERATOR::data_relative", ABORT);
#endif
return ptr;
} // offset from current
/***********************************************************************
* ELIST_ITERATOR::forward
*
* Move the iterator to the next element of the list.
* REMEMBER: ALL LISTS ARE CIRCULAR.
**********************************************************************/
T *forward() {
#ifndef NDEBUG
if (!list)
NO_LIST.error("ELIST_ITERATOR::forward", ABORT);
#endif
if (list->empty()) {
return nullptr;
}
if (current) { // not removed so
// set previous
prev = current;
started_cycling = true;
// In case next is deleted by another iterator, get next from current.
current = current->next;
} else {
if (ex_current_was_cycle_pt) {
cycle_pt = next;
}
current = next;
}
#ifndef NDEBUG
if (!current)
NULL_DATA.error("ELIST_ITERATOR::forward", ABORT);
#endif
next = current->next;
#ifndef NDEBUG
if (!next) {
NULL_NEXT.error("ELIST_ITERATOR::forward", ABORT,
"This is: %p Current is: %p",
static_cast<void *>(this),
static_cast<void *>(current));
}
#endif
return current;
} // move to next element
/***********************************************************************
* ELIST_ITERATOR::extract
*
* Do extraction by removing current from the list, returning it to the
* caller, but NOT updating the iterator. (So that any calling loop can do
* this.) The iterator's current points to nullptr. If the extracted element
* is to be deleted, this is the callers responsibility.
**********************************************************************/
T *extract() {
T *extracted_link;
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::extract", ABORT);
}
if (!current) { // list empty or
// element extracted
NULL_CURRENT.error("ELIST_ITERATOR::extract", ABORT);
}
#endif
if (list->singleton()) {
// Special case where we do need to change the iterator.
prev = next = list->last = nullptr;
} else {
prev->next = next; // remove from list
ex_current_was_last = (current == list->last);
if (ex_current_was_last) {
list->last = prev;
}
}
// Always set ex_current_was_cycle_pt so an add/forward will work in a loop.
ex_current_was_cycle_pt = (current == cycle_pt);
extracted_link = current;
extracted_link->next = nullptr; // for safety
current = nullptr;
return extracted_link;
} // remove from list
/***********************************************************************
* ELIST_ITERATOR::move_to_first()
*
* Move current so that it is set to the start of the list.
* Return data just in case anyone wants it.
**********************************************************************/
T *move_to_first() {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::move_to_first", ABORT);
}
#endif
current = list->First();
prev = list->last;
next = current ? current->next : nullptr;
return current;
} // go to start of list
/***********************************************************************
* ELIST_ITERATOR::move_to_last()
*
* Move current so that it is set to the end of the list.
* Return data just in case anyone wants it.
* (This function can't be INLINEd because it contains a loop)
**********************************************************************/
T *move_to_last() {
#ifndef NDEBUG
if (!list)
NO_LIST.error("ELIST_ITERATOR::move_to_last", ABORT);
#endif
while (current != list->last) {
forward();
}
return current;
} // go to end of list
/***********************************************************************
* ELIST_ITERATOR::mark_cycle_pt()
*
* Remember the current location so that we can tell whether we've returned
* to this point later.
*
* If the current point is deleted either now, or in the future, the cycle
* point will be set to the next item which is set to current. This could be
* by a forward, add_after_then_move or add_after_then_move.
**********************************************************************/
void mark_cycle_pt() {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::mark_cycle_pt", ABORT);
}
#endif
if (current) {
cycle_pt = current;
} else {
ex_current_was_cycle_pt = true;
}
started_cycling = false;
} // remember current
bool empty() const { // is list empty?
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::empty", ABORT);
}
#endif
return list->empty();
}
bool current_extracted() const { // current extracted?
return !current;
}
/***********************************************************************
* ELIST_ITERATOR::at_first()
*
* Are we at the start of the list?
*
**********************************************************************/
bool at_first() const {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::at_first", ABORT);
}
#endif
// we're at a deleted
return ((list->empty()) || (current == list->First()) ||
((current == nullptr) && (prev == list->last) && // NON-last pt between
!ex_current_was_last)); // first and last
} // Current is first?
/***********************************************************************
* ELIST_ITERATOR::at_last()
*
* Are we at the end of the list?
*
**********************************************************************/
bool at_last() const {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::at_last", ABORT);
}
#endif
// we're at a deleted
return ((list->empty()) || (current == list->last) ||
((current == nullptr) && (prev == list->last) && // last point between
ex_current_was_last)); // first and last
} // Current is last?
/***********************************************************************
* ELIST_ITERATOR::cycled_list()
*
* Have we returned to the cycle_pt since it was set?
*
**********************************************************************/
bool cycled_list() const {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::cycled_list", ABORT);
}
#endif
return ((list->empty()) || ((current == cycle_pt) && started_cycling));
} // Completed a cycle?
/***********************************************************************
* ELIST_ITERATOR::add_to_end
*
* Add a new element to the end of the list without moving the iterator.
* This is provided because a single linked list cannot move to the last as
* the iterator couldn't set its prev pointer. Adding to the end is
* essential for implementing
queues.
**********************************************************************/
void add_to_end( // add at end &
T *new_element) {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::add_to_end", ABORT);
}
if (!new_element) {
BAD_PARAMETER.error("ELIST_ITERATOR::add_to_end", ABORT, "new_element is nullptr");
}
if (new_element->next) {
STILL_LINKED.error("ELIST_ITERATOR::add_to_end", ABORT);
}
#endif
if (this->at_last()) {
this->add_after_stay_put(new_element);
} else {
if (this->at_first()) {
this->add_before_stay_put(new_element);
list->last = new_element;
} else { // Iteratr is elsewhere
new_element->next = list->last->next;
list->last->next = new_element;
list->last = new_element;
}
}
} // don't move
/***********************************************************************
* ELIST_ITERATOR::exchange()
*
* Given another iterator, whose current element is a different element on
* the same list list OR an element of another list, exchange the two current
* elements. On return, each iterator points to the element which was the
* other iterators current on entry.
* (This function hasn't been in-lined because its a bit big!)
**********************************************************************/
void exchange( // positions of 2 links
Iterator *other_it) { // other iterator
constexpr ERRCODE DONT_EXCHANGE_DELETED("Can't exchange deleted elements of lists");
T *old_current;
#ifndef NDEBUG
if (!list)
NO_LIST.error("ELIST_ITERATOR::exchange", ABORT);
if (!other_it)
BAD_PARAMETER.error("ELIST_ITERATOR::exchange", ABORT, "other_it nullptr");
if (!(other_it->list))
NO_LIST.error("ELIST_ITERATOR::exchange", ABORT, "other_it");
#endif
/* Do nothing if either list is empty or if both iterators reference the same
link */
if ((list->empty()) || (other_it->list->empty()) || (current == other_it->current)) {
return;
}
/* Error if either current element is deleted */
if (!current || !other_it->current) {
DONT_EXCHANGE_DELETED.error("ELIST_ITERATOR.exchange", ABORT);
}
/* Now handle the 4 cases: doubleton list; non-doubleton adjacent elements
(other before this); non-doubleton adjacent elements (this before other);
non-adjacent elements. */
// adjacent links
if ((next == other_it->current) || (other_it->next == current)) {
// doubleton list
if ((next == other_it->current) && (other_it->next == current)) {
prev = next = current;
other_it->prev = other_it->next = other_it->current;
} else { // non-doubleton with
// adjacent links
// other before this
if (other_it->next == current) {
other_it->prev->next = current;
other_it->current->next = next;
current->next = other_it->current;
other_it->next = other_it->current;
prev = current;
} else { // this before other
prev->next = other_it->current;
current->next = other_it->next;
other_it->current->next = current;
next = current;
other_it->prev = other_it->current;
}
}
} else { // no overlap
prev->next = other_it->current;
current->next = other_it->next;
other_it->prev->next = current;
other_it->current->next = next;
}
/* update end of list pointer when necessary (remember that the 2 iterators
may iterate over different lists!) */
if (list->last == current) {
list->last = other_it->current;
}
if (other_it->list->last == other_it->current) {
other_it->list->last = current;
}
if (current == cycle_pt) {
cycle_pt = other_it->cycle_pt;
}
if (other_it->current == other_it->cycle_pt) {
other_it->cycle_pt = cycle_pt;
}
/* The actual exchange - in all cases*/
old_current = current;
current = other_it->current;
other_it->current = old_current;
} // other iterator
//# elements in list
int32_t length() const {
return list->length();
}
/***********************************************************************
* ELIST_ITERATOR::sort()
*
* Sort the elements of the list, then reposition at the start.
*
**********************************************************************/
void sort( // sort elements
int comparator( // comparison routine
const T *, const T *)) {
#ifndef NDEBUG
if (!list) {
NO_LIST.error("ELIST_ITERATOR::sort", ABORT);
}
#endif
list->sort(comparator);
move_to_first();
}
};
using ITERATOR = Iterator; // compat
private:
T *last = nullptr; // End of list
//(Points to head)
T *First() { // return first
return last ? last->next : nullptr;
}
public:
~IntrusiveForwardList() {
clear();
}
/* delete elements */
void clear() {
internal_clear();
}
/* Become a deep copy of src_list */
template <typename U>
void deep_copy(const U *src_list, T *(*copier)(const T *)) {
Iterator from_it(const_cast<U *>(src_list));
Iterator to_it(this);
for (from_it.mark_cycle_pt(); !from_it.cycled_list(); from_it.forward())
to_it.add_after_then_move((*copier)(from_it.data()));
}
/***********************************************************************
* IntrusiveForwardList::internal_clear
*
* Used by the destructor and the "clear" member function of derived list
* classes to destroy all the elements on the list.
* The calling function passes a "zapper" function which can be called to
* delete each element of the list, regardless of its derived type. This
* technique permits a generic clear function to destroy elements of
* different derived types correctly, without requiring virtual functions and
* the consequential memory overhead.
**********************************************************************/
// destroy all links
void internal_clear() {
T *ptr;
T *next;
if (!empty()) {
ptr = last->next; // set to first
last->next = nullptr; // break circle
last = nullptr; // set list empty
while (ptr) {
next = ptr->next;
delete ptr;
ptr = next;
}
}
}
bool empty() const {
return !last;
}
bool singleton() const {
return last ? (last == last->next) : false;
}
void shallow_copy( // dangerous!!
IntrusiveForwardList *from_list) { // beware destructors!!
last = from_list->last;
}
/***********************************************************************
* IntrusiveForwardList::assign_to_sublist
*
* The list is set to a sublist of another list. "This" list must be empty
* before this function is invoked. The two iterators passed must refer to
* the same list, different from "this" one. The sublist removed is the
* inclusive list from start_it's current position to end_it's current
* position. If this range passes over the end of the source list then the
* source list has its end set to the previous element of start_it. The
* extracted sublist is unaffected by the end point of the source list, its
* end point is always the end_it position.
**********************************************************************/