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

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