Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -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 | |
| 27 | #ifndef HB_ITER_HH |
| 28 | #define HB_ITER_HH |
| 29 | |
| 30 | #include "hb.hh" |
Behdad Esfahbod | e76a3ca | 2018-12-27 17:23:12 -0500 | [diff] [blame] | 31 | #include "hb-meta.hh" |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 32 | |
| 33 | |
| 34 | /* Unified iterator object. |
| 35 | * |
| 36 | * The goal of this template is to make the same iterator interface |
| 37 | * available to all types, and make it very easy and compact to use. |
Behdad Esfahbod | 314d869 | 2018-12-21 00:54:55 -0500 | [diff] [blame] | 38 | * hb_iter_tator objects are small, light-weight, objects that can be |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 39 | * copied by value. If the collection / object being iterated on |
Behdad Esfahbod | 314d869 | 2018-12-21 00:54:55 -0500 | [diff] [blame] | 40 | * is writable, then the iterator returns lvalues, otherwise it |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 41 | * returns rvalues. |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 42 | */ |
| 43 | |
Behdad Esfahbod | 8c6cbbd | 2018-12-28 14:29:09 -0500 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * Base classes for iterators. |
| 47 | */ |
| 48 | |
Behdad Esfahbod | 314d869 | 2018-12-21 00:54:55 -0500 | [diff] [blame] | 49 | /* Base class for all iterators. */ |
Behdad Esfahbod | 6b6783e | 2019-01-26 22:44:09 +0100 | [diff] [blame] | 50 | template <typename iter_t, typename Item = typename iter_t::__item_t__> |
Behdad Esfahbod | cb5011d | 2019-01-04 11:22:32 -0500 | [diff] [blame] | 51 | struct hb_iter_t |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 52 | { |
Behdad Esfahbod | aeb696a | 2018-12-21 01:57:02 -0500 | [diff] [blame] | 53 | typedef Item item_t; |
Behdad Esfahbod | 70a52d6 | 2019-01-22 12:15:23 +0100 | [diff] [blame] | 54 | static constexpr unsigned item_size = hb_static_size (Item); |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 55 | static constexpr bool is_iterator = true; |
| 56 | static constexpr bool is_random_access_iterator = false; |
| 57 | static constexpr bool is_sorted_iterator = false; |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 58 | |
Behdad Esfahbod | 2fc1860 | 2018-12-21 18:09:45 -0500 | [diff] [blame] | 59 | private: |
| 60 | /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */ |
Behdad Esfahbod | aeb696a | 2018-12-21 01:57:02 -0500 | [diff] [blame] | 61 | const iter_t* thiz () const { return static_cast<const iter_t *> (this); } |
| 62 | iter_t* thiz () { return static_cast< iter_t *> (this); } |
Behdad Esfahbod | 2fc1860 | 2018-12-21 18:09:45 -0500 | [diff] [blame] | 63 | public: |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 64 | |
Behdad Esfahbod | 6c548b6 | 2019-01-08 13:43:49 -0800 | [diff] [blame] | 65 | /* TODO: |
| 66 | * Port operators below to use hb_enable_if to sniff which method implements |
Behdad Esfahbod | 84a25d7 | 2019-01-29 13:39:19 -0800 | [diff] [blame] | 67 | * an operator and use it, and remove hb_iter_fallback_mixin_t completely. */ |
Behdad Esfahbod | 6c548b6 | 2019-01-08 13:43:49 -0800 | [diff] [blame] | 68 | |
Behdad Esfahbod | 314d869 | 2018-12-21 00:54:55 -0500 | [diff] [blame] | 69 | /* Operators. */ |
Behdad Esfahbod | 7788ac1 | 2018-12-26 20:06:10 -0500 | [diff] [blame] | 70 | iter_t iter () const { return *thiz(); } |
Behdad Esfahbod | 2f5b1a9 | 2019-01-27 00:49:37 +0100 | [diff] [blame] | 71 | iter_t operator + () const { return *thiz(); } |
Behdad Esfahbod | 7788ac1 | 2018-12-26 20:06:10 -0500 | [diff] [blame] | 72 | explicit_operator bool () const { return thiz()->__more__ (); } |
| 73 | unsigned len () const { return thiz()->__len__ (); } |
Behdad Esfahbod | 47333c8 | 2019-01-07 21:38:49 -0500 | [diff] [blame] | 74 | hb_remove_reference (item_t)* operator -> () const { return hb_addressof (**thiz()); } |
Behdad Esfahbod | 5ec11ce | 2018-12-27 17:17:28 -0500 | [diff] [blame] | 75 | item_t operator * () const { return thiz()->__item__ (); } |
| 76 | item_t operator [] (unsigned i) const { return thiz()->__item_at__ (i); } |
Behdad Esfahbod | 7788ac1 | 2018-12-26 20:06:10 -0500 | [diff] [blame] | 77 | iter_t& operator += (unsigned count) { thiz()->__forward__ (count); return *thiz(); } |
| 78 | iter_t& operator ++ () { thiz()->__next__ (); return *thiz(); } |
| 79 | iter_t& operator -= (unsigned count) { thiz()->__rewind__ (count); return *thiz(); } |
| 80 | iter_t& operator -- () { thiz()->__prev__ (); return *thiz(); } |
Behdad Esfahbod | 8bd96be | 2019-01-28 16:17:36 -0500 | [diff] [blame] | 81 | iter_t operator + (unsigned count) const { auto c = thiz()->iter (); c += count; return c; } |
Behdad Esfahbod | 859a880 | 2018-12-30 02:11:03 -0500 | [diff] [blame] | 82 | friend iter_t operator + (unsigned count, const iter_t &it) { return it + count; } |
Behdad Esfahbod | aeb696a | 2018-12-21 01:57:02 -0500 | [diff] [blame] | 83 | iter_t operator ++ (int) { iter_t c (*thiz()); ++*thiz(); return c; } |
Behdad Esfahbod | 8bd96be | 2019-01-28 16:17:36 -0500 | [diff] [blame] | 84 | iter_t operator - (unsigned count) const { auto c = thiz()->iter (); c -= count; return c; } |
Behdad Esfahbod | aeb696a | 2018-12-21 01:57:02 -0500 | [diff] [blame] | 85 | iter_t operator -- (int) { iter_t c (*thiz()); --*thiz(); return c; } |
Behdad Esfahbod | 57795bc | 2019-01-28 16:23:12 -0500 | [diff] [blame] | 86 | template <typename T> |
| 87 | iter_t& operator >> (T &v) { v = **thiz(); ++*thiz(); return *thiz(); } |
| 88 | template <typename T> |
| 89 | iter_t& operator << (const T v) { **thiz() = v; ++*thiz(); return *thiz(); } |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 90 | |
Behdad Esfahbod | 2fc1860 | 2018-12-21 18:09:45 -0500 | [diff] [blame] | 91 | protected: |
| 92 | hb_iter_t () {} |
Behdad Esfahbod | d25a2f1 | 2018-12-23 20:19:52 -0500 | [diff] [blame] | 93 | hb_iter_t (const hb_iter_t &o HB_UNUSED) {} |
| 94 | void operator = (const hb_iter_t &o HB_UNUSED) {} |
Behdad Esfahbod | 2fc1860 | 2018-12-21 18:09:45 -0500 | [diff] [blame] | 95 | }; |
| 96 | |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 97 | #define HB_ITER_USING(Name) \ |
Behdad Esfahbod | f78f837 | 2019-01-08 16:38:08 -0800 | [diff] [blame] | 98 | using item_t = typename Name::item_t; \ |
Behdad Esfahbod | df138da | 2018-12-28 16:29:48 -0500 | [diff] [blame] | 99 | using Name::item_size; \ |
Behdad Esfahbod | 6cd96ba | 2018-12-30 20:51:31 -0500 | [diff] [blame] | 100 | using Name::is_iterator; \ |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 101 | using Name::iter; \ |
| 102 | using Name::operator bool; \ |
| 103 | using Name::len; \ |
| 104 | using Name::operator ->; \ |
| 105 | using Name::operator *; \ |
| 106 | using Name::operator []; \ |
| 107 | using Name::operator +=; \ |
| 108 | using Name::operator ++; \ |
| 109 | using Name::operator -=; \ |
| 110 | using Name::operator --; \ |
Behdad Esfahbod | 859a880 | 2018-12-30 02:11:03 -0500 | [diff] [blame] | 111 | using Name::operator +; \ |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 112 | using Name::operator -; \ |
Behdad Esfahbod | 6521d5b | 2019-01-29 13:44:39 -0800 | [diff] [blame] | 113 | using Name::operator >>; \ |
| 114 | using Name::operator <<; \ |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 115 | static_assert (true, "") |
| 116 | |
Behdad Esfahbod | 778c96b | 2019-01-27 00:50:54 +0100 | [diff] [blame] | 117 | /* Returns iterator type of a type. */ |
| 118 | #define hb_iter_t(Iterable) decltype (hb_declval (Iterable).iter ()) |
| 119 | |
Behdad Esfahbod | 0363ce6 | 2019-01-27 01:03:56 +0100 | [diff] [blame] | 120 | |
| 121 | /* TODO Change to function-object. */ |
| 122 | |
Behdad Esfahbod | 778c96b | 2019-01-27 00:50:54 +0100 | [diff] [blame] | 123 | template <typename T> |
| 124 | inline hb_iter_t (T) |
| 125 | hb_iter (const T& c) { return c.iter (); } |
| 126 | |
Behdad Esfahbod | 0363ce6 | 2019-01-27 01:03:56 +0100 | [diff] [blame] | 127 | /* Specialization for C arrays. */ |
| 128 | template <typename> struct hb_array_t; |
| 129 | template <typename Type> inline hb_array_t<Type> |
| 130 | hb_iter (Type *array, unsigned int length) { return hb_array_t<Type> (array, length); } |
| 131 | template <typename Type, unsigned int length> hb_array_t<Type> |
| 132 | hb_iter (Type (&array)[length]) { return hb_iter (array, length); } |
| 133 | |
| 134 | |
Behdad Esfahbod | 2fc1860 | 2018-12-21 18:09:45 -0500 | [diff] [blame] | 135 | /* Mixin to fill in what the subclass doesn't provide. */ |
Behdad Esfahbod | 636786e | 2019-01-08 23:48:35 -0800 | [diff] [blame] | 136 | template <typename iter_t, typename item_t = typename iter_t::__item_t__> |
Behdad Esfahbod | 84a25d7 | 2019-01-29 13:39:19 -0800 | [diff] [blame] | 137 | struct hb_iter_fallback_mixin_t |
Behdad Esfahbod | 2fc1860 | 2018-12-21 18:09:45 -0500 | [diff] [blame] | 138 | { |
| 139 | private: |
| 140 | /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */ |
| 141 | const iter_t* thiz () const { return static_cast<const iter_t *> (this); } |
| 142 | iter_t* thiz () { return static_cast< iter_t *> (this); } |
| 143 | public: |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 144 | |
Behdad Esfahbod | 8001e00 | 2018-12-21 01:53:27 -0500 | [diff] [blame] | 145 | /* Access: Implement __item__(), or __item_at__() if random-access. */ |
Behdad Esfahbod | 5ec11ce | 2018-12-27 17:17:28 -0500 | [diff] [blame] | 146 | item_t __item__ () const { return (*thiz())[0]; } |
| 147 | item_t __item_at__ (unsigned i) const { return *(*thiz() + i); } |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 148 | |
Behdad Esfahbod | 2a33ab0 | 2018-12-21 18:49:27 -0500 | [diff] [blame] | 149 | /* Termination: Implement __more__(), or __len__() if random-access. */ |
Behdad Esfahbod | 7788ac1 | 2018-12-26 20:06:10 -0500 | [diff] [blame] | 150 | bool __more__ () const { return thiz()->len (); } |
Behdad Esfahbod | 2a33ab0 | 2018-12-21 18:49:27 -0500 | [diff] [blame] | 151 | unsigned __len__ () const |
| 152 | { iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; } |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 153 | |
Behdad Esfahbod | 314d869 | 2018-12-21 00:54:55 -0500 | [diff] [blame] | 154 | /* Advancing: Implement __next__(), or __forward__() if random-access. */ |
Behdad Esfahbod | 7788ac1 | 2018-12-26 20:06:10 -0500 | [diff] [blame] | 155 | void __next__ () { *thiz() += 1; } |
| 156 | void __forward__ (unsigned n) { while (n--) ++*thiz(); } |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 157 | |
Behdad Esfahbod | 19d2b50 | 2018-12-21 01:17:35 -0500 | [diff] [blame] | 158 | /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */ |
Behdad Esfahbod | 7788ac1 | 2018-12-26 20:06:10 -0500 | [diff] [blame] | 159 | void __prev__ () { *thiz() -= 1; } |
| 160 | void __rewind__ (unsigned n) { while (n--) --*thiz(); } |
Behdad Esfahbod | 19d2b50 | 2018-12-21 01:17:35 -0500 | [diff] [blame] | 161 | |
Behdad Esfahbod | d3976b7 | 2018-12-26 18:54:27 -0500 | [diff] [blame] | 162 | protected: |
Behdad Esfahbod | 84a25d7 | 2019-01-29 13:39:19 -0800 | [diff] [blame] | 163 | hb_iter_fallback_mixin_t () {} |
| 164 | hb_iter_fallback_mixin_t (const hb_iter_fallback_mixin_t &o HB_UNUSED) {} |
| 165 | void operator = (const hb_iter_fallback_mixin_t &o HB_UNUSED) {} |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 166 | }; |
| 167 | |
Behdad Esfahbod | 849a0f1 | 2019-01-29 17:10:19 -0800 | [diff] [blame^] | 168 | template <typename iter_t, typename item_t = typename iter_t::__item_t__> |
| 169 | struct hb_iter_with_fallback_t : |
| 170 | hb_iter_t<iter_t, item_t>, |
| 171 | hb_iter_fallback_mixin_t<iter_t, item_t> |
| 172 | { |
| 173 | protected: |
| 174 | hb_iter_with_fallback_t () {} |
| 175 | hb_iter_with_fallback_t (const hb_iter_with_fallback_t &o HB_UNUSED) {} |
| 176 | void operator = (const hb_iter_with_fallback_t &o HB_UNUSED) {} |
| 177 | }; |
| 178 | |
Behdad Esfahbod | 8c6cbbd | 2018-12-28 14:29:09 -0500 | [diff] [blame] | 179 | /* |
| 180 | * Meta-programming predicates. |
| 181 | */ |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 182 | |
Behdad Esfahbod | df138da | 2018-12-28 16:29:48 -0500 | [diff] [blame] | 183 | /* hb_is_iterable() */ |
| 184 | |
| 185 | template<typename T, typename B> |
Behdad Esfahbod | 8c6cbbd | 2018-12-28 14:29:09 -0500 | [diff] [blame] | 186 | struct _hb_is_iterable |
| 187 | { enum { value = false }; }; |
Behdad Esfahbod | df138da | 2018-12-28 16:29:48 -0500 | [diff] [blame] | 188 | template<typename T> |
Behdad Esfahbod | 5093533 | 2019-01-26 22:47:35 +0100 | [diff] [blame] | 189 | struct _hb_is_iterable<T, hb_bool_tt<true || sizeof (hb_declval (T).iter ())> > |
Behdad Esfahbod | 8c6cbbd | 2018-12-28 14:29:09 -0500 | [diff] [blame] | 190 | { enum { value = true }; }; |
Behdad Esfahbod | 8570da1 | 2018-12-28 14:40:30 -0500 | [diff] [blame] | 191 | |
Behdad Esfahbod | df138da | 2018-12-28 16:29:48 -0500 | [diff] [blame] | 192 | template<typename T> |
Behdad Esfahbod | 8c6cbbd | 2018-12-28 14:29:09 -0500 | [diff] [blame] | 193 | struct hb_is_iterable { enum { value = _hb_is_iterable<T, hb_true_t>::value }; }; |
Behdad Esfahbod | 8c6cbbd | 2018-12-28 14:29:09 -0500 | [diff] [blame] | 194 | #define hb_is_iterable(Iterable) hb_is_iterable<Iterable>::value |
| 195 | |
Behdad Esfahbod | dc0a98c | 2019-01-08 12:57:01 -0800 | [diff] [blame] | 196 | /* TODO Add hb_is_iterable_of(). |
| 197 | * TODO Add random_access / sorted variants. */ |
| 198 | |
Behdad Esfahbod | 8c6cbbd | 2018-12-28 14:29:09 -0500 | [diff] [blame] | 199 | |
Behdad Esfahbod | 2658e40 | 2019-01-08 12:53:02 -0800 | [diff] [blame] | 200 | /* hb_is_iterator() / hb_is_random_access_iterator() / hb_is_sorted_iterator() */ |
Behdad Esfahbod | df138da | 2018-12-28 16:29:48 -0500 | [diff] [blame] | 201 | |
Behdad Esfahbod | ed43366 | 2019-01-07 17:24:23 -0500 | [diff] [blame] | 202 | template <typename Iter> |
Behdad Esfahbod | 445364d | 2019-01-08 12:42:15 -0800 | [diff] [blame] | 203 | struct _hb_is_iterator_of |
Behdad Esfahbod | ed43366 | 2019-01-07 17:24:23 -0500 | [diff] [blame] | 204 | { |
Behdad Esfahbod | 42bf80e | 2019-01-08 19:13:17 -0800 | [diff] [blame] | 205 | char operator () (...) { return 0; } |
Behdad Esfahbod | 6af9c5f | 2019-01-08 16:27:37 -0800 | [diff] [blame] | 206 | template<typename Item> int operator () (hb_iter_t<Iter, Item> *) { return 0; } |
| 207 | template<typename Item> int operator () (hb_iter_t<Iter, const Item> *) { return 0; } |
| 208 | template<typename Item> int operator () (hb_iter_t<Iter, Item&> *) { return 0; } |
| 209 | template<typename Item> int operator () (hb_iter_t<Iter, const Item&> *) { return 0; } |
Behdad Esfahbod | ed43366 | 2019-01-07 17:24:23 -0500 | [diff] [blame] | 210 | static_assert (sizeof (char) != sizeof (int), ""); |
| 211 | }; |
Behdad Esfahbod | 06a44e2 | 2018-12-30 18:42:14 -0500 | [diff] [blame] | 212 | template<typename Iter, typename Item> |
Behdad Esfahbod | 445364d | 2019-01-08 12:42:15 -0800 | [diff] [blame] | 213 | struct hb_is_iterator_of { enum { |
| 214 | value = sizeof (int) == sizeof (hb_declval (_hb_is_iterator_of<Iter>) (hb_declval (Iter*))) }; }; |
| 215 | #define hb_is_iterator_of(Iter, Item) hb_is_iterator_of<Iter, Item>::value |
| 216 | #define hb_is_iterator(Iter) hb_is_iterator_of (Iter, typename Iter::item_t) |
Behdad Esfahbod | df138da | 2018-12-28 16:29:48 -0500 | [diff] [blame] | 217 | |
Behdad Esfahbod | 2658e40 | 2019-01-08 12:53:02 -0800 | [diff] [blame] | 218 | #define hb_is_random_access_iterator_of(Iter, Item) \ |
| 219 | hb_is_iterator_of (Iter, Item) && Iter::is_random_access_iterator |
| 220 | #define hb_is_random_access_iterator(Iter) \ |
| 221 | hb_is_random_access_iterator_of (Iter, typename Iter::item_t) |
| 222 | |
Behdad Esfahbod | 445364d | 2019-01-08 12:42:15 -0800 | [diff] [blame] | 223 | #define hb_is_sorted_iterator_of(Iter, Item) \ |
Behdad Esfahbod | 2658e40 | 2019-01-08 12:53:02 -0800 | [diff] [blame] | 224 | hb_is_iterator_of (Iter, Item) && Iter::is_sorted_iterator |
Behdad Esfahbod | 445364d | 2019-01-08 12:42:15 -0800 | [diff] [blame] | 225 | #define hb_is_sorted_iterator(Iter) \ |
| 226 | hb_is_sorted_iterator_of (Iter, typename Iter::item_t) |
Behdad Esfahbod | df138da | 2018-12-28 16:29:48 -0500 | [diff] [blame] | 227 | |
Behdad Esfahbod | 014c502 | 2019-01-09 09:07:01 -0800 | [diff] [blame] | 228 | |
Behdad Esfahbod | 8c6cbbd | 2018-12-28 14:29:09 -0500 | [diff] [blame] | 229 | /* |
Behdad Esfahbod | 014c502 | 2019-01-09 09:07:01 -0800 | [diff] [blame] | 230 | * Adaptors, combiners, etc. |
Behdad Esfahbod | 8c6cbbd | 2018-12-28 14:29:09 -0500 | [diff] [blame] | 231 | */ |
Behdad Esfahbod | 7557e34 | 2018-12-21 17:21:19 -0500 | [diff] [blame] | 232 | |
Behdad Esfahbod | 343f5a4 | 2019-01-09 12:35:45 -0800 | [diff] [blame] | 233 | template <typename Lhs, typename Rhs, |
| 234 | hb_enable_if (hb_is_iterator (Lhs))> |
| 235 | static inline decltype (hb_declval (Rhs) (hb_declval (Lhs))) |
| 236 | operator | (Lhs lhs, const Rhs &rhs) { return rhs (lhs); } |
| 237 | |
Behdad Esfahbod | 1733e47 | 2019-01-09 11:15:49 -0800 | [diff] [blame] | 238 | /* hb_map(), hb_filter(), hb_reduce() */ |
| 239 | |
| 240 | template <typename Iter, typename Proj, |
| 241 | hb_enable_if (hb_is_iterator (Iter))> |
| 242 | struct hb_map_iter_t : |
Behdad Esfahbod | 343f5a4 | 2019-01-09 12:35:45 -0800 | [diff] [blame] | 243 | hb_iter_t<hb_map_iter_t<Iter, Proj>, decltype (hb_declval (Proj) (hb_declval (typename Iter::item_t)))> |
Behdad Esfahbod | 1733e47 | 2019-01-09 11:15:49 -0800 | [diff] [blame] | 244 | { |
Behdad Esfahbod | 471e96e | 2019-01-09 12:42:01 -0800 | [diff] [blame] | 245 | hb_map_iter_t (const Iter& it, Proj&& f) : it (it), f (f) {} |
Behdad Esfahbod | 1733e47 | 2019-01-09 11:15:49 -0800 | [diff] [blame] | 246 | |
Behdad Esfahbod | 343f5a4 | 2019-01-09 12:35:45 -0800 | [diff] [blame] | 247 | typedef decltype (hb_declval (Proj) (hb_declval (typename Iter::item_t))) __item_t__; |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 248 | static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator; |
Behdad Esfahbod | 1733e47 | 2019-01-09 11:15:49 -0800 | [diff] [blame] | 249 | __item_t__ __item__ () const { return f (*it); } |
| 250 | __item_t__ __item_at__ (unsigned i) const { return f (it[i]); } |
Behdad Esfahbod | 343f5a4 | 2019-01-09 12:35:45 -0800 | [diff] [blame] | 251 | bool __more__ () const { return bool (it); } |
Behdad Esfahbod | 1733e47 | 2019-01-09 11:15:49 -0800 | [diff] [blame] | 252 | unsigned __len__ () const { return it.len (); } |
| 253 | void __next__ () { ++it; } |
| 254 | void __forward__ (unsigned n) { it += n; } |
| 255 | void __prev__ () { --it; } |
| 256 | void __rewind__ (unsigned n) { it -= n; } |
| 257 | |
| 258 | private: |
| 259 | Iter it; |
| 260 | Proj f; |
| 261 | }; |
Behdad Esfahbod | 6b6783e | 2019-01-26 22:44:09 +0100 | [diff] [blame] | 262 | |
Behdad Esfahbod | 1733e47 | 2019-01-09 11:15:49 -0800 | [diff] [blame] | 263 | template <typename Proj> |
| 264 | struct hb_map_iter_factory_t |
| 265 | { |
Behdad Esfahbod | 471e96e | 2019-01-09 12:42:01 -0800 | [diff] [blame] | 266 | hb_map_iter_factory_t (Proj&& f) : f (f) {} |
Behdad Esfahbod | 1733e47 | 2019-01-09 11:15:49 -0800 | [diff] [blame] | 267 | |
| 268 | template <typename Iterable, |
| 269 | hb_enable_if (hb_is_iterable (Iterable))> |
Behdad Esfahbod | 6b6783e | 2019-01-26 22:44:09 +0100 | [diff] [blame] | 270 | hb_map_iter_t<hb_iter_t (Iterable), Proj> operator () (const Iterable &c) const |
| 271 | { return hb_map_iter_t<hb_iter_t (Iterable), Proj> (c.iter (), f); } |
Behdad Esfahbod | 1733e47 | 2019-01-09 11:15:49 -0800 | [diff] [blame] | 272 | |
| 273 | private: |
| 274 | Proj f; |
| 275 | }; |
| 276 | template <typename Proj> |
| 277 | inline hb_map_iter_factory_t<Proj> |
Behdad Esfahbod | 471e96e | 2019-01-09 12:42:01 -0800 | [diff] [blame] | 278 | hb_map (Proj&& f) |
Behdad Esfahbod | 1733e47 | 2019-01-09 11:15:49 -0800 | [diff] [blame] | 279 | { return hb_map_iter_factory_t<Proj> (f); } |
| 280 | |
Behdad Esfahbod | f35568d | 2019-01-09 11:32:33 -0800 | [diff] [blame] | 281 | template <typename Iter, typename Pred, typename Proj, |
| 282 | hb_enable_if (hb_is_iterator (Iter))> |
| 283 | struct hb_filter_iter_t : |
Behdad Esfahbod | 849a0f1 | 2019-01-29 17:10:19 -0800 | [diff] [blame^] | 284 | hb_iter_with_fallback_t<hb_filter_iter_t<Iter, Pred, Proj>, |
| 285 | typename Iter::item_t> |
Behdad Esfahbod | f35568d | 2019-01-09 11:32:33 -0800 | [diff] [blame] | 286 | { |
Behdad Esfahbod | 471e96e | 2019-01-09 12:42:01 -0800 | [diff] [blame] | 287 | hb_filter_iter_t (const Iter& it_, Pred&& p, Proj&& f) : it (it_), p (p), f (f) |
Behdad Esfahbod | f35568d | 2019-01-09 11:32:33 -0800 | [diff] [blame] | 288 | { while (it && !p (f (*it))) ++it; } |
| 289 | |
| 290 | typedef typename Iter::item_t __item_t__; |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 291 | static constexpr bool is_sorted_iterator = Iter::is_sorted_iterator; |
Behdad Esfahbod | f35568d | 2019-01-09 11:32:33 -0800 | [diff] [blame] | 292 | __item_t__ __item__ () const { return *it; } |
Behdad Esfahbod | 343f5a4 | 2019-01-09 12:35:45 -0800 | [diff] [blame] | 293 | bool __more__ () const { return bool (it); } |
Behdad Esfahbod | f35568d | 2019-01-09 11:32:33 -0800 | [diff] [blame] | 294 | void __next__ () { do ++it; while (it && !p (f (*it))); } |
| 295 | void __prev__ () { --it; } |
| 296 | |
| 297 | private: |
| 298 | Iter it; |
| 299 | Pred p; |
| 300 | Proj f; |
| 301 | }; |
| 302 | template <typename Pred, typename Proj> |
| 303 | struct hb_filter_iter_factory_t |
| 304 | { |
Behdad Esfahbod | 471e96e | 2019-01-09 12:42:01 -0800 | [diff] [blame] | 305 | hb_filter_iter_factory_t (Pred&& p, Proj&& f) : p (p), f (f) {} |
Behdad Esfahbod | f35568d | 2019-01-09 11:32:33 -0800 | [diff] [blame] | 306 | |
| 307 | template <typename Iterable, |
| 308 | hb_enable_if (hb_is_iterable (Iterable))> |
Behdad Esfahbod | 6b6783e | 2019-01-26 22:44:09 +0100 | [diff] [blame] | 309 | hb_filter_iter_t<hb_iter_t (Iterable), Pred, Proj> operator () (const Iterable &c) const |
| 310 | { return hb_filter_iter_t<hb_iter_t (Iterable), Pred, Proj> (c.iter (), p, f); } |
Behdad Esfahbod | f35568d | 2019-01-09 11:32:33 -0800 | [diff] [blame] | 311 | |
| 312 | private: |
| 313 | Pred p; |
| 314 | Proj f; |
| 315 | }; |
Behdad Esfahbod | 71157a4 | 2019-01-28 21:20:12 -0500 | [diff] [blame] | 316 | template <typename Pred = decltype ((hb_bool)), typename Proj = decltype ((hb_identity))> |
Behdad Esfahbod | f35568d | 2019-01-09 11:32:33 -0800 | [diff] [blame] | 317 | inline hb_filter_iter_factory_t<Pred, Proj> |
Behdad Esfahbod | fbab07f | 2019-01-27 00:44:45 +0100 | [diff] [blame] | 318 | hb_filter (Pred&& p = hb_bool, Proj&& f = hb_identity) |
Behdad Esfahbod | f35568d | 2019-01-09 11:32:33 -0800 | [diff] [blame] | 319 | { return hb_filter_iter_factory_t<Pred, Proj> (p, f); } |
Behdad Esfahbod | 7557e34 | 2018-12-21 17:21:19 -0500 | [diff] [blame] | 320 | |
Behdad Esfahbod | 014c502 | 2019-01-09 09:07:01 -0800 | [diff] [blame] | 321 | /* hb_zip() */ |
Behdad Esfahbod | 7557e34 | 2018-12-21 17:21:19 -0500 | [diff] [blame] | 322 | |
Behdad Esfahbod | 84e5d00 | 2019-01-08 23:57:16 -0800 | [diff] [blame] | 323 | template <typename A, typename B> |
Behdad Esfahbod | 200cdb7 | 2019-01-09 09:49:12 -0800 | [diff] [blame] | 324 | struct hb_zip_iter_t : |
| 325 | hb_iter_t<hb_zip_iter_t<A, B>, hb_pair_t<typename A::item_t, typename B::item_t> > |
Behdad Esfahbod | 84e5d00 | 2019-01-08 23:57:16 -0800 | [diff] [blame] | 326 | { |
Behdad Esfahbod | 200cdb7 | 2019-01-09 09:49:12 -0800 | [diff] [blame] | 327 | hb_zip_iter_t () {} |
| 328 | hb_zip_iter_t (A a, B b) : a (a), b (b) {} |
Behdad Esfahbod | 84e5d00 | 2019-01-08 23:57:16 -0800 | [diff] [blame] | 329 | |
| 330 | typedef hb_pair_t<typename A::item_t, typename B::item_t> __item_t__; |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 331 | static constexpr bool is_random_access_iterator = |
Behdad Esfahbod | 84e5d00 | 2019-01-08 23:57:16 -0800 | [diff] [blame] | 332 | A::is_random_access_iterator && |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 333 | B::is_random_access_iterator; |
| 334 | static constexpr bool is_sorted_iterator = |
Behdad Esfahbod | 84e5d00 | 2019-01-08 23:57:16 -0800 | [diff] [blame] | 335 | A::is_sorted_iterator && |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 336 | B::is_sorted_iterator; |
Behdad Esfahbod | 84e5d00 | 2019-01-08 23:57:16 -0800 | [diff] [blame] | 337 | __item_t__ __item__ () const { return __item_t__ (*a, *b); } |
| 338 | __item_t__ __item_at__ (unsigned i) const { return __item_t__ (a[i], b[i]); } |
| 339 | bool __more__ () const { return a && b; } |
| 340 | unsigned __len__ () const { return MIN (a.len (), b.len ()); } |
| 341 | void __next__ () { ++a; ++b; } |
| 342 | void __forward__ (unsigned n) { a += n; b += n; } |
| 343 | void __prev__ () { --a; --b; } |
| 344 | void __rewind__ (unsigned n) { a -= n; b -= n; } |
| 345 | |
| 346 | private: |
| 347 | A a; |
| 348 | B b; |
| 349 | }; |
Behdad Esfahbod | 7987095 | 2019-01-09 01:02:38 -0800 | [diff] [blame] | 350 | template <typename A, typename B, |
| 351 | hb_enable_if (hb_is_iterable (A) && hb_is_iterable (B))> |
Behdad Esfahbod | 6b6783e | 2019-01-26 22:44:09 +0100 | [diff] [blame] | 352 | inline hb_zip_iter_t<hb_iter_t (A), hb_iter_t (B)> |
Behdad Esfahbod | f7fcc47 | 2019-01-09 11:00:32 -0800 | [diff] [blame] | 353 | hb_zip (const A& a, const B &b) |
Behdad Esfahbod | 6b6783e | 2019-01-26 22:44:09 +0100 | [diff] [blame] | 354 | { return hb_zip_iter_t<hb_iter_t (A), hb_iter_t (B)> (a.iter (), b.iter ()); } |
Behdad Esfahbod | 84e5d00 | 2019-01-08 23:57:16 -0800 | [diff] [blame] | 355 | |
Behdad Esfahbod | 014c502 | 2019-01-09 09:07:01 -0800 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * Algorithms operating on iterators. |
| 359 | */ |
| 360 | |
| 361 | template <typename C, typename V, |
| 362 | hb_enable_if (hb_is_iterable (C))> |
| 363 | inline void |
| 364 | hb_fill (C& c, const V &v) |
| 365 | { |
Behdad Esfahbod | 6b6783e | 2019-01-26 22:44:09 +0100 | [diff] [blame] | 366 | for (auto i = c.iter (); i; i++) |
Behdad Esfahbod | 014c502 | 2019-01-09 09:07:01 -0800 | [diff] [blame] | 367 | hb_assign (*i, v); |
| 368 | } |
| 369 | |
| 370 | template <typename S, typename D, |
| 371 | hb_enable_if (hb_is_iterator (S) && hb_is_iterator (D))> |
| 372 | inline bool |
| 373 | hb_copy (D id, S is) |
| 374 | { |
| 375 | for (; id && is; ++id, ++is) |
| 376 | *id = *is; |
| 377 | return !is; |
| 378 | } |
| 379 | |
| 380 | |
Behdad Esfahbod | b80b97b | 2018-12-21 00:08:05 -0500 | [diff] [blame] | 381 | #endif /* HB_ITER_HH */ |