summaryrefslogtreecommitdiff
path: root/utils/Chain.cc
blob: badc7fc07e8f69934ba25b44adba4fee553c4d35 (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
/*
 *	CENA C++ Utilities
 *
 *	Copyright 1992
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	generic chains of objects - to be updated
 *
 *	$Id$
 *	$CurLog$
 */

#include "Chain.h"
#include <stdlib.h>
#include <stdio.h>

IvlChain :: ~IvlChain ()
{
}

IvlChainItem*
IvlChain :: GetPrevious (IvlChainItem* i)
{
	register IvlChainItem* c = LastLink;
	register IvlChainItem* n;
	while ((n = GetNext (c)) != i)
		c = n;
	return c;
}

/*?
Empty a list.
?*/
void
IvlChain :: Clear ()
{
	IvlChainItem* del = LastLink;
	while (del) {
		IvlChainItem* next = GetNext (del);
		if (next == LastLink)
			next = 0;
		Delete (del);
		del = next;
	}
	LastLink = 0;
}

/*?nextdoc?*/
IvlChainItem*
IvlChain :: First ()
{
	if (!LastLink) {
		StatusFlag = WasEmpty;
		return 0;
	} 
	StatusFlag = NoError;
	return GetNext (LastLink);
}

/*?
Return the first/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}.
?*/
IvlChainItem*
IvlChain :: Last ()
{
	if (!LastLink) {
		StatusFlag = WasEmpty;
		return 0;
	} 
	StatusFlag = NoError;
	return LastLink;
}

/*?
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}.
?*/
IvlChainItem*
IvlChain :: Nth (int n)
{
	StatusFlag = NoError;
	if (n <= 0)
		return 0;
	IvlChainIter li (*this);
	while (n > 0 && ++li)
		--n;
	if (n != 0)
		StatusFlag = TooFar;
	return *li;
}

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

/*?
Check whether an item is present in a list.
?*/
int
IvlChain :: Find (IvlChainItem* e) const
{
	IvlChainIter li (*this);
	while (++li)
		if (*li == e)
			return 1;
	return 0;
}

/*?nextdoc?*/
void
IvlChain :: Prepend (IvlChainItem* e)
{
	if (LastLink) {
		/* remember that LastLink->Next is the first */
		SetNext (e, GetNext (LastLink));
		SetNext (LastLink, e);
	} else {
		LastLink = e;
		SetNext (e, e);
	}
}

/*?
Insert an element at the beginning/end of a list.
?*/
void
IvlChain :: Append (IvlChainItem* e)
{
	if (LastLink) {
		SetNext (e, GetNext (LastLink));
		SetNext (LastLink, e);
		LastLink = e;
	} else {
		LastLink = e;
		SetNext (e, e);
	}
}

/*?
Remove the first element of a list and return it.
?*/
IvlChainItem*
IvlChain :: RemoveFirst ()
{
	/* Handle the case when the list is empty */
	if (!LastLink) {
		StatusFlag = WasEmpty;
		return 0;
	} 
	StatusFlag = NoError;

	/* Get the first element */
	IvlChainItem* first = GetNext (LastLink);

	/* Remove it from the chain */
	if (LastLink == first)
		LastLink = 0;
	else
		SetNext (LastLink, GetNext (first));
	return first;
}

/*?
Remove the last element of a list and return it. This function has to locate the last
element, and hence has a linear cost.
?*/
IvlChainItem*
IvlChain :: 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 IvlChainItem* newlast = GetPrevious (LastLink);

	/* Save the element to be deleted and its data */
	IvlChainItem* last = LastLink;
	/* Remove it from the chain */
	if (newlast == LastLink)
		LastLink = 0;
	else {
		SetNext (newlast, GetNext (LastLink));
		LastLink = newlast;
	}
	return last;
}

/*?
Insert an element before the link \var{l}. This function has a linear cost.
?*/
void
IvlChain :: InsertBefore (IvlChainItem* l, IvlChainItem* e)
{
	IvlChainItem* prev = GetPrevious (l);
	SetNext (prev, e);
	SetNext (e, l);
}

