Fix substitution error in shape noteheads
[lilypond/mpolesky.git] / lily / moment.cc
blob1284c73691199fd454f4b236d70a9f5f98e69e2f
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1999--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "moment.hh"
22 #include "warn.hh"
24 Moment::Moment ()
28 Moment::Moment (int m)
30 main_part_ = Rational (m);
31 grace_part_ = Rational (0);
34 Moment::Moment (Rational m, Rational g)
36 main_part_ = m;
37 grace_part_ = g;
40 Moment::Moment (Rational m)
42 main_part_ = m;
43 grace_part_ = Rational (0);
46 #include "ly-smobs.icc"
48 IMPLEMENT_SIMPLE_SMOBS (Moment);
49 IMPLEMENT_TYPE_P (Moment, "ly:moment?");
51 SCM
52 Moment::mark_smob (SCM)
54 return SCM_EOL;
57 int
58 Moment::print_smob (SCM s, SCM port, scm_print_state *)
60 Moment *r = (Moment *) SCM_CELL_WORD_1 (s);
62 scm_puts ("#<Mom ", port);
63 string str = r->to_string ();
64 scm_puts ((char *)str.c_str (), port);
65 scm_puts (">", port);
67 return 1;
70 SCM
71 Moment::as_scheme () const
73 return scm_list_5 (ly_symbol2scm ("ly:make-moment"),
74 scm_from_int64 (main_part_.num ()),
75 scm_from_int64 (main_part_.den ()),
76 scm_from_int64 (grace_part_.num ()),
77 scm_from_int64 (grace_part_.den ()));
80 SCM
81 Moment::equal_p (SCM a, SCM b)
83 Moment *m1 = unsmob_moment (a);
84 Moment *m2 = unsmob_moment (b);
86 return (*m1 == *m2) ? SCM_BOOL_T : SCM_BOOL_F;
89 int
90 compare (Moment const &a, Moment const &b)
92 return Moment::compare (a, b);
95 int
96 Moment::compare (Moment const &a, Moment const &b)
98 int c = Rational::compare (a.main_part_, b.main_part_);
99 if (c)
100 return c;
102 return Rational::compare (a.grace_part_, b.grace_part_);
105 void
106 Moment::operator += (Moment const &src)
108 main_part_ += src.main_part_;
109 grace_part_ += src.grace_part_;
112 void
113 Moment::operator -= (Moment const &src)
115 main_part_ -= src.main_part_;
116 grace_part_ -= src.grace_part_;
119 /* Only take the main part of SRC for multiplication. */
120 void
121 Moment::operator *= (Moment const &src)
123 main_part_ *= src.main_part_;
124 grace_part_ *= src.main_part_;
127 /* Only take the main part of SRC for division. */
128 void
129 Moment::operator /= (Moment const &src)
131 main_part_ /= src.main_part_;
132 grace_part_ /= src.main_part_;
135 /* Only take the main part of SRC for division. */
136 void
137 Moment::operator %= (Moment const &src)
139 main_part_ %= src.main_part_;
140 grace_part_ %= src.main_part_;
144 Moment::den () const
146 /* TODO: ensure MSB == 0 here */
147 return main_part_.den ();
151 Moment::num () const
153 return main_part_.num ();
156 bool
157 Moment::to_bool () const
159 return main_part_ || grace_part_;
162 void
163 Moment::set_infinite (int k)
165 main_part_.set_infinite (k);
168 string
169 Moment::to_string () const
171 string s = main_part_.to_string ();
172 if (grace_part_)
173 s += "G" + grace_part_.to_string ();
174 return s;
177 Moment
178 Moment::operator - () const
180 Moment m;
181 m.grace_part_ = -grace_part_;
182 m.main_part_ = -main_part_;
183 return m;
186 #ifdef STREAM_SUPPORT
187 ostream &
188 operator << (ostream &os, Moment const &m)
190 os << m.to_string ();
191 return os;
193 #endif
195 Moment
196 robust_scm2moment (SCM m, Moment d)
198 Moment *p = unsmob_moment (m);
199 if (!p)
200 return d;
201 else
202 return *p;
205 bool
206 moment_less (SCM a, SCM b)
208 return *unsmob_moment (a) < *unsmob_moment (b);