-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathLink.hpp
306 lines (263 loc) · 7.89 KB
/
Link.hpp
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
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corp. and others
*
* 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 http://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] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#ifndef LINK_INCL
#define LINK_INCL
#include <stddef.h>
#include <stdint.h>
#include "env/TRMemory.hpp"
template <class T> class TR_Link0
{
public:
TR_Link0(T *n = 0) : _next(n) {}
T *getNext() { return _next; }
void setNext(T* n) { _next = n; }
private:
T *_next;
};
template<class T> class TR_LinkHead0
{
public:
TR_LinkHead0() : _head(0) {}
T *getFirst() { return _head; }
void setFirst(T *t) { _head = t; }
bool isEmpty() { return _head == 0; }
bool isSingleton() {return _head && !_head->getNext();}
bool isDoubleton() {return _head && _head->getNext() && !_head->getNext()->getNext();}
bool isMultipleEntry() {return _head && _head->getNext();}
void add(T * e) { e->setNext(_head); _head = e; }
T * pop() { T * e = _head; if (e) _head = e->getNext(); return e; }
bool find(T * f)
{
for (T * e = _head; e; e = e->getNext())
if (e == f) return true;
return false;
}
void insertAfter(T * prev, T * e)
{
if (!prev) add(e);
else { e->setNext(prev->getNext()); prev->setNext(e); }
}
void removeAfter(T * prev, T * e)
{
if (prev) prev->setNext(e->getNext());
else setFirst(e->getNext());
}
bool remove(T * e)
{
for (T * c = _head, * p = 0; c; p = c, c = c->getNext())
if (c == e)
{
if (p) p->setNext(e->getNext());
else _head = e->getNext();
e->setNext(0);
return true;
}
return false;
}
int32_t getSize()
{
int32_t size = 0;
for (T * c = _head; c; c = c->getNext())
++size;
return size;
}
private:
T * _head;
};
/**
* This class has one client, the CFG.
* I think the intention here is to support invalidating links without breaking
* pointers, but, it's not clear at all.
*/
template <class T> class TR_Link1
{
public:
TR_Link1(T *n = 0) : _next(n), _valid(false) { }
T *getNext() {
while(_next && !(_next->isValid()))
_next = _next->_next;
return _next;
}
void setNext(T* n) { setValid(true); _next = n; }
bool isValid() { return _valid; }
void setValid(bool b) { _valid = b; }
private:
T *_next;
bool _valid;
};
template<class T> class TR_LinkHead1
{
public:
TR_LinkHead1() : _head(0) {}
T *getFirst() { return _head; }
void setFirst(T *t) { _head = t; t->setValid(true); }
bool isEmpty() { return _head == 0; }
bool isSingleton() {return _head && !_head->getNext();}
bool isDoubleton() {return _head && _head->getNext() && !_head->getNext()->getNext();}
bool isMultipleEntry() {return _head && _head->getNext();}
void add(T * e) { e->setNext(_head); _head = e; }
T * pop() { T * e = _head; if (e && e->isValid()) _head = e->getNext(); else return NULL; return e; }
bool find(T * f)
{
return f->isValid();
}
void insertAfter(T * prev, T * e)
{
if (prev && prev->isValid()) { e->setNext(prev->getNext()); prev->setNext(e); }
else add(e);
}
void removeAfter(T * prev, T * e)
{
if (prev && prev->isValid()) prev->setNext(e->getNext());
else setFirst(e->getNext());
}
bool remove(T * e)
{
bool remove = (e->isValid() || e == _head);
if (e == _head) _head = e->getNext();
if (remove) e->setValid(false);
return remove;
}
int32_t getSize()
{
int32_t size = 0;
for (T * c = _head; c; c = c->getNext())
++size;
return size;
}
private:
T * _head;
};
template <class T> class TR_Link : public TR_Link0<T>
{
public:
TR_ALLOC(TR_Memory::LLLink);
TR_Link(T *n = 0) : TR_Link0<T>(n) {}
};
template<class T> class TR_LinkHead : public TR_LinkHead0<T>
{
public:
TR_ALLOC(TR_Memory::LLLinkHead);
TR_LinkHead() : TR_LinkHead0<T>() {}
};
template<class T> class TR_LinkHeadAndTail
{
public:
TR_LinkHeadAndTail() : _head(0), _tail(0) {}
T * getFirst() { return _head; }
T * getLast() { return _tail; }
void set(T * h, T * t) { _head = h; _tail = t; }
bool isEmpty() { return _head == 0; }
bool isSingleton() {return _head && !_head->getNext();}
bool isDoubleton() {return _head && _head->getNext() && !_head->getNext()->getNext();}
bool isMultipleEntry() {return _head && _head->getNext();}
int32_t getSize()
{
int32_t size = 0;
for (T *c = _head; c; c = c->getNext())
++size;
return size;
}
T * pop()
{
T * e = _head;
if (!e) return 0;
_head = e->getNext();
if (!_head) _tail = 0;
return e;
}
void prepend(T * e)
{
e->setNext(_head);
_head = e;
if (!_tail) _tail = e;
}
void append(T * e)
{
if (_tail) _tail->setNext(e);
else _head = e;
_tail = e;
}
void add(T * e) { prepend(e); }
private:
T * _head;
T * _tail;
};
template <class T> class TR_TwoLinkListIterator
{
public:
TR_TwoLinkListIterator() { }
TR_TwoLinkListIterator(T * l1, T * l2) : _firstListHead(l1), _secondListHead(l2) { }
T * getFirst()
{
if (_firstListHead) { _cursor = _firstListHead; _iteratingThruSecondList = false; }
else { _cursor = _secondListHead; _iteratingThruSecondList = true; }
return _cursor;
}
T * getNext()
{
if (_cursor)
{
_cursor = _cursor->getNext();
if (_cursor)
return _cursor;
}
if (!_iteratingThruSecondList)
{
_iteratingThruSecondList = true;
_cursor = _secondListHead;
}
return _cursor;
}
protected:
T * _firstListHead;
T * _secondListHead;
T * _cursor;
bool _iteratingThruSecondList;
};
template <class Key, class Value> class TR_Pair
{
public:
TR_ALLOC(TR_Memory::Pair); // FIXME
TR_Pair(Key *key, Value *value) : _key(key), _value(value) {}
Key *getKey() { return _key; }
Value *getValue() { return _value; }
void setKey(Key* key) { _key = key; }
void setValue(Value* value) { _value = value; }
private:
Key *_key;
Value *_value;
};
// Template specialization to deal with (ptr,val) pairs that the top class can't deal with unless you do ugly casting
template <class Key, class Value> class TR_Pair<Key *,Value>
{
public:
TR_ALLOC(TR_Memory::Pair); // FIXME
TR_Pair(Key *key, Value value) : _key(key), _value(value) {}
Key *getKey() { return _key; }
Value getValue() { return _value; }
void setKey(Key* key) { _key = key; }
void setValue(Value value) { _value = value; }
private:
Key *_key;
Value _value;
};
#endif