2018-12-21 06:08:05 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Google, 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HB_ITER_HH
|
|
|
|
#define HB_ITER_HH
|
|
|
|
|
|
|
|
#include "hb.hh"
|
2019-01-09 18:05:01 +01:00
|
|
|
#include "hb-algs.hh" // for hb_addressof
|
2018-12-27 23:23:12 +01:00
|
|
|
#include "hb-meta.hh"
|
2018-12-21 23:35:58 +01:00
|
|
|
#include "hb-null.hh"
|
2018-12-21 06:08:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* 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.
|
2018-12-21 06:54:55 +01:00
|
|
|
* hb_iter_tator objects are small, light-weight, objects that can be
|
2018-12-21 06:08:05 +01:00
|
|
|
* copied by value. If the collection / object being iterated on
|
2018-12-21 06:54:55 +01:00
|
|
|
* is writable, then the iterator returns lvalues, otherwise it
|
2018-12-21 06:08:05 +01:00
|
|
|
* returns rvalues.
|
|
|
|
*/
|
|
|
|
|
2018-12-28 20:29:09 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Base classes for iterators.
|
|
|
|
*/
|
|
|
|
|
2018-12-21 06:54:55 +01:00
|
|
|
/* Base class for all iterators. */
|
2019-01-09 08:48:35 +01:00
|
|
|
template <typename Iter, typename Item = typename Iter::__item_t__>
|
2019-01-04 17:22:32 +01:00
|
|
|
struct hb_iter_t
|
2018-12-21 06:08:05 +01:00
|
|
|
{
|
2018-12-21 07:57:02 +01:00
|
|
|
typedef Iter iter_t;
|
|
|
|
typedef Item item_t;
|
2018-12-22 01:29:00 +01:00
|
|
|
enum { item_size = hb_static_size (Item) };
|
2018-12-31 02:51:31 +01:00
|
|
|
enum { is_iterator = true };
|
|
|
|
enum { is_random_access_iterator = false };
|
2019-01-02 22:20:40 +01:00
|
|
|
enum { is_sorted_iterator = false };
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2018-12-22 00:09:45 +01:00
|
|
|
private:
|
|
|
|
/* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
|
2018-12-21 07:57:02 +01:00
|
|
|
const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
|
|
|
|
iter_t* thiz () { return static_cast< iter_t *> (this); }
|
2018-12-22 00:09:45 +01:00
|
|
|
public:
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2019-01-08 22:43:49 +01:00
|
|
|
/* TODO:
|
|
|
|
* Port operators below to use hb_enable_if to sniff which method implements
|
|
|
|
* an operator and use it, and remove hb_iter_mixin_t completely. */
|
|
|
|
|
2018-12-21 06:54:55 +01:00
|
|
|
/* Operators. */
|
2018-12-27 02:06:10 +01:00
|
|
|
iter_t iter () const { return *thiz(); }
|
|
|
|
explicit_operator bool () const { return thiz()->__more__ (); }
|
|
|
|
unsigned len () const { return thiz()->__len__ (); }
|
2019-01-08 03:38:49 +01:00
|
|
|
hb_remove_reference (item_t)* operator -> () const { return hb_addressof (**thiz()); }
|
2018-12-27 23:17:28 +01:00
|
|
|
item_t operator * () const { return thiz()->__item__ (); }
|
|
|
|
item_t operator [] (unsigned i) const { return thiz()->__item_at__ (i); }
|
2018-12-27 02:06:10 +01:00
|
|
|
iter_t& operator += (unsigned count) { thiz()->__forward__ (count); return *thiz(); }
|
|
|
|
iter_t& operator ++ () { thiz()->__next__ (); return *thiz(); }
|
|
|
|
iter_t& operator -= (unsigned count) { thiz()->__rewind__ (count); return *thiz(); }
|
|
|
|
iter_t& operator -- () { thiz()->__prev__ (); return *thiz(); }
|
2018-12-30 08:11:03 +01:00
|
|
|
iter_t operator + (unsigned count) const { iter_t c (*thiz()); c += count; return c; }
|
|
|
|
friend iter_t operator + (unsigned count, const iter_t &it) { return it + count; }
|
2018-12-21 07:57:02 +01:00
|
|
|
iter_t operator ++ (int) { iter_t c (*thiz()); ++*thiz(); return c; }
|
2018-12-27 00:54:27 +01:00
|
|
|
iter_t operator - (unsigned count) const { iter_t c (*thiz()); c -= count; return c; }
|
2018-12-21 07:57:02 +01:00
|
|
|
iter_t operator -- (int) { iter_t c (*thiz()); --*thiz(); return c; }
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2018-12-22 00:09:45 +01:00
|
|
|
protected:
|
|
|
|
hb_iter_t () {}
|
2018-12-24 02:19:52 +01:00
|
|
|
hb_iter_t (const hb_iter_t &o HB_UNUSED) {}
|
|
|
|
void operator = (const hb_iter_t &o HB_UNUSED) {}
|
2018-12-22 00:09:45 +01:00
|
|
|
};
|
|
|
|
|
2018-12-27 19:29:51 +01:00
|
|
|
#define HB_ITER_USING(Name) \
|
2019-01-09 01:38:08 +01:00
|
|
|
using iter_t = typename Name::iter_t; \
|
|
|
|
using item_t = typename Name::item_t; \
|
2018-12-28 22:29:48 +01:00
|
|
|
using Name::item_size; \
|
2018-12-31 02:51:31 +01:00
|
|
|
using Name::is_iterator; \
|
2018-12-27 19:29:51 +01:00
|
|
|
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 --; \
|
2018-12-30 08:11:03 +01:00
|
|
|
using Name::operator +; \
|
2018-12-27 19:29:51 +01:00
|
|
|
using Name::operator -; \
|
|
|
|
static_assert (true, "")
|
|
|
|
|
2018-12-22 00:09:45 +01:00
|
|
|
/* Mixin to fill in what the subclass doesn't provide. */
|
2019-01-09 08:48:35 +01:00
|
|
|
template <typename iter_t, typename item_t = typename iter_t::__item_t__>
|
2018-12-22 00:09:45 +01:00
|
|
|
struct hb_iter_mixin_t
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
/* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
|
|
|
|
const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
|
|
|
|
iter_t* thiz () { return static_cast< iter_t *> (this); }
|
|
|
|
public:
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2018-12-21 07:53:27 +01:00
|
|
|
/* Access: Implement __item__(), or __item_at__() if random-access. */
|
2018-12-27 23:17:28 +01:00
|
|
|
item_t __item__ () const { return (*thiz())[0]; }
|
|
|
|
item_t __item_at__ (unsigned i) const { return *(*thiz() + i); }
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2018-12-22 00:49:27 +01:00
|
|
|
/* Termination: Implement __more__(), or __len__() if random-access. */
|
2018-12-27 02:06:10 +01:00
|
|
|
bool __more__ () const { return thiz()->len (); }
|
2018-12-22 00:49:27 +01:00
|
|
|
unsigned __len__ () const
|
|
|
|
{ iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; }
|
2018-12-21 06:54:55 +01:00
|
|
|
|
|
|
|
/* Advancing: Implement __next__(), or __forward__() if random-access. */
|
2018-12-27 02:06:10 +01:00
|
|
|
void __next__ () { *thiz() += 1; }
|
|
|
|
void __forward__ (unsigned n) { while (n--) ++*thiz(); }
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2018-12-21 07:17:35 +01:00
|
|
|
/* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
|
2018-12-27 02:06:10 +01:00
|
|
|
void __prev__ () { *thiz() -= 1; }
|
|
|
|
void __rewind__ (unsigned n) { while (n--) --*thiz(); }
|
2018-12-21 07:17:35 +01:00
|
|
|
|
2018-12-27 00:54:27 +01:00
|
|
|
protected:
|
|
|
|
hb_iter_mixin_t () {}
|
|
|
|
hb_iter_mixin_t (const hb_iter_mixin_t &o HB_UNUSED) {}
|
|
|
|
void operator = (const hb_iter_mixin_t &o HB_UNUSED) {}
|
2018-12-21 06:08:05 +01:00
|
|
|
};
|
|
|
|
|
2018-12-28 20:29:09 +01:00
|
|
|
/*
|
|
|
|
* Meta-programming predicates.
|
|
|
|
*/
|
|
|
|
|
2018-12-28 22:29:48 +01:00
|
|
|
/* hb_is_iterable() */
|
|
|
|
|
|
|
|
template<typename T, typename B>
|
2018-12-28 20:29:09 +01:00
|
|
|
struct _hb_is_iterable
|
|
|
|
{ enum { value = false }; };
|
2018-12-28 22:29:48 +01:00
|
|
|
template<typename T>
|
2018-12-29 02:16:44 +01:00
|
|
|
struct _hb_is_iterable<T, hb_bool_tt<true || sizeof (Null(T).iter ())> >
|
2018-12-28 20:29:09 +01:00
|
|
|
{ enum { value = true }; };
|
2018-12-28 20:40:30 +01:00
|
|
|
|
2018-12-28 22:29:48 +01:00
|
|
|
template<typename T>
|
2018-12-28 20:29:09 +01:00
|
|
|
struct hb_is_iterable { enum { value = _hb_is_iterable<T, hb_true_t>::value }; };
|
|
|
|
#define hb_is_iterable(Iterable) hb_is_iterable<Iterable>::value
|
2018-12-21 06:08:05 +01:00
|
|
|
|
2019-01-08 21:57:01 +01:00
|
|
|
/* TODO Add hb_is_iterable_of().
|
|
|
|
* TODO Add random_access / sorted variants. */
|
|
|
|
|
2018-12-28 20:29:09 +01:00
|
|
|
|
2019-01-08 21:53:02 +01:00
|
|
|
/* hb_is_iterator() / hb_is_random_access_iterator() / hb_is_sorted_iterator() */
|
2018-12-28 22:29:48 +01:00
|
|
|
|
2019-01-07 23:24:23 +01:00
|
|
|
template <typename Iter>
|
2019-01-08 21:42:15 +01:00
|
|
|
struct _hb_is_iterator_of
|
2019-01-07 23:24:23 +01:00
|
|
|
{
|
2019-01-09 04:13:17 +01:00
|
|
|
char operator () (...) { return 0; }
|
2019-01-09 01:27:37 +01:00
|
|
|
template<typename Item> int operator () (hb_iter_t<Iter, Item> *) { return 0; }
|
|
|
|
template<typename Item> int operator () (hb_iter_t<Iter, const Item> *) { return 0; }
|
|
|
|
template<typename Item> int operator () (hb_iter_t<Iter, Item&> *) { return 0; }
|
|
|
|
template<typename Item> int operator () (hb_iter_t<Iter, const Item&> *) { return 0; }
|
2019-01-07 23:24:23 +01:00
|
|
|
static_assert (sizeof (char) != sizeof (int), "");
|
|
|
|
};
|
2018-12-31 00:42:14 +01:00
|
|
|
template<typename Iter, typename Item>
|
2019-01-08 21:42:15 +01:00
|
|
|
struct hb_is_iterator_of { enum {
|
|
|
|
value = sizeof (int) == sizeof (hb_declval (_hb_is_iterator_of<Iter>) (hb_declval (Iter*))) }; };
|
|
|
|
#define hb_is_iterator_of(Iter, Item) hb_is_iterator_of<Iter, Item>::value
|
|
|
|
#define hb_is_iterator(Iter) hb_is_iterator_of (Iter, typename Iter::item_t)
|
2018-12-28 22:29:48 +01:00
|
|
|
|
2019-01-08 21:53:02 +01:00
|
|
|
#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)
|
|
|
|
|
2019-01-08 21:42:15 +01:00
|
|
|
#define hb_is_sorted_iterator_of(Iter, Item) \
|
2019-01-08 21:53:02 +01:00
|
|
|
hb_is_iterator_of (Iter, Item) && Iter::is_sorted_iterator
|
2019-01-08 21:42:15 +01:00
|
|
|
#define hb_is_sorted_iterator(Iter) \
|
|
|
|
hb_is_sorted_iterator_of (Iter, typename Iter::item_t)
|
2018-12-28 22:29:48 +01:00
|
|
|
|
2019-01-09 18:07:01 +01:00
|
|
|
|
2018-12-28 20:29:09 +01:00
|
|
|
/*
|
2019-01-09 18:07:01 +01:00
|
|
|
* Adaptors, combiners, etc.
|
2018-12-28 20:29:09 +01:00
|
|
|
*/
|
2018-12-21 23:21:19 +01:00
|
|
|
|
2019-01-09 20:15:49 +01:00
|
|
|
/* hb_map(), hb_filter(), hb_reduce() */
|
|
|
|
|
|
|
|
template <typename Iter, typename Proj,
|
|
|
|
hb_enable_if (hb_is_iterator (Iter))>
|
|
|
|
struct hb_map_iter_t :
|
|
|
|
hb_iter_t<hb_map_iter_t<Iter, Proj>, decltype (f (*hb_declval (typename Iter::item_t)))>
|
|
|
|
{
|
|
|
|
hb_map_iter_t (const Iter& it, Proj f) : it (it), f (f) {}
|
|
|
|
|
|
|
|
typedef decltype (f (*hb_declval (typename Iter::item_t))) __item_t__;
|
|
|
|
enum { is_random_access_iterator = Iter::is_random_access_iterator };
|
|
|
|
__item_t__ __item__ () const { return f (*it); }
|
|
|
|
__item_t__ __item_at__ (unsigned i) const { return f (it[i]); }
|
|
|
|
bool __more__ () const { return 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; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Iter it;
|
|
|
|
Proj f;
|
|
|
|
};
|
|
|
|
template <typename Proj>
|
|
|
|
struct hb_map_iter_factory_t
|
|
|
|
{
|
|
|
|
hb_map_iter_factory_t (Proj f) : f (f) {}
|
|
|
|
|
|
|
|
template <typename Iterable,
|
|
|
|
hb_enable_if (hb_is_iterable (Iterable))>
|
|
|
|
hb_map_iter_t<typename Iterable::iter_t, Proj> operator () (const Iterable &c)
|
|
|
|
{ return hb_map_iter_t<typename Iterable::iter_t, Proj> (c.iter (), f); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Proj f;
|
|
|
|
};
|
|
|
|
template <typename Proj>
|
|
|
|
inline hb_map_iter_factory_t<Proj>
|
|
|
|
hb_map (Proj f)
|
|
|
|
{ return hb_map_iter_factory_t<Proj> (f); }
|
|
|
|
|
2018-12-21 23:21:19 +01:00
|
|
|
|
2019-01-09 18:07:01 +01:00
|
|
|
/* hb_zip() */
|
2018-12-21 23:21:19 +01:00
|
|
|
|
2019-01-09 08:57:16 +01:00
|
|
|
template <typename A, typename B>
|
2019-01-09 18:49:12 +01:00
|
|
|
struct hb_zip_iter_t :
|
|
|
|
hb_iter_t<hb_zip_iter_t<A, B>, hb_pair_t<typename A::item_t, typename B::item_t> >
|
2019-01-09 08:57:16 +01:00
|
|
|
{
|
2019-01-09 18:49:12 +01:00
|
|
|
hb_zip_iter_t () {}
|
|
|
|
hb_zip_iter_t (A a, B b) : a (a), b (b) {}
|
2019-01-09 08:57:16 +01:00
|
|
|
|
|
|
|
typedef hb_pair_t<typename A::item_t, typename B::item_t> __item_t__;
|
|
|
|
enum { is_random_access_iterator =
|
|
|
|
A::is_random_access_iterator &&
|
|
|
|
B::is_random_access_iterator };
|
|
|
|
enum { 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 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; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
A a;
|
|
|
|
B b;
|
|
|
|
};
|
2019-01-09 10:02:38 +01:00
|
|
|
template <typename A, typename B,
|
|
|
|
hb_enable_if (hb_is_iterable (A) && hb_is_iterable (B))>
|
2019-01-09 18:49:12 +01:00
|
|
|
inline hb_zip_iter_t<typename A::iter_t, typename B::iter_t>
|
2019-01-09 20:00:32 +01:00
|
|
|
hb_zip (const A& a, const B &b)
|
2019-01-09 18:49:12 +01:00
|
|
|
{ return hb_zip_iter_t<typename A::iter_t, typename B::iter_t> (a.iter (), b.iter ()); }
|
2019-01-09 08:57:16 +01:00
|
|
|
|
2019-01-09 18:07:01 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Algorithms operating on iterators.
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename C, typename V,
|
|
|
|
hb_enable_if (hb_is_iterable (C))>
|
|
|
|
inline void
|
|
|
|
hb_fill (C& c, const V &v)
|
|
|
|
{
|
|
|
|
for (typename C::iter_t i (c); i; i++)
|
|
|
|
hb_assign (*i, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename S, typename D,
|
|
|
|
hb_enable_if (hb_is_iterator (S) && hb_is_iterator (D))>
|
|
|
|
inline bool
|
|
|
|
hb_copy (D id, S is)
|
|
|
|
{
|
|
|
|
for (; id && is; ++id, ++is)
|
|
|
|
*id = *is;
|
|
|
|
return !is;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-21 06:08:05 +01:00
|
|
|
#endif /* HB_ITER_HH */
|