]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-array.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / grob-array.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 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 using std::vector;
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 SCM
44 Grob_array::mark_smob () const
45 {
46 #if 0  /* see System::derived_mark () const */
47   for (vsize i = 0; i < grobs_.size (); i++)
48     scm_gc_mark (grobs_[i]->self_scm ());
49 #endif
50   return SCM_UNDEFINED;
51 }
52
53 int
54 Grob_array::print_smob (SCM port, scm_print_state *) const
55 {
56   scm_puts ("#<Grob_array", port);
57   for (vsize i = 0; i < size (); i++)
58     {
59       scm_display (grob (i)->self_scm (), port);
60       scm_puts (" ", port);
61     }
62   scm_puts (">", port);
63   return 1;
64 }
65
66 SCM
67 Grob_array::make_array ()
68 {
69   Grob_array ga;
70   return ga.smobbed_copy ();
71 }
72
73 void
74 Grob_array::remove_duplicates ()
75 {
76   assert (!ordered_);
77
78   uniquify (grobs_);
79 }
80
81 void
82 Grob_array::filter (bool (*predicate) (const Grob *))
83 {
84   vsize new_size = 0;
85   for (vsize i = 0; i < grobs_.size (); ++i)
86     if (predicate (grobs_[i]))
87       grobs_[new_size++] = grobs_[i];
88   grobs_.resize (new_size);
89   // could call grobs_.shrink_to_fit () with C++11
90 }
91
92 void
93 Grob_array::filter_map (Grob * (*map_fun) (Grob *))
94 {
95   vsize new_size = 0;
96   for (vsize i = 0; i < grobs_.size (); ++i)
97     if (Grob *grob = map_fun (grobs_[i]))
98       grobs_[new_size++] = grob;
99   grobs_.resize (new_size);
100   // could call grobs_.shrink_to_fit () with C++11
101 }
102
103 void
104 Grob_array::filter_map_assign (const Grob_array &src,
105                                Grob * (*map_fun) (Grob *))
106 {
107   if (&src != this)
108     {
109       grobs_.resize (0);
110       grobs_.reserve (src.grobs_.size ());
111       for (vsize i = 0; i < src.grobs_.size (); i++)
112         if (Grob *grob = map_fun (src.grobs_[i]))
113           grobs_.push_back (grob);
114       // could call grobs_.shrink_to_fit () with C++11
115     }
116   else
117     filter_map (map_fun);
118 }
119
120 const char Grob_array::type_p_name_[] = "ly:grob-array?";
121
122
123 SCM
124 grob_list_to_grob_array (SCM lst)
125 {
126   SCM arr_scm = Grob_array::make_array ();
127   Grob_array *ga = unsmob<Grob_array> (arr_scm);
128   for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
129     ga->add (unsmob<Grob> (scm_car (s)));
130   return arr_scm;
131 }
132
133 SCM
134 grob_array_to_list (Grob_array *array)
135 {
136   SCM list = SCM_EOL;
137   SCM *tail = &list;
138
139   for (vsize i = 0; i < array->size (); i++)
140     {
141       *tail = scm_cons (array->grob (i)->self_scm (), SCM_EOL);
142       tail = SCM_CDRLOC (*tail);
143     }
144   return list;
145 }