source: git/factory/templates/ftmpl_list.cc @ 974ce1

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