]> git.donarmstrong.com Git - lilypond.git/blob - lily/lilypond-key.cc
Fix off-by-one error in constrained-breaking.
[lilypond.git] / lily / lilypond-key.cc
1 /*
2   lilypond-key.cc -- implement Lilypond_{grob, context}_key
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "lilypond-key.hh"
10
11 Lilypond_grob_key::Lilypond_grob_key (Object_key const *context,
12                                       Moment start,
13                                       string name,
14                                       int disambiguation_count)
15 {
16   context_ = context;
17   creation_moment_ = start;
18   grob_name_ = name;
19   disambiguation_count_ = disambiguation_count;
20 }
21
22 void
23 Lilypond_grob_key::derived_mark () const
24 {
25   if (context_)
26     scm_gc_mark (context_->self_scm ());
27 }
28
29 // todo: reverse order of comparison for efficiency reasons.
30 int
31 Lilypond_grob_key::do_compare (Object_key const *key) const
32 {
33   Lilypond_grob_key const *other = dynamic_cast<Lilypond_grob_key const *> (key);
34   int c;
35
36   c = context_->compare (other->context_);
37   if (c)
38     return c;
39
40   c = Moment::compare (creation_moment_, other->creation_moment_);
41   if (c)
42     return c;
43
44   c = grob_name_.compare (other->grob_name_);
45   if (c)
46     return c;
47
48   c = sign (disambiguation_count_ - other->disambiguation_count_);
49   if (c)
50     return c;
51
52   return 0;
53 }
54
55 int
56 Lilypond_grob_key::get_type () const
57 {
58   return GROB_KEY;
59 }
60
61 SCM
62 Lilypond_grob_key::as_scheme () const
63 {
64   return scm_list_4 (context_ ? context_->self_scm () : SCM_BOOL_F,
65                      creation_moment_.smobbed_copy (),
66                      scm_makfrom0str (grob_name_.c_str ()),
67                      scm_from_int (disambiguation_count_));
68 }
69
70 Object_key *
71 Lilypond_grob_key::from_scheme (SCM a)
72 {
73   return new Lilypond_grob_key (unsmob_key (scm_car (a)),
74                                 *unsmob_moment (scm_cadr (a)),
75                                 ly_scm2string (scm_caddr (a)),
76                                 scm_to_int (scm_list_ref (a, scm_from_int (3))));
77 }
78
79 /****************************************************************/
80
81 void
82 Lilypond_context_key::derived_mark () const
83 {
84   if (parent_context_)
85     scm_gc_mark (parent_context_->self_scm ());
86 }
87
88 Lilypond_context_key::Lilypond_context_key (Object_key const *parent,
89                                             Moment start,
90                                             string type,
91                                             string id,
92                                             int count)
93 {
94   disambiguation_count_ = count;
95   parent_context_ = parent;
96   start_moment_ = start;
97   context_name_ = type;
98   id_ = id;
99 }
100
101 int
102 Lilypond_context_key::do_compare (Object_key const *key) const
103 {
104   Lilypond_context_key const *other
105     = dynamic_cast<Lilypond_context_key const *> (key);
106
107   int c;
108   if (parent_context_ && other->parent_context_)
109     {
110       c = parent_context_->compare (other->parent_context_);
111       if (c)
112         return c;
113     }
114   else if (parent_context_)
115     return -1;
116   else if (other->parent_context_)
117     return 1;
118
119   c = Moment::compare (start_moment_, other->start_moment_);
120   if (c)
121     return c;
122
123   c = context_name_.compare (other->context_name_);
124   if (c)
125     return c;
126
127   c = id_.compare (other->id_);
128   if (c)
129     return c;
130
131   c = sign (disambiguation_count_ - other->disambiguation_count_);
132   if (c)
133     return c;
134
135   return 0;
136 }
137
138 int
139 Lilypond_context_key::get_type () const
140 {
141   return CONTEXT_KEY;
142 }
143
144 SCM
145 Lilypond_context_key::as_scheme () const
146 {
147   return scm_list_5 (parent_context_ ? parent_context_->self_scm () : SCM_BOOL_F,
148                      start_moment_.smobbed_copy (),
149                      scm_makfrom0str (context_name_.c_str ()),
150                      scm_makfrom0str (id_.c_str ()),
151                      scm_from_int (disambiguation_count_));
152 }
153
154 Object_key *
155 Lilypond_context_key::from_scheme (SCM a)
156 {
157   return new Lilypond_context_key (unsmob_key (scm_car (a)),
158                                    *unsmob_moment (scm_cadr (a)),
159                                    ly_scm2string (scm_list_ref (a, scm_from_int (2))),
160                                    ly_scm2string (scm_list_ref (a, scm_from_int (3))),
161                                    scm_to_int (scm_list_ref (a, scm_from_int (4))));
162 }
163
164 /****************************************************************/
165
166 int
167 Lilypond_general_key::get_type () const
168 {
169   return GENERAL_KEY;
170 }
171
172 void
173 Lilypond_general_key::derived_mark () const
174 {
175   if (parent_)
176     scm_gc_mark (parent_->self_scm ());
177 }
178
179 Lilypond_general_key::Lilypond_general_key (Object_key const *parent,
180                                             string name,
181                                             int count)
182 {
183   parent_ = parent;
184   name_ = name;
185   disambiguation_count_ = count;
186 }
187
188 int
189 Lilypond_general_key::do_compare (Object_key const *key)const
190 {
191   Lilypond_general_key const *other
192     = dynamic_cast<Lilypond_general_key const *> (key);
193
194   if (parent_ && other->parent_)
195     parent_->compare (other->parent_);
196   else if (parent_)
197     return -1;
198   else if (other->parent_)
199     return 1;
200
201   int c = name_.compare (other->name_);
202   if (c)
203     return c;
204
205   c = sign (disambiguation_count_ - other->disambiguation_count_);
206   if (c)
207     return c;
208
209   return 0;
210 }
211
212 SCM
213 Lilypond_general_key::as_scheme () const
214 {
215   return scm_list_3 (parent_ ? parent_->self_scm () : SCM_BOOL_F,
216                      scm_makfrom0str (name_.c_str ()),
217                      scm_from_int (disambiguation_count_));
218 }
219
220 Object_key *
221 Lilypond_general_key::from_scheme (SCM a)
222 {
223   return new Lilypond_general_key (unsmob_key (scm_car (a)),
224                                    ly_scm2string (scm_list_ref (a, scm_from_int (1))),
225                                    scm_to_int (scm_list_ref (a, scm_from_int (2))));
226 }