OLD | NEW |
1 /* | 1 /* |
2 This file is part of LilyPond, the GNU music typesetter. | 2 This file is part of LilyPond, the GNU music typesetter. |
3 | 3 |
4 Copyright (C) 2005--2020 Han-Wen Nienhuys <hanwen@xs4all.nl> | 4 Copyright (C) 2005--2020 Han-Wen Nienhuys <hanwen@xs4all.nl> |
5 | 5 |
6 LilyPond is free software: you can redistribute it and/or modify | 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 | 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 | 8 the Free Software Foundation, either version 3 of the License, or |
9 (at your option) any later version. | 9 (at your option) any later version. |
10 | 10 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 Grob_array::filter_map (Grob * (*map_fun) (Grob *)) | 75 Grob_array::filter_map (Grob * (*map_fun) (Grob *)) |
76 { | 76 { |
77 vsize new_size = 0; | 77 vsize new_size = 0; |
78 for (vsize i = 0; i < grobs_.size (); ++i) | 78 for (vsize i = 0; i < grobs_.size (); ++i) |
79 if (Grob *grob = map_fun (grobs_[i])) | 79 if (Grob *grob = map_fun (grobs_[i])) |
80 grobs_[new_size++] = grob; | 80 grobs_[new_size++] = grob; |
81 grobs_.resize (new_size); | 81 grobs_.resize (new_size); |
82 grobs_.shrink_to_fit (); | 82 grobs_.shrink_to_fit (); |
83 } | 83 } |
84 | 84 |
| 85 template <typename T> |
| 86 void |
| 87 Grob_array::filter_map2 (Grob *(*map_fun) (T, Grob *), T arg) |
| 88 { |
| 89 vsize new_size = 0; |
| 90 for (vsize i = 0; i < grobs_.size (); ++i) |
| 91 if (Grob *grob = map_fun (arg, grobs_[i])) |
| 92 grobs_[new_size++] = grob; |
| 93 grobs_.resize (new_size); |
| 94 grobs_.shrink_to_fit (); |
| 95 } |
| 96 |
85 void | 97 void |
86 Grob_array::filter_map_assign (const Grob_array &src, | 98 Grob_array::filter_map_assign (const Grob_array &src, |
87 Grob * (*map_fun) (Grob *)) | 99 Grob * (*map_fun) (Grob *)) |
88 { | 100 { |
89 if (&src != this) | 101 if (&src != this) |
90 { | 102 { |
91 grobs_.clear (); | 103 grobs_.clear (); |
92 grobs_.reserve (src.grobs_.size ()); | 104 grobs_.reserve (src.grobs_.size ()); |
93 for (vsize i = 0; i < src.grobs_.size (); i++) | 105 for (vsize i = 0; i < src.grobs_.size (); i++) |
94 if (Grob *grob = map_fun (src.grobs_[i])) | 106 if (Grob *grob = map_fun (src.grobs_[i])) |
95 grobs_.push_back (grob); | 107 grobs_.push_back (grob); |
96 grobs_.shrink_to_fit (); | 108 grobs_.shrink_to_fit (); |
97 } | 109 } |
98 else | 110 else |
99 filter_map (map_fun); | 111 filter_map (map_fun); |
100 } | 112 } |
101 | 113 |
| 114 template <typename T> |
| 115 void |
| 116 Grob_array::filter_map_assign2 (const Grob_array &src, |
| 117 Grob *(*map_fun) (T, Grob *), T arg) |
| 118 { |
| 119 if (&src != this) |
| 120 { |
| 121 grobs_.clear (); |
| 122 grobs_.reserve (src.grobs_.size ()); |
| 123 for (vsize i = 0; i < src.grobs_.size (); i++) |
| 124 if (Grob *grob = map_fun (arg, src.grobs_[i])) |
| 125 grobs_.push_back (grob); |
| 126 grobs_.shrink_to_fit (); |
| 127 } |
| 128 else |
| 129 filter_map2 (map_fun, arg); |
| 130 } |
| 131 |
102 const char *const Grob_array::type_p_name_ = "ly:grob-array?"; | 132 const char *const Grob_array::type_p_name_ = "ly:grob-array?"; |
103 | 133 |
104 SCM | 134 SCM |
105 grob_list_to_grob_array (SCM lst) | 135 grob_list_to_grob_array (SCM lst) |
106 { | 136 { |
107 SCM arr_scm = Grob_array::make_array (); | 137 SCM arr_scm = Grob_array::make_array (); |
108 Grob_array *ga = unsmob<Grob_array> (arr_scm); | 138 Grob_array *ga = unsmob<Grob_array> (arr_scm); |
109 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s)) | 139 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s)) |
110 ga->add (unsmob<Grob> (scm_car (s))); | 140 ga->add (unsmob<Grob> (scm_car (s))); |
111 return arr_scm; | 141 return arr_scm; |
112 } | 142 } |
113 | 143 |
114 SCM | 144 SCM |
115 grob_array_to_list (Grob_array *array) | 145 grob_array_to_list (Grob_array *array) |
116 { | 146 { |
117 SCM list = SCM_EOL; | 147 SCM list = SCM_EOL; |
118 SCM *tail = &list; | 148 SCM *tail = &list; |
119 | 149 |
120 for (vsize i = 0; i < array->size (); i++) | 150 for (vsize i = 0; i < array->size (); i++) |
121 { | 151 { |
122 *tail = scm_cons (array->grob (i)->self_scm (), SCM_EOL); | 152 *tail = scm_cons (array->grob (i)->self_scm (), SCM_EOL); |
123 tail = SCM_CDRLOC (*tail); | 153 tail = SCM_CDRLOC (*tail); |
124 } | 154 } |
125 return list; | 155 return list; |
126 } | 156 } |
| 157 |
| 158 template void Grob_array::filter_map2 (Grob *(*map_fun) (SCM, Grob *), SCM arg); |
| 159 |
| 160 template void Grob_array::filter_map_assign2 (const Grob_array &src, |
| 161 Grob *(*map_fun) (SCM, Grob *), |
| 162 SCM arg); |
OLD | NEW |