]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-array.cc
d0cf11106878b12c73a80d6222cfb7aed955100e
[lilypond.git] / lily / grob-array.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "grob-array.hh"
21 #include "item.hh"
22 #include "spanner.hh"
23
24 #include "ly-smobs.icc"
25
26 Item *
27 Grob_array::item (vsize i)
28 {
29   return dynamic_cast<Item *> (grobs_.at (i));
30 }
31
32 Spanner *
33 Grob_array::spanner (vsize i)
34 {
35   return dynamic_cast<Spanner *> (grobs_.at (i));
36 }
37
38 Grob_array::Grob_array ()
39 {
40   ordered_ = true;
41 }
42
43 vector<Grob *> &
44 Grob_array::array_reference ()
45 {
46   return grobs_;
47 }
48
49 vector<Grob *> const &
50 Grob_array::array () const
51 {
52   return grobs_;
53 }
54
55 SCM
56 Grob_array::mark_smob (SCM s)
57 {
58   (void) s;
59
60 #if 0  /* see System::derived_mark () const */
61   Grob_array *ga = unsmob_grob_array (s);
62   for (vsize i = 0; i < ga->grobs_.size (); i++)
63     scm_gc_mark (ga->grobs_[i]->self_scm ());
64 #endif
65   return SCM_UNDEFINED;
66 }
67
68 int
69 Grob_array::print_smob (SCM arr, SCM port, scm_print_state *)
70 {
71   scm_puts ("#<Grob_array", port);
72
73   Grob_array *grob_arr = unsmob (arr);
74   for (vsize i = 0; i < grob_arr->size (); i++)
75     {
76       scm_display (grob_arr->grob (i)->self_scm (), port);
77       scm_puts (" ", port);
78     }
79   scm_puts (">", port);
80   return 1;
81 }
82
83 SCM
84 Grob_array::make_array ()
85 {
86   Grob_array ga;
87   return ga.smobbed_copy ();
88 }
89
90 void
91 Grob_array::clear ()
92 {
93   grobs_.clear ();
94 }
95
96 void
97 Grob_array::remove_duplicates ()
98 {
99   assert (!ordered_);
100
101   uniquify (grobs_);
102 }
103
104 bool
105 Grob_array::empty () const
106 {
107   return grobs_.empty ();
108 }
109
110 void
111 Grob_array::set_array (vector<Grob *> const &src)
112 {
113   grobs_ = src;
114 }
115
116 IMPLEMENT_SIMPLE_SMOBS (Grob_array);
117 IMPLEMENT_TYPE_P (Grob_array, "ly:grob-array?");
118
119 IMPLEMENT_DEFAULT_EQUAL_P (Grob_array);
120
121 SCM
122 grob_list_to_grob_array (SCM lst)
123 {
124   SCM arr_scm = Grob_array::make_array ();
125   Grob_array *ga = unsmob_grob_array (arr_scm);
126   for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
127     ga->add (unsmob_grob (scm_car (s)));
128   return arr_scm;
129 }
130
131 SCM
132 grob_array_to_list (Grob_array *array)
133 {
134   SCM list = SCM_EOL;
135   SCM *tail = &list;
136
137   for (vsize i = 0; i < array->size (); i++)
138     {
139       *tail = scm_cons (array->grob (i)->self_scm (), SCM_EOL);
140       tail = SCM_CDRLOC (*tail);
141     }
142   return list;
143 }