blob: bb966d4c9c6856a6b7b24c6b646f8122e3d062d1 [file] [log] [blame]
Behdad Esfahbodf6d5f1e2018-12-21 00:23:34 -05001/*
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
Behdad Esfahbod865deeb2018-12-21 17:35:58 -050027#include "hb.hh"
Behdad Esfahbodf6d5f1e2018-12-21 00:23:34 -050028#include "hb-iter.hh"
29
Behdad Esfahbod8001e002018-12-21 01:53:27 -050030#include "hb-array.hh"
Behdad Esfahbode4355b12018-12-22 16:11:22 -050031#include "hb-set.hh"
Behdad Esfahbod2cbf5bf2018-12-27 16:55:18 -050032#include "hb-ot-layout-common.hh"
Behdad Esfahbode4355b12018-12-22 16:11:22 -050033
Behdad Esfahbod8001e002018-12-21 01:53:27 -050034template <typename T>
Behdad Esfahbod849a0f12019-01-29 17:10:19 -080035struct array_iter_t : hb_iter_with_fallback_t<array_iter_t<T>, T&>
Behdad Esfahbod8001e002018-12-21 01:53:27 -050036{
37 array_iter_t (hb_array_t<T> arr_) : arr (arr_) {}
38
Behdad Esfahbod636786e2019-01-08 23:48:35 -080039 typedef T& __item_t__;
Behdad Esfahbod090fe562019-01-25 15:34:03 +010040 static constexpr bool is_random_access_iterator = true;
Behdad Esfahbod8001e002018-12-21 01:53:27 -050041 T& __item_at__ (unsigned i) const { return arr[i]; }
Behdad Esfahbod8001e002018-12-21 01:53:27 -050042 void __forward__ (unsigned n) { arr += n; }
43 void __rewind__ (unsigned n) { arr -= n; }
Behdad Esfahbod474a1202018-12-21 18:46:51 -050044 unsigned __len__ () const { return arr.length; }
Behdad Esfahbod4c2fd052019-05-06 19:57:15 -070045 bool operator != (const array_iter_t& o) { return arr != o.arr; }
Behdad Esfahbod8001e002018-12-21 01:53:27 -050046
47 private:
48 hb_array_t<T> arr;
49};
50
Behdad Esfahbod35503d72018-12-21 03:03:46 -050051template <typename T>
52struct some_array_t
53{
54 some_array_t (hb_array_t<T> arr_) : arr (arr_) {}
55
56 typedef array_iter_t<T> iter_t;
57 array_iter_t<T> iter () { return array_iter_t<T> (arr); }
58 operator array_iter_t<T> () { return iter (); }
Ebrahim Byagowi92588782019-04-30 13:05:10 -070059 operator hb_iter_t<array_iter_t<T>> () { return iter (); }
Behdad Esfahbod35503d72018-12-21 03:03:46 -050060
61 private:
62 hb_array_t<T> arr;
63};
64
Behdad Esfahbodd3976b72018-12-26 18:54:27 -050065
Behdad Esfahbod79870952019-01-09 01:02:38 -080066template <typename Iter,
Behdad Esfahbodaf571db2019-05-07 21:39:20 -070067 hb_requires (hb_is_iterator (Iter))>
Behdad Esfahbod79870952019-01-09 01:02:38 -080068static void
Behdad Esfahbod4c2fd052019-05-06 19:57:15 -070069test_iterator_non_default_constructable (Iter it)
Behdad Esfahbodd3976b72018-12-26 18:54:27 -050070{
71 /* Iterate over a copy of it. */
72 for (auto c = it.iter (); c; c++)
73 *c;
74
Behdad Esfahbodb62e7f92019-01-27 00:51:43 +010075 /* Same. */
76 for (auto c = +it; c; c++)
77 *c;
78
Behdad Esfahbod4c2fd052019-05-06 19:57:15 -070079 /* Range-based for over a copy. */
80 for (auto _ : +it)
81 (void) _;
82
Behdad Esfahbod076faf72018-12-30 01:40:08 -050083 it += it.len ();
Behdad Esfahbod606841b2019-05-11 11:54:30 -070084 it = it + 10;
85 it = 10 + it;
Behdad Esfahbodd3976b72018-12-26 18:54:27 -050086
87 assert (*it == it[0]);
Behdad Esfahbodfb053b62018-12-26 19:01:30 -050088
Behdad Esfahbodfbab07f2019-01-27 00:44:45 +010089 static_assert (true || it.is_random_access_iterator, "");
90 static_assert (true || it.is_sorted_iterator, "");
Behdad Esfahbodd3976b72018-12-26 18:54:27 -050091}
92
Behdad Esfahbod4c2fd052019-05-06 19:57:15 -070093template <typename Iter,
Behdad Esfahbodaf571db2019-05-07 21:39:20 -070094 hb_requires (hb_is_iterator (Iter))>
Behdad Esfahbod4c2fd052019-05-06 19:57:15 -070095static void
96test_iterator (Iter it)
97{
98 Iter default_constructed;
99 assert (!default_constructed);
100
101 test_iterator_non_default_constructable (it);
102}
103
Behdad Esfahbod79870952019-01-09 01:02:38 -0800104template <typename Iterable,
Behdad Esfahbodaf571db2019-05-07 21:39:20 -0700105 hb_requires (hb_is_iterable (Iterable))>
Behdad Esfahbod79870952019-01-09 01:02:38 -0800106static void
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430107test_iterable (const Iterable &lst = Null (Iterable))
Behdad Esfahbodd3976b72018-12-26 18:54:27 -0500108{
Behdad Esfahbod4c2fd052019-05-06 19:57:15 -0700109 for (auto _ : lst)
110 (void) _;
111
Behdad Esfahbodd3976b72018-12-26 18:54:27 -0500112 // Test that can take iterator from.
113 test_iterator (lst.iter ());
114}
115
Garret Riegerca227412021-11-30 12:53:15 -0800116template <typename It>
117static void check_sequential (It it)
118{
119 int i = 1;
120 for (int v : +it) {
121 assert (v == i++);
122 }
123}
124
125static void test_concat ()
126{
127 hb_vector_t<int> a = {1, 2, 3};
128 hb_vector_t<int> b = {4, 5};
129
130 hb_vector_t<int> c = {};
131 hb_vector_t<int> d = {1, 2, 3, 4, 5};
132
133 auto it1 = hb_concat (a, b);
Garret Rieger2e935512021-11-30 16:02:05 -0800134 assert (it1.len () == 5);
Garret Rieger071aea42021-11-30 16:16:06 -0800135 assert (it1.is_random_access_iterator);
Garret Riegerca227412021-11-30 12:53:15 -0800136 auto it2 = hb_concat (c, d);
Garret Rieger2e935512021-11-30 16:02:05 -0800137 assert (it2.len () == 5);
Garret Riegerca227412021-11-30 12:53:15 -0800138 auto it3 = hb_concat (d, c);
Garret Rieger2e935512021-11-30 16:02:05 -0800139 assert (it3.len () == 5);
Garret Riegerca227412021-11-30 12:53:15 -0800140 for (int i = 0; i < 5; i++) {
141 assert(it1[i] == i + 1);
142 assert(it2[i] == i + 1);
143 assert(it3[i] == i + 1);
144 }
145
146 check_sequential (it1);
147 check_sequential (it2);
148 check_sequential (it3);
Garret Rieger2e935512021-11-30 16:02:05 -0800149
150 auto it4 = +it1;
Garret Rieger071aea42021-11-30 16:16:06 -0800151 it4 += 0;
152 assert (*it4 == 1);
153
Garret Rieger2e935512021-11-30 16:02:05 -0800154 it4 += 2;
155 assert (*it4 == 3);
156 assert (it4);
157 assert (it4.len () == 3);
158
159 it4 += 2;
160 assert (*it4 == 5);
161 assert (it4);
162 assert (it4.len () == 1);
163
164 it4++;
165 assert (!it4);
166 assert (it4.len () == 0);
167
168 auto it5 = +it1;
169 it5 += 3;
170 assert (*it5 == 4);
Garret Rieger071aea42021-11-30 16:16:06 -0800171
172 hb_set_t s_a = {1, 2, 3};
173 hb_set_t s_b = {4, 5};
174 auto it6 = hb_concat (s_a, s_b);
175 assert (!it6.is_random_access_iterator);
176 check_sequential (it6);
177 assert (it6.len () == 5);
178
179 it6 += 0;
180 assert (*it6 == 1);
181
182 it6 += 3;
183 assert (*it6 == 4);
184 assert (it6);
185 assert (it6.len () == 2);
Garret Riegerca227412021-11-30 12:53:15 -0800186}
187
Behdad Esfahbodf6d5f1e2018-12-21 00:23:34 -0500188int
189main (int argc, char **argv)
190{
Behdad Esfahbod314d8692018-12-21 00:54:55 -0500191 const int src[10] = {};
192 int dst[20];
Behdad Esfahbod44af7382018-12-21 01:59:37 -0500193 hb_vector_t<int> v;
Behdad Esfahbod314d8692018-12-21 00:54:55 -0500194
Behdad Esfahbod44af7382018-12-21 01:59:37 -0500195 array_iter_t<const int> s (src); /* Implicit conversion from static array. */
196 array_iter_t<const int> s2 (v); /* Implicit conversion from vector. */
Behdad Esfahbod8001e002018-12-21 01:53:27 -0500197 array_iter_t<int> t (dst);
Behdad Esfahbod314d8692018-12-21 00:54:55 -0500198
Behdad Esfahboded972d52019-05-09 16:58:28 -0700199 static_assert (array_iter_t<int>::is_random_access_iterator, "");
Behdad Esfahbod2658e402019-01-08 12:53:02 -0800200
Behdad Esfahbod35503d72018-12-21 03:03:46 -0500201 some_array_t<const int> a (src);
202
Behdad Esfahbod314d8692018-12-21 00:54:55 -0500203 s2 = s;
204
Behdad Esfahbod0363ce62019-01-27 01:03:56 +0100205 hb_iter (src);
206 hb_iter (src, 2);
207
Behdad Esfahbod12e506f2018-12-21 02:47:04 -0500208 hb_fill (t, 42);
Behdad Esfahbod78fc43f2019-03-31 19:17:07 -0700209 hb_copy (s, t);
210 hb_copy (a.iter (), t);
Behdad Esfahbod314d8692018-12-21 00:54:55 -0500211
Behdad Esfahbodd3976b72018-12-26 18:54:27 -0500212 test_iterable (v);
213 hb_set_t st;
Behdad Esfahbodbdbfdc92019-05-07 22:52:43 -0700214 st << 1 << 15 << 43;
Behdad Esfahbodd3976b72018-12-26 18:54:27 -0500215 test_iterable (st);
Behdad Esfahbod570473a2018-12-27 13:29:51 -0500216 hb_sorted_array_t<int> sa;
Behdad Esfahbod59a8fa52019-04-24 12:19:13 -0400217 (void) static_cast<hb_iter_t<hb_sorted_array_t<int>, hb_sorted_array_t<int>::item_t>&> (sa);
218 (void) static_cast<hb_iter_t<hb_sorted_array_t<int>, hb_sorted_array_t<int>::__item_t__>&> (sa);
219 (void) static_cast<hb_iter_t<hb_sorted_array_t<int>, int&>&>(sa);
220 (void) static_cast<hb_iter_t<hb_sorted_array_t<int>>&>(sa);
221 (void) static_cast<hb_iter_t<hb_array_t<int>, int&>&> (sa);
Behdad Esfahbod570473a2018-12-27 13:29:51 -0500222 test_iterable (sa);
Behdad Esfahbodd3976b72018-12-26 18:54:27 -0500223
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700224 test_iterable<hb_array_t<int>> ();
225 test_iterable<hb_sorted_array_t<const int>> ();
226 test_iterable<hb_vector_t<float>> ();
Behdad Esfahbod2cbf5bf2018-12-27 16:55:18 -0500227 test_iterable<hb_set_t> ();
Behdad Esfahbod8b379dd2022-07-14 16:20:04 -0600228 test_iterable<OT::Array16Of<OT::HBUINT16>> ();
Behdad Esfahbod2cbf5bf2018-12-27 16:55:18 -0500229
Behdad Esfahbod84e5d002019-01-08 23:57:16 -0800230 test_iterator (hb_zip (st, v));
Behdad Esfahbod4c2fd052019-05-06 19:57:15 -0700231 test_iterator_non_default_constructable (hb_enumerate (st));
Behdad Esfahbod2c24ea32019-05-09 11:07:38 -0700232 test_iterator_non_default_constructable (hb_enumerate (st, -5));
Behdad Esfahbodcf61acb2019-05-07 22:45:01 -0700233 test_iterator_non_default_constructable (hb_enumerate (hb_iter (st)));
234 test_iterator_non_default_constructable (hb_enumerate (hb_iter (st) + 1));
Behdad Esfahbod025eaa32019-05-07 00:05:37 -0700235 test_iterator_non_default_constructable (hb_iter (st) | hb_filter ());
Behdad Esfahbodc9b287a2019-05-09 12:43:57 -0700236 test_iterator_non_default_constructable (hb_iter (st) | hb_map (hb_lidentity));
Behdad Esfahbod84e5d002019-01-08 23:57:16 -0800237
Behdad Esfahbodbdbfdc92019-05-07 22:52:43 -0700238 assert (true == hb_all (st));
239 assert (false == hb_all (st, 42u));
240 assert (true == hb_any (st));
Behdad Esfahbodfe14a402019-05-08 09:32:19 -0700241 assert (false == hb_any (st, 14u));
242 assert (true == hb_any (st, 14u, [] (unsigned _) { return _ - 1u; }));
243 assert (true == hb_any (st, [] (unsigned _) { return _ == 15u; }));
244 assert (true == hb_any (st, 15u));
Behdad Esfahbodbdbfdc92019-05-07 22:52:43 -0700245 assert (false == hb_none (st));
Behdad Esfahbodfe14a402019-05-08 09:32:19 -0700246 assert (false == hb_none (st, 15u));
247 assert (true == hb_none (st, 17u));
Behdad Esfahbod77060bc2019-02-15 16:55:08 -0800248
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700249 hb_array_t<hb_vector_t<int>> pa;
Behdad Esfahbod47333c82019-01-07 21:38:49 -0500250 pa->as_array ();
251
Behdad Esfahbodc93eeba2019-05-08 10:56:09 -0700252 hb_map_t m;
253
Behdad Esfahbodcdb61eb2019-05-08 11:00:18 -0700254 hb_iter (st);
255 hb_iter (&st);
256
Behdad Esfahbod0363ce62019-01-27 01:03:56 +0100257 + hb_iter (src)
Behdad Esfahbodc93eeba2019-05-08 10:56:09 -0700258 | hb_map (m)
259 | hb_map (&m)
Behdad Esfahbodfbab07f2019-01-27 00:44:45 +0100260 | hb_filter ()
Behdad Esfahbodc93eeba2019-05-08 10:56:09 -0700261 | hb_filter (st)
262 | hb_filter (&st)
Behdad Esfahbodfbab07f2019-01-27 00:44:45 +0100263 | hb_filter (hb_bool)
264 | hb_filter (hb_bool, hb_identity)
Behdad Esfahbod5fa52e62019-02-14 10:51:02 -0800265 | hb_sink (st)
Behdad Esfahbodfbab07f2019-01-27 00:44:45 +0100266 ;
267
Behdad Esfahbod773d7562019-02-14 11:40:22 -0800268 + hb_iter (src)
Behdad Esfahbod8479eb52019-05-08 09:48:55 -0700269 | hb_sink (hb_array (dst))
270 ;
271
272 + hb_iter (src)
Behdad Esfahbodfa35d3f2019-02-14 14:04:05 -0800273 | hb_apply (&st)
274 ;
275
276 + hb_iter (src)
Behdad Esfahbod78d35f02019-05-15 18:15:05 -0700277 | hb_map ([] (int i) { return 1; })
278 | hb_reduce ([=] (int acc, int value) { return acc; }, 2)
Ebrahim Byagowie5264142019-03-31 12:41:58 +0430279 ;
280
rsheeterb8271812019-05-08 13:51:11 -0700281 using map_pair_t = hb_item_type<hb_map_t>;
rsheeterba605122019-05-08 12:09:10 -0700282 + hb_iter (m)
Behdad Esfahbodf5705d72019-05-08 14:46:55 -0700283 | hb_map ([] (map_pair_t p) { return p.first * p.second; })
284 ;
rsheeterba605122019-05-08 12:09:10 -0700285
Behdad Esfahbodf5705d72019-05-08 14:46:55 -0700286 m.keys ();
287 using map_key_t = decltype (*m.keys());
288 + hb_iter (m.keys ())
289 | hb_filter ([] (map_key_t k) { return k < 42; })
Behdad Esfahboda3048272019-05-08 15:08:10 -0700290 | hb_drain
291 ;
292
293 m.values ();
294 using map_value_t = decltype (*m.values());
295 + hb_iter (m.values ())
296 | hb_filter ([] (map_value_t k) { return k < 42; })
297 | hb_drain
Behdad Esfahbodf5705d72019-05-08 14:46:55 -0700298 ;
rsheeter492af0f2019-05-08 12:47:18 -0700299
Ebrahim Byagowib8642082019-04-02 00:30:06 +0430300 unsigned int temp1 = 10;
301 unsigned int temp2 = 0;
302 hb_map_t *result =
303 + hb_iter (src)
304 | hb_map ([&] (int i) -> hb_set_t *
305 {
306 hb_set_t *set = hb_set_create ();
307 for (unsigned int i = 0; i < temp1; ++i)
Ebrahim Byagowi05584132019-10-01 13:49:55 +0330308 hb_set_add (set, i);
Ebrahim Byagowib8642082019-04-02 00:30:06 +0430309 temp1++;
310 return set;
311 })
312 | hb_reduce ([&] (hb_map_t *acc, hb_set_t *value) -> hb_map_t *
313 {
314 hb_map_set (acc, temp2++, hb_set_get_population (value));
Ebrahim Byagowi062f5d62019-04-02 20:27:00 +0430315 /* This is not a memory managed language, take care! */
Ebrahim Byagowib8642082019-04-02 00:30:06 +0430316 hb_set_destroy (value);
317 return acc;
318 }, hb_map_create ())
319 ;
Ebrahim Byagowi062f5d62019-04-02 20:27:00 +0430320 /* The result should be something like 0->10, 1->11, ..., 9->19 */
Ebrahim Byagowib8642082019-04-02 00:30:06 +0430321 assert (hb_map_get (result, 9) == 19);
Behdad Esfahbod9027d152022-11-16 12:39:30 -0700322 hb_map_destroy (result);
Behdad Esfahbod25dd88e2019-04-22 17:45:23 -0400323
Behdad Esfahbod0a97d272022-11-15 16:23:48 -0700324 /* Like above, but passing hb_set_t instead of hb_set_t * */
325 temp1 = 10;
326 temp2 = 0;
327 result =
328 + hb_iter (src)
329 | hb_map ([&] (int i) -> hb_set_t
330 {
331 hb_set_t set;
332 for (unsigned int i = 0; i < temp1; ++i)
333 hb_set_add (&set, i);
334 temp1++;
335 return set;
336 })
337 | hb_reduce ([&] (hb_map_t *acc, hb_set_t value) -> hb_map_t *
338 {
339 hb_map_set (acc, temp2++, hb_set_get_population (&value));
340 return acc;
341 }, hb_map_create ())
342 ;
343 /* The result should be something like 0->10, 1->11, ..., 9->19 */
344 assert (hb_map_get (result, 9) == 19);
Behdad Esfahbod9027d152022-11-16 12:39:30 -0700345 hb_map_destroy (result);
Behdad Esfahbod0a97d272022-11-15 16:23:48 -0700346
Ebrahim Byagowiafdbf962019-04-02 20:25:06 +0430347 unsigned int temp3 = 0;
348 + hb_iter(src)
Behdad Esfahbodd0df9962019-05-15 00:32:41 -0700349 | hb_map([&] (int i) { return ++temp3; })
350 | hb_reduce([&] (float acc, int value) { return acc + value; }, 0)
Ebrahim Byagowiafdbf962019-04-02 20:25:06 +0430351 ;
Ebrahim Byagowib8642082019-04-02 00:30:06 +0430352
Ebrahim Byagowie5264142019-03-31 12:41:58 +0430353 + hb_iter (src)
Behdad Esfahbodfa35d3f2019-02-14 14:04:05 -0800354 | hb_drain
355 ;
Behdad Esfahbod773d7562019-02-14 11:40:22 -0800356
Behdad Esfahbod57795bc2019-01-28 16:23:12 -0500357 t << 1;
358 long vl;
359 s >> vl;
360
Behdad Esfahbod50dc3e72019-05-08 10:35:02 -0700361 hb_iota ();
Behdad Esfahbod7675d0d2019-05-09 11:02:56 -0700362 hb_iota (3);
363 hb_iota (3, 2);
Behdad Esfahbod1f88dae2019-08-31 12:24:42 -0500364 assert ((&vl) + 1 == *++hb_iota (&vl, hb_inc));
Behdad Esfahbod7675d0d2019-05-09 11:02:56 -0700365 hb_range ();
Behdad Esfahbodb1378d82019-08-30 12:10:45 -0500366 hb_repeat (7u);
367 hb_repeat (nullptr);
Behdad Esfahbod67ec9fa2019-08-31 14:51:49 -0500368 hb_repeat (vl) | hb_chop (3);
Behdad Esfahbod398b2962019-08-31 12:44:24 -0500369 assert (hb_len (hb_range (10) | hb_take (3)) == 3);
Behdad Esfahbod7675d0d2019-05-09 11:02:56 -0700370 assert (hb_range (9).len () == 9);
371 assert (hb_range (2, 9).len () == 7);
372 assert (hb_range (2, 9, 3).len () == 3);
373 assert (hb_range (2, 8, 3).len () == 2);
374 assert (hb_range (2, 7, 3).len () == 2);
375 assert (hb_range (-2, -9, -3).len () == 3);
376 assert (hb_range (-2, -8, -3).len () == 2);
377 assert (hb_range (-2, -7, -3).len () == 2);
Behdad Esfahbod50dc3e72019-05-08 10:35:02 -0700378
Garret Riegerca227412021-11-30 12:53:15 -0800379 test_concat ();
380
Behdad Esfahbodf6d5f1e2018-12-21 00:23:34 -0500381 return 0;
382}