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