1 /*
2 * Copyright © 2018 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
27 #include "hb.hh"
28 #include "hb-iter.hh"
29
30 #include "hb-array.hh"
31 #include "hb-set.hh"
32 #include "hb-ot-layout-common.hh"
33
34
35 template <typename T>
36 struct array_iter_t : hb_iter_with_fallback_t<array_iter_t<T>, T&>
37 {
array_iter_tarray_iter_t38 array_iter_t (hb_array_t<T> arr_) : arr (arr_) {}
39
40 typedef T& __item_t__;
41 static constexpr bool is_random_access_iterator = true;
__item_at__array_iter_t42 T& __item_at__ (unsigned i) const { return arr[i]; }
__forward__array_iter_t43 void __forward__ (unsigned n) { arr += n; }
__rewind__array_iter_t44 void __rewind__ (unsigned n) { arr -= n; }
__len__array_iter_t45 unsigned __len__ () const { return arr.length; }
operator !=array_iter_t46 bool operator != (const array_iter_t& o) { return arr != o.arr; }
47
48 private:
49 hb_array_t<T> arr;
50 };
51
52 template <typename T>
53 struct some_array_t
54 {
some_array_tsome_array_t55 some_array_t (hb_array_t<T> arr_) : arr (arr_) {}
56
57 typedef array_iter_t<T> iter_t;
itersome_array_t58 array_iter_t<T> iter () { return array_iter_t<T> (arr); }
operator array_iter_t<T>some_array_t59 operator array_iter_t<T> () { return iter (); }
operator hb_iter_t<array_iter_t<T>>some_array_t60 operator hb_iter_t<array_iter_t<T>> () { return iter (); }
61
62 private:
63 hb_array_t<T> arr;
64 };
65
66
67 template <typename Iter,
68 hb_requires (hb_is_iterator (Iter))>
69 static void
test_iterator_non_default_constructable(Iter it)70 test_iterator_non_default_constructable (Iter it)
71 {
72 /* Iterate over a copy of it. */
73 for (auto c = it.iter (); c; c++)
74 *c;
75
76 /* Same. */
77 for (auto c = +it; c; c++)
78 *c;
79
80 /* Range-based for over a copy. */
81 for (auto _ : +it)
82 (void) _;
83
84 it += it.len ();
85 it = it + 10;
86 it = 10 + it;
87
88 assert (*it == it[0]);
89
90 static_assert (true || it.is_random_access_iterator, "");
91 static_assert (true || it.is_sorted_iterator, "");
92 }
93
94 template <typename Iter,
95 hb_requires (hb_is_iterator (Iter))>
96 static void
test_iterator(Iter it)97 test_iterator (Iter it)
98 {
99 Iter default_constructed;
100 assert (!default_constructed);
101
102 test_iterator_non_default_constructable (it);
103 }
104
105 template <typename Iterable,
106 hb_requires (hb_is_iterable (Iterable))>
107 static void
test_iterable(const Iterable & lst=Null (Iterable))108 test_iterable (const Iterable &lst = Null (Iterable))
109 {
110 for (auto _ : lst)
111 (void) _;
112
113 // Test that can take iterator from.
114 test_iterator (lst.iter ());
115 }
116
117 int
main(int argc,char ** argv)118 main (int argc, char **argv)
119 {
120 const int src[10] = {};
121 int dst[20];
122 hb_vector_t<int> v;
123
124 array_iter_t<const int> s (src); /* Implicit conversion from static array. */
125 array_iter_t<const int> s2 (v); /* Implicit conversion from vector. */
126 array_iter_t<int> t (dst);
127
128 static_assert (array_iter_t<int>::is_random_access_iterator, "");
129
130 some_array_t<const int> a (src);
131
132 s2 = s;
133
134 hb_iter (src);
135 hb_iter (src, 2);
136
137 hb_fill (t, 42);
138 hb_copy (s, t);
139 hb_copy (a.iter (), t);
140
141 test_iterable (v);
142 hb_set_t st;
143 st << 1 << 15 << 43;
144 test_iterable (st);
145 hb_sorted_array_t<int> sa;
146 (void) static_cast<hb_iter_t<hb_sorted_array_t<int>, hb_sorted_array_t<int>::item_t>&> (sa);
147 (void) static_cast<hb_iter_t<hb_sorted_array_t<int>, hb_sorted_array_t<int>::__item_t__>&> (sa);
148 (void) static_cast<hb_iter_t<hb_sorted_array_t<int>, int&>&>(sa);
149 (void) static_cast<hb_iter_t<hb_sorted_array_t<int>>&>(sa);
150 (void) static_cast<hb_iter_t<hb_array_t<int>, int&>&> (sa);
151 test_iterable (sa);
152
153 test_iterable<hb_array_t<int>> ();
154 test_iterable<hb_sorted_array_t<const int>> ();
155 test_iterable<hb_vector_t<float>> ();
156 test_iterable<hb_set_t> ();
157 test_iterable<OT::Coverage> ();
158
159 test_iterator (hb_zip (st, v));
160 test_iterator_non_default_constructable (hb_enumerate (st));
161 test_iterator_non_default_constructable (hb_enumerate (st, -5));
162 test_iterator_non_default_constructable (hb_enumerate (hb_iter (st)));
163 test_iterator_non_default_constructable (hb_enumerate (hb_iter (st) + 1));
164 test_iterator_non_default_constructable (hb_iter (st) | hb_filter ());
165 test_iterator_non_default_constructable (hb_iter (st) | hb_map (hb_lidentity));
166
167 assert (true == hb_all (st));
168 assert (false == hb_all (st, 42u));
169 assert (true == hb_any (st));
170 assert (false == hb_any (st, 14u));
171 assert (true == hb_any (st, 14u, [] (unsigned _) { return _ - 1u; }));
172 assert (true == hb_any (st, [] (unsigned _) { return _ == 15u; }));
173 assert (true == hb_any (st, 15u));
174 assert (false == hb_none (st));
175 assert (false == hb_none (st, 15u));
176 assert (true == hb_none (st, 17u));
177
178 hb_array_t<hb_vector_t<int>> pa;
179 pa->as_array ();
180
181 hb_map_t m;
182
183 hb_iter (st);
184 hb_iter (&st);
185
186 + hb_iter (src)
187 | hb_map (m)
188 | hb_map (&m)
189 | hb_filter ()
190 | hb_filter (st)
191 | hb_filter (&st)
192 | hb_filter (hb_bool)
193 | hb_filter (hb_bool, hb_identity)
194 | hb_sink (st)
195 ;
196
197 + hb_iter (src)
198 | hb_sink (hb_array (dst))
199 ;
200
201 + hb_iter (src)
202 | hb_apply (&st)
203 ;
204
205 + hb_iter (src)
206 | hb_map ([] (int i) { return 1; })
207 | hb_reduce ([=] (int acc, int value) { return acc; }, 2)
208 ;
209
210 using map_pair_t = hb_item_type<hb_map_t>;
211 + hb_iter (m)
212 | hb_map ([] (map_pair_t p) { return p.first * p.second; })
213 ;
214
215 m.keys ();
216 using map_key_t = decltype (*m.keys());
217 + hb_iter (m.keys ())
218 | hb_filter ([] (map_key_t k) { return k < 42; })
219 | hb_drain
220 ;
221
222 m.values ();
223 using map_value_t = decltype (*m.values());
224 + hb_iter (m.values ())
225 | hb_filter ([] (map_value_t k) { return k < 42; })
226 | hb_drain
227 ;
228
229 unsigned int temp1 = 10;
230 unsigned int temp2 = 0;
231 hb_map_t *result =
232 + hb_iter (src)
233 | hb_map ([&] (int i) -> hb_set_t *
234 {
235 hb_set_t *set = hb_set_create ();
236 for (unsigned int i = 0; i < temp1; ++i)
237 hb_set_add (set, i);
238 temp1++;
239 return set;
240 })
241 | hb_reduce ([&] (hb_map_t *acc, hb_set_t *value) -> hb_map_t *
242 {
243 hb_map_set (acc, temp2++, hb_set_get_population (value));
244 /* This is not a memory managed language, take care! */
245 hb_set_destroy (value);
246 return acc;
247 }, hb_map_create ())
248 ;
249 /* The result should be something like 0->10, 1->11, ..., 9->19 */
250 assert (hb_map_get (result, 9) == 19);
251
252 unsigned int temp3 = 0;
253 + hb_iter(src)
254 | hb_map([&] (int i) { return ++temp3; })
255 | hb_reduce([&] (float acc, int value) { return acc + value; }, 0)
256 ;
257 hb_map_destroy (result);
258
259 + hb_iter (src)
260 | hb_drain
261 ;
262
263 t << 1;
264 long vl;
265 s >> vl;
266
267 hb_iota ();
268 hb_iota (3);
269 hb_iota (3, 2);
270 assert ((&vl) + 1 == *++hb_iota (&vl, hb_inc));
271 hb_range ();
272 hb_repeat (7u);
273 hb_repeat (nullptr);
274 hb_repeat (vl) | hb_chop (3);
275 assert (hb_len (hb_range (10) | hb_take (3)) == 3);
276 assert (hb_range (9).len () == 9);
277 assert (hb_range (2, 9).len () == 7);
278 assert (hb_range (2, 9, 3).len () == 3);
279 assert (hb_range (2, 8, 3).len () == 2);
280 assert (hb_range (2, 7, 3).len () == 2);
281 assert (hb_range (-2, -9, -3).len () == 3);
282 assert (hb_range (-2, -8, -3).len () == 2);
283 assert (hb_range (-2, -7, -3).len () == 2);
284
285 return 0;
286 }
287