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