1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | // $Id: ftmpl_list.cc,v 1.3 1997-04-30 13:08:26 schmidt Exp $ |
---|
3 | |
---|
4 | /* |
---|
5 | $Log: not supported by cvs2svn $ |
---|
6 | Revision 1.2 1997/04/15 10:10:20 schmidt |
---|
7 | #include <config.h> added |
---|
8 | the headers config.h and assert.h will be included |
---|
9 | be makeheader now |
---|
10 | |
---|
11 | Revision 1.1 1997/03/27 10:31:57 schmidt |
---|
12 | stream-io wrapped by NOSTREAMIO |
---|
13 | error check changed to ASSERT |
---|
14 | |
---|
15 | Revision 1.0 1996/05/17 11:06:32 stobbe |
---|
16 | Initial revision |
---|
17 | |
---|
18 | */ |
---|
19 | |
---|
20 | #include <factoryconf.h> |
---|
21 | |
---|
22 | #include <templates/list.h> |
---|
23 | |
---|
24 | template <class T> |
---|
25 | ListItem<T>::ListItem( const ListItem<T>& i ) |
---|
26 | { |
---|
27 | next = i.next; |
---|
28 | prev = i.prev; |
---|
29 | item = i.item; |
---|
30 | } |
---|
31 | |
---|
32 | |
---|
33 | template <class T> |
---|
34 | ListItem<T>::ListItem( const T& t, ListItem<T>* n, ListItem<T>* p ) |
---|
35 | { |
---|
36 | next = n; |
---|
37 | prev = p; |
---|
38 | item = new T( t ); |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | template <class T> |
---|
43 | ListItem<T>::ListItem( T* t, ListItem<T>* n, ListItem<T>* p ) |
---|
44 | { |
---|
45 | next = n; |
---|
46 | prev = p; |
---|
47 | item = t; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | template <class T> |
---|
52 | ListItem<T>::~ListItem() |
---|
53 | { |
---|
54 | delete item; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | template <class T> |
---|
59 | ListItem<T>& ListItem<T>::operator=( const ListItem<T>& i ) |
---|
60 | { |
---|
61 | if ( this != &i ) { |
---|
62 | next = i.next; |
---|
63 | prev = i.prev; |
---|
64 | item = i.item; |
---|
65 | } |
---|
66 | return *this; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | template <class T> |
---|
71 | ListItem<T>* ListItem<T>::getNext() |
---|
72 | { |
---|
73 | return next; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | template <class T> |
---|
78 | ListItem<T>* ListItem<T>::getPrev() |
---|
79 | { |
---|
80 | return prev; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | template <class T> |
---|
85 | T& ListItem<T>::getItem() |
---|
86 | { |
---|
87 | return *item; |
---|
88 | } |
---|
89 | |
---|
90 | #ifndef NOSTREAMIO |
---|
91 | template <class T> |
---|
92 | void ListItem<T>::print( ostream & os ) |
---|
93 | { |
---|
94 | if ( item ) |
---|
95 | os << *item; |
---|
96 | else |
---|
97 | os << "(no item)"; |
---|
98 | } |
---|
99 | #endif /* NOSTREAMIO */ |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | template <class T> |
---|
104 | List<T>::List() |
---|
105 | { |
---|
106 | first = last = 0; |
---|
107 | _length = 0; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | template <class T> |
---|
112 | List<T>::List( const List<T>& l ) |
---|
113 | { |
---|
114 | ListItem<T>* cur = l.last; |
---|
115 | if ( cur ) { |
---|
116 | first = new ListItem<T>( *(cur->item), 0, 0 ); |
---|
117 | last = first; |
---|
118 | cur = cur->prev; |
---|
119 | while ( cur ) { |
---|
120 | first = new ListItem<T>( *(cur->item), first, 0 ); |
---|
121 | first->next->prev = first; |
---|
122 | cur = cur->prev; |
---|
123 | } |
---|
124 | _length = l._length; |
---|
125 | } |
---|
126 | else { |
---|
127 | first = last = 0; |
---|
128 | _length = 0; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | template <class T> |
---|
134 | List<T>::List( const T& t ) |
---|
135 | { |
---|
136 | first = last = new ListItem<T>( t, 0, 0 ); |
---|
137 | _length = 1; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | template <class T> |
---|
142 | List<T>::~List() |
---|
143 | { |
---|
144 | ListItem<T> *dummy; |
---|
145 | while ( first ) { |
---|
146 | dummy = first; |
---|
147 | first = first->next; |
---|
148 | delete dummy; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | template <class T> |
---|
154 | List<T>& List<T>::operator=( const List<T>& l ) |
---|
155 | { |
---|
156 | if ( this != &l ) { |
---|
157 | ListItem<T> *dummy; |
---|
158 | while ( first ) { |
---|
159 | dummy = first; |
---|
160 | first = first->next; |
---|
161 | delete dummy; |
---|
162 | } |
---|
163 | ListItem<T>* cur = l.last; |
---|
164 | if ( cur ) { |
---|
165 | first = new ListItem<T>( *(cur->item), 0, 0 ); |
---|
166 | last = first; |
---|
167 | cur = cur->prev; |
---|
168 | while ( cur ) { |
---|
169 | first = new ListItem<T>( *(cur->item), first, 0 ); |
---|
170 | first->next->prev = first; |
---|
171 | cur = cur->prev; |
---|
172 | } |
---|
173 | _length = l._length; |
---|
174 | } |
---|
175 | else { |
---|
176 | first = last = 0; |
---|
177 | _length = 0; |
---|
178 | } |
---|
179 | _length = l._length; |
---|
180 | } |
---|
181 | return *this; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | template <class T> |
---|
186 | void List<T>::insert ( const T& t ) |
---|
187 | { |
---|
188 | first = new ListItem<T>( t, first, 0 ); |
---|
189 | if ( last ) |
---|
190 | first->next->prev = first; |
---|
191 | last = ( last ) ? last : first; |
---|
192 | _length++; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | template <class T> |
---|
197 | void List<T>::insert ( const T& t, int (*cmpf)( const T&, const T& ) ) |
---|
198 | { |
---|
199 | if ( ! first || cmpf( *first->item, t ) > 0 ) |
---|
200 | insert( t ); |
---|
201 | else if ( cmpf( *last->item, t ) < 0 ) |
---|
202 | append( t ); |
---|
203 | else { |
---|
204 | ListItem<T> * cursor = first; |
---|
205 | int c; |
---|
206 | while ( (c = cmpf( *cursor->item, t )) < 0 ) |
---|
207 | cursor = cursor->next; |
---|
208 | if ( c == 0 ) |
---|
209 | *cursor->item = t; |
---|
210 | else { |
---|
211 | cursor = cursor->prev; |
---|
212 | cursor->next = new ListItem<T>( t, cursor->next, cursor ); |
---|
213 | cursor->next->next->prev = cursor->next; |
---|
214 | _length++; |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | template <class T> |
---|
221 | void List<T>::insert ( const T& t, int (*cmpf)( const T&, const T& ), void (*insf)( T&, const T& ) ) |
---|
222 | { |
---|
223 | if ( ! first || cmpf( *first->item, t ) > 0 ) |
---|
224 | insert( t ); |
---|
225 | else if ( cmpf( *last->item, t ) < 0 ) |
---|
226 | append( t ); |
---|
227 | else { |
---|
228 | ListItem<T> * cursor = first; |
---|
229 | int c; |
---|
230 | while ( (c = cmpf( *cursor->item, t )) < 0 ) |
---|
231 | cursor = cursor->next; |
---|
232 | if ( c == 0 ) |
---|
233 | insf( *cursor->item, t ); |
---|
234 | else { |
---|
235 | cursor = cursor->prev; |
---|
236 | cursor->next = new ListItem<T>( t, cursor->next, cursor ); |
---|
237 | cursor->next->next->prev = cursor->next; |
---|
238 | _length++; |
---|
239 | } |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | template <class T> |
---|
245 | void List<T>::append ( const T& t ) |
---|
246 | { |
---|
247 | last = new ListItem<T>( t, 0, last ); |
---|
248 | if ( first ) |
---|
249 | last->prev->next = last; |
---|
250 | first = ( first ) ? first : last; |
---|
251 | _length++; |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | template <class T> |
---|
256 | int List<T>::isEmpty() const |
---|
257 | { |
---|
258 | return ( first == 0 ); |
---|
259 | } |
---|
260 | |
---|
261 | template <class T> |
---|
262 | int List<T>::length() const |
---|
263 | { |
---|
264 | return _length; |
---|
265 | } |
---|
266 | |
---|
267 | template <class T> |
---|
268 | T List<T>::getFirst() const |
---|
269 | { |
---|
270 | ASSERT( first, "List: no item available" ); |
---|
271 | return first->getItem(); |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | template <class T> |
---|
276 | void List<T>::removeFirst() |
---|
277 | { |
---|
278 | if ( first ) { |
---|
279 | _length--; |
---|
280 | if ( first == last ) { |
---|
281 | delete first; |
---|
282 | first = last = 0; |
---|
283 | } |
---|
284 | else { |
---|
285 | ListItem<T> *dummy = first; |
---|
286 | first->next->prev = 0; |
---|
287 | first = first->next; |
---|
288 | delete dummy; |
---|
289 | } |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | |
---|
294 | template <class T> |
---|
295 | T List<T>::getLast() const |
---|
296 | { |
---|
297 | ASSERT( first, "List: no item available" ); |
---|
298 | return last->getItem(); |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | template <class T> |
---|
303 | void List<T>::removeLast() |
---|
304 | { |
---|
305 | if ( last ) { |
---|
306 | _length--; |
---|
307 | if ( first == last ) { |
---|
308 | delete last; |
---|
309 | first = last = 0; |
---|
310 | } |
---|
311 | else { |
---|
312 | ListItem<T> *dummy = last; |
---|
313 | last->prev->next = 0; |
---|
314 | last = last->prev; |
---|
315 | delete dummy; |
---|
316 | } |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | |
---|
321 | template <class T> |
---|
322 | void List<T>::sort( int (*swapit) ( const T&, const T& ) ) |
---|
323 | { |
---|
324 | if ( first != last ) { |
---|
325 | int swap = 1; |
---|
326 | while ( swap ) { |
---|
327 | swap = 0; |
---|
328 | ListItem<T> *cur = first; |
---|
329 | while ( cur->next ) { |
---|
330 | if ( swapit( *(cur->item), *(cur->next->item) ) ) { |
---|
331 | T* dummy = cur->item; |
---|
332 | cur->item = cur->next->item; |
---|
333 | cur->next->item = dummy; |
---|
334 | swap = 1; |
---|
335 | } |
---|
336 | cur = cur->next; |
---|
337 | } |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | |
---|
343 | #ifndef NOSTREAMIO |
---|
344 | template <class T> |
---|
345 | void List<T>::print ( ostream & os ) const |
---|
346 | { |
---|
347 | ListItem<T> *cur = first; |
---|
348 | os << "( "; |
---|
349 | while ( cur ) { |
---|
350 | cur->print( os ); |
---|
351 | if ( (cur = cur->getNext()) ) |
---|
352 | os << ", "; |
---|
353 | } |
---|
354 | os << " )"; |
---|
355 | } |
---|
356 | #endif /* NOSTREAMIO */ |
---|
357 | |
---|
358 | |
---|
359 | template <class T> |
---|
360 | ListIterator<T>::ListIterator() |
---|
361 | { |
---|
362 | theList = 0; |
---|
363 | current = 0; |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | template <class T> |
---|
368 | ListIterator<T>::ListIterator ( const ListIterator<T> & i ) |
---|
369 | { |
---|
370 | theList = i.theList; |
---|
371 | current = i.current; |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | template <class T> |
---|
376 | ListIterator<T>::ListIterator ( const List<T> & l ) |
---|
377 | { |
---|
378 | theList = (List<T>*)&l; |
---|
379 | current = l.first; |
---|
380 | } |
---|
381 | |
---|
382 | |
---|
383 | template <class T> |
---|
384 | ListIterator<T>::~ListIterator() { } |
---|
385 | |
---|
386 | |
---|
387 | template <class T> |
---|
388 | ListIterator<T>& ListIterator<T>::operator= ( const ListIterator<T> & I ) |
---|
389 | { |
---|
390 | if ( this != &I ) { |
---|
391 | theList = I.theList; |
---|
392 | current = I.current; |
---|
393 | } |
---|
394 | return *this; |
---|
395 | } |
---|
396 | |
---|
397 | |
---|
398 | template <class T> |
---|
399 | ListIterator<T>& ListIterator<T>::operator= ( const List<T> & l ) |
---|
400 | { |
---|
401 | theList = (List<T>*)&l; |
---|
402 | current = l.first; |
---|
403 | return *this; |
---|
404 | } |
---|
405 | |
---|
406 | |
---|
407 | template <class T> |
---|
408 | T& ListIterator<T>::getItem () const |
---|
409 | { |
---|
410 | ASSERT( current, "ListIterator: no item available" ); |
---|
411 | return current->getItem(); |
---|
412 | } |
---|
413 | |
---|
414 | |
---|
415 | template <class T> |
---|
416 | int ListIterator<T>::hasItem () |
---|
417 | { |
---|
418 | return current != 0; |
---|
419 | } |
---|
420 | |
---|
421 | |
---|
422 | template <class T> |
---|
423 | void ListIterator<T>::operator++ () |
---|
424 | { |
---|
425 | if ( current ) |
---|
426 | current = current->next; |
---|
427 | } |
---|
428 | |
---|
429 | |
---|
430 | template <class T> |
---|
431 | void ListIterator<T>::operator-- () |
---|
432 | { |
---|
433 | if ( current ) |
---|
434 | current = current->prev; |
---|
435 | } |
---|
436 | |
---|
437 | |
---|
438 | template <class T> |
---|
439 | void ListIterator<T>::operator++ ( int ) |
---|
440 | { |
---|
441 | if ( current ) |
---|
442 | current = current->next; |
---|
443 | } |
---|
444 | |
---|
445 | |
---|
446 | template <class T> |
---|
447 | void ListIterator<T>::operator-- ( int ) |
---|
448 | { |
---|
449 | if ( current ) |
---|
450 | current = current->prev; |
---|
451 | } |
---|
452 | |
---|
453 | |
---|
454 | template <class T> |
---|
455 | void ListIterator<T>::firstItem () |
---|
456 | { |
---|
457 | current = theList->first; |
---|
458 | } |
---|
459 | |
---|
460 | |
---|
461 | template <class T> |
---|
462 | void ListIterator<T>::lastItem () |
---|
463 | { |
---|
464 | current = theList->last; |
---|
465 | } |
---|
466 | |
---|
467 | |
---|
468 | template <class T> |
---|
469 | void ListIterator<T>::insert ( const T & t ) |
---|
470 | { |
---|
471 | if ( current ) { |
---|
472 | if ( ! current->prev ) |
---|
473 | theList->insert( t ); |
---|
474 | else { |
---|
475 | current->prev = new ListItem<T>( t, current, current->prev ); |
---|
476 | current->prev->prev->next = current->prev; |
---|
477 | theList->_length++; |
---|
478 | } |
---|
479 | } |
---|
480 | } |
---|
481 | |
---|
482 | |
---|
483 | template <class T> |
---|
484 | void ListIterator<T>::append ( const T & t ) |
---|
485 | { |
---|
486 | if ( current ) { |
---|
487 | if ( ! current->next ) |
---|
488 | theList->append( t ); |
---|
489 | else { |
---|
490 | current->next = new ListItem<T>( t, current->next, current ); |
---|
491 | current->next->next->prev = current->next; |
---|
492 | theList->_length++; |
---|
493 | } |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | |
---|
498 | template <class T> |
---|
499 | void ListIterator<T>::remove ( int moveright ) |
---|
500 | { |
---|
501 | if ( current ) { |
---|
502 | ListItem <T>*dummynext = current->next, *dummyprev = current->prev; |
---|
503 | if ( current->prev ) { |
---|
504 | current->prev->next = current->next; |
---|
505 | if ( current->next ) |
---|
506 | current->next->prev = current->prev; |
---|
507 | else |
---|
508 | theList->last = current->prev; |
---|
509 | delete current; |
---|
510 | current = ( moveright ) ? dummynext : dummyprev; |
---|
511 | } |
---|
512 | else { |
---|
513 | if ( current->next ) |
---|
514 | current->next->prev = 0; |
---|
515 | theList->first = current->next; |
---|
516 | delete current; |
---|
517 | current = ( moveright ) ? dummynext : dummyprev; |
---|
518 | } |
---|
519 | } |
---|
520 | } |
---|
521 | |
---|
522 | #ifndef NOSTREAMIO |
---|
523 | template <class T> |
---|
524 | ostream& operator<<( ostream & os, const List<T> & l ) |
---|
525 | { |
---|
526 | l.print( os ); |
---|
527 | return os; |
---|
528 | } |
---|
529 | #endif /* NOSTREAMIO */ |
---|
530 | |
---|
531 | template <class T> |
---|
532 | List<T> Union ( const List<T> & F, const List<T> & G ) |
---|
533 | { |
---|
534 | List<T> L = G; |
---|
535 | ListIterator<T> i, j; |
---|
536 | T f; |
---|
537 | bool iselt; |
---|
538 | |
---|
539 | for ( i = F; i.hasItem(); i++ ) { |
---|
540 | f = i.getItem(); |
---|
541 | iselt = false; |
---|
542 | j = G; |
---|
543 | while ( ( ! iselt ) && j.hasItem() ) { |
---|
544 | iselt = f == j.getItem(); |
---|
545 | j++; |
---|
546 | } |
---|
547 | if ( ! iselt ) |
---|
548 | L.append( f ); |
---|
549 | } |
---|
550 | return L; |
---|
551 | } |
---|
552 | |
---|
553 | template <class T> |
---|
554 | List<T> Union ( const List<T> & F, const List<T> & G, int (*cmpf)( const T&, const T& ), void (*insf)( T&, const T& ) ) |
---|
555 | { |
---|
556 | List<T> L = G; |
---|
557 | ListIterator<T> i; |
---|
558 | |
---|
559 | for ( i = F; i.hasItem(); ++i ) |
---|
560 | L.insert( i.getItem(), cmpf, insf ); |
---|
561 | return L; |
---|
562 | } |
---|
563 | |
---|
564 | template <class T> |
---|
565 | List<T> Difference ( const List<T> & F, const List<T> & G ) |
---|
566 | { |
---|
567 | List<T> L; |
---|
568 | ListIterator<T> i, j; |
---|
569 | T f; |
---|
570 | int found; |
---|
571 | for ( i = F; i.hasItem(); ++i ) { |
---|
572 | f = i.getItem(); |
---|
573 | found = 0; |
---|
574 | for ( j = G; j.hasItem() && (!found); ++j ) |
---|
575 | found = f == j.getItem(); |
---|
576 | if ( ! found ) |
---|
577 | L.append( f ); |
---|
578 | } |
---|
579 | return L; |
---|
580 | } |
---|
581 | |
---|
582 | template <class T> |
---|
583 | T prod ( const List<T> & F ) |
---|
584 | { |
---|
585 | ListIterator<T> i; |
---|
586 | T p = 1; |
---|
587 | for ( i = F; i.hasItem(); i++ ) |
---|
588 | p = p * i.getItem(); |
---|
589 | return p; |
---|
590 | } |
---|