-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathllistof.h
566 lines (450 loc) · 14.4 KB
/
llistof.h
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
/*******************************************************************************
* Copyright IBM Corp. and others 1996
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
/***************************************************************************/
/* */
/* File name: llistof.h */
/* Purpose: Definition of the LinkedListOf template class. */
/* */
/***************************************************************************/
#ifndef CS2_LLISTOF_H
#define CS2_LLISTOF_H
#define IN_CS2_LLISTOF_H
#include "cs2/cs2.h"
#include "cs2/allocator.h"
#ifdef CS2_ALLOCINFO
#define allocate(x) allocate(x, __FILE__, __LINE__)
#define deallocate(x,y) deallocate(x, y, __FILE__, __LINE__)
#define reallocate(x,y,z) reallocate(x, y, z, __FILE__, __LINE__)
#endif
namespace CS2 {
#define CS2_LL_TEMP template <class ADataType, class Allocator>
#define CS2_LL_DECL LinkedListOf <ADataType, Allocator>
template <class ADataType, class Allocator>
class LinkedListOf : private Allocator {
public:
LinkedListOf (const Allocator &a = Allocator()) : Allocator(a), fFirst(NULL) {}
~LinkedListOf();
LinkedListOf (const CS2_LL_DECL &);
const Allocator& allocator() const { return *this;}
Allocator& allocator() { return *this;}
CS2_LL_DECL &operator= (const CS2_LL_DECL &);
// Add an item to the list. By default, items are added at the beginning
// of the list. Specify atEnd = true if the item is to be added at the end.
// For adding items anywhere else in the list, use the cursor class.
void Add (const ADataType &, bool atEnd = false);
// Remove an item from the list. By default, items are removed from the beginning
// of the list. Specify atEnd = true if the item is to be removed from the end.
// For removing items anywhere else in the list, use the cursor class.
void Remove (bool atEnd = false);
// Convenience methods for Add and Remove with atEnd = false
void Push (const ADataType &);
void Pop ();
// Return first item of the list, or NULL if empty.
ADataType *Head ();
// Append another list at the end of this list
void Append (const CS2_LL_DECL &);
// Check if the list is empty.
bool IsEmpty() const;
// Make the list empty.
void MakeEmpty();
// Compute the number of elements (expensive)
uint32_t NumberOfElements() const;
// Return the number of bytes of memory used by the list
unsigned long MemoryUsage() const;
// Dump the linked list
template <class str>
friend str &operator<< (str &out, const CS2_LL_DECL &ll) {
LinkedListItem *p = ll.fFirst;
out << "( ";
while (p) {
out << p->Data() << " ";
p = p->Next();
}
out << ")";
return out;
}
private:
#define CS2_LL_ITEM CS2_LL_DECL::LinkedListItem
class LinkedListItem {
public:
void *operator new (size_t, void *ptr) { return ptr;}
LinkedListItem();
// ~LinkedListItem();
LinkedListItem (const ADataType &, typename CS2_LL_ITEM *);
ADataType &Data();
void SetData (const ADataType &);
typename CS2_LL_ITEM *Next() const;
void SetNext (typename CS2_LL_ITEM *);
private:
LinkedListItem (const LinkedListItem &);
LinkedListItem &operator= (const LinkedListItem &);
ADataType fData;
typename CS2_LL_ITEM *fNext;
};
friend class LinkedListItem;
public:
#define CS2_LLC_DECL CS2_LL_DECL::Cursor
class Cursor {
public:
Cursor (CS2_LL_DECL &);
// ~Cursor();
Cursor (const typename CS2_LLC_DECL &);
// Set the cursor position - return flag indicating if position is valid
bool SetToFirst();
bool SetToNext();
bool SetToLast();
typename CS2_LL_ITEM *Next() const;
// Determine if the cursor points at a valid position
bool Valid() const;
// Check if a cursor points to the last position (must be valid)
bool IsLast() const;
// Get/set the data at the current position in the list.
ADataType &Data() const;
ADataType &operator*();
ADataType *operator->();
void SetData (const ADataType &);
// Add an item to the list after the current position.
void AddAfter (const ADataType &);
// Delete the item on the list after the current position.
void DeleteAfter();
// Check for equality
int operator== (const typename CS2_LLC_DECL &) const;
friend class CS2_LL_DECL;
private:
typename CS2_LLC_DECL &operator= (const typename CS2_LLC_DECL &);
protected:
CS2_LL_DECL& fList;
typename CS2_LL_ITEM *fItem;
};
friend class Cursor;
private:
LinkedListItem *fFirst;
};
// LinkedListOf::MakeEmpty
//
// Make the list empty.
CS2_LL_TEMP inline
void CS2_LL_DECL::MakeEmpty() {
typename CS2_LL_ITEM *p, *np;
for (p = fFirst; p != NULL; p = np) {
np = p->Next();
p->~LinkedListItem();
Allocator::deallocate(p, sizeof(LinkedListItem));
}
fFirst = NULL;
}
CS2_LL_TEMP inline
CS2_LL_DECL::~LinkedListOf() {
MakeEmpty();
}
// LinkedListOf::Add
//
// Add an item to the list. By default, items are added at the beginning
// of the list. Specify atEnd = true if the item is to be added at the end.
// For adding items anywhere else in the list, use the cursor class.
CS2_LL_TEMP inline
void CS2_LL_DECL::Add (const ADataType &data, bool atEnd) {
typename CS2_LL_ITEM *newp;
if (atEnd && fFirst) {
typename CS2_LL_ITEM *p = fFirst;
while (p->Next()) p = p->Next();
newp = (LinkedListItem *)Allocator::allocate(sizeof(LinkedListItem));
newp = new (newp) typename CS2_LL_ITEM (data, NULL);
p->SetNext (newp);
} else {
newp = (LinkedListItem *) Allocator::allocate(sizeof(LinkedListItem));
newp = new (newp) typename CS2_LL_ITEM (data, fFirst);
fFirst = newp;
}
}
// LinkedListOf::Remove
//
// Remove an item from the list. By default, items are removed from the beginning
// of the list. Specify atEnd = true if the item is to be removed from the end.
// For removing items anywhere else in the list, use the cursor class.
CS2_LL_TEMP inline
void CS2_LL_DECL::Remove (bool atEnd) {
typename CS2_LL_ITEM *p, *np;
if (fFirst != NULL) {
if (atEnd) {
p = NULL; np = fFirst;
while (np->Next()) {p = np; np = np->Next(); }
// np is last, p is previous to last
np->~LinkedListItem();
Allocator::deallocate(np, sizeof(LinkedListItem));
if (p) {
p->SetNext(NULL);
} else {
fFirst = NULL;
}
} else {
p = fFirst;
np = p->Next();
p->~LinkedListItem();
Allocator::deallocate(p, sizeof(LinkedListItem));
fFirst = np;
}
}
}
// LinkedListOf::Push
//
// Convenience method pushing an item onto the list. Identical to Add(data)
CS2_LL_TEMP inline
void CS2_LL_DECL::Push (const ADataType &data) {
Add(data);
}
// LinkedListOf::Pop
//
// Convenience method popping an item from the top of the list. Identical to Remove(data)
CS2_LL_TEMP inline
void CS2_LL_DECL::Pop () {
Remove();
}
// LinkedListOf::Head
//
// return the first item of the list, or NULL if empty.
CS2_LL_TEMP inline
ADataType *CS2_LL_DECL::Head () {
if (fFirst == NULL)
return NULL;
return &(fFirst->Data());
}
// LinkedListOf::IsEmpty
//
// Check if the list is empty.
CS2_LL_TEMP inline
bool CS2_LL_DECL::IsEmpty() const {
return (fFirst == NULL);
}
// LinkedListOf::NumberOfElements
//
// Compute the number of elements (expensive)
CS2_LL_TEMP inline
uint32_t CS2_LL_DECL::NumberOfElements() const {
uint32_t count = 0;
typename CS2_LL_ITEM *p = fFirst;
while (p) {
count++;
p = p->Next();
}
return count;
}
CS2_LL_TEMP inline
CS2_LL_ITEM::LinkedListItem() : fNext(NULL) { }
CS2_LL_TEMP inline
CS2_LL_ITEM::LinkedListItem (const ADataType &data, typename CS2_LL_ITEM *next) :
fData(data) { fNext = (typename CS2_LL_ITEM *) next; }
CS2_LL_TEMP inline
ADataType &CS2_LL_ITEM::Data() {
return fData;
}
CS2_LL_TEMP inline
void CS2_LL_ITEM::SetData (const ADataType &data) {
fData = data;
}
CS2_LL_TEMP inline
typename CS2_LL_ITEM *CS2_LL_ITEM::Next() const {
return fNext;
}
CS2_LL_TEMP inline
void CS2_LL_ITEM::SetNext (typename CS2_LL_ITEM *next) {
fNext = (typename CS2_LL_ITEM *) next;
}
CS2_LL_TEMP inline
CS2_LLC_DECL::Cursor (CS2_LL_DECL &ll) : fList(ll), fItem(ll.fFirst) { }
CS2_LL_TEMP inline
CS2_LLC_DECL::Cursor (const typename CS2_LLC_DECL &c) :
fList(c.fList), fItem(c.fItem) { }
// LinkedListOf::Cursor::operator==
//
CS2_LL_TEMP inline
int CS2_LLC_DECL::operator== (const typename CS2_LLC_DECL &c) const {
return (&fList == &(c.fList)) && (fItem == c.fItem);
}
// LinkedListOf::Cursor::SetTo...
//
// Set the cursor position - return flag indicating if position is valid
CS2_LL_TEMP inline
bool CS2_LLC_DECL::SetToFirst() {
fItem = fList.fFirst;
return (fItem != NULL);
}
CS2_LL_TEMP inline
bool CS2_LLC_DECL::SetToNext() {
fItem = fItem->Next();
return (fItem != NULL);
}
CS2_LL_TEMP inline
bool CS2_LLC_DECL::SetToLast() {
fItem = fList.fFirst;
if (fItem == NULL) return false;
while (fItem->Next()) fItem = fItem->Next();
return true;
}
// LinkedListOf::Cursor::Valid
//
// Determine if the cursor points at a valid position
CS2_LL_TEMP inline
bool CS2_LLC_DECL::Valid() const {
return (fItem != NULL);
}
// LinkedListOf::Cursor::IsLast
//
// Check if a cursor points to the last position (must be valid)
CS2_LL_TEMP inline
bool CS2_LLC_DECL::IsLast() const {
CS2Assert (fItem != NULL, ("Expecting valid cursor"));
return (fItem->Next() == NULL);
}
// LinkedListOf::Cursor::Data
//
// Get/set the data at the current position in the list.
CS2_LL_TEMP inline
ADataType &CS2_LLC_DECL::Data() const {
return fItem->Data();
}
CS2_LL_TEMP inline
ADataType &CS2_LLC_DECL::operator*() {
return fItem->Data();
}
CS2_LL_TEMP inline
ADataType *CS2_LLC_DECL::operator->() {
return &(fItem->Data());
}
CS2_LL_TEMP inline
void CS2_LLC_DECL::SetData (const ADataType &data) {
fItem->SetData (data);
}
// LinkedListOf::Cursor::AddAfter
//
// Add an item to the list after the current position.
CS2_LL_TEMP inline
void CS2_LLC_DECL::AddAfter (const ADataType &data) {
typename CS2_LL_ITEM *newp;
CS2Assert (fItem != NULL, ("Expecting valid cursor"));
newp = (LinkedListItem *)fList.allocator().allocate(sizeof(LinkedListItem));
newp = new (newp) typename CS2_LL_ITEM (data, fItem->Next());
fItem->SetNext (newp);
}
// LinkedListOf::Cursor::DeleteAfter
//
// Delete the item on the list after the current position.
CS2_LL_TEMP inline
void CS2_LLC_DECL::DeleteAfter() {
CS2Assert (fItem != NULL, ("Expecting valid cursor"));
typename CS2_LL_ITEM *deletep = fItem->Next();
if (deletep) {
fItem->SetNext(deletep->Next());
deletep->~LinkedListItem();
fList.allocator().deallocate(deletep, sizeof(LinkedListItem));
}
}
CS2_LL_TEMP inline
typename CS2_LL_ITEM *CS2_LLC_DECL::Next () const {
CS2Assert (fItem != NULL, ("Expecting valid cursor"));
return fItem->Next();
}
CS2_LL_TEMP inline
CS2_LL_DECL::LinkedListOf (const CS2_LL_DECL &ll) : Allocator(ll.allocator()), fFirst(NULL) {
*this = ll;
}
CS2_LL_TEMP inline
CS2_LL_DECL &CS2_LL_DECL::operator= (const CS2_LL_DECL &ll) {
MakeEmpty();
if (ll.fFirst) {
typename CS2_LL_ITEM *p, *thisp, *nextp;
thisp = (LinkedListItem *) Allocator::allocate(sizeof(LinkedListItem));
thisp = fFirst = new (thisp) typename CS2_LL_ITEM (ll.fFirst->Data(), NULL);
p = ll.fFirst->Next();
while (p) {
nextp = (LinkedListItem *)Allocator::allocate(sizeof(LinkedListItem));
nextp = new (nextp) typename CS2_LL_ITEM (p->Data(), NULL);
thisp->SetNext (nextp);
thisp = nextp;
p = p->Next();
}
} else {
fFirst = NULL;
}
return *this;
}
// LinkedListOf::Append
//
// Append the given list at the end of this list
CS2_LL_TEMP inline
void CS2_LL_DECL::Append (const CS2_LL_DECL &inList) {
if (inList.IsEmpty()) return;
typename CS2_LLC_DECL thisc(*this), inc((CS2_LL_DECL &)inList);
inc.SetToFirst();
if (IsEmpty()) {
Add (*inc);
inc.SetToNext();
}
thisc.SetToLast();
while (inc.Valid()) {
thisc.AddAfter (*inc);
thisc.SetToNext();
inc.SetToNext();
}
}
// LinkedListOf::MemoryUsage
//
// Return the number of bytes of memory used by the list
CS2_LL_TEMP inline
unsigned long CS2_LL_DECL::MemoryUsage() const {
typename CS2_LL_ITEM *p = fFirst;
unsigned long mem = sizeof (CS2_LL_DECL);
while (p) {
mem += sizeof (typename CS2_LL_ITEM);
p = p->Next();
}
return mem;
}
template <class ADataType, class Allocator>
class QueueOf : public LinkedListOf<ADataType, Allocator>
{
public:
QueueOf (const Allocator &a = Allocator()) : LinkedListOf<ADataType, Allocator>(a) {}
using LinkedListOf<ADataType, Allocator>::Add;
using LinkedListOf<ADataType, Allocator>::Remove;
using LinkedListOf<ADataType, Allocator>::Head;
void Push(const ADataType &data) { Add(data, true); }
ADataType Pop() { ADataType head = *(Head()); Remove(); return head; }
};
template <class ADataType, class Allocator>
class StackOf : public QueueOf<ADataType, Allocator>
{
public:
StackOf (const Allocator &a = Allocator()) : QueueOf<ADataType, Allocator>(a) {}
using LinkedListOf<ADataType, Allocator>::Add;
void Push(const ADataType &data) { Add(data); }
};
}
#undef CS2_LLC_DECL
#undef CS2_LL_TEMPARGS
#undef CS2_LL_TEMP
#undef CS2_LL_DECL
#ifdef CS2_ALLOCINFO
#undef allocate
#undef deallocate
#undef reallocate
#endif
#endif // CS2_LLISTOF_H