summaryrefslogtreecommitdiff
path: root/utils/List.cc
blob: c954ca7dd7b2f113ce057ca5a9e64ae8b0efdcd0 (plain)
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
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1992-1995
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	plain and generic lists (original ideas by Yves Berteaud)
 *
 *	$Id$
 *	$CurLog$
 */

#include "List.h"
#include "Allocator.h"
#include <stdlib.h>
#include <stdio.h>

/*?class IvlList

\begin{figure}[hbtp]
\hfil
\psfig{figure=FIGURES/IvlList.epsf}\par \caption{Internal structure of \typ{IvlList}s}
\hfil
\end{figure}

?*/

#ifdef CPLUS_BUG20
class IvlListLink {
static	IvlAllocator*	ListLinkMem;
static	void	ClassInit ();
public:
	IvlListItem*	Entry;
	IvlListLink*	Next;
inline		IvlListLink (IvlListItem* e, IvlListLink* n = 0)	{ Entry = e; Next = n ? n : this; }
	IvlListLink*	Previous ();
	void*	operator new (size_t);
	void	operator delete (void*);
};
#endif

#ifdef CPLUS_BUG20
IvlAllocator* IvlListLink::ListLinkMem = 0;
#else
IvlAllocator* IvlList::IvlListLink::ListLinkMem = 0;
#endif

/*?nodoc?*/
void*
#ifdef CPLUS_BUG20
IvlListLink :: operator new (size_t)
#else
IvlList::IvlListLink :: operator new (size_t)
#endif
{
	if (!ListLinkMem)
#ifdef CPLUS_BUG20
		ListLinkMem = new IvlAllocator (sizeof (IvlListLink));
#else
		ListLinkMem = new IvlAllocator (sizeof (IvlList::IvlListLink));
#endif
	return ListLinkMem->Alloc ();
}

/*?nodoc?*/
void
#ifdef CPLUS_BUG20
IvlListLink :: operator delete (void* that)
#else
IvlList::IvlListLink :: operator delete (void* that)
#endif
{
	ListLinkMem->Free (that);
}

/*!
Get the link whose next link is this one.
!*/
/*?hidden?*/
#ifndef CPLUS_BUG20
IvlList::IvlListLink*
IvlList::IvlListLink :: Previous ()
#else
IvlListLink*
IvlListLink :: Previous ()
#endif
{
#ifndef CPLUS_BUG20
	register IvlList::IvlListLink* pl = this;
#else
	register IvlListLink* pl = this;
#endif
	while (pl->Next != this)
		pl = pl->Next;
	return pl;
}

#ifdef DOC
/*?
Create an empty list.
?*/
IvlList :: IvlList ()
{
}
#endif

/*?
Create a list with one element \var{e}.
?*/
IvlList :: IvlList (IvlListItem* e)
: LastLink (new IvlListLink (e)),
  StatusFlag (NoError)
{
}

/*?nodoc?*/
IvlList :: IvlList (const IvlList& l)
: LastLink (0),
  StatusFlag (NoError)
{
	IvlListIter li (l);
	while (++li)
		Append (*li);
}

/*?
Destructor for lists. No operation is performed on the elements.
?*/
IvlList :: ~IvlList ()
{
	Clear ();
}

/*?
Empty a list.
?*/
void
IvlList :: Clear ()
{
	IvlListLink* del = LastLink;
	while (del) {
		IvlListLink* next = del->Next;
		if (next == LastLink)
			next = 0;
		delete del;
		del = next;
	}
	LastLink = 0;
}

/*?nodoc?*/
IvlList&
IvlList :: operator = (const IvlList& l)
{
	if (this != &l) {
		Clear ();
		IvlListIter li (l);
		while (++li)
			Append (*li);
	}
	return *this;
}

#ifdef DOC
/*?
Get the status flag of a list. This can be one of \var{IvlList::NoError, IvlList::WasEmpty,
IvlList::TooEarly, IvlList::TooFar} or \var{IvlList::BadIterator}.
The status flag is modified by most operations on lists. Those which
modify it when errors are encountered also set it to \var{IvlList::NoError}
when there is no error.
?*/
list_status
IvlList :: GetStatus () const
{
}
#endif	/* DOC */

