hb-iter.hh 23.4 KB
Newer Older
1 2
/*
 * Copyright © 2018  Google, Inc.
B
Behdad Esfahbod 已提交
3
 * Copyright © 2019  Facebook, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  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
B
Behdad Esfahbod 已提交
26
 * Facebook Author(s): Behdad Esfahbod
27 28 29 30 31 32
 */

#ifndef HB_ITER_HH
#define HB_ITER_HH

#include "hb.hh"
33
#include "hb-algs.hh"
34
#include "hb-meta.hh"
35 36 37 38 39 40


/* 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.
41
 * hb_iter_tator objects are small, light-weight, objects that can be
42
 * copied by value.  If the collection / object being iterated on
43
 * is writable, then the iterator returns lvalues, otherwise it
44
 * returns rvalues.
45 46 47 48 49 50 51 52 53 54 55
 *
 * 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()?
56 57
 */

B
Behdad Esfahbod 已提交
58 59 60 61 62

/*
 * Base classes for iterators.
 */

63
/* Base class for all iterators. */
64
template <typename iter_t, typename Item = typename iter_t::__item_t__>
65
struct hb_iter_t
66
{
B
Behdad Esfahbod 已提交
67
  typedef Item item_t;
68
  static constexpr unsigned item_size = hb_static_size (Item);
69 70 71
  static constexpr bool is_iterator = true;
  static constexpr bool is_random_access_iterator = false;
  static constexpr bool is_sorted_iterator = false;
72

73 74
  private:
  /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
75 76
  const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
        iter_t* thiz ()       { return static_cast<      iter_t *> (this); }
77
  public:
78

B
Behdad Esfahbod 已提交
79 80
  /* TODO:
   * Port operators below to use hb_enable_if to sniff which method implements
B
Behdad Esfahbod 已提交
81
   * an operator and use it, and remove hb_iter_fallback_mixin_t completely. */
B
Behdad Esfahbod 已提交
82

83
  /* Operators. */
84 85
  iter_t iter () const { return *thiz(); }
  iter_t operator + () const { return *thiz(); }
86 87
  iter_t begin () const { return *thiz(); }
  iter_t end () const { return thiz()->__end__ (); }
88 89
  explicit operator bool () const { return thiz()->__more__ (); }
  unsigned len () const { return thiz()->__len__ (); }
B
Behdad Esfahbod 已提交
90
  /* The following can only be enabled if item_t is reference type.  Otherwise
91 92
   * it will be returning pointer to temporary rvalue.
   * TODO Use a wrapper return type to fix for non-reference type. */
B
Behdad Esfahbod 已提交
93 94
  template <typename T = item_t,
	    hb_enable_if (hb_is_reference (T))>
95
  hb_remove_reference<item_t>* operator -> () const { return hb_addressof (**thiz()); }
96 97 98 99
  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); }
100 101
  iter_t& operator += (unsigned count) &  { thiz()->__forward__ (count); return *thiz(); }
  iter_t  operator += (unsigned count) && { thiz()->__forward__ (count); return *thiz(); }
102 103
  iter_t& operator ++ () &  { thiz()->__next__ (); return *thiz(); }
  iter_t  operator ++ () && { thiz()->__next__ (); return *thiz(); }
104 105
  iter_t& operator -= (unsigned count) &  { thiz()->__rewind__ (count); return *thiz(); }
  iter_t  operator -= (unsigned count) && { thiz()->__rewind__ (count); return *thiz(); }
106 107
  iter_t& operator -- () &  { thiz()->__prev__ (); return *thiz(); }
  iter_t  operator -- () && { thiz()->__prev__ (); return *thiz(); }
108 109 110 111 112
  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; }
113
  template <typename T>
114
  iter_t& operator >> (T &v) &  { v = **thiz(); ++*thiz(); return *thiz(); }
115
  template <typename T>
116 117 118 119 120
  iter_t  operator >> (T &v) && { v = **thiz(); ++*thiz(); return *thiz(); }
  template <typename T>
  iter_t& operator << (const T v) &  { **thiz() = v; ++*thiz(); return *thiz(); }
  template <typename T>
  iter_t  operator << (const T v) && { **thiz() = v; ++*thiz(); return *thiz(); }
121

122
  protected:
