/* * Copyright © 2018 Google, Inc. * Copyright © 2019 Facebook, Inc. * * This is part of HarfBuzz, a text shaping library. * * Permission is hereby granted, without written agreement and without * license or royalty fees, to use, copy, modify, and distribute this * software and its documentation for any purpose, provided that the * above copyright notice and the following two paragraphs appear in * all copies of this software. * * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. * * Google Author(s): Behdad Esfahbod * Facebook Author(s): Behdad Esfahbod */ #ifndef HB_ITER_HH #define HB_ITER_HH #include "hb.hh" #include "hb-algs.hh" #include "hb-meta.hh" /* Unified iterator object. * * The goal of this template is to make the same iterator interface * available to all types, and make it very easy and compact to use. * hb_iter_tator objects are small, light-weight, objects that can be * copied by value. If the collection / object being iterated on * is writable, then the iterator returns lvalues, otherwise it * returns rvalues. * * TODO Document more. * * If iterator implementation implements operator!=, then can be * used in range-based for loop. That comes free if the iterator * is random-access. Otherwise, the range-based for loop incurs * one traversal to find end(), which can be avoided if written * as a while-style for loop, or if iterator implements a faster * __end__() method. * TODO When opting in for C++17, address this by changing return * type of .end()? */ /* * Base classes for iterators. */ /* Base class for all iterators. */ template struct hb_iter_t { typedef Item item_t; static constexpr unsigned item_size = hb_static_size (Item); static constexpr bool is_iterator = true; static constexpr bool is_random_access_iterator = false; static constexpr bool is_sorted_iterator = false; private: /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */ const iter_t* thiz () const { return static_cast (this); } iter_t* thiz () { return static_cast< iter_t *> (this); } public: /* TODO: * Port operators below to use hb_enable_if to sniff which method implements * an operator and use it, and remove hb_iter_fallback_mixin_t completely. */ /* Operators. */ iter_t iter () const { return *thiz(); } iter_t operator + () const { return *thiz(); } iter_t begin () const { return *thiz(); } iter_t end () const { return thiz()->__end__ (); } explicit operator bool () const { return thiz()->__more__ (); } unsigned len () const { return thiz()->__len__ (); } /* The following can only be enabled if item_t is reference type. Otherwise * it will be returning pointer to temporary rvalue. * TODO Use a wrapper return type to fix for non-reference type. */ template hb_remove_reference* operator -> () const { return hb_addressof (**thiz()); } item_t operator * () const { return thiz()->__item__ (); } item_t operator * () { return thiz()->__item__ (); } item_t operator [] (unsigned i) const { return thiz()->__item_at__ (i); } item_t operator [] (unsigned i) { return thiz()->__item_at__ (i); } iter_t& operator += (unsigned count) & { thiz()->__forward__ (count); return *thiz(); } iter_t operator += (unsigned count) && { thiz()->__forward__ (count); return *thiz(); } iter_t& operator ++ () & { thiz()->__next__ (); return *thiz(); } iter_t operator ++ () && { thiz()->__next__ (); return *thiz(); } iter_t& operator -= (unsigned count) & { thiz()->__rewind__ (count); return *thiz(); } iter_t operator -= (unsigned count) && { thiz()->__rewind__ (count); return *thiz(); } iter_t& operator -- () & { thiz()->__prev__ (); return *thiz(); } iter_t operator -- () && { thiz()->__prev__ (); return *thiz(); } iter_t operator + (unsigned count) const { auto c = thiz()->iter (); c += count; return c; } friend iter_t operator + (unsigned count, const iter_t &it) { return it + count; } iter_t operator ++ (int) { iter_t c (*thiz()); ++*thiz(); return c; } iter_t operator - (unsigned count) const { auto c = thiz()->iter (); c -= count; return c; } iter_t operator -- (int) { iter_t c (*thiz()); --*thiz(); return c; } template iter_t& operator >> (T &v) & { v = **thiz(); ++*thiz(); return *thiz(); } template iter_t operator >> (T &v) && { v = **thiz(); ++*thiz(); return *thiz(); } template iter_t& operator << (const T v) & { **thiz() = v; ++*thiz(); return *thiz(); } template iter_t operator << (const T v) && { **thiz() = v; ++*thiz(); return *thiz(); } protected: hb_iter_t () {} hb_iter_t (const hb_iter_t &o HB_UNUSED) {} void operator = (const hb_iter_t &o HB_UNUSED) {} }; #define HB_ITER_USING(Name) \ using item_t = typename Name::item_t; \ using Name::begin; \ using Name::end; \ using Name::item_size; \ using Name::is_iterator; \ using Name::iter; \ using Name::operator bool; \ using Name::len; \ using Name::operator ->; \ using Name::operator *; \ using Name::operator []; \ using Name::operator +=; \ using Name::operator ++; \ using Name::operator -=; \ using Name::operator --; \ using Name::operator +; \ using Name::operator -; \ using Name::operator >>; \ using Name::operator <<; \ static_assert (true, "") /* Returns iterator / item type of a type. */ template using hb_iter_type = decltype (hb_deref (hb_declval (Iterable)).iter ()); template using hb_item_type = decltype (*hb_deref (hb_declval (Iterable)).iter ()); template struct hb_array_t; struct { template hb_iter_type operator () (T&& c) const { return hb_deref (hb_forward (c)).iter (); } /* Specialization for C arrays. */ template inline hb_array_t operator () (Type *array, unsigned int length) const { return hb_array_t (array, length); } template hb_array_t operator () (Type (&array)[length]) const { return hb_array_t (array, length); } } HB_FUNCOBJ (hb_iter); /* Mixin to fill in what the subclass doesn't provide. */ template struct hb_iter_fallback_mixin_t { private: /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */ const iter_t* thiz () const { return static_cast (this); } iter_t* thiz () { return static_cast< iter_t *> (this); } public: /* Access: Implement __item__(), or __item_at__() if random-access. */ item_t __item__ () const { return (*thiz())[0]; } item_t __item_at__ (unsigned i) const { return *(*thiz() + i); } /* Termination: Implement __more__(), or __len__() if random-access. */ bool __more__ () const { return thiz()->len (); } unsigned __len__ () const { iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; } /* Advancing: Implement __next__(), or __forward__() if random-access. */ void __next__ () { *thiz() += 1; } void __forward__ (unsigned n) { while (n--) ++*thiz(); } /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */ void __prev__ () { *thiz() -= 1; } void __rewind__ (unsigned n) { while (n--) --*thiz(); } /* Range-based for: Implement __end__() if can be done faster, * and operator!=. */ iter_t __end__ () const { if (thiz()->is_random_access_iterator) return *thiz() + thiz()->len (); /* Above expression loops twice. Following loops once. */ auto it = *thiz(); while (it) ++it; return it; } protected: hb_iter_fallback_mixin_t () {} hb_iter_fallback_mixin_t (const hb_iter_fallback_mixin_t &o HB_UNUSED) {} void operator = (const hb_iter_fallback_mixin_t &o HB_UNUSED) {} }; template struct hb_iter_with_fallback_t : hb_iter_t, hb_iter_fallback_mixin_t { protected: hb_iter_with_fallback_t () {} hb_iter_with_fallback_t (const hb_iter_with_fallback_t &o HB_UNUSED) : hb_iter_t (o), hb_iter_fallback_mixin_t (o) {} void operator = (const hb_iter_with_fallback_t &o HB_UNUSED) {} }; /* * Meta-programming predicates. */ /* hb_is_iterable() */ template struct hb_is_iterable { private: template static auto impl (hb_priority<1>) -> decltype (hb_declval (U).iter (), hb_true_t ()); template static hb_false_t impl (hb_priority<0>); public: enum { value = decltype (impl (hb_prioritize))::value }; }; #define hb_is_iterable(Iterable) hb_is_iterable::value /* TODO Add hb_is_iterable_of(). * TODO Add random_access / sorted variants. */ /* hb_is_iterator() / hb_is_random_access_iterator() / hb_is_sorted_iterator() */ template static inline char _hb_is_iterator_of (hb_priority<0>, const void *) { return 0; } template static inline int _hb_is_iterator_of (hb_priority<2>, hb_iter_t *) { return 0; } template struct hb_is_iterator_of { enum { value = sizeof (int) == sizeof (_hb_is_iterator_of (hb_prioritize, hb_declval (Iter*))) }; }; #define hb_is_iterator_of(Iter, Item) hb_is_iterator_of::value #define hb_is_iterator(Iter) hb_is_iterator_of (Iter, typename Iter::item_t) #define hb_is_random_access_iterator_of(Iter, Item) \ hb_is_iterator_of (Iter, Item) && Iter::is_random_access_iterator #define hb_is_random_access_iterator(Iter) \ hb_is_random_access_iterator_of (Iter, typename Iter::item_t) #define hb_is_sorted_iterator_of(Iter, Item) \ hb_is_iterator_of (Iter, Item) && Iter::is_sorted_iterator #define hb_is_sorted_iterator(Iter) \ hb_is_sorted_iterator_of (Iter, typename Iter::item_t) /* Range-based 'for' for iterables. */ template static inline auto begin (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).begin ()) template static inline auto end (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).end ()) /* begin()/end() are NOT looked up non-ADL. So each namespace must declare them. * Do it for namespace OT. */ namespace OT { template static inline auto begin (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).begin ()) template static inline auto end (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).end ()) } /* * Adaptors, combiners, etc. */ template static inline auto operator | (Lhs&& lhs, Rhs&& rhs) HB_AUTO_RETURN (hb_forward (rhs) (hb_forward (lhs))) /* hb_map(), hb_filter(), hb_reduce() */ template struct hb_map_iter_t : hb_iter_t, decltype (hb_get (hb_declval (Proj), *hb_declval (Iter)))> { hb_map_iter_t (const Iter& it, Proj f_) : it (it), f (f_) {} typedef decltype (hb_get (hb_declval (Proj), *hb_declval (Iter))) __item_t__; static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator; __item_t__ __item__ () const { return hb_get (f.get (), *it); } __item_t__ __item_at__ (unsigned i) const { return hb_get (f.get (), it[i]); } bool __more__ () const { return bool (it); } unsigned __len__ () const { return it.len (); } void __next__ () { ++it; } void __forward__ (unsigned n) { it += n; } void __prev__ () { --it; } void __rewind__ (unsigned n) { it -= n; } hb_map_iter_t __end__ () const { return hb_map_iter_t (it.end (), f); } bool operator != (const hb_map_iter_t& o) const { return it != o.it || f != o.f; } private: Iter it; hb_reference_wrapper f; }; template struct hb_map_iter_factory_t { hb_map_iter_factory_t (Proj f) : f (f) {} template hb_map_iter_t operator () (Iter it) { return hb_map_iter_t (it, f); } private: Proj f; }; struct { template hb_map_iter_factory_t operator () (Proj&& f) const { return hb_map_iter_factory_t (f); } } HB_FUNCOBJ (hb_map); template struct hb_filter_iter_t : hb_iter_with_fallback_t, typename Iter::item_t> { hb_filter_iter_t (const Iter& it_, Pred p_, Proj f_) : it (it_), p (p_), f (f_) { while (it && !hb_has (p.get (), hb_get (f.get (), *it))) ++it; } typedef typename Iter::item_t __item_t__; static constexpr bool is_sorted_iterator = Iter::is_sorted_iterator; __item_t__ __item__ () const { return *it; } bool __more__ () const { return bool (it); } void __next__ () { do ++it; while (it && !hb_has (p.get (), hb_get (f.get (), *it))); } void __prev__ () { --it; } hb_filter_iter_t __end__ () const { return hb_filter_iter_t (it.end (), p, f); } bool operator != (const hb_filter_iter_t& o) const { return it != o.it || p != o.p || f != o.f; } private: Iter it; hb_reference_wrapper p; hb_reference_wrapper f; }; template struct hb_filter_iter_factory_t { hb_filter_iter_factory_t (Pred p, Proj f) : p (p), f (f) {} template hb_filter_iter_t operator () (Iter it) { return hb_filter_iter_t (it, p, f); } private: Pred p; Proj f; }; struct { template hb_filter_iter_factory_t operator () (Pred&& p = hb_identity, Proj&& f = hb_identity) const { return hb_filter_iter_factory_t (p, f); } } HB_FUNCOBJ (hb_filter); template struct hb_reduce_t { hb_reduce_t (Redu r, InitT init_value) : r (r), init_value (init_value) {} template AccuT operator () (Iter it) { AccuT value = init_value; for (; it; ++it) value = r (value, *it); return value; } private: Redu r; InitT init_value; }; struct { template hb_reduce_t operator () (Redu&& r, InitT init_value) const { return hb_reduce_t (r, init_value); } } HB_FUNCOBJ (hb_reduce); /* hb_zip() */ template struct hb_zip_iter_t : hb_iter_t, hb_pair_t> { hb_zip_iter_t () {} hb_zip_iter_t (const A& a, const B& b) : a (a), b (b) {} typedef hb_pair_t __item_t__; static constexpr bool is_random_access_iterator = A::is_random_access_iterator && B::is_random_access_iterator; static constexpr bool is_sorted_iterator = A::is_sorted_iterator && B::is_sorted_iterator; __item_t__ __item__ () const { return __item_t__ (*a, *b); } __item_t__ __item_at__ (unsigned i) const { return __item_t__ (a[i], b[i]); } bool __more__ () const { return a && b; } unsigned __len__ () const { return hb_min (a.len (), b.len ()); } void __next__ () { ++a; ++b; } void __forward__ (unsigned n) { a += n; b += n; } void __prev__ () { --a; --b; } void __rewind__ (unsigned n) { a -= n; b -= n; } hb_zip_iter_t __end__ () const { return hb_zip_iter_t (a.end (), b.end ()); } bool operator != (const hb_zip_iter_t& o) const { return a != o.a || b != o.b; } private: A a; B b; }; struct { template hb_zip_iter_t, hb_iter_type> operator () (A&& a, B&& b) const { return hb_zip_iter_t, hb_iter_type> (hb_iter (a), hb_iter (b)); } } HB_FUNCOBJ (hb_zip); /* hb_apply() */ template struct hb_apply_t { hb_apply_t (Appl a) : a (a) {} template void operator () (Iter it) { for (; it; ++it) (void) hb_invoke (a, *it); } private: Appl a; }; struct { template hb_apply_t operator () (Appl&& a) const { return hb_apply_t (a); } template hb_apply_t operator () (Appl *a) const { return hb_apply_t (*a); } } HB_FUNCOBJ (hb_apply); /* hb_iota()/hb_range() */ template struct hb_counter_iter_t : hb_iter_t, T> { hb_counter_iter_t (T start, T end_, S step) : v (start), end_ (end__for (start, end_, step)), step (step) {} typedef T __item_t__; static constexpr bool is_random_access_iterator = true; static constexpr bool is_sorted_iterator = true; __item_t__ __item__ () const { return v; } __item_t__ __item_at__ (unsigned j) const { return v + j * step; } bool __more__ () const { return v != end_; } unsigned __len__ () const { return (end_ - v) / step; } void __next__ () { v += step; } void __forward__ (unsigned n) { v += n * step; } void __prev__ () { v -= step; } void __rewind__ (unsigned n) { v -= n * step; } hb_counter_iter_t __end___ () const { hb_counter_iter_t (end_, end_, step); } bool operator != (const hb_counter_iter_t& o) const { return v != o.v || end_ != o.end_ || step != o.step; } private: static inline T end__for (T start, T end_, S step) { auto res = (end_ - start) % step; if (!res) return end_; end_ += step - res; return end_; } private: T v; T end_; S step; }; struct { template hb_counter_iter_t operator () (T start = 0u, S&& step = 1u) const { return hb_counter_iter_t (start, hb_int_max (T), step); } } HB_FUNCOBJ (hb_iota); struct { template hb_counter_iter_t operator () (T end = (unsigned) -1) const { return hb_counter_iter_t (0, end, 1u); } template hb_counter_iter_t operator () (T start, T end, S&& step = 1u) const { return hb_counter_iter_t (start, end, step); } } HB_FUNCOBJ (hb_range); /* hb_enumerate */ struct { template auto operator () (Iterable&& it, Index start = 0u) const HB_AUTO_RETURN ( hb_zip (hb_iota (start), it) ) } HB_FUNCOBJ (hb_enumerate); /* hb_sink() */ template struct hb_sink_t { hb_sink_t (Sink s) : s (s) {} template void operator () (Iter it) { for (; it; ++it) s << *it; } private: Sink s; }; struct { template hb_sink_t operator () (Sink&& s) const { return hb_sink_t (s); } template hb_sink_t operator () (Sink *s) const { return hb_sink_t (*s); } } HB_FUNCOBJ (hb_sink); /* hb-drain: hb_sink to void / blackhole / /dev/null. */ struct { template void operator () (Iter it) const { for (; it; ++it) (void) *it; } } HB_FUNCOBJ (hb_drain); /* hb_unzip(): unzip and sink to two sinks. */ template struct hb_unzip_t { hb_unzip_t (Sink1 s1, Sink2 s2) : s1 (s1), s2 (s2) {} template void operator () (Iter it) { for (; it; ++it) { const auto &v = *it; s1 << v.first; s2 << v.second; } } private: Sink1 s1; Sink2 s2; }; struct { template hb_unzip_t operator () (Sink1&& s1, Sink2&& s2) const { return hb_unzip_t (s1, s2); } template hb_unzip_t operator () (Sink1 *s1, Sink2 *s2) const { return hb_unzip_t (*s1, *s2); } } HB_FUNCOBJ (hb_unzip); /* hb-all, hb-any, hb-none. */ struct { template bool operator () (Iterable&& c, Pred&& p = hb_identity, Proj&& f = hb_identity) const { for (auto it = hb_iter (c); it; ++it) if (!hb_match (hb_forward (p), hb_get (hb_forward (f), *it))) return false; return true; } } HB_FUNCOBJ (hb_all); struct { template bool operator () (Iterable&& c, Pred&& p = hb_identity, Proj&& f = hb_identity) const { for (auto it = hb_iter (c); it; ++it) if (hb_match (hb_forward (p), hb_get (hb_forward (f), *it))) return true; return false; } } HB_FUNCOBJ (hb_any); struct { template bool operator () (Iterable&& c, Pred&& p = hb_identity, Proj&& f = hb_identity) const { for (auto it = hb_iter (c); it; ++it) if (hb_match (hb_forward (p), hb_get (hb_forward (f), *it))) return false; return true; } } HB_FUNCOBJ (hb_none); /* * Algorithms operating on iterators. */ template inline void hb_fill (C& c, const V &v) { for (auto i = hb_iter (c); i; i++) *i = v; } template inline void hb_copy (S&& is, D&& id) { hb_iter (is) | hb_sink (id); } #endif /* HB_ITER_HH */