]> git.donarmstrong.com Git - lilypond.git/blob - flower/list.cc
55e1e205072e4547ce1a1a36e01efa622c3238f9
[lilypond.git] / flower / list.cc
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> next(*this);
42     for ( Cursor<T> c( *this ); c.ok(); c = next ) {
43         next = c;
44         next++;
45         remove( c );
46     }
47 }
48
49 template<class T>
50 void
51 List<T>::add( const T& thing, Cursor<T> &after_me )
52 {
53     if (!size_) {               // not much choice if list is empty
54         bottom_ = top_ = new Link<T>( thing );
55         if (!after_me.ok())
56             after_me = bottom();
57     } else {                    // add at aprioprate place
58         if (!after_me.ok())
59             after_me = bottom();
60         Link<T> *p =after_me.pointer();
61         p->add(thing);
62         if (p == bottom_)       // adjust bottom_ if necessary.
63             bottom_ = p->next();
64     }
65
66     size_++;
67 }
68 /** 
69
70   Procedure:
71   \begin{itemize}
72   \item if #after_me# is #ok()#, add after #after_me#, else
73   \item if list !empty simply add to bottom, else
74   \item list is empty: create first \Ref{Link} and initialize 
75   #bottom_# and #top_#.
76   \end{itemize}
77 */
78
79 template<class T>
80 void
81 List<T>::insert( const T& thing, Cursor<T> &before_me )
82 {
83     if (!size_) {
84         bottom_ = top_ = new Link<T>( thing );
85         if (!before_me.ok())
86             before_me = top();
87         
88     } else {
89         if (!before_me.ok())
90             before_me = top();
91         
92         Link<T> *p = before_me.pointer() ;
93
94         p->insert(thing);
95         if (p == top_)
96             top_ = p->previous();
97     }
98
99     size_++;
100 }
101
102
103 template<class T>
104 void
105 List<T>::concatenate(List<T> const&s)
106 {
107     Cursor<T> b(bottom());
108     for (Cursor<T> c(s); c.ok(); c++) {
109         b.add(c);
110         b++;
111     }
112 }
113 #endif