123 124 125
  hb_iter_t () {}
  hb_iter_t (const hb_iter_t &o HB_UNUSED) {}
  void operator = (const hb_iter_t &o HB_UNUSED) {}
126 127
};

128
#define HB_ITER_USING(Name) \
129
  using item_t = typename Name::item_t; \
130 131
  using Name::begin; \
  using Name::end; \
132
  using Name::item_size; \
133
  using Name::is_iterator; \
134 135 136 137 138 139 140 141 142 143
  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 已提交
144
  using Name::operator +; \
145
  using Name::operator -; \
B
Behdad Esfahbod 已提交
146 147
  using Name::operator >>; \
  using Name::operator <<; \
148 149
  static_assert (true, "")

150 151 152 153 154
/* Returns iterator / item type of a type. */
template <typename Iterable>
using hb_iter_type = decltype (hb_deref (hb_declval (Iterable)).iter ());
template <typename Iterable>
using hb_item_type = decltype (*hb_deref (hb_declval (Iterable)).iter ());
B
Behdad Esfahbod 已提交
155

156 157

template <typename> struct hb_array_t;
158

B
Behdad Esfahbod 已提交
159
struct
160
{
161
  template <typename T> hb_iter_type<T>
162
  operator () (T&& c) const
163
  { return hb_deref (hb_forward<T> (c)).iter (); }
164 165 166 167

  /* Specialization for C arrays. */

  template <typename Type> inline hb_array_t<Type>
168
  operator () (Type *array, unsigned int length) const
169 170 171
  { return hb_array_t<Type> (array, length); }

  template <typename Type, unsigned int length> hb_array_t<Type>
172
  operator () (Type (&array)[length]) const
173 174
  { return hb_array_t<Type> (array, length); }

B
Behdad Esfahbod 已提交
175 176
}
HB_FUNCOBJ (hb_iter);
177

