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