/*?nextdoc?*/
IvlListItem*
IvlList :: 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}: the status flag is
set to \var{IvlList::WasEmpty} in the latter case.
?*/
IvlListItem*
IvlList :: 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{IvlList::TooFar}. If \var{n} is negative,
this function returns 0 but sets the status to \var{IvlList::NoError}.
?*/
IvlListItem*
IvlList :: Nth (int n)
{
	StatusFlag = NoError;
	if (n <= 0)
		return 0;
	IvlListIter li (*this);
	while (n > 0 && ++li)
		--n;
	if (n != 0)
		StatusFlag = TooFar;
	return *li;
}

/*?
Compute the number of elements in a list.
?*/
int
IvlList :: Length () const
{
	int result = 0;
	IvlListIter li (*this);
	while (++li)
		++result;
	return result;
}

/*?
Check whether an item is present in a list. If \var{rank} is not null, it is filled with the
rank of the first occurence of \var{e} in the list.
?*/
int
IvlList :: Find (IvlListItem* e, int* rank) const
{
	int rk = 0;
	int ret = 0;

	IvlListIter li (*this);
	while (++rk, ++li)
		if (*li == e) {
			ret = 1;
			break;
		}

	if (rank)
		*rank = ret ? rk : 0;
	return ret;
}

/*?nextdoc?*/
IvlListIndex
IvlList :: Prepend (IvlListItem* e)
{
	if (LastLink)
		/* remember that LastLink->Next is the first */
		LastLink->Next = new IvlListLink (e, LastLink->Next);
	else
		LastLink = new IvlListLink (e);
	return LastLink;
}

/*?
Add an element at the beginning (resp. end) of a list.
?*/
IvlListIndex
IvlList :: Append (IvlListItem* e)
{
	if (LastLink) {
		LastLink = LastLink->Next = new IvlListLink (e, LastLink->Next);
	} else
		LastLink = new IvlListLink (e);
	return LastLink;
}

/*?
Remove the first element of a list and return it. The status flag is set to
\var{IvlList::WasEmpty} when the list was empty.
?*/
IvlListItem*
IvlList :: RemoveFirst ()
{
	/* Handle the case when the list is empty */
	if (!LastLink) {
		StatusFlag = WasEmpty;
		return 0;
	} 
	StatusFlag = NoError;

	/* Get the first element and its data */
	IvlListLink* first = LastLink->Next;
	IvlListItem* data = first->Entry;
	/* Remove it from the chain */
	if (LastLink == first)
		LastLink = 0;
	else
		LastLink->Next = first->Next;
	/* delete it */
	delete first;
	/* return its data */
	return data;
}

/*?
Remove the last element of a list and return it. The status flag is set to
\var{IvlList::WasEmpty} when the list was empty. This function has to locate the last
element, and hence has a linear cost.
?*/
IvlListItem*
IvlList :: RemoveLast ()
{
	/* Handle the case when the list is empty */
	if (!LastLink) {
		StatusFlag = WasEmpty;
		return 0;
	}
	StatusFlag = NoError;

	/* Find the element that will be the new LastLink */
	register IvlListLink* newlast = LastLink->Previous ();

	/* Save the element to be deleted and its data */
	IvlListLink* del = LastLink;
	IvlListItem* data = LastLink->Entry;
	/* Remove it from the chain */
	if (newlast == LastLink)
		LastLink = 0;
	else {
		newlast->Next = LastLink->Next;
		LastLink = newlast;
	}
	/* delete it */
	delete del;
	/* return its data */
	return data;
}

/*!
Insert an element before the link \var{l}. This function has a linear cost.
!*/
/*?hidden?*/
IvlListIndex
IvlList :: InsertBeforeLink (IvlListLink* l, IvlListItem* e)
{
	IvlListLink* prev = l->Previous ();
	IvlListLink* newlink = new IvlListLink (e, l);
	prev->Next = newlink;
	return newlink;
}

/*!
Insert an element after the link \var{l}.
!*/
/*?hidden?*/
IvlListIndex
IvlList :: InsertAfterLink (IvlListLink* l, IvlListItem* e)
{
	IvlListLink* newlink = new IvlListLink (e, l->Next);
	l->Next = newlink;
	if (LastLink == l)
		LastLink = newlink;
	return newlink;
}