178
/* Mixin to fill in what the subclass doesn't provide. */
179
template <typename iter_t, typename item_t = typename iter_t::__item_t__>
B
Behdad Esfahbod 已提交
180
struct hb_iter_fallback_mixin_t
181 182 183
{
  private:
  /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
184 185
  const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
        iter_t* thiz ()       { return static_cast<      iter_t *> (this); }
186
  public:
187

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

192
  /* Termination: Implement __more__(), or __len__() if random-access. */
B
Behdad Esfahbod 已提交
193
  bool __more__ () const { return bool (thiz()->len ()); }
194
  unsigned __len__ () const
195
  { iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; }
196 197

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

B
Behdad Esfahbod 已提交
201
  /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
202 203
  void __prev__ () { *thiz() -= 1; }
  void __rewind__ (unsigned n) { while (n--) --*thiz(); }
B
Behdad Esfahbod 已提交
204

205 206 207 208 209 210 211 212 213 214 215 216
  /* 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;
  }

B
Behdad Esfahbod 已提交
217
  protected:
218 219 220
  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) {}
221 222
};

223 224 225 226 227 228
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:
229 230
  hb_iter_with_fallback_t () {}
  hb_iter_with_fallback_t (const hb_iter_with_fallback_t &o HB_UNUSED) :
B
Behdad Esfahbod 已提交
231 232
    hb_iter_t<iter_t, item_t> (o),
    hb_iter_fallback_mixin_t<iter_t, item_t> (o) {}
233
  void operator = (const hb_iter_with_fallback_t &o HB_UNUSED) {}
234 235
};

B
Behdad Esfahbod 已提交
236 237 238 239
/*
 * Meta-programming predicates.
 */

B
Behdad Esfahbod 已提交
240 241 242 243 244 245
/* hb_is_iterator() / hb_is_iterator_of() */

template<typename Iter, typename Item>
struct hb_is_iterator_of
{
  template <typename Item2 = Item>
246 247
  static hb_true_type impl (hb_priority<2>, hb_iter_t<Iter, hb_type_identity<Item2>> *);
  static hb_false_type impl (hb_priority<0>, const void *);
B
Behdad Esfahbod 已提交
248 249 250 251 252 253 254

  public:
  static constexpr bool value = decltype (impl (hb_prioritize, hb_declval (Iter*)))::value;
};
#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)

255 256
/* hb_is_iterable() */

257 258 259 260
template <typename T>
struct hb_is_iterable
{
  private:
B
Minor  
Behdad Esfahbod 已提交
261

262
  template <typename U>
263
  static auto impl (hb_priority<1>) -> decltype (hb_declval (U).iter (), hb_true_type ());
B
Minor  
Behdad Esfahbod 已提交
264

265
  template <typename>
266
  static hb_false_type impl (hb_priority<0>);
267 268

  public:
B
Behdad Esfahbod 已提交
269
  static constexpr bool value = decltype (impl<T> (hb_prioritize))::value;
270
};
B
Behdad Esfahbod 已提交
271
#define hb_is_iterable(Iterable) hb_is_iterable<Iterable>::value
272

B
Behdad Esfahbod 已提交
273
/* hb_is_source_of() / hb_is_sink_of() */
B
Behdad Esfahbod 已提交
274

B
Behdad Esfahbod 已提交
275 276 277 278 279
template<typename Iter, typename Item>
struct hb_is_source_of
{
  private:
  template <typename Iter2 = Iter,
280
	    hb_enable_if (hb_is_convertible (typename Iter2::item_t, hb_add_lvalue_reference<hb_add_const<Item>>))>
281
  static hb_true_type impl (hb_priority<2>);
282
  template <typename Iter2 = Iter>
283 284
  static auto impl (hb_priority<1>) -> decltype (hb_declval (Iter2) >> hb_declval (Item &), hb_true_type ());
  static hb_false_type impl (hb_priority<0>);
285

B
Behdad Esfahbod 已提交
286 287 288 289
  public:
  static constexpr bool value = decltype (impl (hb_prioritize))::value;
};
#define hb_is_source_of(Iter, Item) hb_is_source_of<Iter, Item>::value
290

291
template<typename Iter, typename Item>
B
Behdad Esfahbod 已提交
292 293 294
struct hb_is_sink_of
{
  private:
295
  template <typename Iter2 = Iter,
296
	    hb_enable_if (hb_is_convertible (typename Iter2::item_t, hb_add_lvalue_reference<Item>))>
297
  static hb_true_type impl (hb_priority<2>);
298
  template <typename Iter2 = Iter>
299 300
  static auto impl (hb_priority<1>) -> decltype (hb_declval (Iter2) << hb_declval (Item), hb_true_type ());
  static hb_false_type impl (hb_priority<0>);
301

B
Behdad Esfahbod 已提交
302 303 304 305
  public:
  static constexpr bool value = decltype (impl (hb_prioritize))::value;
};
#define hb_is_sink_of(Iter, Item) hb_is_sink_of<Iter, Item>::value
306

B
Behdad Esfahbod 已提交
307 308 309
/* This is commonly used, so define: */
#define hb_is_sorted_source_of(Iter, Item) \
	(hb_is_source_of(Iter, Item) && Iter::is_sorted_iterator)
310

B
Behdad Esfahbod 已提交
311

312 313 314
/* Range-based 'for' for iterables. */

template <typename Iterable,
315
	  hb_requires (hb_is_iterable (Iterable))>
316 317 318
static inline auto begin (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).begin ())

template <typename Iterable,
319
	  hb_requires (hb_is_iterable (Iterable))>
320 321 322 323 324 325 326
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 <typename Iterable,
327
	  hb_requires (hb_is_iterable (Iterable))>
328 329 330
static inline auto begin (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).begin ())

template <typename Iterable,
331
	  hb_requires (hb_is_iterable (Iterable))>
332 333 334 335 336
static inline auto end (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).end ())

}


B
Behdad Esfahbod 已提交
337
/*
B
Behdad Esfahbod 已提交
338
 * Adaptors, combiners, etc.
B
Behdad Esfahbod 已提交
339
 */
340

341
template <typename Lhs, typename Rhs,
342
	  hb_requires (hb_is_iterator (Lhs))>
B
Behdad Esfahbod 已提交
343
static inline auto
344
operator | (Lhs&& lhs, Rhs&& rhs) HB_AUTO_RETURN (hb_forward<Rhs> (rhs) (hb_forward<Lhs> (lhs)))
345

B
Behdad Esfahbod 已提交
346 347 348
/* hb_map(), hb_filter(), hb_reduce() */

template <typename Iter, typename Proj,
349
	 hb_requires (hb_is_iterator (Iter))>
B
Behdad Esfahbod 已提交
350
struct hb_map_iter_t :
B
Behdad Esfahbod 已提交
351
  hb_iter_t<hb_map_iter_t<Iter, Proj>,
