hb-iter.hh 13.7 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"
31
#include "hb-meta.hh"
32 33 34 35 36 37


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

B
Behdad Esfahbod 已提交
44 45 46 47 48

/*
 * Base classes for iterators.
 */

49
/* Base class for all iterators. */
50
template <typename iter_t, typename Item = typename iter_t::__item_t__>
51
struct hb_iter_t
52
{
B
Behdad Esfahbod 已提交
53
  typedef Item item_t;
54
  static constexpr unsigned item_size = hb_static_size (Item);
55 56 57
  static constexpr bool is_iterator = true;
  static constexpr bool is_random_access_iterator = false;
  static constexpr bool is_sorted_iterator = false;
58

59 60
  private:
  /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
B
Behdad Esfahbod 已提交
61 62
  const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
        iter_t* thiz ()       { return static_cast<      iter_t *> (this); }
63
  public:
64

B
Behdad Esfahbod 已提交
65 66
  /* TODO:
   * Port operators below to use hb_enable_if to sniff which method implements
B
Behdad Esfahbod 已提交
67
   * an operator and use it, and remove hb_iter_fallback_mixin_t completely. */
B
Behdad Esfahbod 已提交
68

69
  /* Operators. */
70
  iter_t iter () const { return *thiz(); }
71
  iter_t operator + () const { return *thiz(); }
72 73
  explicit_operator bool () const { return thiz()->__more__ (); }
  unsigned len () const { return thiz()->__len__ (); }
B
Behdad Esfahbod 已提交
74 75 76 77
  /* The following can only be enabled if item_t is reference type.  Otherwise
   * it will be returning pointer to temporary rvalue. */
  template <typename T = item_t,
	    hb_enable_if (hb_is_reference (T))>
B
Behdad Esfahbod 已提交
78
  hb_remove_reference (item_t)* operator -> () const { return hb_addressof (**thiz()); }
79 80
  item_t operator * () const { return thiz()->__item__ (); }
  item_t operator [] (unsigned i) const { return thiz()->__item_at__ (i); }
81 82 83 84
  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(); }
85
  iter_t operator + (unsigned count) const { auto c = thiz()->iter (); c += count; return c; }
B
Behdad Esfahbod 已提交
86
  friend iter_t operator + (unsigned count, const iter_t &it) { return it + count; }
B
Behdad Esfahbod 已提交
87
  iter_t operator ++ (int) { iter_t c (*thiz()); ++*thiz(); return c; }
88
  iter_t operator - (unsigned count) const { auto c = thiz()->iter (); c -= count; return c; }
B
Behdad Esfahbod 已提交
89
  iter_t operator -- (int) { iter_t c (*thiz()); --*thiz(); return c; }
90 91 92 93
  template <typename T>
  iter_t& operator >> (T &v) { v = **thiz(); ++*thiz(); return *thiz(); }
  template <typename T>
  iter_t& operator << (const T v) { **thiz() = v; ++*thiz(); return *thiz(); }
94

95 96
  protected:
  hb_iter_t () {}
B
Behdad Esfahbod 已提交
97 98
  hb_iter_t (const hb_iter_t &o HB_UNUSED) {}
  void operator = (const hb_iter_t &o HB_UNUSED) {}
99 100
};

101
#define HB_ITER_USING(Name) \
102
  using item_t = typename Name::item_t; \
103
  using Name::item_size; \
104
  using Name::is_iterator; \
105 106 107 108 109 110 111 112 113 114
  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 --; \
B
Behdad Esfahbod 已提交
115
  using Name::operator +; \
116
  using Name::operator -; \
B
Behdad Esfahbod 已提交
117 118
  using Name::operator >>; \
  using Name::operator <<; \
119 120
  static_assert (true, "")

B
Behdad Esfahbod 已提交
121 122 123
/* Returns iterator type of a type. */
#define hb_iter_t(Iterable) decltype (hb_declval (Iterable).iter ())

124 125 126

/* TODO Change to function-object. */

B
Behdad Esfahbod 已提交
127 128 129 130
template <typename T>
inline hb_iter_t (T)
hb_iter (const T& c) { return c.iter (); }

