*** empty log message ***
[lilypond.git] / flower / include / cons.hh
blob922ce5b32dbc36c0da5e0afe03559a717a5f8a1f
1 /*
2 cons.hh -- declare LISP like datatypes
4 source file of the GNU LilyPond music typesetter
6 (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #ifndef CONS_HH
11 #define CONS_HH
14 #include <assert.h>
16 template<class T>
17 class Cons
19 public:
20 T * car_;
21 Cons * next_;
22 Cons ()
24 car_=0;
25 next_ =0;
27 Cons (T*t, Cons<T>*c)
29 car_ = t;
30 next_ = c;
32 virtual ~Cons ()
34 delete next_;
38 template<class T>
39 class Killing_cons : public Cons<T>
41 public:
42 Killing_cons (T *t, Cons<T> *p)
43 : Cons<T> ( t,p)
46 virtual ~Killing_cons ();
50 /// remove the link pointed to by *p.
51 template<class T>
52 Cons<T> *remove_cons (Cons<T> **pp)
54 Cons<T> *knip = *pp;
55 *pp = (*pp)->next_;
56 knip->next_ = 0;
57 return knip;
60 template<class T> int cons_list_size (Cons<T> *l)
62 int i=0;
63 while (l)
65 l = l->next_;
66 i++;
68 return i;
75 template<class T>
76 Cons<T> * last_cons (Cons<T> * head)
78 while (head && head->next_)
80 head = head->next_;
82 return head;
85 /**
87 Invariants:
89 (*tail_) is either the head_ pointer, or a next_ pointer from the list.
91 **tail_ == NULL
94 template<class T>
95 class Cons_list
97 public:
98 Cons<T> * head_;
99 Cons<T> ** nil_pointer_address_;
100 Cons_list ()
102 init ();
104 void init ()
106 head_ =0;
107 nil_pointer_address_ = &head_;
109 void append (T *c)
111 append (new Cons<T> (c,0));
113 void append (Cons<T> *c)
115 assert (!c->next_);
116 *nil_pointer_address_ = c;
117 while (*nil_pointer_address_)
118 nil_pointer_address_ = & (*nil_pointer_address_)->next_;
121 PRE: *pp should either be the head_ pointer, or the next_ pointer
122 from a list cell.
124 Cons<T> *remove_cons (Cons<T> **pp)
126 if (& (*pp)->next_ == nil_pointer_address_)
127 nil_pointer_address_ = pp;
129 return ::remove_cons (pp);
132 /// junk everything after the first I elements.
133 void truncate (int i)
135 Cons<T> **p = &head_;
136 for (; *p && i; p = & ((*p)->next_))
138 i--;
141 if (*p)
143 delete *p;
144 *p = 0;
146 nil_pointer_address_ = p;
149 void junk ()
151 delete head_;
152 head_ =0;
154 ~Cons_list ()
156 junk ();
158 int size ()
160 return cons_list_size (head_);
165 template<class T>
166 void copy_killing_cons_list (Cons_list<T>&, Cons<T> *src);
167 template<class T>
168 void
169 clone_killing_cons_list (Cons_list<T>&, Cons<T> *src);
171 #endif /* CONS_HH */