Behdad Esfahbod | f6d5f1e | 2018-12-21 00:23:34 -0500 | [diff] [blame] | 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 | |
Behdad Esfahbod | 865deeb | 2018-12-21 17:35:58 -0500 | [diff] [blame] | 27 | #include "hb.hh" |
Behdad Esfahbod | f6d5f1e | 2018-12-21 00:23:34 -0500 | [diff] [blame] | 28 | #include "hb-iter.hh" |
| 29 | |
Behdad Esfahbod | 8001e00 | 2018-12-21 01:53:27 -0500 | [diff] [blame] | 30 | #include "hb-array.hh" |
Behdad Esfahbod | e4355b1 | 2018-12-22 16:11:22 -0500 | [diff] [blame] | 31 | #include "hb-set.hh" |
Behdad Esfahbod | 2cbf5bf | 2018-12-27 16:55:18 -0500 | [diff] [blame] | 32 | #include "hb-ot-layout-common.hh" |
Behdad Esfahbod | e4355b1 | 2018-12-22 16:11:22 -0500 | [diff] [blame] | 33 | |
Behdad Esfahbod | 8001e00 | 2018-12-21 01:53:27 -0500 | [diff] [blame] | 34 | template <typename T> |
Behdad Esfahbod | 849a0f1 | 2019-01-29 17:10:19 -0800 | [diff] [blame] | 35 | struct array_iter_t : hb_iter_with_fallback_t<array_iter_t<T>, T&> |
Behdad Esfahbod | 8001e00 | 2018-12-21 01:53:27 -0500 | [diff] [blame] | 36 | { |
| 37 | array_iter_t (hb_array_t<T> arr_) : arr (arr_) {} |
| 38 | |
Behdad Esfahbod | 636786e | 2019-01-08 23:48:35 -0800 | [diff] [blame] | 39 | typedef T& __item_t__; |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 40 | static constexpr bool is_random_access_iterator = true; |
Behdad Esfahbod | 8001e00 | 2018-12-21 01:53:27 -0500 | [diff] [blame] | 41 | T& __item_at__ (unsigned i) const { return arr[i]; } |
Behdad Esfahbod | 8001e00 | 2018-12-21 01:53:27 -0500 | [diff] [blame] | 42 | void __forward__ (unsigned n) { arr += n; } |
| 43 | void __rewind__ (unsigned n) { arr -= n; } |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 44 | unsigned __len__ () const { return arr.length; } |
Behdad Esfahbod | 4c2fd05 | 2019-05-06 19:57:15 -0700 | [diff] [blame] | 45 | bool operator != (const array_iter_t& o) { return arr != o.arr; } |
Behdad Esfahbod | 8001e00 | 2018-12-21 01:53:27 -0500 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | hb_array_t<T> arr; |
| 49 | }; |
| 50 | |
Behdad Esfahbod | 35503d7 | 2018-12-21 03:03:46 -0500 | [diff] [blame] | 51 | template <typename T> |
| 52 | struct 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 Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 59 | operator hb_iter_t<array_iter_t<T>> () { return iter (); } |
Behdad Esfahbod | 35503d7 | 2018-12-21 03:03:46 -0500 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | hb_array_t<T> arr; |
| 63 | }; |
| 64 | |
Behdad Esfahbod | d3976b7 | 2018-12-26 18:54:27 -0500 | [diff] [blame] | 65 | |
Behdad Esfahbod | 7987095 | 2019-01-09 01:02:38 -0800 | [diff] [blame] | 66 | template <typename Iter, |
Behdad Esfahbod | af571db | 2019-05-07 21:39:20 -0700 | [diff] [blame] | 67 | hb_requires (hb_is_iterator (Iter))> |
Behdad Esfahbod | 7987095 | 2019-01-09 01:02:38 -0800 | [diff] [blame] | 68 | static void |
Behdad Esfahbod | 4c2fd05 | 2019-05-06 19:57:15 -0700 | [diff] [blame] | 69 | test_iterator_non_default_constructable (Iter it) |
Behdad Esfahbod | d3976b7 | 2018-12-26 18:54:27 -0500 | [diff] [blame] | 70 | { |
| 71 | /* Iterate over a copy of it. */ |
| 72 | for (auto c = it.iter (); c; c++) |
| 73 | *c; |
| 74 | |
Behdad Esfahbod | b62e7f9 | 2019-01-27 00:51:43 +0100 | [diff] [blame] | 75 | /* Same. */ |
| 76 | for (auto c = +it; c; c++) |
| 77 | *c; |
| 78 | |
Behdad Esfahbod | 4c2fd05 | 2019-05-06 19:57:15 -0700 | [diff] [blame] | 79 | /* Range-based for over a copy. */ |
| 80 | for (auto _ : +it) |
| 81 | (void) _; |
| 82 | |
Behdad Esfahbod | 076faf7 | 2018-12-30 01:40:08 -0500 | [diff] [blame] | 83 | it += it.len (); |
Behdad Esfahbod | 606841b | 2019-05-11 11:54:30 -0700 | [diff] [blame] | 84 | it = it + 10; |
| 85 | it = 10 + it; |
Behdad Esfahbod | d3976b7 | 2018-12-26 18:54:27 -0500 | [diff] [blame] | 86 | |
| 87 | assert (*it == it[0]); |
Behdad Esfahbod | fb053b6 | 2018-12-26 19:01:30 -0500 | [diff] [blame] | 88 | |
Behdad Esfahbod | fbab07f | 2019-01-27 00:44:45 +0100 | [diff] [blame] | 89 | static_assert (true || it.is_random_access_iterator, ""); |
| 90 | static_assert (true || it.is_sorted_iterator, ""); |
Behdad Esfahbod | d3976b7 | 2018-12-26 18:54:27 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Behdad Esfahbod | 4c2fd05 | 2019-05-06 19:57:15 -0700 | [diff] [blame] | 93 | template <typename Iter, |
Behdad Esfahbod | af571db | 2019-05-07 21:39:20 -0700 | [diff] [blame] | 94 | hb_requires (hb_is_iterator (Iter))> |
Behdad Esfahbod | 4c2fd05 | 2019-05-06 19:57:15 -0700 | [diff] [blame] | 95 | static void |
| 96 | test_iterator (Iter it) |
| 97 | { |
| 98 | Iter default_constructed; |
| 99 | assert (!default_constructed); |
| 100 | |
| 101 | test_iterator_non_default_constructable (it); |
| 102 | } |
| 103 | |
Behdad Esfahbod | 7987095 | 2019-01-09 01:02:38 -0800 | [diff] [blame] | 104 | template <typename Iterable, |
Behdad Esfahbod | af571db | 2019-05-07 21:39:20 -0700 | [diff] [blame] | 105 | hb_requires (hb_is_iterable (Iterable))> |
Behdad Esfahbod | 7987095 | 2019-01-09 01:02:38 -0800 | [diff] [blame] | 106 | static void |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 107 | test_iterable (const Iterable &lst = Null (Iterable)) |
Behdad Esfahbod | d3976b7 | 2018-12-26 18:54:27 -0500 | [diff] [blame] | 108 | { |
Behdad Esfahbod | 4c2fd05 | 2019-05-06 19:57:15 -0700 | [diff] [blame] | 109 | for (auto _ : lst) |
| 110 | (void) _; |
| 111 | |
Behdad Esfahbod | d3976b7 | 2018-12-26 18:54:27 -0500 | [diff] [blame] | 112 | // Test that can take iterator from. |
| 113 | test_iterator (lst.iter ()); |
| 114 | } |
| 115 | |
Garret Rieger | ca22741 | 2021-11-30 12:53:15 -0800 | [diff] [blame] | 116 | template <typename It> |
| 117 | static void check_sequential (It it) |
| 118 | { |
| 119 | int i = 1; |
| 120 | for (int v : +it) { |
| 121 | assert (v == i++); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static 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 Rieger | 2e93551 | 2021-11-30 16:02:05 -0800 | [diff] [blame] | 134 | assert (it1.len () == 5); |
Garret Rieger | 071aea4 | 2021-11-30 16:16:06 -0800 | [diff] [blame] | 135 | assert (it1.is_random_access_iterator); |
Garret Rieger | ca22741 | 2021-11-30 12:53:15 -0800 | [diff] [blame] | 136 | auto it2 = hb_concat (c, d); |
Garret Rieger | 2e93551 | 2021-11-30 16:02:05 -0800 | [diff] [blame] | 137 | assert (it2.len () == 5); |
Garret Rieger | ca22741 | 2021-11-30 12:53:15 -0800 | [diff] [blame] | 138 | auto it3 = hb_concat (d, c); |
Garret Rieger | 2e93551 | 2021-11-30 16:02:05 -0800 | [diff] [blame] | 139 | assert (it3.len () == 5); |
Garret Rieger | ca22741 | 2021-11-30 12:53:15 -0800 | [diff] [blame] | 140 | 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 Rieger | 2e93551 | 2021-11-30 16:02:05 -0800 | [diff] [blame] | 149 | |
| 150 | auto it4 = +it1; |
Garret Rieger | 071aea4 | 2021-11-30 16:16:06 -0800 | [diff] [blame] | 151 | it4 += 0; |
| 152 | assert (*it4 == 1); |
| 153 | |
Garret Rieger | 2e93551 | 2021-11-30 16:02:05 -0800 | [diff] [blame] | 154 | 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 Rieger | 071aea4 | 2021-11-30 16:16:06 -0800 | [diff] [blame] | 171 | |
| 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 Rieger | ca22741 | 2021-11-30 12:53:15 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Behdad Esfahbod | f6d5f1e | 2018-12-21 00:23:34 -0500 | [diff] [blame] | 188 | int |
| 189 | main (int argc, char **argv) |
| 190 | { |
Behdad Esfahbod | 314d869 | 2018-12-21 00:54:55 -0500 | [diff] [blame] | 191 | const int src[10] = {}; |
| 192 | int dst[20]; |
Behdad Esfahbod | 44af738 | 2018-12-21 01:59:37 -0500 | [diff] [blame] | 193 | hb_vector_t<int> v; |
Behdad Esfahbod | 314d869 | 2018-12-21 00:54:55 -0500 | [diff] [blame] | 194 | |
Behdad Esfahbod | 44af738 | 2018-12-21 01:59:37 -0500 | [diff] [blame] | 195 | 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 Esfahbod | 8001e00 | 2018-12-21 01:53:27 -0500 | [diff] [blame] | 197 | array_iter_t<int> t (dst); |
Behdad Esfahbod | 314d869 | 2018-12-21 00:54:55 -0500 | [diff] [blame] | 198 | |
Behdad Esfahbod | ed972d5 | 2019-05-09 16:58:28 -0700 | [diff] [blame] | 199 | static_assert (array_iter_t<int>::is_random_access_iterator, ""); |
Behdad Esfahbod | 2658e40 | 2019-01-08 12:53:02 -0800 | [diff] [blame] | 200 | |
Behdad Esfahbod | 35503d7 | 2018-12-21 03:03:46 -0500 | [diff] [blame] | 201 | some_array_t<const int> a (src); |
| 202 | |
Behdad Esfahbod | 314d869 | 2018-12-21 00:54:55 -0500 | [diff] [blame] | 203 | s2 = s; |
| 204 | |
Behdad Esfahbod | 0363ce6 | 2019-01-27 01:03:56 +0100 | [diff] [blame] | 205 | hb_iter (src); |
| 206 | hb_iter (src, 2); |
| 207 | |
Behdad Esfahbod | 12e506f | 2018-12-21 02:47:04 -0500 | [diff] [blame] | 208 | hb_fill (t, 42); |
Behdad Esfahbod | 78fc43f | 2019-03-31 19:17:07 -0700 | [diff] [blame] | 209 | hb_copy (s, t); |
| 210 | hb_copy (a.iter (), t); |
Behdad Esfahbod | 314d869 | 2018-12-21 00:54:55 -0500 | [diff] [blame] | 211 | |
Behdad Esfahbod | d3976b7 | 2018-12-26 18:54:27 -0500 | [diff] [blame] | 212 | test_iterable (v); |
| 213 | hb_set_t st; |
Behdad Esfahbod | bdbfdc9 | 2019-05-07 22:52:43 -0700 | [diff] [blame] | 214 | st << 1 << 15 << 43; |
Behdad Esfahbod | d3976b7 | 2018-12-26 18:54:27 -0500 | [diff] [blame] | 215 | test_iterable (st); |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 216 | hb_sorted_array_t<int> sa; |
Behdad Esfahbod | 59a8fa5 | 2019-04-24 12:19:13 -0400 | [diff] [blame] | 217 | (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 Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 222 | test_iterable (sa); |
Behdad Esfahbod | d3976b7 | 2018-12-26 18:54:27 -0500 | [diff] [blame] | 223 | |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 224 | test_iterable<hb_array_t<int>> (); |
| 225 | test_iterable<hb_sorted_array_t<const int>> (); |
| 226 | test_iterable<hb_vector_t<float>> (); |
Behdad Esfahbod | 2cbf5bf | 2018-12-27 16:55:18 -0500 | [diff] [blame] | 227 | test_iterable<hb_set_t> (); |
Behdad Esfahbod | 8b379dd | 2022-07-14 16:20:04 -0600 | [diff] [blame] | 228 | test_iterable<OT::Array16Of<OT::HBUINT16>> (); |
Behdad Esfahbod | 2cbf5bf | 2018-12-27 16:55:18 -0500 | [diff] [blame] | 229 | |
Behdad Esfahbod | 84e5d00 | 2019-01-08 23:57:16 -0800 | [diff] [blame] | 230 | test_iterator (hb_zip (st, v)); |
Behdad Esfahbod | 4c2fd05 | 2019-05-06 19:57:15 -0700 | [diff] [blame] | 231 | test_iterator_non_default_constructable (hb_enumerate (st)); |
Behdad Esfahbod | 2c24ea3 | 2019-05-09 11:07:38 -0700 | [diff] [blame] | 232 | test_iterator_non_default_constructable (hb_enumerate (st, -5)); |
Behdad Esfahbod | cf61acb | 2019-05-07 22:45:01 -0700 | [diff] [blame] | 233 | test_iterator_non_default_constructable (hb_enumerate (hb_iter (st))); |
| 234 | test_iterator_non_default_constructable (hb_enumerate (hb_iter (st) + 1)); |
Behdad Esfahbod | 025eaa3 | 2019-05-07 00:05:37 -0700 | [diff] [blame] | 235 | test_iterator_non_default_constructable (hb_iter (st) | hb_filter ()); |
Behdad Esfahbod | c9b287a | 2019-05-09 12:43:57 -0700 | [diff] [blame] | 236 | test_iterator_non_default_constructable (hb_iter (st) | hb_map (hb_lidentity)); |
Behdad Esfahbod | 84e5d00 | 2019-01-08 23:57:16 -0800 | [diff] [blame] | 237 | |
Behdad Esfahbod | bdbfdc9 | 2019-05-07 22:52:43 -0700 | [diff] [blame] | 238 | assert (true == hb_all (st)); |
| 239 | assert (false == hb_all (st, 42u)); |
| 240 | assert (true == hb_any (st)); |
Behdad Esfahbod | fe14a40 | 2019-05-08 09:32:19 -0700 | [diff] [blame] | 241 | 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 Esfahbod | bdbfdc9 | 2019-05-07 22:52:43 -0700 | [diff] [blame] | 245 | assert (false == hb_none (st)); |
Behdad Esfahbod | fe14a40 | 2019-05-08 09:32:19 -0700 | [diff] [blame] | 246 | assert (false == hb_none (st, 15u)); |
| 247 | assert (true == hb_none (st, 17u)); |
Behdad Esfahbod | 77060bc | 2019-02-15 16:55:08 -0800 | [diff] [blame] | 248 | |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 249 | hb_array_t<hb_vector_t<int>> pa; |
Behdad Esfahbod | 47333c8 | 2019-01-07 21:38:49 -0500 | [diff] [blame] | 250 | pa->as_array (); |
| 251 | |
Behdad Esfahbod | c93eeba | 2019-05-08 10:56:09 -0700 | [diff] [blame] | 252 | hb_map_t m; |
| 253 | |
Behdad Esfahbod | cdb61eb | 2019-05-08 11:00:18 -0700 | [diff] [blame] | 254 | hb_iter (st); |
| 255 | hb_iter (&st); |
| 256 | |
Behdad Esfahbod | 0363ce6 | 2019-01-27 01:03:56 +0100 | [diff] [blame] | 257 | + hb_iter (src) |
Behdad Esfahbod | c93eeba | 2019-05-08 10:56:09 -0700 | [diff] [blame] | 258 | | hb_map (m) |
| 259 | | hb_map (&m) |
Behdad Esfahbod | fbab07f | 2019-01-27 00:44:45 +0100 | [diff] [blame] | 260 | | hb_filter () |
Behdad Esfahbod | c93eeba | 2019-05-08 10:56:09 -0700 | [diff] [blame] | 261 | | hb_filter (st) |
| 262 | | hb_filter (&st) |
Behdad Esfahbod | fbab07f | 2019-01-27 00:44:45 +0100 | [diff] [blame] | 263 | | hb_filter (hb_bool) |
| 264 | | hb_filter (hb_bool, hb_identity) |
Behdad Esfahbod | 5fa52e6 | 2019-02-14 10:51:02 -0800 | [diff] [blame] | 265 | | hb_sink (st) |
Behdad Esfahbod | fbab07f | 2019-01-27 00:44:45 +0100 | [diff] [blame] | 266 | ; |
| 267 | |
Behdad Esfahbod | 773d756 | 2019-02-14 11:40:22 -0800 | [diff] [blame] | 268 | + hb_iter (src) |
Behdad Esfahbod | 8479eb5 | 2019-05-08 09:48:55 -0700 | [diff] [blame] | 269 | | hb_sink (hb_array (dst)) |
| 270 | ; |
| 271 | |
| 272 | + hb_iter (src) |
Behdad Esfahbod | fa35d3f | 2019-02-14 14:04:05 -0800 | [diff] [blame] | 273 | | hb_apply (&st) |
| 274 | ; |
| 275 | |
| 276 | + hb_iter (src) |
Behdad Esfahbod | 78d35f0 | 2019-05-15 18:15:05 -0700 | [diff] [blame] | 277 | | hb_map ([] (int i) { return 1; }) |
| 278 | | hb_reduce ([=] (int acc, int value) { return acc; }, 2) |
Ebrahim Byagowi | e526414 | 2019-03-31 12:41:58 +0430 | [diff] [blame] | 279 | ; |
| 280 | |
rsheeter | b827181 | 2019-05-08 13:51:11 -0700 | [diff] [blame] | 281 | using map_pair_t = hb_item_type<hb_map_t>; |
rsheeter | ba60512 | 2019-05-08 12:09:10 -0700 | [diff] [blame] | 282 | + hb_iter (m) |
Behdad Esfahbod | f5705d7 | 2019-05-08 14:46:55 -0700 | [diff] [blame] | 283 | | hb_map ([] (map_pair_t p) { return p.first * p.second; }) |
| 284 | ; |
rsheeter | ba60512 | 2019-05-08 12:09:10 -0700 | [diff] [blame] | 285 | |
Behdad Esfahbod | f5705d7 | 2019-05-08 14:46:55 -0700 | [diff] [blame] | 286 | 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 Esfahbod | a304827 | 2019-05-08 15:08:10 -0700 | [diff] [blame] | 290 | | 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 Esfahbod | f5705d7 | 2019-05-08 14:46:55 -0700 | [diff] [blame] | 298 | ; |
rsheeter | 492af0f | 2019-05-08 12:47:18 -0700 | [diff] [blame] | 299 | |
Ebrahim Byagowi | b864208 | 2019-04-02 00:30:06 +0430 | [diff] [blame] | 300 | 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 Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 308 | hb_set_add (set, i); |
Ebrahim Byagowi | b864208 | 2019-04-02 00:30:06 +0430 | [diff] [blame] | 309 | 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 Byagowi | 062f5d6 | 2019-04-02 20:27:00 +0430 | [diff] [blame] | 315 | /* This is not a memory managed language, take care! */ |
Ebrahim Byagowi | b864208 | 2019-04-02 00:30:06 +0430 | [diff] [blame] | 316 | hb_set_destroy (value); |
| 317 | return acc; |
| 318 | }, hb_map_create ()) |
| 319 | ; |
Ebrahim Byagowi | 062f5d6 | 2019-04-02 20:27:00 +0430 | [diff] [blame] | 320 | /* The result should be something like 0->10, 1->11, ..., 9->19 */ |
Ebrahim Byagowi | b864208 | 2019-04-02 00:30:06 +0430 | [diff] [blame] | 321 | assert (hb_map_get (result, 9) == 19); |
Behdad Esfahbod | 9027d15 | 2022-11-16 12:39:30 -0700 | [diff] [blame] | 322 | hb_map_destroy (result); |
Behdad Esfahbod | 25dd88e | 2019-04-22 17:45:23 -0400 | [diff] [blame] | 323 | |
Behdad Esfahbod | 0a97d27 | 2022-11-15 16:23:48 -0700 | [diff] [blame] | 324 | /* 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 Esfahbod | 9027d15 | 2022-11-16 12:39:30 -0700 | [diff] [blame] | 345 | hb_map_destroy (result); |
Behdad Esfahbod | 0a97d27 | 2022-11-15 16:23:48 -0700 | [diff] [blame] | 346 | |
Ebrahim Byagowi | afdbf96 | 2019-04-02 20:25:06 +0430 | [diff] [blame] | 347 | unsigned int temp3 = 0; |
| 348 | + hb_iter(src) |
Behdad Esfahbod | d0df996 | 2019-05-15 00:32:41 -0700 | [diff] [blame] | 349 | | hb_map([&] (int i) { return ++temp3; }) |
| 350 | | hb_reduce([&] (float acc, int value) { return acc + value; }, 0) |
Ebrahim Byagowi | afdbf96 | 2019-04-02 20:25:06 +0430 | [diff] [blame] | 351 | ; |
Ebrahim Byagowi | b864208 | 2019-04-02 00:30:06 +0430 | [diff] [blame] | 352 | |
Ebrahim Byagowi | e526414 | 2019-03-31 12:41:58 +0430 | [diff] [blame] | 353 | + hb_iter (src) |
Behdad Esfahbod | fa35d3f | 2019-02-14 14:04:05 -0800 | [diff] [blame] | 354 | | hb_drain |
| 355 | ; |
Behdad Esfahbod | 773d756 | 2019-02-14 11:40:22 -0800 | [diff] [blame] | 356 | |
Behdad Esfahbod | 57795bc | 2019-01-28 16:23:12 -0500 | [diff] [blame] | 357 | t << 1; |
| 358 | long vl; |
| 359 | s >> vl; |
| 360 | |
Behdad Esfahbod | 50dc3e7 | 2019-05-08 10:35:02 -0700 | [diff] [blame] | 361 | hb_iota (); |
Behdad Esfahbod | 7675d0d | 2019-05-09 11:02:56 -0700 | [diff] [blame] | 362 | hb_iota (3); |
| 363 | hb_iota (3, 2); |
Behdad Esfahbod | 1f88dae | 2019-08-31 12:24:42 -0500 | [diff] [blame] | 364 | assert ((&vl) + 1 == *++hb_iota (&vl, hb_inc)); |
Behdad Esfahbod | 7675d0d | 2019-05-09 11:02:56 -0700 | [diff] [blame] | 365 | hb_range (); |
Behdad Esfahbod | b1378d8 | 2019-08-30 12:10:45 -0500 | [diff] [blame] | 366 | hb_repeat (7u); |
| 367 | hb_repeat (nullptr); |
Behdad Esfahbod | 67ec9fa | 2019-08-31 14:51:49 -0500 | [diff] [blame] | 368 | hb_repeat (vl) | hb_chop (3); |
Behdad Esfahbod | 398b296 | 2019-08-31 12:44:24 -0500 | [diff] [blame] | 369 | assert (hb_len (hb_range (10) | hb_take (3)) == 3); |
Behdad Esfahbod | 7675d0d | 2019-05-09 11:02:56 -0700 | [diff] [blame] | 370 | 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 Esfahbod | 50dc3e7 | 2019-05-08 10:35:02 -0700 | [diff] [blame] | 378 | |
Garret Rieger | ca22741 | 2021-11-30 12:53:15 -0800 | [diff] [blame] | 379 | test_concat (); |
| 380 | |
Behdad Esfahbod | f6d5f1e | 2018-12-21 00:23:34 -0500 | [diff] [blame] | 381 | return 0; |
| 382 | } |