/*!
Remove the link which is after \var{l} and return its entry. If \var{l} is the last link of
the list, the first one is removed.
!*/
/*?hidden?*/
IvlListItem*
IvlList :: RemoveAfterLink (IvlListLink* l)
{
	IvlListLink* del = l->Next;
	IvlListItem* data = del->Entry;
	if (del == l)  {
		LastLink = 0;
	} else {
		l->Next = del->Next;
		if (LastLink == del)
			LastLink = l;
	}
	delete del;
	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
IvlList :: Remove (IvlListItem* entry, int num)
{
	int removed = 0;
	IvlListIter li (*this);
	IvlListIter lj (*this);
	while ((num < 0 || removed < num) && ++li) {
		if (*li == entry) {
			RemoveAfter (lj);
			++removed;
			li = lj;
		} else
			++lj;
	}
	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
IvlList :: Remove (int (*p) (IvlListItem*), int num)
{
	int removed = 0;
	IvlListIter li (*this);
	IvlListIter lj (*this);
	while ((num < 0 || removed < num) && ++li) {
		if ((*p) (*li)) {
			RemoveAfter (lj);
			++removed;
			li = lj;
		} else
			++lj;
	}
	return removed;
}

/*?nextdoc?*/
IvlListIndex
IvlList :: InsertAfter (const IvlListIter& li, IvlListItem* e)
{
	if (li.TheList != this) {
		StatusFlag = BadIterator;
		return 0;
	}
	StatusFlag = NoError;
	IvlListIndex idx = 0;
	if (!li.CurLink) {
		if (li.StatusFlag == IvlListIter::StartOfList) {
			idx = Prepend (e);
		} else {
			fprintf (stderr, "abnormal status in IvlList::InsertAfter\n");
			abort ();
		}
	} else if (li.StatusFlag == IvlListIter::EndOfList) {
		idx = Append (e);
	} else {
		idx = InsertAfterLink (li.CurLink, e);
	}
	return idx;
}

/*?
Insert an element before (resp. after) the current position of the iterator \var{li}.
These functions are both equivalent to \fun{IvlList::Prepend} if the iterator is at the 
beginning of the list (ie. before the first element).
\fun{InsertAfter} is performed in a constant time, while \fun{InsertBefore}
has a linear cost. If \var{li} is not an iterator on this list, the status flag of the list
is set to \var{IvlList::BadIterator}.
?*/
IvlListIndex
IvlList :: InsertBefore (const IvlListIter& li, IvlListItem* e)
{
	if (li.TheList != this) {
		StatusFlag = BadIterator;
		return 0;
	}
	StatusFlag = NoError;
	IvlListIndex idx = 0;
	if (!li.CurLink) {
		if (li.StatusFlag == IvlListIter::StartOfList) {
			idx = Prepend (e);
		} else {
			fprintf (stderr, "abnormal status in IvlList::InsertAfter\n");
			abort ();
		}
	} else if (li.StatusFlag == IvlListIter::EndOfList) {
		idx = Append (e);
	} else {
		idx = InsertBeforeLink (li.CurLink, e);
	}
	return idx;
}

/*?
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 removed.
If \var{li} points at the last element, the status flag is set to \var{IvlList::TooFar}.
If the list is empty, the flag is set to \var{IvlList::EmptyList}. In both cases, the 
return value is null. If \var{li} is not an iterator
on this list, the flag is set to \var{IvlList::BadIterator}.
This function may be used
when one wants to iterate through a list and remove some elements:
\begin{ccode}
    IvlListIter li = l;
    IvlListIter lj = l;
    while (++li)
        if (do_remove (*li)) {
            l.RemoveAfter (lj);
            li = lj;
        } else {
            ++lj;
        }
\end{ccode}
?*/
IvlListItem*
IvlList :: RemoveAfter (const IvlListIter& 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 RemoveAfterLink (li.CurLink);
	} else {
		return RemoveAfterLink (LastLink);
	}
}

#ifdef DOC

/*?class IvlListIter
The class \typ{IvlListIter} 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. Refer to the implementation figure for more details.
Of course, the functions such as \var{InsertAfter} or \var{RemoveAfter} are designed
to be harmless. However, they may be dangerous if another iterator is used at the same
time on the same list.
The basic usage of an iterator is the following:
\begin{ccode}
    extern IvlList& l;
    IvlListIter li = l;
    while (++li)
        foo (*li);    // *li is the value that was stored with Append
\end{ccode}
?*/

/*?
Initialize an iterator associated to the list \var{l}. That iterator will point at the beginning
of the list, {\em before} the first element.
?*/
IvlListIter :: IvlListIter (const IvlList& l)
{
}
#endif	/* DOC */

/*?
Build an iterator on list \var{l}, pointing at the element denoted by \var{idx}. No check is done on whether 
\var{idx} is a valid index of list \var{l}.
?*/
IvlListIter :: IvlListIter (const IvlList& l, IvlListIndex idx)
 : TheList (&l),
#ifdef CPLUS_BUG20
  CurLink ((IvlListLink*) idx),
#else
  CurLink ((IvlList::IvlListLink*) idx),
#endif
  StatusFlag (idx ? Normal : StartOfList)
{
}

#ifdef DOC
/*?
Get the status of an iterator. The status may be one of \var{IvlListIter::Normal,
IvlListIter::StartOfList}, or \var{IvlListIter::EndOfList}
?*/
listiter_status
IvlListIter :: GetStatus () const
{
}

/*?
Reset an iterator to the beginning of the list.
?*/
void
IvlListIter :: Reset ()
{
}

#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}.
?*/
IvlListItem*
IvlListIter :: operator * () const
{
	return (CurLink && StatusFlag == Normal) ? CurLink->Entry : 0;
}

#ifdef DOC
/*?
Check whether the status of an iterator is \var{IvlListIter::Normal}.
?*/
IvlListIter :: operator int () const
{
}
#endif	/* DOC */

/*?
Take one step of iteration.
?*/ 
IvlListIter&
IvlListIter :: 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;
}

