File: rtlist.templ.cpp

package info (click to toggle)
robotour 3.1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,596 kB
  • ctags: 2,972
  • sloc: cpp: 17,705; sh: 3,060; ansic: 2,778; makefile: 144
file content (368 lines) | stat: -rwxr-xr-x 7,810 bytes parent folder | download
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
/*
 * rtlist.templ.cpp
 * 
 * Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
 * and Martin Trautmann (martintrautmann@gmx.de) 
 * 
 * This file may be distributed and/or modified under the terms of the 
 * GNU General Public License version 2 as published by the Free Software 
 * Foundation. 
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * 
 */

////////////// Implementation of the double-linked list template List

#ifndef __LRT_LIST_TEMPL__
#define __LRT_LIST_TEMPL__

#include "rtlist.h"

namespace lrt {

//template<class T> List<T>::Iterator List<T>::noElement;

template<class T> List<T>::List() : len(0), _begin(0), _end(0)
{}

template<class T> List<T>::List(const List<T> &list) : len(0),
	_begin(0), _end(0)
{
	for(Iterator<T> i = list.begin(); i.hasElement(); ++i)
	{
		append(*i);
	}
}

template<class T> List<T>& List<T>::operator=(const List<T> &list)
{
	clear();
	for(Iterator<T> i = list.begin(); i.hasElement(); ++i)
	{
		append(*i);
	}
	return *this;
}

template<class T> List<T>::~List()
{
	clear();
}

template<class T> T& List<T>::append( const T& elem )
{
	Node *newNode = new Node(elem);
	if(len == 0)
	{
		_begin = newNode;
		_end = newNode;
	}
	else 
	{
		_end->setNext(newNode);
		newNode->setPrev(_end);
		_end = newNode;
	}
	len++;
	return newNode->accessElement();
}

template<class T> T& List<T>::prepend( const T& elem )
{
	Node *newNode = new Node(elem);
	if(len == 0)
	{
		_begin = newNode;
		_end = newNode;
	}
	else 
	{
		newNode->setNext(_begin);
		_begin->setPrev(newNode);
		_begin = newNode;
	}
	len++;
	return newNode->accessElement();
}

template<class T> T& List<T>::insertBefore( Iterator<T> &p, const T& elem )
{
	IteratorImpl* pos = LRT_DYNAMIC_CAST(IteratorImpl*)(p.getImpl());
	if(!pos) return const_cast<T&>(elem);

	if(!pos->hasElement()) { return append(elem); }

	Node* bef = pos->element;
	if(bef == _begin) { return prepend(elem); }
	
	Node* after = bef->getPrev();
	Node* newNode = new Node(elem);

	after->setNext(newNode);
	newNode->setPrev(after);
	bef->setPrev(newNode);
	newNode->setNext(bef);
	
	len++;
	return newNode->accessElement();
}

template<class T> T& List<T>::insertAfter ( Iterator<T> &p, const T& elem )
{
	IteratorImpl* pos = LRT_DYNAMIC_CAST(IteratorImpl*)(p.getImpl());
	if(!pos) return const_cast<T&>(elem);

	if(!pos->hasElement()) { return append(elem); }

	Node* after = pos->element;
	if(after == _end) { return append(elem); }
	
	Node* bef = after->getNext();
	Node* newNode = new Node(elem);

	after->setNext(newNode);
	newNode->setPrev(after);
	bef->setPrev(newNode);
	newNode->setNext(bef);
	
	len++;
	return newNode->accessElement();
}

template<class T> void List<T>::remove( Iterator<T> &p )
{
	IteratorImpl* pos = LRT_DYNAMIC_CAST(IteratorImpl*)(p.getImpl());
	if(!pos) return; // only delete from List::Iterators!

	if(pos->list != this) return; // only delete from this list!
	if(!pos->hasElement()) return; // only delete from iterators with elements!

	Node* elem = pos->element;
	pos->goToNext(); // point the iterator to the next element (not to the deleted one)
	remove(elem);
}

template<class T> typename List<T>::Node* List<T>::remove(Node* node)
{
	Node* before = node->getPrev();
	Node* after = node->getNext();

	if(!before) { // remove _begin
		_begin = after;
	}
	else {
		before->setNext(after);
	}
	if(!after) { // remove _end
		_end = before;
	}
	else {
		after->setPrev(before);
	}

	delete node;
	len--;	

	return after;
}

template<class T> void List<T>::clear()
{
	Node* oldPos = 0;
	for(Node* pos = _begin; pos != 0; pos = pos->getNext())
	{
		delete oldPos;
		oldPos = pos;
	}
	delete oldPos; // last node

	_begin = 0;
	_end = 0;
	len = 0;
}

template<class T> int List<T>::length() const
{
	return len;
}

template<class T> int List<T>::getSize() const
{
	return len;
}

// unmodifyable iterators ("constant")
template<class T> Iterator<T> List<T>::begin() const
{
	return Iterator<T>(new IteratorImpl(const_cast<List<T>* const>(this), _begin, false));
}

template<class T> Iterator<T> List<T>::end() const
{
	return Iterator<T>(new IteratorImpl(const_cast<List<T>* const>(this), _end, false));
}

// modifyable iterators 
template<class T> Iterator<T> List<T>::begin()
{
	return Iterator<T>(new IteratorImpl(this, _begin, true));
}

template<class T> Iterator<T> List<T>::end()
{
	return Iterator<T>(new IteratorImpl(this, _end, true));
}


// Direct access to the list nodes
template<class T> typename List<T>::Node* List<T>::first()
{
	return _begin;
}
template<class T> typename List<T>::Node* List<T>::last()
{
	return _end;
}
template<class T> const typename List<T>::Node* List<T>::first() const
{
	return _begin;
}
template<class T> const typename List<T>::Node* List<T>::last() const
{
	return _end;
}

template<class T> void deleteNode(List<T>& list, typename List<T>::Node*& n)
{
	if(!n) return;
	delete n->accessElement();
	n = list.remove(n);
}


////////////////// Iterator
/*
template<class T> List<T>::Iterator::Iterator() : 
	element(0)
{}

// STL-like access
template<class T> List<T>::Iterator List<T>::Iterator::operator++() const
{
	if(element == 0) return List<T>::noElement;
	return Iterator(element->getNext());
}

template<class T> List<T>::Iterator& List<T>::Iterator::operator++(int)
{
	if(element == 0) return List<T>::noElement;
	element = element->getNext();
	return *this;
}

template<class T> List<T>::Iterator List<T>::Iterator::operator--() const
{
	if(element == 0) return List<T>::noElement;
	return Iterator(element->getPrev());
}

template<class T> List<T>::Iterator& List<T>::Iterator::operator--(int)
{
	if(element == 0) return List<T>::noElement;
	element = element->getPrev();
	return *this;
}

template<class T> T& List<T>::Iterator::operator*()
{
	return element->accessElement();
}

template<class T> const T& List<T>::Iterator::operator*() const
{
	return element->accessElement();
}

template<class T> bool List<T>::Iterator::operator==( const Iterator & iter) const
{
	return (element == iter.element);
}

// Java-like access

template<class T> T& List<T>::Iterator::next()
{
	Node* ret = element;
	if(ret != 0) element = ret->getNext();
	return ret->accessElement();
}

template<class T> bool List<T>::Iterator::hasNext() const
{
	if(!element) return false;
	return (element->getNext() != 0);
}

template<class T> bool List<T>::Iterator::hasPrev() const
{
	if(!element) return false;
	return (element->getPrev() != 0);
}

template<class T> bool List<T>::Iterator::hasElement() const
{
	return (element != 0);
}

template<class T> List<T>::Iterator List<T>::Iterator::getNext() const
{
	if(!element) return List<T>::noElement;
	return Iterator(element->getNext());
}

//// Iterator:: private
template<class T> List<T>::Iterator::Iterator( Node* el) : 
	element(el)
{}

*/
/*
//////////////// Node /////////////////////

template<class T> List<T>::Node::Node( const T &element ) : 
	element(element), next(0), prev(0)
{	
}

template<class T> List<T>::Node* List<T>::Node::getNext() const
{
	return next;
}

template<class T> List<T>::Node* List<T>::Node::getPrev() const
{
	return prev;
}

template<class T> void List<T>::Node::setNext( Node* node)
{
	next = node;
}

template<class T> void List<T>::Node::setPrev( Node* node)
{
	prev = node;
}

template<class T> T& List<T>::Node::accessElement()
{
	return element;
}
*/

} // namespace

#endif