131 132 133 134 135 136 137 138
/* Specialization for C arrays. */
template <typename> struct hb_array_t;
template <typename Type> inline hb_array_t<Type>
hb_iter (Type *array, unsigned int length) { return hb_array_t<Type> (array, length); }
template <typename Type, unsigned int length> hb_array_t<Type>
hb_iter (Type (&array)[length]) { return hb_iter (array, length); }


139
/* Mixin to fill in what the subclass doesn't provide. */
140
template <typename iter_t, typename item_t = typename iter_t::__item_t__>
B
Behdad Esfahbod 已提交
141
struct hb_iter_fallback_mixin_t
142 143 144 145 146 147
{
  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:
148

B
Behdad Esfahbod 已提交
149
  /* Access: Implement __item__(), or __item_at__() if random-access. */
150 151
  item_t __item__ () const { return (*thiz())[0]; }
  item_t __item_at__ (unsigned i) const { return *(*thiz() + i); }
152

153
  /* Termination: Implement __more__(), or __len__() if random-access. */
154
  bool __more__ () const { return thiz()->len (); }
155 156
  unsigned __len__ () const
  { iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; }
157 158

  /* Advancing: Implement __next__(), or __forward__() if random-access. */
159 160
  void __next__ () { *thiz() += 1; }
  void __forward__ (unsigned n) { while (n--) ++*thiz(); }
161

B
Behdad Esfahbod 已提交
162
  /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
163 164
  void __prev__ () { *thiz() -= 1; }
  void __rewind__ (unsigned n) { while (n--) --*thiz(); }
B
Behdad Esfahbod 已提交
165

B
Behdad Esfahbod 已提交
166
  protected:
B
Behdad Esfahbod 已提交
167 168 169
  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) {}
170 171
};

172 173 174 175 176 177 178
template <typename iter_t, typename item_t = typename iter_t::__item_t__>
struct hb_iter_with_fallback_t :
  hb_iter_t<iter_t, item_t>,
  hb_iter_fallback_mixin_t<iter_t, item_t>
{
  protected:
  hb_iter_with_fallback_t () {}
B
Behdad Esfahbod 已提交
179 180 181
  hb_iter_with_fallback_t (const hb_iter_with_fallback_t &o HB_UNUSED) :
    hb_iter_t<iter_t, item_t> (o),
    hb_iter_fallback_mixin_t<iter_t, item_t> (o) {}
182 183 184
  void operator = (const hb_iter_with_fallback_t &o HB_UNUSED) {}
};

B
Behdad Esfahbod 已提交
185 186 187 188
/*
 * Meta-programming predicates.
 */

189 190 191
/* hb_is_iterable() */

template<typename T, typename B>
B
Behdad Esfahbod 已提交
192 193
struct _hb_is_iterable
{ enum { value = false }; };
194
template<typename T>
195
struct _hb_is_iterable<T, hb_bool_tt<true || sizeof (hb_declval (T).iter ())> >
B
Behdad Esfahbod 已提交
196
{ enum { value = true }; };
B
Behdad Esfahbod 已提交
197

198
template<typename T>
B
Behdad Esfahbod 已提交
199 200
struct hb_is_iterable { enum { value = _hb_is_iterable<T, hb_true_t>::value }; };
#define hb_is_iterable(Iterable) hb_is_iterable<Iterable>::value
201

B
Behdad Esfahbod 已提交
202 203 204
/* TODO Add hb_is_iterable_of().
 * TODO Add random_access / sorted variants. */

B
Behdad Esfahbod 已提交
205

206
/* hb_is_iterator() / hb_is_random_access_iterator() / hb_is_sorted_iterator() */
207

208
template <typename Iter>
209
struct _hb_is_iterator_of
210
{
B
Behdad Esfahbod 已提交
211
  char operator () (...) { return 0; }
B
Behdad Esfahbod 已提交
212 213 214 215
  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; }
216 217
  static_assert (sizeof (char) != sizeof (int), "");
};
218
template<typename Iter, typename Item>
219 220 221 222
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)
223