352
	    decltype (hb_get (hb_declval (Proj), *hb_declval (Iter)))>
B
Behdad Esfahbod 已提交
353
{
354
  hb_map_iter_t (const Iter& it, Proj f_) : it (it), f (f_) {}
B
Behdad Esfahbod 已提交
355

356
  typedef decltype (hb_get (hb_declval (Proj), *hb_declval (Iter))) __item_t__;
357
  static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator;
358 359
  __item_t__ __item__ () const { return hb_get (f.get (), *it); }
  __item_t__ __item_at__ (unsigned i) const { return hb_get (f.get (), it[i]); }
360
  bool __more__ () const { return bool (it); }
B
Behdad Esfahbod 已提交
361 362 363 364 365
  unsigned __len__ () const { return it.len (); }
  void __next__ () { ++it; }
  void __forward__ (unsigned n) { it += n; }
  void __prev__ () { --it; }
  void __rewind__ (unsigned n) { it -= n; }
B
Behdad Esfahbod 已提交
366
  hb_map_iter_t __end__ () const { return hb_map_iter_t (it.end (), f); }
367 368
  bool operator != (const hb_map_iter_t& o) const
  { return it != o.it || f != o.f; }
B
Behdad Esfahbod 已提交
369 370 371

  private:
  Iter it;
372
  hb_reference_wrapper<Proj> f;
B
Behdad Esfahbod 已提交
373
};
374

B
Behdad Esfahbod 已提交
375 376 377
template <typename Proj>
struct hb_map_iter_factory_t
{
378
  hb_map_iter_factory_t (Proj f) : f (f) {}
B
Behdad Esfahbod 已提交
379

380
  template <typename Iter,
381
	    hb_requires (hb_is_iterator (Iter))>
382
  hb_map_iter_t<Iter, Proj>
383
  operator () (Iter it)
384
  { return hb_map_iter_t<Iter, Proj> (it, f); }
B
Behdad Esfahbod 已提交
385 386 387 388

  private:
  Proj f;
};
B
Behdad Esfahbod 已提交
389
struct
390 391 392 393 394
{
  template <typename Proj>
  hb_map_iter_factory_t<Proj>
  operator () (Proj&& f) const
  { return hb_map_iter_factory_t<Proj> (f); }
B
Behdad Esfahbod 已提交
395 396
}
HB_FUNCOBJ (hb_map);
B
Behdad Esfahbod 已提交
397

B
Behdad Esfahbod 已提交
398
template <typename Iter, typename Pred, typename Proj,
399
	 hb_requires (hb_is_iterator (Iter))>
B
Behdad Esfahbod 已提交
400
struct hb_filter_iter_t :
401 402
  hb_iter_with_fallback_t<hb_filter_iter_t<Iter, Pred, Proj>,
			  typename Iter::item_t>
B
Behdad Esfahbod 已提交
403
{
404 405
  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; }
B
Behdad Esfahbod 已提交
406 407

  typedef typename Iter::item_t __item_t__;
408
  static constexpr bool is_sorted_iterator = Iter::is_sorted_iterator;
B
Behdad Esfahbod 已提交
409
  __item_t__ __item__ () const { return *it; }
410
  bool __more__ () const { return bool (it); }
411
  void __next__ () { do ++it; while (it && !hb_has (p.get (), hb_get (f.get (), *it))); }
B
Behdad Esfahbod 已提交
412
  void __prev__ () { --it; }
B
Behdad Esfahbod 已提交
413
  hb_filter_iter_t __end__ () const { return hb_filter_iter_t (it.end (), p, f); }
414 415
  bool operator != (const hb_filter_iter_t& o) const
  { return it != o.it || p != o.p || f != o.f; }
B
Behdad Esfahbod 已提交
416 417 418

  private:
  Iter it;
419 420
  hb_reference_wrapper<Pred> p;
  hb_reference_wrapper<Proj> f;
B
Behdad Esfahbod 已提交
421 422 423 424
};
template <typename Pred, typename Proj>
struct hb_filter_iter_factory_t
{
425
  hb_filter_iter_factory_t (Pred p, Proj f) : p (p), f (f) {}
B
Behdad Esfahbod 已提交
426

427
  template <typename Iter,
428
	    hb_requires (hb_is_iterator (Iter))>
429
  hb_filter_iter_t<Iter, Pred, Proj>
430
  operator () (Iter it)
431
  { return hb_filter_iter_t<Iter, Pred, Proj> (it, p, f); }
B
Behdad Esfahbod 已提交
432 433 434 435 436

