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