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