  private:
  Pred p;
  Proj f;
};
B
Behdad Esfahbod 已提交
437
struct
438
{
439
  template <typename Pred = decltype ((hb_identity)),
440 441
	    typename Proj = decltype ((hb_identity))>
  hb_filter_iter_factory_t<Pred, Proj>
442
  operator () (Pred&& p = hb_identity, Proj&& f = hb_identity) const
443
  { return hb_filter_iter_factory_t<Pred, Proj> (p, f); }
B
Behdad Esfahbod 已提交
444 445
}
HB_FUNCOBJ (hb_filter);
446

447
template <typename Redu, typename InitT>
E
Ebrahim Byagowi 已提交
448 449
struct hb_reduce_t
{
450
  hb_reduce_t (Redu r, InitT init_value) : r (r), init_value (init_value) {}
E
Ebrahim Byagowi 已提交
451 452

  template <typename Iter,
453
	    hb_requires (hb_is_iterator (Iter)),
454 455
	    typename AccuT = decltype (hb_declval (Redu) (hb_declval (InitT), hb_declval (typename Iter::item_t)))>
  AccuT
456
  operator () (Iter it)
E
Ebrahim Byagowi 已提交
457
  {
458
    AccuT value = init_value;
E
Ebrahim Byagowi 已提交
459
    for (; it; ++it)
460
      value = r (value, *it);
E
Ebrahim Byagowi 已提交
461 462 463 464 465
    return value;
  }

  private:
  Redu r;
466
  InitT init_value;
E
Ebrahim Byagowi 已提交
467
};
B
Behdad Esfahbod 已提交
468
struct
E
Ebrahim Byagowi 已提交
469
{
470 471 472 473
  template <typename Redu, typename InitT>
  hb_reduce_t<Redu, InitT>
  operator () (Redu&& r, InitT init_value) const
  { return hb_reduce_t<Redu, InitT> (r, init_value); }
B
Behdad Esfahbod 已提交
474 475
}
HB_FUNCOBJ (hb_reduce);
E
Ebrahim Byagowi 已提交
476 477


B
Behdad Esfahbod 已提交
478
/* hb_zip() */
479

B
Behdad Esfahbod 已提交
480
template <typename A, typename B>
481
struct hb_zip_iter_t :
B
Behdad Esfahbod 已提交
482
  hb_iter_t<hb_zip_iter_t<A, B>,
483
	    hb_pair_t<typename A::item_t, typename B::item_t>>
B
Behdad Esfahbod 已提交
484
{
485
  hb_zip_iter_t () {}
486
  hb_zip_iter_t (const A& a, const B& b) : a (a), b (b) {}
B
Behdad Esfahbod 已提交
487 488

  typedef hb_pair_t<typename A::item_t, typename B::item_t> __item_t__;
489
  static constexpr bool is_random_access_iterator =
B
Behdad Esfahbod 已提交
490 491
    A::is_random_access_iterator &&
    B::is_random_access_iterator;
492
  static constexpr bool is_sorted_iterator =
B
Behdad Esfahbod 已提交
493 494
    A::is_sorted_iterator &&
    B::is_sorted_iterator;
B
Behdad Esfahbod 已提交
495 496
  __item_t__ __item__ () const { return __item_t__ (*a, *b); }
  __item_t__ __item_at__ (unsigned i) const { return __item_t__ (a[i], b[i]); }
B
Behdad Esfahbod 已提交
497
  bool __more__ () const { return bool (a) && bool (b); }
498
  unsigned __len__ () const { return hb_min (a.len (), b.len ()); }
B
Behdad Esfahbod 已提交
499 500 501 502
  void __next__ () { ++a; ++b; }
  void __forward__ (unsigned n) { a += n; b += n; }
  void __prev__ () { --a; --b; }
  void __rewind__ (unsigned n) { a -= n; b -= n; }
503 504
  hb_zip_iter_t __end__ () const { return hb_zip_iter_t (a.end (), b.end ()); }
  bool operator != (const hb_zip_iter_t& o) const
505
  { return a != o.a && b != o.b; }
B
Behdad Esfahbod 已提交
506 507 508 509 510

  private:
  A a;
  B b;
};
B
Behdad Esfahbod 已提交
511
struct
512 513
{
  template <typename A, typename B,
514
	    hb_requires (hb_is_iterable (A) && hb_is_iterable (B))>
515
  hb_zip_iter_t<hb_iter_type<A>, hb_iter_type<B>>
516
  operator () (A&& a, B&& b) const
517
  { return hb_zip_iter_t<hb_iter_type<A>, hb_iter_type<B>> (hb_iter (a), hb_iter (b)); }
B
Behdad Esfahbod 已提交
518 519
}
HB_FUNCOBJ (hb_zip);
B
Behdad Esfahbod 已提交
520