224 225 226 227 228
#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)

229
#define hb_is_sorted_iterator_of(Iter, Item) \
230
  hb_is_iterator_of (Iter, Item) && Iter::is_sorted_iterator
231 232
#define hb_is_sorted_iterator(Iter) \
  hb_is_sorted_iterator_of (Iter, typename Iter::item_t)
233

B
Behdad Esfahbod 已提交
234

B
Behdad Esfahbod 已提交
235
/*
B
Behdad Esfahbod 已提交
236
 * Adaptors, combiners, etc.
B
Behdad Esfahbod 已提交
237
 */
238

239 240 241 242 243
template <typename Lhs, typename Rhs,
	  hb_enable_if (hb_is_iterator (Lhs))>
static inline decltype (hb_declval (Rhs) (hb_declval (Lhs)))
operator | (Lhs lhs, const Rhs &rhs) { return rhs (lhs); }

B
Behdad Esfahbod 已提交
244 245 246 247 248
/* hb_map(), hb_filter(), hb_reduce() */

template <typename Iter, typename Proj,
	 hb_enable_if (hb_is_iterator (Iter))>
struct hb_map_iter_t :
B
Behdad Esfahbod 已提交
249 250
  hb_iter_t<hb_map_iter_t<Iter, Proj>,
	    decltype (hb_declval (Proj) (hb_declval (typename Iter::item_t)))>
B
Behdad Esfahbod 已提交
251
{
252
  hb_map_iter_t (const Iter& it, Proj&& f) : it (it), f (f) {}
B
Behdad Esfahbod 已提交
253

254
  typedef decltype (hb_declval (Proj) (hb_declval (typename Iter::item_t))) __item_t__;
255
  static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator;
B
Behdad Esfahbod 已提交
256 257
  __item_t__ __item__ () const { return f (*it); }
  __item_t__ __item_at__ (unsigned i) const { return f (it[i]); }
258
  bool __more__ () const { return bool (it); }
B
Behdad Esfahbod 已提交
259 260 261 262 263 264 265 266 267 268
  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;
};
269

B
Behdad Esfahbod 已提交
270 271 272
template <typename Proj>
struct hb_map_iter_factory_t
{
273
  hb_map_iter_factory_t (Proj&& f) : f (f) {}
B
Behdad Esfahbod 已提交
274

275 276 277 278 279
  template <typename Iter,
	    hb_enable_if (hb_is_iterator (Iter))>
  hb_map_iter_t<Iter, Proj>
  operator () (Iter it) const
  { return hb_map_iter_t<Iter, Proj> (it, f); }
B
Behdad Esfahbod 已提交
280 281 282 283 284 285

  private:
  Proj f;
};
template <typename Proj>
inline hb_map_iter_factory_t<Proj>
286
hb_map (Proj&& f)
B
Behdad Esfahbod 已提交
287 288
{ return hb_map_iter_factory_t<Proj> (f); }

B
Behdad Esfahbod 已提交
289 290 291
template <typename Iter, typename Pred, typename Proj,
	 hb_enable_if (hb_is_iterator (Iter))>
struct hb_filter_iter_t :
292 293
  hb_iter_with_fallback_t<hb_filter_iter_t<Iter, Pred, Proj>,
			  typename Iter::item_t>
B
Behdad Esfahbod 已提交
294
{
295
  hb_filter_iter_t (const Iter& it_, Pred&& p, Proj&& f) : it (it_), p (p), f (f)
B
Behdad Esfahbod 已提交
296 297 298
  { while (it && !p (f (*it))) ++it; }

  typedef typename Iter::item_t __item_t__;
299
  static constexpr bool is_sorted_iterator = Iter::is_sorted_iterator;
B
Behdad Esfahbod 已提交
300
  __item_t__ __item__ () const { return *it; }
301
  bool __more__ () const { return bool (it); }
B
Behdad Esfahbod 已提交
302 303 304 305 306 307 308 309 310 311 312
  void __next__ () { do ++it; while (it && !p (f (*it))); }
  void __prev__ () { --it; }

  private:
  Iter it;
  Pred p;
  Proj f;
};
template <typename Pred, typename Proj>
struct hb_filter_iter_factory_t
{
313
  hb_filter_iter_factory_t (Pred&& p, Proj&& f) : p (p), f (f) {}
B
Behdad Esfahbod 已提交
314

315 316 317 318 319
  template <typename Iter,
	    hb_enable_if (hb_is_iterator (Iter))>
  hb_filter_iter_t<Iter, Pred, Proj>
  operator () (Iter it) const
  { return hb_filter_iter_t<Iter, Pred, Proj> (it, p, f); }
B
Behdad Esfahbod 已提交
320 321 322 323 324

