X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Finclude%2Fcons.hh;h=db922dba087f32742c85c87d824af98d1fd085b8;hb=33dc014ec7a5445fd4e70aeefc8e36421acf435c;hp=6e62c4c865b97cec07ba92663c2e156928cc15cf;hpb=67a763d2c94cf4e24c01186d9f2df06b31778778;p=lilypond.git diff --git a/flower/include/cons.hh b/flower/include/cons.hh index 6e62c4c865..db922dba08 100644 --- a/flower/include/cons.hh +++ b/flower/include/cons.hh @@ -3,7 +3,7 @@ source file of the GNU LilyPond music typesetter - (c) 1999 Han-Wen Nienhuys + (c) 1999--2003 Han-Wen Nienhuys */ @@ -11,6 +11,8 @@ #define CONS_HH +#include + template class Cons { @@ -38,7 +40,7 @@ class Killing_cons : public Cons { public: Killing_cons (T *t, Cons *p) - : Cons( t,p) + : Cons ( t,p) { } virtual ~Killing_cons (); @@ -55,11 +57,36 @@ Cons *remove_cons (Cons **pp) return knip; } +template int cons_list_size (Cons *l) +{ + int i=0; + while (l) + { + l = l->next_; + i++; + } + return i; +} + + + + + +template +Cons * last_cons (Cons * head) +{ + while (head && head->next_) + { + head = head->next_; + } + return head; +} + /** Invariants: - (*tail_) is either the head_ pointer, or a next_ pointer from the list. + (*tail_) is either the head_ pointer, or a next_ pointer from the list. **tail_ == NULL */ @@ -69,15 +96,26 @@ class Cons_list { public: Cons * head_; - Cons ** tail_; - Cons_list () { init_list (); } - void init_list () {head_ =0; tail_ = &head_; } + Cons ** nil_pointer_address_; + Cons_list () + { + init (); + } + void init () + { + head_ =0; + nil_pointer_address_ = &head_; + } + void append (T *c) + { + append (new Cons (c,0)); + } void append (Cons *c) { assert (!c->next_); - *tail_ = c; - while (*tail_) - tail_ = &(*tail_)->next_; + *nil_pointer_address_ = c; + while (*nil_pointer_address_) + nil_pointer_address_ = & (*nil_pointer_address_)->next_; } /** PRE: *pp should either be the head_ pointer, or the next_ pointer @@ -85,17 +123,42 @@ public: */ Cons *remove_cons (Cons **pp) { - if (&(*pp)->next_ == tail_) - tail_ = pp; + if (& (*pp)->next_ == nil_pointer_address_) + nil_pointer_address_ = pp; return ::remove_cons (pp); } + + /// junk everything after the first I elements. + void truncate (int i) + { + Cons **p = &head_; + for (; *p && i; p = & ((*p)->next_)) + { + i--; + } + + if (*p) + { + delete *p; + *p = 0; + } + nil_pointer_address_ = p; + } + void junk () { delete head_; head_ =0; } - ~Cons_list () { junk (); } + ~Cons_list () + { + junk (); + } + int size () + { + return cons_list_size (head_); + } }; @@ -105,19 +168,5 @@ template void clone_killing_cons_list (Cons_list&, Cons *src); -template int cons_list_size_i (Cons *l) -{ - int i=0; - while (l) - { - l = l->next_; - i++; - } - return i; -} - - - - #endif /* CONS_HH */