B
Behdad Esfahbod 已提交
521 522 523 524 525
/* hb_apply() */

template <typename Appl>
struct hb_apply_t
{
526
  hb_apply_t (Appl a) : a (a) {}
B
Behdad Esfahbod 已提交
527 528

  template <typename Iter,
529
	    hb_requires (hb_is_iterator (Iter))>
530
  void operator () (Iter it)
B
Behdad Esfahbod 已提交
531 532
  {
    for (; it; ++it)
B
Behdad Esfahbod 已提交
533
      (void) hb_invoke (a, *it);
B
Behdad Esfahbod 已提交
534 535 536 537 538
  }

  private:
  Appl a;
};
B
Behdad Esfahbod 已提交
539
struct
B
Behdad Esfahbod 已提交
540 541 542 543 544 545 546 547
{
  template <typename Appl> hb_apply_t<Appl>
  operator () (Appl&& a) const
  { return hb_apply_t<Appl> (a); }

  template <typename Appl> hb_apply_t<Appl&>
  operator () (Appl *a) const
  { return hb_apply_t<Appl&> (*a); }
B
Behdad Esfahbod 已提交
548 549
}
HB_FUNCOBJ (hb_apply);
B
Behdad Esfahbod 已提交
550

B
Behdad Esfahbod 已提交
551
/* hb_iota()/hb_range() */
B
Behdad Esfahbod 已提交
552 553

template <typename T, typename S>
B
Behdad Esfahbod 已提交
554 555
struct hb_counter_iter_t :
  hb_iter_t<hb_counter_iter_t<T, S>, T>
B
Behdad Esfahbod 已提交
556
{
B
Behdad Esfahbod 已提交
557
  hb_counter_iter_t (T start, T end_, S step) : v (start), end_ (end_for (start, end_, step)), step (step) {}
B
Behdad Esfahbod 已提交
558 559 560 561

  typedef T __item_t__;
  static constexpr bool is_random_access_iterator = true;
  static constexpr bool is_sorted_iterator = true;
B
Behdad Esfahbod 已提交
562
  __item_t__ __item__ () const { return +v; }
B
Behdad Esfahbod 已提交
563
  __item_t__ __item_at__ (unsigned j) const { return v + j * step; }
564
  bool __more__ () const { return v != end_; }
565
  unsigned __len__ () const { return !step ? UINT_MAX : (end_ - v) / step; }
B
Behdad Esfahbod 已提交
566 567 568 569
  void __next__ () { v += step; }
  void __forward__ (unsigned n) { v += n * step; }
  void __prev__ () { v -= step; }
  void __rewind__ (unsigned n) { v -= n * step; }
B
Behdad Esfahbod 已提交
570
  hb_counter_iter_t __end__ () const { return hb_counter_iter_t (end_, end_, step); }
B
Behdad Esfahbod 已提交
571
  bool operator != (const hb_counter_iter_t& o) const
572
  { return v != o.v || end_ != o.end_ || step != o.step; }
B
Behdad Esfahbod 已提交
573 574

  private:
B
Behdad Esfahbod 已提交
575
  static inline T end_for (T start, T end_, S step)
B
Behdad Esfahbod 已提交
576
  {
B
Behdad Esfahbod 已提交
577 578
    if (!step)
      return end_;
579
    auto res = (end_ - start) % step;
B
Behdad Esfahbod 已提交
580
    if (!res)
581 582 583
      return end_;
    end_ += step - res;
    return end_;
B
Behdad Esfahbod 已提交
584 585 586 587
  }

  private:
  T v;
588
  T end_;
B
Behdad Esfahbod 已提交
589 590 591 592
  S step;
};
struct
{
B
Behdad Esfahbod 已提交
593 594
  template <typename T = unsigned, typename S = unsigned> hb_counter_iter_t<T, S>
  operator () (T start = 0u, S&& step = 1u) const
595
  { return hb_counter_iter_t<T, S> (start, step >= 0 ? hb_int_max (T) : hb_int_min (T), step); }
B
Behdad Esfahbod 已提交
596 597 598 599 600
}
HB_FUNCOBJ (hb_iota);
struct
{
  template <typename T = unsigned> hb_counter_iter_t<T, unsigned>
B
Behdad Esfahbod 已提交
601
  operator () (T end = (unsigned) -1) const
B
Behdad Esfahbod 已提交
602
  { return hb_counter_iter_t<T, unsigned> (0, end, 1u); }
B
Behdad Esfahbod 已提交
603

B
Behdad Esfahbod 已提交
604
  template <typename T, typename S = unsigned> hb_counter_iter_t<T, S>
B
Behdad Esfahbod 已提交
605
  operator () (T start, T end, S&& step = 1u) const
B
Behdad Esfahbod 已提交
606
  { return hb_counter_iter_t<T, S> (start, end, step); }
B
Behdad Esfahbod 已提交
607
}
B
Behdad Esfahbod 已提交
608
HB_FUNCOBJ (hb_range);
B
Behdad Esfahbod 已提交
609

