]> git.donarmstrong.com Git - lilypond.git/blob - lily/dimension-cache.cc
release: 1.1.30
[lilypond.git] / lily / dimension-cache.cc
1 /*   
2   dimension-cache.cc --  implement Dimension_cache
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7  */
8
9 #include "dimension-cache.hh"
10 #include "parray.hh"
11
12 Dimension_cache::Dimension_cache (Dimension_cache const &d)
13 {
14   init();
15   empty_b_ = d.empty_b_;
16   offset_ = d.offset_; //let's hope others will copy  the refpoint appropriately. 
17 }
18
19 Dimension_cache::Dimension_cache ()
20 {
21   init();
22 }
23
24 void
25 Dimension_cache::init()
26 {
27   offset_ =0.0;
28   elt_l_ = 0;
29   dim_.set_empty ();
30   parent_l_ =0;
31   valid_b_ = false;
32   empty_b_ = false;
33 }
34
35
36 void
37 Dimension_cache::invalidate ()
38 {
39   valid_b_ = false;
40   invalidate_dependencies ();
41 }
42
43 void
44 Dimension_cache::invalidate_dependencies ()
45 {
46   for (int i=0; i < dependencies_l_arr_.size (); i++)
47     {
48       Dimension_cache * g = dependencies_l_arr_[i];
49       if (g->valid_b_)
50         {
51           g->invalidate ();
52         }
53     }
54 }
55
56 void
57 Dimension_cache::set_offset (Real x)
58 {
59   invalidate_dependencies ();
60   offset_ = x;
61 }
62
63 void
64 Dimension_cache::translate (Real x)
65 {
66   invalidate_dependencies ();
67   offset_ += x;
68 }
69
70
71 Real
72 Dimension_cache::absolute_coordinate () const
73 {
74   Real r = offset_;
75   for (Dimension_cache * c = parent_l_;
76        c; c = c->parent_l_)
77     r += c->offset_;
78   return r;
79 }
80
81 /*
82   what *should* these functions *do* anyway.
83  */
84 Real
85 Dimension_cache::relative_coordinate (Dimension_cache *d) const
86 {
87   Real r =0.0;
88   if (d == this)                // UGH
89     return 0.0;
90
91   for (Dimension_cache* c = parent_l_;
92        c != d;
93        c = c->parent_l_)
94     r +=  c->offset_;
95   return r;
96 }
97
98 Dimension_cache *
99 Dimension_cache::common_group (Dimension_cache const* s) const
100 {
101   Link_array<Dimension_cache const> my_groups;
102   for (Dimension_cache const *c = this;
103        c ; c = c->parent_l_)
104     my_groups.push (c);
105   
106   
107   Dimension_cache const *common=0;
108   
109   for (Dimension_cache const * d = s;
110        !common && d;
111        d = d->parent_l_)
112     common = my_groups.find_l (d);
113
114   return (Dimension_cache*)common;
115 }
116
117
118
119 void
120 Dimension_cache::set_empty (bool b)
121 {
122   if (empty_b_ != b)
123     {
124       empty_b_ = b;
125       if (!empty_b_)
126         invalidate ();
127     }
128 }  
129
130 void
131 Dimension_cache::set_dim (Interval v)
132 {
133   dim_ = v;
134   valid_b_ = true;
135 }
136   
137
138 Interval
139 Dimension_cache::get_dim () const
140 {
141   Interval r;
142   if (empty_b_)
143     {
144       r.set_empty ();
145       return r;
146     }
147       
148   assert (valid_b_);
149
150   r=dim_;
151   if (!r.empty_b()) // float exception on DEC Alpha
152     r += offset_;
153
154   return r;
155 }
156
157