source: git/factory/templates/ftmpl_list.cc @ 27f57a6

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