610 611 612 613 614 615 616 617 618 619 620 621
/* hb_enumerate */

struct
{
  template <typename Iterable,
	    typename Index = unsigned,
	    hb_requires (hb_is_iterable (Iterable))>
  auto operator () (Iterable&& it, Index start = 0u) const HB_AUTO_RETURN
  ( hb_zip (hb_iota (start), it) )
}
HB_FUNCOBJ (hb_enumerate);

B
Behdad Esfahbod 已提交
622

B
Behdad Esfahbod 已提交
623 624 625 626 627
/* hb_sink() */

template <typename Sink>
struct hb_sink_t
{
628
  hb_sink_t (Sink s) : s (s) {}
B
Behdad Esfahbod 已提交
629

630
  template <typename Iter,
631
	    hb_requires (hb_is_iterator (Iter))>
632
  void operator () (Iter it)
B
Behdad Esfahbod 已提交
633
  {
634
    for (; it; ++it)
B
Behdad Esfahbod 已提交
635 636 637 638 639 640
      s << *it;
  }

  private:
  Sink s;
};
B
Behdad Esfahbod 已提交
641
struct
642 643 644 645
{
  template <typename Sink> hb_sink_t<Sink>
  operator () (Sink&& s) const
  { return hb_sink_t<Sink> (s); }
646 647 648 649

  template <typename Sink> hb_sink_t<Sink&>
  operator () (Sink *s) const
  { return hb_sink_t<Sink&> (*s); }
B
Behdad Esfahbod 已提交
650 651
}
HB_FUNCOBJ (hb_sink);
B
Behdad Esfahbod 已提交
652

B
Behdad Esfahbod 已提交
653 654
/* hb-drain: hb_sink to void / blackhole / /dev/null. */

B
Behdad Esfahbod 已提交
655
struct
B
Behdad Esfahbod 已提交
656 657
{
  template <typename Iter,
658 659
	    hb_requires (hb_is_iterator (Iter))>
  void operator () (Iter it) const
B
Behdad Esfahbod 已提交
660 661 662 663
  {
    for (; it; ++it)
      (void) *it;
  }
B
Behdad Esfahbod 已提交
664 665
}
HB_FUNCOBJ (hb_drain);
B
Behdad Esfahbod 已提交
666

B
Behdad Esfahbod 已提交
667 668 669 670 671
/* hb_unzip(): unzip and sink to two sinks. */

template <typename Sink1, typename Sink2>
struct hb_unzip_t
{
672
  hb_unzip_t (Sink1 s1, Sink2 s2) : s1 (s1), s2 (s2) {}
B
Behdad Esfahbod 已提交
673 674

  template <typename Iter,
675
	    hb_requires (hb_is_iterator (Iter))>
676
  void operator () (Iter it)
B
Behdad Esfahbod 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689
  {
    for (; it; ++it)
    {
      const auto &v = *it;
      s1 << v.first;
      s2 << v.second;
    }
  }

