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