]> git.donarmstrong.com Git - lilypond.git/blob - flower/list.inl
release: 0.0.2
[lilypond.git] / flower / list.inl
1 // -*-c++-*-
2 #ifndef LIST_INL
3 #define LIST_INL
4 template<class T>
5 inline
6 List<T>::List()
7 {
8     top_ = bottom_ = 0;
9     size_ = 0;
10 }
11
12 template<class T>
13 inline
14 List<T>::List( const T& thing )
15 {
16     top_ = bottom_ = 0;
17     size_ = 0;
18     add( thing, Cursor<T>( *this, bottom_ ) );
19 }
20
21 template<class T>
22 inline
23 List<T>::~List()
24 {
25     Cursor<T> next(*this);
26     for ( Cursor<T> c( *this ); c.ok(); c = next ) {
27         next = c;
28         next++;
29         remove( c );
30     }
31 }
32
33 template<class T>
34 inline void
35 List<T>::add( const T& thing, Cursor<T> after_me )
36 {
37 #if 0
38     if ( after_me.ok() )
39         after_me.pointer()->add( thing );
40     else if ( size_ )
41         bottom().pointer()->add( thing );
42     else
43         bottom_ = top_ = new Link<T>( thing );
44 #endif
45     
46     if (!size_) {               // not much choice if list is empty
47         bottom_ = top_ = new Link<T>( thing );
48     } else {                    // add at aprioprate place
49         Link<T> *p =  ( after_me.ok() ) ?
50             after_me.pointer() : bottom().pointer();
51         p->add(thing);
52         if (p == bottom_)       // adjust bottom_ if necessary.
53             bottom_ = p->next();
54     }
55
56     size_++;
57 }
58 /** 
59
60   Procedure:
61   \begin{itemize}
62   \item if #after_me# is #ok()#, add after #after_me#, else
63   \item if list !empty simply add to bottom, else
64   \item list is empty: create first \Ref{Link} and initialize 
65   #bottom_# and #top_#.
66   \end{itemize}
67 */
68
69 template<class T>
70 inline void
71 List<T>::insert( const T& thing, Cursor<T> before_me )
72 {
73     if (!size_) {
74         bottom_ = top_ = new Link<T>( thing );
75     } else {
76         Link<T> *p = 
77             (before_me.ok())?
78             before_me.pointer() : top().pointer();
79
80         p->insert(thing);
81         if (p == top_)
82             top_ = p->previous();
83     }
84         
85     size_++;
86 #if 0 // rewrite hwn 16/9               
87     if ( before_me.ok() )
88         before_me.pointer()->insert( thing );
89     else if ( size_ )
90         top().pointer()->insert( thing );
91     else
92         bottom_ = top_ = new Link<T>( thing );
93     size_++;
94 #endif
95 }
96
97 template<class T>
98 inline void
99 List<T>::remove( Cursor<T> me )
100 {
101     if ( me.ok() ){
102         Link<T> *lp = me.pointer();     
103         lp->remove(*this);
104         delete lp;
105         size_--;
106     }
107 }
108
109 template<class T>
110 inline int
111 List<T>::size() const
112
113     return size_;
114 }
115
116 template<class T>
117 inline
118 PointerList<T>::PointerList() :
119     List<T>()
120 {
121 }
122
123 template<class T>
124 inline
125 PointerList<T>::PointerList( const T& thing ) :
126     List<T>( thing )
127 {
128 }
129
130 template<class T>
131 inline
132 PointerList<T>::~PointerList()
133 {
134     Cursor<T> next(*this);
135     for ( Cursor<T> c( *this ); c.ok(); c = next ) {
136         next = c;
137         next++;
138         remove( c );            // PointerList::remove deletes the  real data
139     }
140 }
141
142 template<class T>
143 inline void
144 PointerList_print( PointerList<T> const & l  ) 
145 {
146     List<T>& promises_to_be_const = (List<T>&) l;
147     for ( Cursor<T> c( promises_to_be_const ); c.ok(); c++ )
148         (*c)->print();  
149 }
150
151
152 #endif