  private:
  Sink1 s1;
  Sink2 s2;
};
B
Behdad Esfahbod 已提交
690
struct
B
Behdad Esfahbod 已提交
691 692 693 694 695 696 697 698
{
  template <typename Sink1, typename Sink2> hb_unzip_t<Sink1, Sink2>
  operator () (Sink1&& s1, Sink2&& s2) const
  { return hb_unzip_t<Sink1, Sink2> (s1, s2); }

  template <typename Sink1, typename Sink2> hb_unzip_t<Sink1&, Sink2&>
  operator () (Sink1 *s1, Sink2 *s2) const
  { return hb_unzip_t<Sink1&, Sink2&> (*s1, *s2); }
B
Behdad Esfahbod 已提交
699 700
}
HB_FUNCOBJ (hb_unzip);
B
Behdad Esfahbod 已提交
701 702


703 704
/* hb-all, hb-any, hb-none. */

B
Behdad Esfahbod 已提交
705
struct
706 707
{
  template <typename Iterable,
708
	    typename Pred = decltype ((hb_identity)),
709
	    typename Proj = decltype ((hb_identity)),
710
	    hb_requires (hb_is_iterable (Iterable))>
711
  bool operator () (Iterable&& c,
712
		    Pred&& p = hb_identity,
713
		    Proj&& f = hb_identity) const
714 715
  {
    for (auto it = hb_iter (c); it; ++it)
B
Behdad Esfahbod 已提交
716
      if (!hb_match (hb_forward<Pred> (p), hb_get (hb_forward<Proj> (f), *it)))
717 718 719
	return false;
    return true;
  }
B
Behdad Esfahbod 已提交
720 721
}
HB_FUNCOBJ (hb_all);
B
Behdad Esfahbod 已提交
722
struct
723 724
{
  template <typename Iterable,
725
	    typename Pred = decltype ((hb_identity)),
726
	    typename Proj = decltype ((hb_identity)),
727
	    hb_requires (hb_is_iterable (Iterable))>
728
  bool operator () (Iterable&& c,
729
		    Pred&& p = hb_identity,
730
		    Proj&& f = hb_identity) const
731 732
  {
    for (auto it = hb_iter (c); it; ++it)
B
Behdad Esfahbod 已提交
733
      if (hb_match (hb_forward<Pred> (p), hb_get (hb_forward<Proj> (f), *it)))
734 735 736
	return true;
    return false;
  }
B
Behdad Esfahbod 已提交
737 738
}
HB_FUNCOBJ (hb_any);
B
Behdad Esfahbod 已提交
739
struct
740 741
{
  template <typename Iterable,
742
	    typename Pred = decltype ((hb_identity)),
743
	    typename Proj = decltype ((hb_identity)),
744
	    hb_requires (hb_is_iterable (Iterable))>
745
  bool operator () (Iterable&& c,
746
		    Pred&& p = hb_identity,
747
		    Proj&& f = hb_identity) const
748 749
  {
    for (auto it = hb_iter (c); it; ++it)
B
Behdad Esfahbod 已提交
750
      if (hb_match (hb_forward<Pred> (p), hb_get (hb_forward<Proj> (f), *it)))
751 752 753
	return false;
    return true;
  }
B
Behdad Esfahbod 已提交
754 755
}
HB_FUNCOBJ (hb_none);
756

B
Behdad Esfahbod 已提交
757 758 759 760 761
/*
 * Algorithms operating on iterators.
 */

template <typename C, typename V,
762
	  hb_requires (hb_is_iterable (C))>
B
Behdad Esfahbod 已提交
763 764 765
inline void
hb_fill (C& c, const V &v)
{
B
Behdad Esfahbod 已提交
766
  for (auto i = hb_iter (c); i; i++)
B
Behdad Esfahbod 已提交
767
    *i = v;
B
Behdad Esfahbod 已提交
768 769
}

770 771
template <typename S, typename D>
inline void
B
Behdad Esfahbod 已提交
772
hb_copy (S&& is, D&& id)
B
Behdad Esfahbod 已提交
773
{
B
Behdad Esfahbod 已提交
774
  hb_iter (is) | hb_sink (id);
B
Behdad Esfahbod 已提交
775 776 777
}


778
#endif /* HB_ITER_HH */