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