/*?
Insert an element after the link \var{l}.
?*/
void
IvlChain :: InsertAfter (IvlChainItem* l, IvlChainItem* e)
{
	SetNext (e, GetNext (l));
	SetNext  (l, e);
	if (LastLink == l)
		LastLink = e;
}

/*?
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.
?*/
IvlChainItem*
IvlChain :: RemoveAfter (IvlChainItem* l)
{
	IvlChainItem* del = GetNext (l);
	if (del == l)  {
		LastLink = 0;
	} else {
		SetNext (l, GetNext (l));
		if (LastLink == del)
			LastLink = l;
	}
	return del;
}

/*?
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
IvlChain :: Remove (IvlChainItem* entry)
{
	int removed = 0;
	IvlChainIter li (*this);
	IvlChainIter lj (*this);
	while (++li) {
		if (*li == entry) {
			RemoveAfter (lj);
			return 1;
		} else
			++lj;
	}
	return 0;
}

/*?
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
IvlChain :: Remove (int (*p) (IvlChainItem*))
{
	int removed = 0;
	IvlChainIter li (*this);
	IvlChainIter lj (*this);
	while (++li) {
		if ((*p) (*li)) {
			RemoveAfter (lj);
			return 1;
		} else
			++lj;
	}
	return 0;
}

/*?nextdoc?*/
void
IvlChain :: InsertAfter (const IvlChainIter& li, IvlChainItem* e)
{
	if (li.TheChain != this) {
		StatusFlag = BadIterator;
		return;
	}
	StatusFlag = NoError;
	if (!li.CurLink) {
		if (li.StatusFlag == IvlChainIter::StartOfChain) {
			Prepend (e);
		} else {
			fprintf (stderr, "abnormal status in IvlChain::InsertAfter\n");
			abort ();
		}
	} else if (li.StatusFlag == IvlChainIter::EndOfChain) {
		Append (e);
	} else {
		InsertAfter (li.CurLink, e);
	}
}

/*?
Insert an element before (resp. after) the current position of the iterator \var{li}.
These functions are both equivalent to \fun{IvlChain::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.
?*/
void
IvlChain :: InsertBefore (const IvlChainIter& li, IvlChainItem* e)
{
	if (li.TheChain != this) {
		StatusFlag = BadIterator;
		return;
	}
	StatusFlag = NoError;
	if (!li.CurLink) {
		if (li.StatusFlag == IvlChainIter::StartOfChain) {
			Prepend (e);
		} else {
			fprintf (stderr, "abnormal status in IvlChain::InsertBefore\n");
			abort ();
		}
	} else if (li.StatusFlag == IvlChainIter::EndOfChain) {
		Append (e);
	} else {
		InsertBefore (li.CurLink, e);
	}
}

/*?
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{EmptyChain}. In both cases, the 
return value is null.
This function may be used
when one wants to iterate through a list and remove some elements:
    IvlChainIter li (l);
    IvlChainIter lj (l);
    while (++li)
        if (do_remove (*li)) {
        	RemoveAfter (lj);
        	li = lj;
        } else
        	++lj;
?*/
IvlChainItem*
IvlChain :: RemoveAfter (const IvlChainIter& li)
{
	if (li.TheChain != this) {
		StatusFlag = BadIterator;
		return 0;
	}
	if (li.CurLink == LastLink) {
		StatusFlag = LastLink ? TooFar : WasEmpty;
		return 0;
	}
	StatusFlag = NoError;
	if (li.CurLink) {
		return RemoveAfter (li.CurLink);
	} else {
		return RemoveAfter (LastLink);
	}
}

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

/*?
Take one step of iteration.
?*/ 
IvlChainIter&
IvlChainIter :: 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 == TheChain->LastLink) {
		StatusFlag = EndOfChain;
	} else {
		CurLink = CurLink ? TheChain->GetNext (CurLink) : TheChain->GetNext (TheChain->LastLink);
		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, the iterator will
be correctly positionned. If not, it will have reached the end of the list.
?*/
int
IvlChainIter :: Find (IvlChainItem* e)
{
	while (++(*this))
		if (**this == e)
			return 1;
	return 0;
}