xref: /aosp_15_r20/external/harfbuzz_ng/src/hb-iter.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
1 /*
2  * Copyright © 2018  Google, Inc.
3  * Copyright © 2019  Facebook, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Google Author(s): Behdad Esfahbod
26  * Facebook Author(s): Behdad Esfahbod
27  */
28 
29 #ifndef HB_ITER_HH
30 #define HB_ITER_HH
31 
32 #include "hb.hh"
33 #include "hb-algs.hh"
34 #include "hb-meta.hh"
35 
36 
37 /* Unified iterator object.
38  *
39  * The goal of this template is to make the same iterator interface
40  * available to all types, and make it very easy and compact to use.
41  * hb_iter_tator objects are small, light-weight, objects that can be
42  * copied by value.  If the collection / object being iterated on
43  * is writable, then the iterator returns lvalues, otherwise it
44  * returns rvalues.
45  *
46  * If iterator implementation implements operator!=, then it can be
47  * used in range-based for loop.  That already happens if the iterator
48  * is random-access.  Otherwise, the range-based for loop incurs
49  * one traversal to find end(), which can be avoided if written
50  * as a while-style for loop, or if iterator implements a faster
51  * __end__() method. */
52 
53 /*
54  * Base classes for iterators.
55  */
56 
57 /* Base class for all iterators. */
58 template <typename iter_t, typename Item = typename iter_t::__item_t__>
59 struct hb_iter_t
60 {
61   typedef Item item_t;
get_item_sizehb_iter_t62   constexpr unsigned get_item_size () const { return hb_static_size (Item); }
63   static constexpr bool is_iterator = true;
64   static constexpr bool is_random_access_iterator = false;
65   static constexpr bool is_sorted_iterator = false;
66   static constexpr bool has_fast_len = false; // Should be checked in combination with is_random_access_iterator.
67 
68   private:
69   /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
thizhb_iter_t70   const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
thizhb_iter_t71 	iter_t* thiz ()       { return static_cast<      iter_t *> (this); }
72   public:
73 
74   /* Operators. */
iterhb_iter_t75   iter_t iter () const { return *thiz(); }
operator +hb_iter_t76   iter_t operator + () const { return *thiz(); }
_beginhb_iter_t77   iter_t _begin () const { return *thiz(); }
beginhb_iter_t78   iter_t begin () const { return _begin (); }
_endhb_iter_t79   iter_t _end () const { return thiz()->__end__ (); }
endhb_iter_t80   iter_t end () const { return _end (); }
operator boolhb_iter_t81   explicit operator bool () const { return thiz()->__more__ (); }
lenhb_iter_t82   unsigned len () const { return thiz()->__len__ (); }
83   /* The following can only be enabled if item_t is reference type.  Otherwise
84    * it will be returning pointer to temporary rvalue. */
85   template <typename T = item_t,
86 	    hb_enable_if (std::is_reference<T>::value)>
operator ->hb_iter_t87   hb_remove_reference<item_t>* operator -> () const { return std::addressof (**thiz()); }
operator *hb_iter_t88   item_t operator * () const { return thiz()->__item__ (); }
operator *hb_iter_t89   item_t operator * () { return thiz()->__item__ (); }
operator []hb_iter_t90   item_t operator [] (unsigned i) const { return thiz()->__item_at__ (i); }
operator []hb_iter_t91   item_t operator [] (unsigned i) { return thiz()->__item_at__ (i); }
operator +=hb_iter_t92   iter_t& operator += (unsigned count) &  { thiz()->__forward__ (count); return *thiz(); }
operator +=hb_iter_t93   iter_t  operator += (unsigned count) && { thiz()->__forward__ (count); return *thiz(); }
operator ++hb_iter_t94   iter_t& operator ++ () &  { thiz()->__next__ (); return *thiz(); }
operator ++hb_iter_t95   iter_t  operator ++ () && { thiz()->__next__ (); return *thiz(); }
operator -=hb_iter_t96   iter_t& operator -= (unsigned count) &  { thiz()->__rewind__ (count); return *thiz(); }
operator -=hb_iter_t97   iter_t  operator -= (unsigned count) && { thiz()->__rewind__ (count); return *thiz(); }
operator --hb_iter_t98   iter_t& operator -- () &  { thiz()->__prev__ (); return *thiz(); }
operator --hb_iter_t99   iter_t  operator -- () && { thiz()->__prev__ (); return *thiz(); }
operator +hb_iter_t100   iter_t operator + (unsigned count) const { auto c = thiz()->iter (); c += count; return c; }
operator +(unsigned count,const iter_t & it)101   friend iter_t operator + (unsigned count, const iter_t &it) { return it + count; }
operator ++hb_iter_t102   iter_t operator ++ (int) { iter_t c (*thiz()); ++*thiz(); return c; }
operator -hb_iter_t103   iter_t operator - (unsigned count) const { auto c = thiz()->iter (); c -= count; return c; }
operator --hb_iter_t104   iter_t operator -- (int) { iter_t c (*thiz()); --*thiz(); return c; }
105   template <typename T>
operator >>hb_iter_t106   iter_t& operator >> (T &v) &  { v = **thiz(); ++*thiz(); return *thiz(); }
107   template <typename T>
operator >>hb_iter_t108   iter_t  operator >> (T &v) && { v = **thiz(); ++*thiz(); return *thiz(); }
109   template <typename T>
operator <<hb_iter_t110   iter_t& operator << (const T v) &  { **thiz() = v; ++*thiz(); return *thiz(); }
111   template <typename T>
operator <<hb_iter_t112   iter_t  operator << (const T v) && { **thiz() = v; ++*thiz(); return *thiz(); }
113 
114   protected:
115   hb_iter_t () = default;
116   hb_iter_t (const hb_iter_t &o HB_UNUSED) = default;
117   hb_iter_t (hb_iter_t &&o HB_UNUSED) = default;
118   hb_iter_t& operator = (const hb_iter_t &o HB_UNUSED) = default;
119   hb_iter_t& operator = (hb_iter_t &&o HB_UNUSED) = default;
120 };
121 
122 #define HB_ITER_USING(Name) \
123   using item_t = typename Name::item_t; \
124   using Name::_begin; \
125   using Name::begin; \
126   using Name::_end; \
127   using Name::end; \
128   using Name::get_item_size; \
129   using Name::is_iterator; \
130   using Name::iter; \
131   using Name::operator bool; \
132   using Name::len; \
133   using Name::operator ->; \
134   using Name::operator *; \
135   using Name::operator []; \
136   using Name::operator +=; \
137   using Name::operator ++; \
138   using Name::operator -=; \
139   using Name::operator --; \
140   using Name::operator +; \
141   using Name::operator -; \
142   using Name::operator >>; \
143   using Name::operator <<; \
144   static_assert (true, "")
145 
146 /* Returns iterator / item type of a type. */
147 template <typename Iterable>
148 using hb_iter_type = decltype (hb_deref (hb_declval (Iterable)).iter ());
149 template <typename Iterable>
150 using hb_item_type = decltype (*hb_deref (hb_declval (Iterable)).iter ());
151 
152 
153 template <typename> struct hb_array_t;
154 template <typename> struct hb_sorted_array_t;
155 
156 struct
157 {
158   template <typename T> hb_iter_type<T>
operator ()__anon3e77a5160108159   operator () (T&& c) const
160   { return hb_deref (std::forward<T> (c)).iter (); }
161 
162   /* Specialization for C arrays. */
163 
164   template <typename Type> inline hb_array_t<Type>
operator ()__anon3e77a5160108165   operator () (Type *array, unsigned int length) const
166   { return hb_array_t<Type> (array, length); }
167 
168   template <typename Type, unsigned int length> hb_array_t<Type>
operator ()__anon3e77a5160108169   operator () (Type (&array)[length]) const
170   { return hb_array_t<Type> (array, length); }
171 
172 }
173 HB_FUNCOBJ (hb_iter);
174 struct
175 {
176   template <typename T> auto
177   impl (T&& c, hb_priority<1>) const HB_RETURN (unsigned, c.len ())
178 
179   template <typename T> auto
180   impl (T&& c, hb_priority<0>) const HB_RETURN (unsigned, c.len)
181 
182   public:
183 
184   template <typename T> auto
185   operator () (T&& c) const HB_RETURN (unsigned, impl (std::forward<T> (c), hb_prioritize))
186 }
187 HB_FUNCOBJ (hb_len);
188 
189 /* Mixin to fill in what the subclass doesn't provide. */
190 template <typename iter_t, typename item_t = typename iter_t::__item_t__>
191 struct hb_iter_fallback_mixin_t
192 {
193   private:
194   /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
thizhb_iter_fallback_mixin_t195   const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
thizhb_iter_fallback_mixin_t196 	iter_t* thiz ()       { return static_cast<      iter_t *> (this); }
197   public:
198 
199   /* Access: Implement __item__(), or __item_at__() if random-access. */
__item__hb_iter_fallback_mixin_t200   item_t __item__ () const { return (*thiz())[0]; }
__item_at__hb_iter_fallback_mixin_t201   item_t __item_at__ (unsigned i) const { return *(*thiz() + i); }
202 
203   /* Termination: Implement __more__(), or __len__() if random-access. */
__more__hb_iter_fallback_mixin_t204   bool __more__ () const { return bool (thiz()->len ()); }
__len__hb_iter_fallback_mixin_t205   unsigned __len__ () const
206   { iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; } return l; }
207 
208   /* Advancing: Implement __next__(), or __forward__() if random-access. */
__next__hb_iter_fallback_mixin_t209   void __next__ () { *thiz() += 1; }
__forward__hb_iter_fallback_mixin_t210   void __forward__ (unsigned n) { while (*thiz() && n--) ++*thiz(); }
211 
212   /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
__prev__hb_iter_fallback_mixin_t213   void __prev__ () { *thiz() -= 1; }
__rewind__hb_iter_fallback_mixin_t214   void __rewind__ (unsigned n) { while (*thiz() && n--) --*thiz(); }
215 
216   /* Range-based for: Implement __end__() if can be done faster,
217    * and operator!=. */
__end__hb_iter_fallback_mixin_t218   iter_t __end__ () const
219   {
220     if (thiz()->is_random_access_iterator)
221       return *thiz() + thiz()->len ();
222     /* Above expression loops twice. Following loops once. */
223     auto it = *thiz();
224     while (it) ++it;
225     return it;
226   }
227 
228   protected:
229   hb_iter_fallback_mixin_t () = default;
230   hb_iter_fallback_mixin_t (const hb_iter_fallback_mixin_t &o HB_UNUSED) = default;
231   hb_iter_fallback_mixin_t (hb_iter_fallback_mixin_t &&o HB_UNUSED) = default;
232   hb_iter_fallback_mixin_t& operator = (const hb_iter_fallback_mixin_t &o HB_UNUSED) = default;
233   hb_iter_fallback_mixin_t& operator = (hb_iter_fallback_mixin_t &&o HB_UNUSED) = default;
234 };
235 
236 template <typename iter_t, typename item_t = typename iter_t::__item_t__>
237 struct hb_iter_with_fallback_t :
238   hb_iter_t<iter_t, item_t>,
239   hb_iter_fallback_mixin_t<iter_t, item_t>
240 {
241   protected:
242   hb_iter_with_fallback_t () = default;
243   hb_iter_with_fallback_t (const hb_iter_with_fallback_t &o HB_UNUSED) = default;
244   hb_iter_with_fallback_t (hb_iter_with_fallback_t &&o HB_UNUSED) = default;
245   hb_iter_with_fallback_t& operator = (const hb_iter_with_fallback_t &o HB_UNUSED) = default;
246   hb_iter_with_fallback_t& operator = (hb_iter_with_fallback_t &&o HB_UNUSED) = default;
247 };
248 
249 /*
250  * Meta-programming predicates.
251  */
252 
253 /* hb_is_iterator() / hb_is_iterator_of() */
254 
255 template<typename Iter, typename Item>
256 struct hb_is_iterator_of
257 {
258   template <typename Item2 = Item>
259   static hb_true_type impl (hb_priority<2>, hb_iter_t<Iter, hb_type_identity<Item2>> *);
260   static hb_false_type impl (hb_priority<0>, const void *);
261 
262   public:
263   static constexpr bool value = decltype (impl (hb_prioritize, hb_declval (Iter*)))::value;
264 };
265 #define hb_is_iterator_of(Iter, Item) hb_is_iterator_of<Iter, Item>::value
266 #define hb_is_iterator(Iter) hb_is_iterator_of (Iter, typename Iter::item_t)
267 #define hb_is_sorted_iterator_of(Iter, Item) (hb_is_iterator_of<Iter, Item>::value && Iter::is_sorted_iterator)
268 #define hb_is_sorted_iterator(Iter) hb_is_sorted_iterator_of (Iter, typename Iter::item_t)
269 
270 /* hb_is_iterable() */
271 
272 template <typename T>
273 struct hb_is_iterable
274 {
275   private:
276 
277   template <typename U>
278   static auto impl (hb_priority<1>) -> decltype (hb_declval (U).iter (), hb_true_type ());
279 
280   template <typename>
281   static hb_false_type impl (hb_priority<0>);
282 
283   public:
284   static constexpr bool value = decltype (impl<T> (hb_prioritize))::value;
285 };
286 #define hb_is_iterable(Iterable) hb_is_iterable<Iterable>::value
287 
288 /* hb_is_source_of() / hb_is_sink_of() */
289 
290 template<typename Iter, typename Item>
291 struct hb_is_source_of
292 {
293   private:
294   template <typename Iter2 = Iter,
295 	    hb_enable_if (hb_is_convertible (typename Iter2::item_t, hb_add_lvalue_reference<const Item>))>
296   static hb_true_type impl (hb_priority<2>);
297   template <typename Iter2 = Iter>
298   static auto impl (hb_priority<1>) -> decltype (hb_declval (Iter2) >> hb_declval (Item &), hb_true_type ());
299   static hb_false_type impl (hb_priority<0>);
300 
301   public:
302   static constexpr bool value = decltype (impl (hb_prioritize))::value;
303 };
304 #define hb_is_source_of(Iter, Item) hb_is_source_of<Iter, Item>::value
305 
306 template<typename Iter, typename Item>
307 struct hb_is_sink_of
308 {
309   private:
310   template <typename Iter2 = Iter,
311 	    hb_enable_if (hb_is_convertible (typename Iter2::item_t, hb_add_lvalue_reference<Item>))>
312   static hb_true_type impl (hb_priority<2>);
313   template <typename Iter2 = Iter>
314   static auto impl (hb_priority<1>) -> decltype (hb_declval (Iter2) << hb_declval (Item), hb_true_type ());
315   static hb_false_type impl (hb_priority<0>);
316 
317   public:
318   static constexpr bool value = decltype (impl (hb_prioritize))::value;
319 };
320 #define hb_is_sink_of(Iter, Item) hb_is_sink_of<Iter, Item>::value
321 
322 /* This is commonly used, so define: */
323 #define hb_is_sorted_source_of(Iter, Item) \
324 	(hb_is_source_of(Iter, Item) && Iter::is_sorted_iterator)
325 
326 
327 struct
328 {
329   template <typename Iterable,
330 	    hb_requires (hb_is_iterable (Iterable))>
operator ()__anon3e77a5160308331   unsigned operator () (const Iterable &_) const { return hb_len (hb_iter (_)); }
332 
operator ()__anon3e77a5160308333   unsigned operator () (unsigned _) const { return _; }
334 }
335 HB_FUNCOBJ (hb_len_of);
336 
337 /* Range-based 'for' for iterables. */
338 
339 template <typename Iterable,
340 	  hb_requires (hb_is_iterable (Iterable))>
341 static inline auto begin (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).begin ())
342 
343 template <typename Iterable,
344 	  hb_requires (hb_is_iterable (Iterable))>
345 static inline auto end (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).end ())
346 
347 /* begin()/end() are NOT looked up non-ADL.  So each namespace must declare them.
348  * Do it for namespace OT. */
349 namespace OT {
350 
351 template <typename Iterable,
352 	  hb_requires (hb_is_iterable (Iterable))>
353 static inline auto begin (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).begin ())
354 
355 template <typename Iterable,
356 	  hb_requires (hb_is_iterable (Iterable))>
357 static inline auto end (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).end ())
358 
359 }
360 
361 
362 /*
363  * Adaptors, combiners, etc.
364  */
365 
366 template <typename Lhs, typename Rhs,
367 	  hb_requires (hb_is_iterator (Lhs))>
368 static inline auto
369 operator | (Lhs&& lhs, Rhs&& rhs) HB_AUTO_RETURN (std::forward<Rhs> (rhs) (std::forward<Lhs> (lhs)))
370 
371 /* hb_map(), hb_filter(), hb_reduce() */
372 
373 enum  class hb_function_sortedness_t {
374   NOT_SORTED,
375   RETAINS_SORTING,
376   SORTED,
377 };
378 
379 template <typename Iter, typename Proj, hb_function_sortedness_t Sorted,
380 	 hb_requires (hb_is_iterator (Iter))>
381 struct hb_map_iter_t :
382   hb_iter_t<hb_map_iter_t<Iter, Proj, Sorted>,
383 	    decltype (hb_get (hb_declval (Proj), *hb_declval (Iter)))>
384 {
hb_map_iter_thb_map_iter_t385   hb_map_iter_t (const Iter& it, Proj f_) : it (it), f (f_) {}
386 
387   typedef decltype (hb_get (hb_declval (Proj), *hb_declval (Iter))) __item_t__;
388   static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator;
389   static constexpr bool is_sorted_iterator =
390     Sorted == hb_function_sortedness_t::SORTED ? true :
391     Sorted == hb_function_sortedness_t::RETAINS_SORTING ? Iter::is_sorted_iterator :
392     false;
__item__hb_map_iter_t393   __item_t__ __item__ () const { return hb_get (f.get (), *it); }
__item_at__hb_map_iter_t394   __item_t__ __item_at__ (unsigned i) const { return hb_get (f.get (), it[i]); }
__more__hb_map_iter_t395   bool __more__ () const { return bool (it); }
__len__hb_map_iter_t396   unsigned __len__ () const { return it.len (); }
__next__hb_map_iter_t397   void __next__ () { ++it; }
__forward__hb_map_iter_t398   void __forward__ (unsigned n) { it += n; }
__prev__hb_map_iter_t399   void __prev__ () { --it; }
__rewind__hb_map_iter_t400   void __rewind__ (unsigned n) { it -= n; }
__end__hb_map_iter_t401   hb_map_iter_t __end__ () const { return hb_map_iter_t (it._end (), f); }
operator !=hb_map_iter_t402   bool operator != (const hb_map_iter_t& o) const
403   { return it != o.it; }
404 
405   private:
406   Iter it;
407   mutable hb_reference_wrapper<Proj> f;
408 };
409 
410 template <typename Proj, hb_function_sortedness_t Sorted>
411 struct hb_map_iter_factory_t
412 {
hb_map_iter_factory_thb_map_iter_factory_t413   hb_map_iter_factory_t (Proj f) : f (f) {}
414 
415   template <typename Iter,
416 	    hb_requires (hb_is_iterator (Iter))>
417   hb_map_iter_t<Iter, Proj, Sorted>
operator ()hb_map_iter_factory_t418   operator () (Iter it)
419   { return hb_map_iter_t<Iter, Proj, Sorted> (it, f); }
420 
421   private:
422   Proj f;
423 };
424 struct
425 {
426   template <typename Proj>
427   hb_map_iter_factory_t<Proj, hb_function_sortedness_t::NOT_SORTED>
operator ()__anon3e77a5160408428   operator () (Proj&& f) const
429   { return hb_map_iter_factory_t<Proj, hb_function_sortedness_t::NOT_SORTED> (f); }
430 }
431 HB_FUNCOBJ (hb_map);
432 struct
433 {
434   template <typename Proj>
435   hb_map_iter_factory_t<Proj, hb_function_sortedness_t::RETAINS_SORTING>
operator ()__anon3e77a5160508436   operator () (Proj&& f) const
437   { return hb_map_iter_factory_t<Proj, hb_function_sortedness_t::RETAINS_SORTING> (f); }
438 }
439 HB_FUNCOBJ (hb_map_retains_sorting);
440 struct
441 {
442   template <typename Proj>
443   hb_map_iter_factory_t<Proj, hb_function_sortedness_t::SORTED>
operator ()__anon3e77a5160608444   operator () (Proj&& f) const
445   { return hb_map_iter_factory_t<Proj, hb_function_sortedness_t::SORTED> (f); }
446 }
447 HB_FUNCOBJ (hb_map_sorted);
448 
449 template <typename Iter, typename Pred, typename Proj,
450 	 hb_requires (hb_is_iterator (Iter))>
451 struct hb_filter_iter_t :
452   hb_iter_with_fallback_t<hb_filter_iter_t<Iter, Pred, Proj>,
453 			  typename Iter::item_t>
454 {
hb_filter_iter_thb_filter_iter_t455   hb_filter_iter_t (const Iter& it_, Pred p_, Proj f_) : it (it_), p (p_), f (f_)
456   { while (it && !hb_has (p.get (), hb_get (f.get (), *it))) ++it; }
457 
458   typedef typename Iter::item_t __item_t__;
459   static constexpr bool is_sorted_iterator = Iter::is_sorted_iterator;
__item__hb_filter_iter_t460   __item_t__ __item__ () const { return *it; }
__more__hb_filter_iter_t461   bool __more__ () const { return bool (it); }
__next__hb_filter_iter_t462   void __next__ () { do ++it; while (it && !hb_has (p.get (), hb_get (f.get (), *it))); }
__prev__hb_filter_iter_t463   void __prev__ () { do --it; while (it && !hb_has (p.get (), hb_get (f.get (), *it))); }
__end__hb_filter_iter_t464   hb_filter_iter_t __end__ () const { return hb_filter_iter_t (it._end (), p, f); }
operator !=hb_filter_iter_t465   bool operator != (const hb_filter_iter_t& o) const
466   { return it != o.it; }
467 
468   private:
469   Iter it;
470   mutable hb_reference_wrapper<Pred> p;
471   mutable hb_reference_wrapper<Proj> f;
472 };
473 template <typename Pred, typename Proj>
474 struct hb_filter_iter_factory_t
475 {
hb_filter_iter_factory_thb_filter_iter_factory_t476   hb_filter_iter_factory_t (Pred p, Proj f) : p (p), f (f) {}
477 
478   template <typename Iter,
479 	    hb_requires (hb_is_iterator (Iter))>
480   hb_filter_iter_t<Iter, Pred, Proj>
operator ()hb_filter_iter_factory_t481   operator () (Iter it)
482   { return hb_filter_iter_t<Iter, Pred, Proj> (it, p, f); }
483 
484   private:
485   Pred p;
486   Proj f;
487 };
488 struct
489 {
490   template <typename Pred = decltype ((hb_identity)),
491 	    typename Proj = decltype ((hb_identity))>
492   hb_filter_iter_factory_t<Pred, Proj>
operator ()__anon3e77a5160708493   operator () (Pred&& p = hb_identity, Proj&& f = hb_identity) const
494   { return hb_filter_iter_factory_t<Pred, Proj> (p, f); }
495 }
496 HB_FUNCOBJ (hb_filter);
497 
498 template <typename Redu, typename InitT>
499 struct hb_reduce_t
500 {
hb_reduce_thb_reduce_t501   hb_reduce_t (Redu r, InitT init_value) : r (r), init_value (init_value) {}
502 
503   template <typename Iter,
504 	    hb_requires (hb_is_iterator (Iter)),
505 	    typename AccuT = hb_decay<decltype (hb_declval (Redu) (hb_declval (InitT), hb_declval (typename Iter::item_t)))>>
506   AccuT
operator ()hb_reduce_t507   operator () (Iter it)
508   {
509     AccuT value = init_value;
510     for (; it; ++it)
511       value = r (value, *it);
512     return value;
513   }
514 
515   private:
516   Redu r;
517   InitT init_value;
518 };
519 struct
520 {
521   template <typename Redu, typename InitT>
522   hb_reduce_t<Redu, InitT>
operator ()__anon3e77a5160808523   operator () (Redu&& r, InitT init_value) const
524   { return hb_reduce_t<Redu, InitT> (r, init_value); }
525 }
526 HB_FUNCOBJ (hb_reduce);
527 
528 
529 /* hb_zip() */
530 
531 template <typename A, typename B>
532 struct hb_zip_iter_t :
533   hb_iter_t<hb_zip_iter_t<A, B>,
534 	    hb_pair_t<typename A::item_t, typename B::item_t>>
535 {
hb_zip_iter_thb_zip_iter_t536   hb_zip_iter_t () {}
hb_zip_iter_thb_zip_iter_t537   hb_zip_iter_t (const A& a, const B& b) : a (a), b (b) {}
538 
539   typedef hb_pair_t<typename A::item_t, typename B::item_t> __item_t__;
540   static constexpr bool is_random_access_iterator =
541     A::is_random_access_iterator &&
542     B::is_random_access_iterator;
543   /* Note.  The following categorization is only valid if A is strictly sorted,
544    * ie. does NOT have duplicates.  Previously I tried to categorize sortedness
545    * more granularly, see commits:
546    *
547    *   513762849a683914fc266a17ddf38f133cccf072
548    *   4d3cf2adb669c345cc43832d11689271995e160a
549    *
550    * However, that was not enough, since hb_sorted_array_t, hb_sorted_vector_t,
551    * SortedArrayOf, etc all needed to be updated to add more variants.  At that
552    * point I saw it not worth the effort, and instead we now deem all sorted
553    * collections as essentially strictly-sorted for the purposes of zip.
554    *
555    * The above assumption is not as bad as it sounds.  Our "sorted" comes with
556    * no guarantees.  It's just a contract, put in place to help you remember,
557    * and think about, whether an iterator you receive is expected to be
558    * sorted or not.  As such, it's not perfect by definition, and should not
559    * be treated so.  The inaccuracy here just errs in the direction of being
560    * more permissive, so your code compiles instead of erring on the side of
561    * marking your zipped iterator unsorted in which case your code won't
562    * compile.
563    *
564    * This semantical limitation does NOT affect logic in any other place I
565    * know of as of this writing.
566    */
567   static constexpr bool is_sorted_iterator = A::is_sorted_iterator;
568 
__item__hb_zip_iter_t569   __item_t__ __item__ () const { return __item_t__ (*a, *b); }
__item_at__hb_zip_iter_t570   __item_t__ __item_at__ (unsigned i) const { return __item_t__ (a[i], b[i]); }
__more__hb_zip_iter_t571   bool __more__ () const { return bool (a) && bool (b); }
__len__hb_zip_iter_t572   unsigned __len__ () const { return hb_min (a.len (), b.len ()); }
__next__hb_zip_iter_t573   void __next__ () { ++a; ++b; }
__forward__hb_zip_iter_t574   void __forward__ (unsigned n) { a += n; b += n; }
__prev__hb_zip_iter_t575   void __prev__ () { --a; --b; }
__rewind__hb_zip_iter_t576   void __rewind__ (unsigned n) { a -= n; b -= n; }
__end__hb_zip_iter_t577   hb_zip_iter_t __end__ () const { return hb_zip_iter_t (a._end (), b._end ()); }
578   /* Note, we should stop if ANY of the iters reaches end.  As such two compare
579    * unequal if both items are unequal, NOT if either is unequal. */
operator !=hb_zip_iter_t580   bool operator != (const hb_zip_iter_t& o) const
581   { return a != o.a && b != o.b; }
582 
583   private:
584   A a;
585   B b;
586 };
587 struct
588 { HB_PARTIALIZE(2);
589   template <typename A, typename B,
590 	    hb_requires (hb_is_iterable (A) && hb_is_iterable (B))>
591   hb_zip_iter_t<hb_iter_type<A>, hb_iter_type<B>>
operator ()__anon3e77a5160908592   operator () (A&& a, B&& b) const
593   { return hb_zip_iter_t<hb_iter_type<A>, hb_iter_type<B>> (hb_iter (a), hb_iter (b)); }
594 }
595 HB_FUNCOBJ (hb_zip);
596 
597 /* hb_concat() */
598 
599 template <typename A, typename B>
600 struct hb_concat_iter_t :
601     hb_iter_t<hb_concat_iter_t<A, B>, typename A::item_t>
602 {
hb_concat_iter_thb_concat_iter_t603   hb_concat_iter_t () {}
hb_concat_iter_thb_concat_iter_t604   hb_concat_iter_t (A& a, B& b) : a (a), b (b) {}
hb_concat_iter_thb_concat_iter_t605   hb_concat_iter_t (const A& a, const B& b) : a (a), b (b) {}
606 
607 
608   typedef typename A::item_t __item_t__;
609   static constexpr bool is_random_access_iterator =
610     A::is_random_access_iterator &&
611     B::is_random_access_iterator;
612   static constexpr bool is_sorted_iterator = false;
613 
__item__hb_concat_iter_t614   __item_t__ __item__ () const
615   {
616     if (!a)
617       return *b;
618     return *a;
619   }
620 
__item_at__hb_concat_iter_t621   __item_t__ __item_at__ (unsigned i) const
622   {
623     unsigned a_len = a.len ();
624     if (i < a_len)
625       return a[i];
626     return b[i - a_len];
627   }
628 
__more__hb_concat_iter_t629   bool __more__ () const { return bool (a) || bool (b); }
630 
__len__hb_concat_iter_t631   unsigned __len__ () const { return a.len () + b.len (); }
632 
__next__hb_concat_iter_t633   void __next__ ()
634   {
635     if (a)
636       ++a;
637     else
638       ++b;
639   }
640 
__forward__hb_concat_iter_t641   void __forward__ (unsigned n)
642   {
643     if (!n) return;
644     if (!is_random_access_iterator) {
645       while (n-- && *this) {
646         (*this)++;
647       }
648       return;
649     }
650 
651     unsigned a_len = a.len ();
652     if (n > a_len) {
653       n -= a_len;
654       a.__forward__ (a_len);
655       b.__forward__ (n);
656     } else {
657       a.__forward__ (n);
658     }
659   }
660 
__end__hb_concat_iter_t661   hb_concat_iter_t __end__ () const { return hb_concat_iter_t (a._end (), b._end ()); }
operator !=hb_concat_iter_t662   bool operator != (const hb_concat_iter_t& o) const
663   {
664     return a != o.a
665         || b != o.b;
666   }
667 
668   private:
669   A a;
670   B b;
671 };
672 struct
673 { HB_PARTIALIZE(2);
674   template <typename A, typename B,
675 	    hb_requires (hb_is_iterable (A) && hb_is_iterable (B))>
676   hb_concat_iter_t<hb_iter_type<A>, hb_iter_type<B>>
operator ()__anon3e77a5160a08677   operator () (A&& a, B&& b) const
678   { return hb_concat_iter_t<hb_iter_type<A>, hb_iter_type<B>> (hb_iter (a), hb_iter (b)); }
679 }
680 HB_FUNCOBJ (hb_concat);
681 
682 /* hb_apply() */
683 
684 template <typename Appl>
685 struct hb_apply_t
686 {
hb_apply_thb_apply_t687   hb_apply_t (Appl a) : a (a) {}
688 
689   template <typename Iter,
690 	    hb_requires (hb_is_iterator (Iter))>
operator ()hb_apply_t691   void operator () (Iter it)
692   {
693     for (; it; ++it)
694       (void) hb_invoke (a, *it);
695   }
696 
697   private:
698   Appl a;
699 };
700 struct
701 {
702   template <typename Appl> hb_apply_t<Appl>
operator ()__anon3e77a5160b08703   operator () (Appl&& a) const
704   { return hb_apply_t<Appl> (a); }
705 
706   template <typename Appl> hb_apply_t<Appl&>
operator ()__anon3e77a5160b08707   operator () (Appl *a) const
708   { return hb_apply_t<Appl&> (*a); }
709 }
710 HB_FUNCOBJ (hb_apply);
711 
712 /* hb_range()/hb_iota()/hb_repeat() */
713 
714 template <typename T, typename S>
715 struct hb_range_iter_t :
716   hb_iter_t<hb_range_iter_t<T, S>, T>
717 {
hb_range_iter_thb_range_iter_t718   hb_range_iter_t (T start, T end_, S step) : v (start), end_ (end_for (start, end_, step)), step (step) {}
719 
720   typedef T __item_t__;
721   static constexpr bool is_random_access_iterator = true;
722   static constexpr bool is_sorted_iterator = true;
__item__hb_range_iter_t723   __item_t__ __item__ () const { return hb_ridentity (v); }
__item_at__hb_range_iter_t724   __item_t__ __item_at__ (unsigned j) const { return v + j * step; }
__more__hb_range_iter_t725   bool __more__ () const { return v != end_; }
__len__hb_range_iter_t726   unsigned __len__ () const { return !step ? UINT_MAX : (end_ - v) / step; }
__next__hb_range_iter_t727   void __next__ () { v += step; }
__forward__hb_range_iter_t728   void __forward__ (unsigned n) { v += n * step; }
__prev__hb_range_iter_t729   void __prev__ () { v -= step; }
__rewind__hb_range_iter_t730   void __rewind__ (unsigned n) { v -= n * step; }
__end__hb_range_iter_t731   hb_range_iter_t __end__ () const { return hb_range_iter_t (end_, end_, step); }
operator !=hb_range_iter_t732   bool operator != (const hb_range_iter_t& o) const
733   { return v != o.v; }
734 
735   private:
end_forhb_range_iter_t736   static inline T end_for (T start, T end_, S step)
737   {
738     if (!step)
739       return end_;
740     auto res = (end_ - start) % step;
741     if (!res)
742       return end_;
743     end_ += step - res;
744     return end_;
745   }
746 
747   private:
748   T v;
749   T end_;
750   S step;
751 };
752 struct
753 {
754   template <typename T = unsigned> hb_range_iter_t<T, unsigned>
operator ()__anon3e77a5160c08755   operator () (T end = (unsigned) -1) const
756   { return hb_range_iter_t<T, unsigned> (0, end, 1u); }
757 
758   template <typename T, typename S = unsigned> hb_range_iter_t<T, S>
operator ()__anon3e77a5160c08759   operator () (T start, T end, S step = 1u) const
760   { return hb_range_iter_t<T, S> (start, end, step); }
761 }
762 HB_FUNCOBJ (hb_range);
763 
764 template <typename T, typename S>
765 struct hb_iota_iter_t :
766   hb_iter_with_fallback_t<hb_iota_iter_t<T, S>, T>
767 {
hb_iota_iter_thb_iota_iter_t768   hb_iota_iter_t (T start, S step) : v (start), step (step) {}
769 
770   private:
771 
772   template <typename S2 = S>
773   auto
inchb_iota_iter_t774   inc (hb_type_identity<S2> s, hb_priority<1>)
775     -> hb_void_t<decltype (hb_invoke (std::forward<S2> (s), hb_declval<T&> ()))>
776   { v = hb_invoke (std::forward<S2> (s), v); }
777 
778   void
inchb_iota_iter_t779   inc (S s, hb_priority<0>)
780   { v += s; }
781 
782   public:
783 
784   typedef T __item_t__;
785   static constexpr bool is_random_access_iterator = true;
786   static constexpr bool is_sorted_iterator = true;
__item__hb_iota_iter_t787   __item_t__ __item__ () const { return hb_ridentity (v); }
__more__hb_iota_iter_t788   bool __more__ () const { return true; }
__len__hb_iota_iter_t789   unsigned __len__ () const { return UINT_MAX; }
__next__hb_iota_iter_t790   void __next__ () { inc (step, hb_prioritize); }
__prev__hb_iota_iter_t791   void __prev__ () { v -= step; }
__end__hb_iota_iter_t792   hb_iota_iter_t __end__ () const { return *this; }
operator !=hb_iota_iter_t793   bool operator != (const hb_iota_iter_t& o) const { return true; }
794 
795   private:
796   T v;
797   S step;
798 };
799 struct
800 {
801   template <typename T = unsigned, typename S = unsigned> hb_iota_iter_t<T, S>
operator ()__anon3e77a5160d08802   operator () (T start = 0u, S step = 1u) const
803   { return hb_iota_iter_t<T, S> (start, step); }
804 }
805 HB_FUNCOBJ (hb_iota);
806 
807 template <typename T>
808 struct hb_repeat_iter_t :
809   hb_iter_t<hb_repeat_iter_t<T>, T>
810 {
hb_repeat_iter_thb_repeat_iter_t811   hb_repeat_iter_t (T value) : v (value) {}
812 
813   typedef T __item_t__;
814   static constexpr bool is_random_access_iterator = true;
815   static constexpr bool is_sorted_iterator = true;
__item__hb_repeat_iter_t816   __item_t__ __item__ () const { return v; }
__item_at__hb_repeat_iter_t817   __item_t__ __item_at__ (unsigned j) const { return v; }
__more__hb_repeat_iter_t818   bool __more__ () const { return true; }
__len__hb_repeat_iter_t819   unsigned __len__ () const { return UINT_MAX; }
__next__hb_repeat_iter_t820   void __next__ () {}
__forward__hb_repeat_iter_t821   void __forward__ (unsigned) {}
__prev__hb_repeat_iter_t822   void __prev__ () {}
__rewind__hb_repeat_iter_t823   void __rewind__ (unsigned) {}
__end__hb_repeat_iter_t824   hb_repeat_iter_t __end__ () const { return *this; }
operator !=hb_repeat_iter_t825   bool operator != (const hb_repeat_iter_t& o) const { return true; }
826 
827   private:
828   T v;
829 };
830 struct
831 {
832   template <typename T> hb_repeat_iter_t<T>
operator ()__anon3e77a5160e08833   operator () (T value) const
834   { return hb_repeat_iter_t<T> (value); }
835 }
836 HB_FUNCOBJ (hb_repeat);
837 
838 /* hb_enumerate()/hb_take() */
839 
840 struct
841 {
842   template <typename Iterable,
843 	    typename Index = unsigned,
844 	    hb_requires (hb_is_iterable (Iterable))>
845   auto operator () (Iterable&& it, Index start = 0u) const HB_AUTO_RETURN
846   ( hb_zip (hb_iota (start), it) )
847 }
848 HB_FUNCOBJ (hb_enumerate);
849 
850 struct
851 { HB_PARTIALIZE(2);
852   template <typename Iterable,
853 	    hb_requires (hb_is_iterable (Iterable))>
operator ()__anon3e77a5161008854   auto operator () (Iterable&& it, unsigned count) const HB_AUTO_RETURN
855   ( hb_zip (hb_range (count), it) | hb_map_retains_sorting (hb_second) )
856 
857   /* Specialization arrays. */
858 
859   template <typename Type> inline hb_array_t<Type>
860   operator () (hb_array_t<Type> array, unsigned count) const
861   { return array.sub_array (0, count); }
862 
863   template <typename Type> inline hb_sorted_array_t<Type>
operator ()__anon3e77a5161008864   operator () (hb_sorted_array_t<Type> array, unsigned count) const
865   { return array.sub_array (0, count); }
866 }
867 HB_FUNCOBJ (hb_take);
868 
869 struct
870 { HB_PARTIALIZE(2);
871   template <typename Iter,
872 	    hb_requires (hb_is_iterator (Iter))>
873   auto operator () (Iter it, unsigned count) const HB_AUTO_RETURN
874   (
875     + hb_iota (it, hb_add (count))
876     | hb_map (hb_take (count))
877     | hb_take ((hb_len (it) + count - 1) / count)
878   )
879 }
880 HB_FUNCOBJ (hb_chop);
881 
882 /* hb_sink() */
883 
884 template <typename Sink>
885 struct hb_sink_t
886 {
hb_sink_thb_sink_t887   hb_sink_t (Sink s) : s (s) {}
888 
889   template <typename Iter,
890 	    hb_requires (hb_is_iterator (Iter))>
operator ()hb_sink_t891   void operator () (Iter it)
892   {
893     for (; it; ++it)
894       s << *it;
895   }
896 
897   private:
898   Sink s;
899 };
900 struct
901 {
902   template <typename Sink> hb_sink_t<Sink>
operator ()__anon3e77a5161208903   operator () (Sink&& s) const
904   { return hb_sink_t<Sink> (s); }
905 
906   template <typename Sink> hb_sink_t<Sink&>
operator ()__anon3e77a5161208907   operator () (Sink *s) const
908   { return hb_sink_t<Sink&> (*s); }
909 }
910 HB_FUNCOBJ (hb_sink);
911 
912 /* hb-drain: hb_sink to void / blackhole / /dev/null. */
913 
914 struct
915 {
916   template <typename Iter,
917 	    hb_requires (hb_is_iterator (Iter))>
operator ()__anon3e77a5161308918   void operator () (Iter it) const
919   {
920     for (; it; ++it)
921       (void) *it;
922   }
923 }
924 HB_FUNCOBJ (hb_drain);
925 
926 /* hb_unzip(): unzip and sink to two sinks. */
927 
928 template <typename Sink1, typename Sink2>
929 struct hb_unzip_t
930 {
hb_unzip_thb_unzip_t931   hb_unzip_t (Sink1 s1, Sink2 s2) : s1 (s1), s2 (s2) {}
932 
933   template <typename Iter,
934 	    hb_requires (hb_is_iterator (Iter))>
operator ()hb_unzip_t935   void operator () (Iter it)
936   {
937     for (; it; ++it)
938     {
939       const auto &v = *it;
940       s1 << v.first;
941       s2 << v.second;
942     }
943   }
944 
945   private:
946   Sink1 s1;
947   Sink2 s2;
948 };
949 struct
950 {
951   template <typename Sink1, typename Sink2> hb_unzip_t<Sink1, Sink2>
operator ()__anon3e77a5161408952   operator () (Sink1&& s1, Sink2&& s2) const
953   { return hb_unzip_t<Sink1, Sink2> (s1, s2); }
954 
955   template <typename Sink1, typename Sink2> hb_unzip_t<Sink1&, Sink2&>
operator ()__anon3e77a5161408956   operator () (Sink1 *s1, Sink2 *s2) const
957   { return hb_unzip_t<Sink1&, Sink2&> (*s1, *s2); }
958 }
959 HB_FUNCOBJ (hb_unzip);
960 
961 
962 /* hb-all, hb-any, hb-none. */
963 
964 struct
965 {
966   template <typename Iterable,
967 	    typename Pred = decltype ((hb_identity)),
968 	    typename Proj = decltype ((hb_identity)),
969 	    hb_requires (hb_is_iterable (Iterable))>
operator ()__anon3e77a5161508970   bool operator () (Iterable&& c,
971 		    Pred&& p = hb_identity,
972 		    Proj&& f = hb_identity) const
973   {
974     for (auto it = hb_iter (c); it; ++it)
975       if (!hb_match (std::forward<Pred> (p), hb_get (std::forward<Proj> (f), *it)))
976 	return false;
977     return true;
978   }
979 }
980 HB_FUNCOBJ (hb_all);
981 struct
982 {
983   template <typename Iterable,
984 	    typename Pred = decltype ((hb_identity)),
985 	    typename Proj = decltype ((hb_identity)),
986 	    hb_requires (hb_is_iterable (Iterable))>
operator ()__anon3e77a5161608987   bool operator () (Iterable&& c,
988 		    Pred&& p = hb_identity,
989 		    Proj&& f = hb_identity) const
990   {
991     for (auto it = hb_iter (c); it; ++it)
992       if (hb_match (std::forward<Pred> (p), hb_get (std::forward<Proj> (f), *it)))
993 	return true;
994     return false;
995   }
996 }
997 HB_FUNCOBJ (hb_any);
998 struct
999 {
1000   template <typename Iterable,
1001 	    typename Pred = decltype ((hb_identity)),
1002 	    typename Proj = decltype ((hb_identity)),
1003 	    hb_requires (hb_is_iterable (Iterable))>
operator ()__anon3e77a51617081004   bool operator () (Iterable&& c,
1005 		    Pred&& p = hb_identity,
1006 		    Proj&& f = hb_identity) const
1007   {
1008     for (auto it = hb_iter (c); it; ++it)
1009       if (hb_match (std::forward<Pred> (p), hb_get (std::forward<Proj> (f), *it)))
1010 	return false;
1011     return true;
1012   }
1013 }
1014 HB_FUNCOBJ (hb_none);
1015 
1016 /*
1017  * Algorithms operating on iterators.
1018  */
1019 
1020 template <typename C, typename V,
1021 	  hb_requires (hb_is_iterable (C))>
1022 inline void
hb_fill(C && c,const V & v)1023 hb_fill (C&& c, const V &v)
1024 {
1025   for (auto i = hb_iter (c); i; i++)
1026     *i = v;
1027 }
1028 
1029 template <typename S, typename D>
1030 inline void
hb_copy(S && is,D && id)1031 hb_copy (S&& is, D&& id)
1032 {
1033   hb_iter (is) | hb_sink (id);
1034 }
1035 
1036 
1037 #endif /* HB_ITER_HH */
1038