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"
|
2018-12-27 00:58:42 +01:00
|
|
|
#include "hb-dsalgs.hh" // for hb_addressof
|
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-21 06:54:55 +01:00
|
|
|
/* Base class for all iterators. */
|
2018-12-21 07:53:27 +01:00
|
|
|
template <typename Iter, typename Item = typename Iter::__item_type__>
|
2018-12-21 06:54:55 +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-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
|
|
|
|
|
|
|
/* 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__ (); }
|
|
|
|
item_t* operator -> () const { return hb_addressof (*thiz()); }
|
|
|
|
item_t& operator * () const { return thiz()->__item__ (); }
|
|
|
|
item_t& operator [] (unsigned i) const { return thiz()->__item_at__ (i); }
|
|
|
|
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-27 00:54:27 +01:00
|
|
|
iter_t operator + (unsigned count) const { iter_t c (*thiz()); c += count; return c; }
|
2018-12-27 01:54:52 +01:00
|
|
|
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-27 01:01:30 +01:00
|
|
|
constexpr bool is_random_access () const { return thiz()->__random_access__ (); }
|
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-22 01:55:02 +01:00
|
|
|
/* Base class for sorted iterators. Does not enforce anything.
|
|
|
|
* Just for class taxonomy and requirements. */
|
|
|
|
template <typename Iter, typename Item = typename Iter::__item_type__>
|
|
|
|
struct hb_sorted_iter_t : hb_iter_t<Iter, Item>
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
hb_sorted_iter_t () {}
|
2018-12-24 02:19:52 +01:00
|
|
|
hb_sorted_iter_t (const hb_sorted_iter_t &o) : hb_iter_t<Iter, Item> (o) {}
|
|
|
|
void operator = (const hb_sorted_iter_t &o HB_UNUSED) {}
|
2018-12-22 01:55:02 +01:00
|
|
|
};
|
|
|
|
|
2018-12-22 00:09:45 +01:00
|
|
|
/* Mixin to fill in what the subclass doesn't provide. */
|
|
|
|
template <typename iter_t, typename item_t = typename iter_t::__item_type__>
|
|
|
|
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 02:06:10 +01:00
|
|
|
item_t& __item__ () const { return (*thiz())[0]; }
|
2018-12-27 00:54:27 +01:00
|
|
|
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 02:06:10 +01:00
|
|
|
/* Random access: Implement if __item_at__(), __len__(), __forward__() are. */
|
2018-12-27 01:01:30 +01:00
|
|
|
constexpr bool __random_access__ () const { return false; }
|
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-21 23:21:19 +01:00
|
|
|
/* Functions operating on iterators or iteratables. */
|
|
|
|
|
|
|
|
template <typename C, typename V> inline void
|
|
|
|
hb_fill (const C& c, const V &v)
|
|
|
|
{
|
|
|
|
for (typename C::iter_t i (c); i; i++)
|
|
|
|
hb_assign (*i, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename S, typename D> inline bool
|
|
|
|
hb_copy (hb_iter_t<D> &id, hb_iter_t<S> &is)
|
|
|
|
{
|
|
|
|
for (; id && is; ++id, ++is)
|
|
|
|
*id = *is;
|
|
|
|
return !is;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-21 06:08:05 +01:00
|
|
|
#endif /* HB_ITER_HH */
|