]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/list.tcc
release: 0.0.42.pre3
[lilypond.git] / flower / include / list.tcc
1 #ifndef LIST_CC
2 #define LIST_CC
3
4 #include "list.hh"
5
6 template<class T>
7 List<T>::List(List const&src)
8 {
9     set_empty();
10     // probably el stupido
11     for (Cursor<T> c(src); c.ok(); c++)
12         bottom().add(c);
13 }
14
15 template<class T>
16 void
17 List<T>::OK() const
18 {
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);
35 }
36
37
38 template<class T>
39 List<T>::~List()
40 {
41     Cursor<T> c(*this);
42     while (c.ok())
43         c.del();
44 }
45
46 /** 
47
48   add after after_me.
49
50   Procedure:
51   \begin{itemize}
52   \item if #after_me# is #ok()#, add after #after_me#, else
53   \item if list !empty simply add to bottom, else
54   \item list is empty: create first \Ref{Link} and initialize 
55   #bottom_# and #top_#.
56   \end{itemize}
57 */
58 template<class T>
59 void
60 List<T>::add( T const & thing, Cursor<T> &after_me )
61 {
62     if (!size_) {               // not much choice if list is empty
63         bottom_ = top_ = new Link<T>( thing );
64         if (!after_me.ok())
65             after_me = bottom();
66     } else {                    // add at aprioprate place
67         if (!after_me.ok())
68             after_me = bottom();
69         Link<T> *p =after_me.pointer();
70         p->add(thing);
71         if (p == bottom_)       // adjust bottom_ if necessary.
72             bottom_ = p->next();
73     }
74
75     size_++;
76 }
77
78 template<class T>
79 void
80 List<T>::insert( T const & thing, Cursor<T> &before_me )
81 {
82     if (!size_) {
83         bottom_ = top_ = new Link<T>( thing );
84         if (!before_me.ok())
85             before_me = top();
86         
87     } else {
88         if (!before_me.ok())
89             before_me = top();
90         
91         Link<T> *p = before_me.pointer() ;
92
93         p->insert(thing);
94         if (p == top_)
95             top_ = p->previous();
96     }
97
98     size_++;
99 }
100
101
102 template<class T>
103 void
104 List<T>::concatenate(List<T> const&s)
105 {
106     Cursor<T> b(bottom());
107     for (Cursor<T> c(s); c.ok(); c++) {
108         b.add(c);
109         b++;
110     }
111 }
112 #endif