]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/cons.hh
7e2a995450a0a48fceb4dcca6c197fb2330fa927
[lilypond.git] / lily / include / cons.hh
1 /*   
2   cons.hh -- declare LISP like datatypes
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #ifndef CONS_HH
11 #define CONS_HH
12
13
14 template<class T>
15 class Cons
16 {
17 public:
18   T * car_;
19   Cons * next_;
20   Cons ()
21     {
22       car_=0;
23       next_ =0;
24     }
25   Cons (T*t, Cons<T>*c)
26     {
27       car_ = t;
28       next_ = c;
29     }
30  virtual ~Cons ()
31     {
32       delete next_;
33     }
34 };
35
36 template<class T>
37 class Killing_cons : public Cons<T>
38 {
39 public:
40   Killing_cons (T *t, Cons<T> *p)
41     : Cons<T>( t,p)
42     {
43     }
44   virtual ~Killing_cons ();
45 };
46
47
48 /// remove the link pointed to by *p.
49 template<class T>
50 Cons<T> *remove_cons (Cons<T> **pp)
51 {
52   Cons<T> *knip = *pp;
53   *pp = (*pp)->next_;
54   knip->next_ = 0;
55   return knip;
56 }
57
58
59 template<class T>
60 class Cons_list
61 {
62 public:
63   Cons<T> * head_;
64   Cons<T> ** tail_;
65   Cons_list () { init_list (); }
66   void init_list () {head_ =0; tail_ = &head_; }
67   void append (Cons<T> *c)
68     {
69       assert (!c->next_);
70       *tail_ = c;
71       while (*tail_)
72         tail_ = &(*tail_)->next_;
73     }
74   Cons<T> *remove_cons (Cons<T> **pp)
75     {
76       if (&(*pp)->next_ == tail_)
77         tail_ = pp;
78
79       return ::remove_cons (pp);
80     }
81   void junk ()
82     {
83       delete head_;
84       head_ =0;
85     }
86   ~Cons_list () { junk (); }
87 };
88
89
90 template<class T>
91 void  copy_killing_cons_list (Cons_list<T>&, Cons<T> *src);
92 template<class T>
93 void
94 clone_killing_cons_list (Cons_list<T>&, Cons<T> *src);
95
96 template<class T> int cons_list_size_i (Cons<T> *l)
97 {
98   int i=0;
99   while  (l)
100     {
101       l = l->next_;
102         i++;
103     }
104   return i;
105 }
106
107
108
109
110 #endif /* CONS_HH */
111