hb-iter.hh 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * 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"
B
Behdad Esfahbod 已提交
31
#include "hb-dsalgs.hh" // for hb_addressof
32
#include "hb-null.hh"
33 34 35 36 37 38


/* 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.
39
 * hb_iter_tator objects are small, light-weight, objects that can be
40
 * copied by value.  If the collection / object being iterated on
41
 * is writable, then the iterator returns lvalues, otherwise it
42 43 44
 * returns rvalues.
 */

45
/* Base class for all iterators. */
B
Behdad Esfahbod 已提交
46
template <typename Iter, typename Item = typename Iter::__item_type__>
47
struct hb_iter_t
48
{
B
Behdad Esfahbod 已提交
49 50
  typedef Iter iter_t;
  typedef Item item_t;
B
Behdad Esfahbod 已提交
51
  enum { item_size = hb_static_size (Item) };
52

53 54
  private:
  /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
B
Behdad Esfahbod 已提交
55 56
  const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
        iter_t* thiz ()       { return static_cast<      iter_t *> (this); }
57
  public:
58 59

  /* Operators. */
B
Behdad Esfahbod 已提交
60
  operator iter_t () { return iter(); }
61
  explicit_operator bool () const { return more (); }
B
Behdad Esfahbod 已提交
62
  item_t& operator * () const { return item (); }
B
Behdad Esfahbod 已提交
63
  item_t* operator -> () const { return hb_addressof (item ()); }
64
  item_t& operator [] (unsigned i) const { return item_at (i); }
B
Behdad Esfahbod 已提交
65 66 67 68
  iter_t& operator += (unsigned count) { forward (count); return *thiz(); }
  iter_t& operator ++ () { next (); return *thiz(); }
  iter_t& operator -= (unsigned count) { rewind (count); return *thiz(); }
  iter_t& operator -- () { prev (); return *thiz(); }
B
Behdad Esfahbod 已提交
69
  iter_t operator + (unsigned count) const { iter_t c (*thiz()); c += count; return c; }
70
  friend iter_t operator + (unsigned count, const iter_t &it) { return it + count; }
B
Behdad Esfahbod 已提交
71
  iter_t operator ++ (int) { iter_t c (*thiz()); ++*thiz(); return c; }
B
Behdad Esfahbod 已提交
72
  iter_t operator - (unsigned count) const { iter_t c (*thiz()); c -= count; return c; }
B
Behdad Esfahbod 已提交
73
  iter_t operator -- (int) { iter_t c (*thiz()); --*thiz(); return c; }
74 75

  /* Methods. */
B
Behdad Esfahbod 已提交
76 77 78
  iter_t iter () const { return *thiz(); }
  item_t& item () const { return thiz()->__item__ (); }
  item_t& item_at (unsigned i) const { return thiz()->__item_at__ (i); }
79
  bool more () const { return thiz()->__more__ (); }
80
  unsigned len () const { return thiz()->__len__ (); }
B
Behdad Esfahbod 已提交
81
  void next () { thiz()->__next__ (); }
82
  void forward (unsigned n) { thiz()->__forward__ (n); }
B
Behdad Esfahbod 已提交
83 84
  void prev () { thiz()->__prev__ (); }
  void rewind (unsigned n) { thiz()->__rewind__ (n); }
85
  constexpr bool is_random_access () const { return thiz()->__random_access__ (); }
86

87 88
  protected:
  hb_iter_t () {}
B
Behdad Esfahbod 已提交
89 90
  hb_iter_t (const hb_iter_t &o HB_UNUSED) {}
  void operator = (const hb_iter_t &o HB_UNUSED) {}
91 92
};

B
Behdad Esfahbod 已提交
93 94 95 96 97 98 99
/* 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 () {}
B
Behdad Esfahbod 已提交
100 101
  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) {}
B
Behdad Esfahbod 已提交
102 103
};

104 105 106 107 108 109 110 111 112
/* 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:
113

B
Behdad Esfahbod 已提交
114
  /* Access: Implement __item__(), or __item_at__() if random-access. */
B
Behdad Esfahbod 已提交
115
  item_t& __item__ () const { return thiz()->item_at (0); }
B
Behdad Esfahbod 已提交
116
  item_t& __item_at__ (unsigned i) const { return *(*thiz() + i); }
117

118 119 120 121
  /* 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; }
122 123 124

  /* Advancing: Implement __next__(), or __forward__() if random-access. */
  void __next__ () { thiz()->forward (1); }
125
  void __forward__ (unsigned n) { while (n--) thiz()->next (); }
126

B
Behdad Esfahbod 已提交
127 128
  /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
  void __prev__ () { thiz()->rewind (1); }
129
  void __rewind__ (unsigned n) { while (n--) thiz()->prev (); }
B
Behdad Esfahbod 已提交
130

131
  /* Random access: Return true if item_at(), len(), forward() are fast. */
132
  constexpr bool __random_access__ () const { return false; }
B
Behdad Esfahbod 已提交
133 134 135 136 137

  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) {}
138 139 140
};


141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/* 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;
}


159
#endif /* HB_ITER_HH */