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
|
/*
* CENA C++ Utilities
*
* by Stephane Chatty
*
* Copyright 1992-1995
* Centre d'Etudes de la Navigation Aerienne (CENA)
*
* plain and generic double-linked lists (original ideas by Yves Berteaud)
*
* $Id$
* $CurLog$
*/
#include "DList.h"
#include "Allocator.h"
#include <stdlib.h>
#include <stdio.h>
/*?class CcuDList
\begin{figure}[hbtp]
\hfil
\psfig{figure=FIGURES/CcuDList.epsf}\par \caption{Internal structure of \typ{CcuDList}s}
\hfil
\end{figure}
?*/
#ifdef CPLUS_BUG20
class CcuDListLink {
static CcuAllocator* DListLinkMem;
static void ClassInit ();
public:
CcuDListItem* Entry;
CcuDListLink* Previous;
CcuDListLink* Next;
inline CcuDListLink (CcuDListItem* e, CcuDListLink* p) : Entry (e), Previous (p), Next (p->Next) { Previous->Next = Next->Previous = this; }
inline CcuDListLink (CcuDListItem* e) : Entry (e), Previous (this), Next (this) {}
void* operator new (size_t);
void operator delete (void*);
};
#endif
#ifdef CPLUS_BUG20
CcuAllocator* CcuDListLink::DListLinkMem = 0;
#else
CcuAllocator* CcuDList::CcuDListLink::DListLinkMem = 0;
#endif
/*?nodoc?*/
void*
#ifdef CPLUS_BUG20
CcuDListLink :: operator new (size_t)
#else
CcuDList::CcuDListLink :: operator new (size_t)
#endif
{
if (!DListLinkMem)
#ifdef CPLUS_BUG20
DListLinkMem = new CcuAllocator (sizeof (CcuDListLink));
#else
DListLinkMem = new CcuAllocator (sizeof (CcuDList::CcuDListLink));
#endif
return DListLinkMem->Alloc ();
}
/*?nodoc?*/
void
#ifdef CPLUS_BUG20
CcuDListLink :: operator delete (void* that)
#else
CcuDList::CcuDListLink :: operator delete (void* that)
#endif
{
DListLinkMem->Free (that);
}
#ifdef DOC
/*?
Create an empty list.
?*/
CcuDList :: CcuDList ()
{
}
#endif
/*?
Create a list with one element \var{e}.
?*/
CcuDList :: CcuDList (CcuDListItem* e)
: LastLink (new CcuDListLink (e)),
StatusFlag (NoError)
{
}
/*?nodoc?*/
CcuDList :: CcuDList (const CcuDList& l)
: LastLink (0),
StatusFlag (NoError)
{
CcuDListIter li (l);
while (++li)
Append (*li);
}
/*?
Destructor for lists. No operation is performed on the elements.
?*/
CcuDList :: ~CcuDList ()
{
Clear ();
}
/*?
Empty a list.
?*/
void
CcuDList :: Clear ()
{
CcuDListLink* del = LastLink;
while (del) {
CcuDListLink* next = del->Next;
if (next == LastLink)
next = 0;
delete del;
del = next;
}
LastLink = 0;
}
/*?nodoc?*/
CcuDList&
CcuDList :: operator = (const CcuDList& l)
{
if (this != &l) {
Clear ();
CcuDListIter li (l);
while (++li)
Append (*li);
}
return *this;
}
/*?nextdoc?*/
CcuDListItem*
CcuDList :: First ()
{
if (!LastLink) {
StatusFlag = WasEmpty;
return 0;
}
StatusFlag = NoError;
return LastLink->Next->Entry;
}
/*?
Return the first (resp. last) element in the list, or 0 if the list was empty.
The case of a null element can be distinguished from the case
when the list is empty with the function \fun{GetStatus}.
?*/
CcuDListItem*
CcuDList :: Last ()
{
if (!LastLink) {
StatusFlag = WasEmpty;
return 0;
}
StatusFlag = NoError;
return LastLink->Entry;
}
/*?
Get the \var{n}-th element of a list. If \var{n} is greater than the length of the list
this function returns 0 and sets the status to \var{TooFar}. If \var{n} is negative,
this function returns 0 but sets the status to \var{NoError}.
?*/
CcuDListItem*
CcuDList :: Nth (int n)
{
StatusFlag = NoError;
if (n <= 0)
return 0;
CcuDListIter li (*this);
while (n > 0 && ++li)
--n;
if (n != 0)
StatusFlag = TooFar;
return *li;
}
/*?
Compute the number of elements in a list.
?*/
int
CcuDList :: Length () const
{
int result = 0;
CcuDListIter li (*this);
while (++li)
++result;
return result;
}
/*?
Check whether an item is present in a list.
?*/
int
CcuDList :: Find (CcuDListItem* e) const
{
CcuDListIter li (*this);
while (++li)
if (*li == e)
return 1;
return 0;
}
/*?nextdoc?*/
void
CcuDList :: Prepend (CcuDListItem* e)
{
if (LastLink)
new CcuDListLink (e, LastLink);
else
LastLink = new CcuDListLink (e);
}
/*?
Add an element at the beginning (resp. end) of a list.
?*/
void
CcuDList :: Append (CcuDListItem* e)
{
if (LastLink) {
LastLink = new CcuDListLink (e, LastLink);
} else
LastLink = new CcuDListLink (e);
}
/*?
Remove the first element of a list and return it.
?*/
CcuDListItem*
CcuDList :: RemoveFirst ()
{
/* Handle the case when the list is empty */
if (!LastLink) {
StatusFlag = WasEmpty;
return 0;
}
StatusFlag = NoError;
return RemoveLink (LastLink->Next);
}
/*?
Remove the last element of a list and return it.
?*/
CcuDListItem*
CcuDList :: RemoveLast ()
{
/* Handle the case when the list is empty */
if (!LastLink) {
StatusFlag = WasEmpty;
return 0;
}
StatusFlag = NoError;
return RemoveLink (LastLink);
}
/*?
Insert an element after the link \var{l}.
?*/
void
CcuDList :: InsertAfterLink (CcuDListLink* l, CcuDListItem* e)
{
CcuDListLink* newlink = new CcuDListLink (e, l);
if (LastLink == l)
LastLink = newlink;
}
/*?
Remove a link.
?*/
CcuDListItem*
CcuDList :: RemoveLink (CcuDListLink* l)
{
/* store its data */
CcuDListItem* data = l->Entry;
if (l->Next == l)
LastLink = 0;
else {
l->Previous->Next = l->Next;
l->Next->Previous = l->Previous;
if (l == LastLink)
LastLink = l->Previous;
}
/* delete it */
delete l;
/* return its data */
return data;
}
/*?
Remove an element from a list. This function iterates through the list until it deletes
\var{num} appearances of \var{entry}. The actual number of removals is returned.
If \var{num} is \var{All} or is negative, all appearances of \var{entry} are deleted.
?*/
int
CcuDList :: Remove (CcuDListItem* entry, int num)
{
int removed = 0;
CcuDListIter li (*this);
while ((num < 0 || removed < num) && ++li) {
if (*li == entry) {
RemoveLink (li.CurLink);
removed++;
break;
}
}
return removed;
}
/*?
Remove elements from a list. This function iterates through the list until it deletes
\var{num} elements for which the function \var{p} returns a non-null value.
The actual number of removals is returned.
If \var{num} is \var{All} or is negative, all matching elements are deleted.
?*/
int
CcuDList :: Remove (int (*p) (CcuDListItem*), int num)
{
int removed = 0;
CcuDListIter li (*this);
while ((num < 0 || removed < num) && ++li) {
if ((*p) (*li)) {
RemoveAt (li);
++removed;
}
}
return removed;
}
/*?nextdoc?*/
void
CcuDList :: InsertAfter (const CcuDListIter& li, CcuDListItem* e)
{
if (li.TheList != this) {
StatusFlag = BadIterator;
return;
}
if (!li.CurLink) {
if (li.StatusFlag == CcuDListIter::StartOfList) {
Prepend (e);
} else {
fprintf (stderr, "abnormal status in CcuDList::InsertAfter\n");
abort ();
}
} else if (li.StatusFlag == CcuDListIter::EndOfList) {
Append (e);
} else {
InsertAfterLink (li.CurLink, e);
}
}
/*?
Insert an element before (resp. after) the current position of the iterator \var{li}.
These functions are both equivalent to \fun{CcuDList::Prepend} if the iterator is at the
beginning of the list (ie. before the first element).
\fun{InsertAfter} and \fun{InsertAfter} are performed in a constant time.
?*/
void
CcuDList :: InsertBefore (const CcuDListIter& li, CcuDListItem* e)
{
if (li.TheList != this) {
StatusFlag = BadIterator;
return;
}
if (!li.CurLink) {
if (li.StatusFlag == CcuDListIter::StartOfList) {
Prepend (e);
} else {
fprintf (stderr, "abnormal status in CcuDList::InsertAfter\n");
abort ();
}
} else if (li.StatusFlag == CcuDListIter::EndOfList) {
Append (e);
} else {
new CcuDListLink (e, li.CurLink->Previous);
}
}
/*?
Remove the element after the current element of the iterator \var{li}.
If \var{li} points before the beginning of the list, the first element is returned.
If \var{li} points at the last element, the status flag is set to \var{TooFar}.
If the list is empty, the flag is set to \var{EmptyList}. In both cases, the
return value is null.
?*/
CcuDListItem*
CcuDList :: RemoveAfter (const CcuDListIter& li)
{
if (li.TheList != this) {
StatusFlag = BadIterator;
return 0;
}
if (li.CurLink == LastLink) {
StatusFlag = LastLink ? TooFar : WasEmpty;
return 0;
}
StatusFlag = NoError;
if (li.CurLink) {
return RemoveLink (li.CurLink->Next);
} else {
return RemoveLink (LastLink->Next);
}
}
/*?
Remove the element pointed at by the iterator \var{li}.
If \var{li} points before the beginning of the list, or has reached
its end, the return value is null, and the status flag is set to \var{TooEarly}
(resp. \var{TooFar}). In all other cases, \var{li} is rewinded by one step,
in order to allow such loops as:
\begin{ccode}
CcuDListIter li (l);
while (++li)
if (do_remove (*li))
l.RemoveAt (l);
\end{ccode}
?*/
CcuDListItem*
CcuDList :: RemoveAt (CcuDListIter& li)
{
if (li.TheList != this) {
StatusFlag = BadIterator;
return 0;
}
if (!li.CurLink) {
StatusFlag = TooEarly;
return 0;
}
if (li.StatusFlag == CcuDListIter::EndOfList) {
StatusFlag = TooFar;
return 0;
}
StatusFlag = NoError;
CcuDListLink* del = li.CurLink;
--li;
return RemoveLink (del);
}
#ifdef DOC
/*?class CcuDListIter
The class \typ{CcuDListIter} allows iterations on lists.
Several iterators may be used at a time on the same list. It is dangerous to modify
a list that is being iterated.
?*/
/*?
Initialize an iterator associated to the list \var{l}. That iterator will point at the beginning
of the list, {\em before} the first element.
?*/
CcuDListIter :: CcuDListIter (const CcuDList& l)
{
}
/*?
Get the status of an iterator. The status may be one of \var{Normal, StartOfList}, or
\var{EndOfList}
?*/
dlistiter_status
CcuDListIter :: GetStatus () const
{
}
/*?nextdoc?*/
void
CcuDListIter :: Reset ()
{
}
/*?
Reset an iterator to the beginning (resp. the end) of the list.
?*/
void
CcuDListIter :: GotoEnd ()
{
}
#endif /* DOC */
/*?
Get the current entry pointed by an iterator. This operator returns 0 if the iterator is
at the beginning or the end of the list. To distinguish those cases from the
case when the entry is null, you can use the method \fun{GetStatus}.
?*/
CcuDListItem*
CcuDListIter :: operator * () const
{
return (CurLink && StatusFlag == Normal) ? CurLink->Entry : 0;
}
#ifdef DOC
/*?
Check whether the status of an iterator is \var{Normal}.
?*/
CcuDListIter :: operator int () const
{
}
#endif
/*?
Take one step of iteration.
?*/
CcuDListIter&
CcuDListIter :: operator ++ ()
{
/* This test covers all the cases :
- the iteration has already begun, and is at its end.
- the iteration has not begun, but the list is empty.
*/
if (CurLink == TheList->LastLink) {
StatusFlag = EndOfList;
} else {
CurLink = CurLink ? CurLink->Next : TheList->LastLink->Next;
StatusFlag = Normal;
}
return *this;
}
/*?
Rewind iteration by one step.
?*/
CcuDListIter&
CcuDListIter :: operator -- ()
{
if (StatusFlag == Normal) {
CurLink = CurLink->Previous;
if (CurLink == TheList->LastLink)
Reset ();
} else if (StatusFlag == EndOfList) {
StatusFlag = Normal;
}
return *this;
}
/*?
Try and find the element \var{e} in the list, starting from the
current position of the iterator (this position not included).
If \var{e} is present in the list, the iterator will
be correctly positionned. If not, it will have reached the end of the list.
?*/
int
CcuDListIter :: Find (CcuDListItem* e)
{
while (++(*this))
if (**this == e)
return 1;
return 0;
}
/*?
Try and find the element \var{e} in the list, searching backward from the
current position of the iterator (this position not included).
If \var{e} is present in the list, the iterator will
be correctly positionned. If not, it will have reached the end of the list.
?*/
int
CcuDListIter :: FindBack (CcuDListItem* e)
{
while (--(*this))
if (**this == e)
return 1;
return 0;
}
#ifdef DOC
/*?class CcuDListOf
The generic classes \typ{CcuDListOf} and \typ{CcuDListIterOf} are derived classes
of \typ{CcuDList} and \typ{CcuDListIter} that provide lists of pointers to class objects.
When parameterized by the class \typ{ITEM}, the following functions are redefined:
?*/
/*?nextdoc?*/
CcuDListOf :: CcuDListOf (ITEM* it)
{
}
/*?nextdoc?*/
ITEM*
CcuDListOf :: First ()
{
}
/*?nextdoc?*/
ITEM*
CcuDListOf :: Last ()
{
}
/*?nextdoc?*/
ITEM*
CcuDListOf :: Nth (int n)
{
}
/*?nextdoc?*/
int
CcuDListOf :: Find (ITEM* it) const
{
}
/*?nextdoc?*/
void
CcuDListOf :: Append (ITEM* it)
{
}
/*?nextdoc?*/
void
CcuDListOf :: Prepend (ITEM* it)
{
}
/*?nextdoc?*/
CcuDListOf&
CcuDListOf :: operator << (ITEM* it)
{
}
/*?nextdoc?*/
ITEM*
CcuDListOf :: RemoveFirst ()
{
}
/*?nextdoc?*/
ITEM*
CcuDListOf :: RemoveLast ()
{
}
/*?nextdoc?*/
int
CcuDListOf :: Remove (ITEM* it, int nb = 1)
{
}
/*?nextdoc?*/
int
CcuDListOf :: Remove (int (*p) (ITEM*), int nb = 1)
{
}
/*?nextdoc?*/
void
CcuDListOf :: InsertAfter (const CcuDListIterOf <ITEM>& li, ITEM*it)
{
}
/*?nextdoc?*/
void
CcuDListOf :: InsertBefore (const CcuDListIterOf <ITEM>& li, ITEM* it)
{
}
/*?nextdoc?*/
ITEM*
CcuDListOf :: RemoveAfter (const CcuDListIterOf <ITEM>& li)
{
}
/*?
These functions are equivalent to their homonyms in the class \typ{CcuDList},
except that they are customized in order to manipulate pointers to \typ{ITEM}
instead of \typ{void*}.
?*/
ITEM*
CcuDListOf :: RemoveAt (CcuDListIterOf <ITEM>& li)
{
}
/*?nextdoc?*/
CcuDListIterOf :: CcuDListIterOf (const CcuDListOf <ITEM>& l)
{
}
/*?nextdoc?*/
ITEM*
CcuDListIterOf :: operator * () const
{
}
/*?nextdoc?*/
int
CcuDListIterOf :: Find (ITEM* it)
{
}
/*?
These functions are equivalent to their homonyms in the class \typ{CcuDListIter},
except that they are customized in order to manipulate pointers to \typ{ITEM}
instead of \typ{void*}.
?*/
int
CcuDListIterOf :: FindBack (ITEM* it)
{
}
#endif /* DOC */
|