source: git/factory/templates/ftmpl_list.cc @ 684544

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