flower-1.0.25
[lilypond.git] / flower / list.tcc
blob37ded81bbe83ae82ccc762823613dca7726ed3be
1 #ifndef LIST_CC
2 #define LIST_CC
4 #include "list.hh"
6 template<class T>
7 List<T>::List(List const&src)
9     set_empty();
10     // probably el stupido
11     for (Cursor<T> c(src); c.ok(); c++)
12         bottom().add(c);
15 template<class T>
16 void
17 List<T>::OK() const
19     int i = size_;
20     Link<T> *lp = top_;
21     while (i--) {
22         assert(lp);
23         lp->OK();
24         lp = lp->next();
25     }
26     assert(!lp);
27      i = size_;
28     lp = bottom_;
29     while (i--) {
30         assert(lp);
31         lp->OK();
32         lp = lp->previous();
33     }
34     assert(!lp);
38 template<class T>
39 List<T>::~List()
41     Cursor<T> c(*this);
42     while (c.ok())
43         c.del();
46 template<class T>
47 void
48 List<T>::add( const T& thing, Cursor<T> &after_me )
50     if (!size_) {               // not much choice if list is empty
51         bottom_ = top_ = new Link<T>( thing );
52         if (!after_me.ok())
53             after_me = bottom();
54     } else {                    // add at aprioprate place
55         if (!after_me.ok())
56             after_me = bottom();
57         Link<T> *p =after_me.pointer();
58         p->add(thing);
59         if (p == bottom_)       // adjust bottom_ if necessary.
60             bottom_ = p->next();
61     }
63     size_++;
65 /** 
67   Procedure:
68   \begin{itemize}
69   \item if #after_me# is #ok()#, add after #after_me#, else
70   \item if list !empty simply add to bottom, else
71   \item list is empty: create first \Ref{Link} and initialize 
72   #bottom_# and #top_#.
73   \end{itemize}
76 template<class T>
77 void
78 List<T>::insert( const T& thing, Cursor<T> &before_me )
80     if (!size_) {
81         bottom_ = top_ = new Link<T>( thing );
82         if (!before_me.ok())
83             before_me = top();
84         
85     } else {
86         if (!before_me.ok())
87             before_me = top();
88         
89         Link<T> *p = before_me.pointer() ;
91         p->insert(thing);
92         if (p == top_)
93             top_ = p->previous();
94     }
96     size_++;
100 template<class T>
101 void
102 List<T>::concatenate(List<T> const&s)
104     Cursor<T> b(bottom());
105     for (Cursor<T> c(s); c.ok(); c++) {
106         b.add(c);
107         b++;
108     }
110 #endif