#ifndef CPLUS_BUG16
/*?
Post-increment equivalent of the previous operator. This operator is just here
as a placeholder: it does the same thing as the previous one.
?*/
IvlListIter&
IvlListIter :: operator ++ (int)
{
	++(*this);
	return *this;
}
#endif

/*?
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
IvlListIter :: Find (IvlListItem* e)
{
	while (++(*this))
		if (**this == e)
			return 1;
	return 0;
}

#ifdef DOC

/*?class IvlListOf
The generic classes \typ{IvlListOf} and \typ{IvlListIterOf} are derived classes
of \typ{IvlList} and \typ{IvlListIter} that provide lists of pointers to class objects.
When parameterized by the class \typ{ITEM}, the following functions are redefined:
?*/

/*?nextdoc?*/
IvlListOf :: IvlListOf (ITEM* it)
{
}

/*?nextdoc?*/
ITEM*
IvlListOf :: First ()
{
}

/*?nextdoc?*/
ITEM*
IvlListOf :: Last ()
{
}

/*?nextdoc?*/
ITEM*
IvlListOf :: Nth (int n)
{
}

/*?nextdoc?*/
int
IvlListOf :: Find (ITEM* it) const
{
}

/*?nextdoc?*/
void
IvlListOf :: Append (ITEM* it)
{
}

/*?nextdoc?*/
void
IvlListOf :: Prepend (ITEM* it)
{
}

/*?nextdoc?*/
IvlListOf&
IvlListOf :: operator << (ITEM* it)
{
}

/*?nextdoc?*/
ITEM*
IvlListOf :: RemoveFirst ()
{
}

/*?nextdoc?*/
ITEM*
IvlListOf :: RemoveLast ()
{
}

/*?nextdoc?*/
int
IvlListOf :: Remove (ITEM* it, int nb = 1)
{
}

/*?nextdoc?*/
int
IvlListOf :: Remove (int (*p) (ITEM*), int nb = 1)
{
}

/*?nextdoc?*/
void
IvlListOf :: InsertAfter (const IvlListIterOf <ITEM>& li, ITEM*it)
{
}

/*?nextdoc?*/
void
IvlListOf :: InsertBefore (const IvlListIterOf <ITEM>& li, ITEM* it)
{
}

/*?
These functions are equivalent to their homonyms in the class \typ{IvlList},
except that they are customized in order to manipulate pointers to \typ{ITEM}
instead of \typ{void*}.
?*/
ITEM*
IvlListOf :: RemoveAfter (const IvlListIterOf <ITEM>& li)	
{
}

/*?nextdoc?*/
IvlListIterOf  :: IvlListIterOf (const IvlListOf <ITEM>& l)
{
}
	
/*?nextdoc?*/
ITEM*
IvlListIterOf :: operator * () const
{
}

/*?
These functions are equivalent to their homonyms in the class \typ{IvlListIter},
except that they are customized in order to manipulate pointers to \typ{ITEM}
instead of \typ{void*}.
?*/
int
IvlListIterOf :: Find (ITEM* it)	
{
}

#endif	/* DOC */