  private:
  Pred p;
  Proj f;
};
B
Behdad Esfahbod 已提交
325 326
template <typename Pred = decltype ((hb_bool)),
	  typename Proj = decltype ((hb_identity))>
B
Behdad Esfahbod 已提交
327
inline hb_filter_iter_factory_t<Pred, Proj>
328
hb_filter (Pred&& p = hb_bool, Proj&& f = hb_identity)
B
Behdad Esfahbod 已提交
329
{ return hb_filter_iter_factory_t<Pred, Proj> (p, f); }
330

B
Behdad Esfahbod 已提交
331
/* hb_zip() */
332

B
Behdad Esfahbod 已提交
333
template <typename A, typename B>
334
struct hb_zip_iter_t :
B
Behdad Esfahbod 已提交
335 336
  hb_iter_t<hb_zip_iter_t<A, B>,
	    hb_pair_t<typename A::item_t, typename B::item_t> >
B
Behdad Esfahbod 已提交
337
{
338 339
  hb_zip_iter_t () {}
  hb_zip_iter_t (A a, B b) : a (a), b (b) {}
B
Behdad Esfahbod 已提交
340 341

  typedef hb_pair_t<typename A::item_t, typename B::item_t> __item_t__;
342
  static constexpr bool is_random_access_iterator =
B
Behdad Esfahbod 已提交
343 344
    A::is_random_access_iterator &&
    B::is_random_access_iterator;
345
  static constexpr bool is_sorted_iterator =
B
Behdad Esfahbod 已提交
346 347
    A::is_sorted_iterator &&
    B::is_sorted_iterator;
B
Behdad Esfahbod 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360
  __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;
};
B
Behdad Esfahbod 已提交
361 362
template <typename A, typename B,
	  hb_enable_if (hb_is_iterable (A) && hb_is_iterable (B))>
363
inline hb_zip_iter_t<hb_iter_t (A), hb_iter_t (B)>
364
hb_zip (const A& a, const B &b)
365
{ return hb_zip_iter_t<hb_iter_t (A), hb_iter_t (B)> (a.iter (), b.iter ()); }
B
Behdad Esfahbod 已提交
366

B
Behdad Esfahbod 已提交
367 368 369 370 371 372 373
/* hb_sink() */

template <typename Sink>
struct hb_sink_t
{
  hb_sink_t (Sink&& s) : s (s) {}

374 375
  template <typename Iter,
	    hb_enable_if (hb_is_iterator (Iter))>
B
Behdad Esfahbod 已提交
376
  void
377
  operator () (Iter it) const
B
Behdad Esfahbod 已提交
378
  {
379
    for (; it; ++it)
B
Behdad Esfahbod 已提交
380 381 382 383 384 385
      s << *it;
  }

  private:
  Sink s;
};
386 387 388 389 390
static const struct
{
  template <typename Sink> hb_sink_t<Sink>
  operator () (Sink&& s) const
  { return hb_sink_t<Sink> (s); }
391 392 393 394

  template <typename Sink> hb_sink_t<Sink&>
  operator () (Sink *s) const
  { return hb_sink_t<Sink&> (*s); }
395
} hb_sink HB_UNUSED;
B
Behdad Esfahbod 已提交
396

B
Behdad Esfahbod 已提交
397 398 399 400 401 402 403 404 405 406

/*
 * 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)
{
407
  for (auto i = c.iter (); i; i++)
B
Behdad Esfahbod 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421
    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;
}


422
#endif /* HB_ITER_HH */