hb-iter.hh 18.1 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 45 46
 * returns rvalues.
 */

B
Behdad Esfahbod 已提交
47 48 49 50 51

/*
 * Base classes for iterators.
 */

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

62 63
  private:
  /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
64 65
  const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
        iter_t* thiz ()       { return static_cast<      iter_t *> (this); }
66
  public:
67

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

72
  /* Operators. */
73 74 75 76
  iter_t iter () const { return *thiz(); }
  iter_t operator + () const { return *thiz(); }
  explicit operator bool () const { return thiz()->__more__ (); }
  unsigned len () const { return thiz()->__len__ (); }
B
Behdad Esfahbod 已提交
77 78 79 80
  /* 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))>
81
  hb_remove_reference<item_t>* operator -> () const { return hb_addressof (**thiz()); }
82 83 84 85 86 87 88 89 90 91 92 93 94
  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); }
  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(); }
  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; }
95
  template <typename T>
96
  iter_t& operator >> (T &v) { v = **thiz(); ++*thiz(); return *thiz(); }
97
  template <typename T>
98
  iter_t& operator >> (T &v) const { v = **thiz(); ++*thiz(); return *thiz(); }
B
Behdad Esfahbod 已提交
99
  template <typename T>
100
  iter_t& operator << (const T v) { **thiz() = v; ++*thiz(); return *thiz(); }
101

102
  protected:
103 104 105
  hb_iter_t () {}
  hb_iter_t (const hb_iter_t &o HB_UNUSED) {}
  void operator = (const hb_iter_t &o HB_UNUSED) {}
106 107
};

108
#define HB_ITER_USING(Name) \
109
  using item_t = typename Name::item_t; \
110
  using Name::item_size; \
111
  using Name::is_iterator; \
112 113 114 115 116 117 118 119 120 121
  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 已提交
122
  using Name::operator +; \
123
  using Name::operator -; \
B
Behdad Esfahbod 已提交
124 125
  using Name::operator >>; \
  using Name::operator <<; \
126 127
  static_assert (true, "")

B
Behdad Esfahbod 已提交
128 129 130
/* Returns iterator type of a type. */
#define hb_iter_t(Iterable) decltype (hb_declval (Iterable).iter ())

131 132

template <typename> struct hb_array_t;
133

B
Behdad Esfahbod 已提交
134
struct
135 136 137
{
  template <typename T>
  hb_iter_t (T)
138
  operator () (T&& c) const
139 140 141 142 143
  { return c.iter (); }

  /* Specialization for C arrays. */

  template <typename Type> inline hb_array_t<Type>
144
  operator () (Type *array, unsigned int length) const
145 146 147
  { return hb_array_t<Type> (array, length); }

  template <typename Type, unsigned int length> hb_array_t<Type>
148
  operator () (Type (&array)[length]) const
149 150
  { return hb_array_t<Type> (array, length); }

B
Behdad Esfahbod 已提交
151
} HB_FUNCOBJ (hb_iter);
152 153


154
/* Mixin to fill in what the subclass doesn't provide. */
155
template <typename iter_t, typename item_t = typename iter_t::__item_t__>
B
Behdad Esfahbod 已提交
156
struct hb_iter_fallback_mixin_t
157 158 159
{
  private:
  /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
160 161
  const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
        iter_t* thiz ()       { return static_cast<      iter_t *> (this); }
162
  public:
163

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

168
  /* Termination: Implement __more__(), or __len__() if random-access. */
169 170
  bool __more__ () const { return thiz()->len (); }
  unsigned __len__ () const
171
  { iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; }
172 173

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

B
Behdad Esfahbod 已提交
177
  /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
178 179
  void __prev__ () { *thiz() -= 1; }
  void __rewind__ (unsigned n) { while (n--) --*thiz(); }
B
Behdad Esfahbod 已提交
180

B
Behdad Esfahbod 已提交
181
  protected:
182 183 184
  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) {}
185 186
};

187 188 189 190 191 192
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:
193 194
  hb_iter_with_fallback_t () {}
  hb_iter_with_fallback_t (const hb_iter_with_fallback_t &o HB_UNUSED) :
B
Behdad Esfahbod 已提交
195 196
    hb_iter_t<iter_t, item_t> (o),
    hb_iter_fallback_mixin_t<iter_t, item_t> (o) {}
197
  void operator = (const hb_iter_with_fallback_t &o HB_UNUSED) {}
198 199
};

B
Behdad Esfahbod 已提交
200 201 202 203
/*
 * Meta-programming predicates.
 */

204 205
/* hb_is_iterable() */

206 207 208 209
template <typename T>
struct hb_is_iterable
{
  private:
B
Minor  
Behdad Esfahbod 已提交
210

211
  template <typename U>
B
Minor  
Behdad Esfahbod 已提交
212 213
  static auto impl (hb_priority<1>) -> decltype (hb_declval (U).iter (), hb_true_t ());

214
  template <typename>
B
Minor  
Behdad Esfahbod 已提交
215
  static hb_false_t impl (hb_priority<0>);
216 217

  public:
B
Minor  
Behdad Esfahbod 已提交
218 219

  enum { value = decltype (impl<T> (hb_prioritize))::value };
220
};
B
Behdad Esfahbod 已提交
221
#define hb_is_iterable(Iterable) hb_is_iterable<Iterable>::value
222

B
Behdad Esfahbod 已提交
223 224 225
/* TODO Add hb_is_iterable_of().
 * TODO Add random_access / sorted variants. */

226
/* hb_is_iterator() / hb_is_random_access_iterator() / hb_is_sorted_iterator() */
227

228 229 230 231 232 233 234 235
template <typename Iter, typename Item>
static inline char _hb_is_iterator_of (hb_priority<0>, const void *) { return 0; }
template <typename Iter,
	  typename Item,
	  typename Item2 = typename Iter::item_t,
	  hb_enable_if (hb_is_cr_convertible_to (Item2, Item))>
static inline int _hb_is_iterator_of (hb_priority<2>, hb_iter_t<Iter, Item2> *) { return 0; }

236
template<typename Iter, typename Item>
237
struct hb_is_iterator_of { enum {
238
  value = sizeof (int) == sizeof (_hb_is_iterator_of<Iter, Item> (hb_prioritize, hb_declval (Iter*))) }; };
239 240
#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)
241

242 243 244 245 246
#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)

247
#define hb_is_sorted_iterator_of(Iter, Item) \
248
  hb_is_iterator_of (Iter, Item) && Iter::is_sorted_iterator
249 250
#define hb_is_sorted_iterator(Iter) \
  hb_is_sorted_iterator_of (Iter, typename Iter::item_t)
251

B
Behdad Esfahbod 已提交
252

B
Behdad Esfahbod 已提交
253
/*
B
Behdad Esfahbod 已提交
254
 * Adaptors, combiners, etc.
B
Behdad Esfahbod 已提交
255
 */
256

257 258
template <typename Lhs, typename Rhs,
	  hb_enable_if (hb_is_iterator (Lhs))>
B
Behdad Esfahbod 已提交
259 260
static inline auto
operator | (Lhs lhs, const Rhs &rhs) HB_AUTO_RETURN (rhs (lhs))
261

B
Behdad Esfahbod 已提交
262 263 264 265 266
/* 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 已提交
267 268
  hb_iter_t<hb_map_iter_t<Iter, Proj>,
	    decltype (hb_declval (Proj) (hb_declval (typename Iter::item_t)))>
B
Behdad Esfahbod 已提交
269
{
B
Behdad Esfahbod 已提交
270
  hb_map_iter_t (const Iter& it, Proj f) : it (it), f (f) {}
B
Behdad Esfahbod 已提交
271

272
  typedef decltype (hb_declval (Proj) (hb_declval (typename Iter::item_t))) __item_t__;
273
  static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator;
274 275
  __item_t__ __item__ () const { return hb_get (f, *it); }
  __item_t__ __item_at__ (unsigned i) const { return hb_get (f, it[i]); }
276
  bool __more__ () const { return bool (it); }
B
Behdad Esfahbod 已提交
277 278 279 280 281 282 283 284 285 286
  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;
};
287

B
Behdad Esfahbod 已提交
288 289 290
template <typename Proj>
struct hb_map_iter_factory_t
{
291
  hb_map_iter_factory_t (Proj f) : f (f) {}
B
Behdad Esfahbod 已提交
292

293 294 295 296 297
  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 已提交
298 299 300 301

  private:
  Proj f;
};
B
Behdad Esfahbod 已提交
302
struct
303 304 305 306 307
{
  template <typename Proj>
  hb_map_iter_factory_t<Proj>
  operator () (Proj&& f) const
  { return hb_map_iter_factory_t<Proj> (f); }
B
Behdad Esfahbod 已提交
308
} HB_FUNCOBJ (hb_map);
B
Behdad Esfahbod 已提交
309

B
Behdad Esfahbod 已提交
310 311 312
template <typename Iter, typename Pred, typename Proj,
	 hb_enable_if (hb_is_iterator (Iter))>
struct hb_filter_iter_t :
313 314
  hb_iter_with_fallback_t<hb_filter_iter_t<Iter, Pred, Proj>,
			  typename Iter::item_t>
B
Behdad Esfahbod 已提交
315
{
B
Behdad Esfahbod 已提交
316
  hb_filter_iter_t (const Iter& it_, Pred p, Proj f) : it (it_), p (p), f (f)
317
  { while (it && !hb_has (p, hb_get (f, *it))) ++it; }
B
Behdad Esfahbod 已提交
318 319

  typedef typename Iter::item_t __item_t__;
320
  static constexpr bool is_sorted_iterator = Iter::is_sorted_iterator;
B
Behdad Esfahbod 已提交
321
  __item_t__ __item__ () const { return *it; }
322
  bool __more__ () const { return bool (it); }
B
Behdad Esfahbod 已提交
323 324 325 326 327 328 329 330 331 332 333
  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
{
334
  hb_filter_iter_factory_t (Pred p, Proj f) : p (p), f (f) {}
B
Behdad Esfahbod 已提交
335

336 337 338 339 340
  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 已提交
341 342 343 344 345

  private:
  Pred p;
  Proj f;
};
B
Behdad Esfahbod 已提交
346
struct
347 348 349 350 351 352
{
  template <typename Pred = decltype ((hb_bool)),
	    typename Proj = decltype ((hb_identity))>
  hb_filter_iter_factory_t<Pred, Proj>
  operator () (Pred&& p = hb_bool, Proj&& f = hb_identity) const
  { return hb_filter_iter_factory_t<Pred, Proj> (p, f); }
B
Behdad Esfahbod 已提交
353
} HB_FUNCOBJ (hb_filter);
354

355
template <typename Redu, typename InitT>
E
Ebrahim Byagowi 已提交
356 357
struct hb_reduce_t
{
358
  hb_reduce_t (Redu r, InitT init_value) : r (r), init_value (init_value) {}
E
Ebrahim Byagowi 已提交
359 360

  template <typename Iter,
361 362 363
	    hb_enable_if (hb_is_iterator (Iter)),
	    typename AccuT = decltype (hb_declval (Redu) (hb_declval (InitT), hb_declval (typename Iter::item_t)))>
  AccuT
E
Ebrahim Byagowi 已提交
364 365
  operator () (Iter it) const
  {
366
    AccuT value = init_value;
E
Ebrahim Byagowi 已提交
367
    for (; it; ++it)
368
      value = r (value, *it);
E
Ebrahim Byagowi 已提交
369 370 371 372 373
    return value;
  }

  private:
  Redu r;
374
  InitT init_value;
E
Ebrahim Byagowi 已提交
375
};
B
Behdad Esfahbod 已提交
376
struct
E
Ebrahim Byagowi 已提交
377
{
378 379 380 381
  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 已提交
382
} HB_FUNCOBJ (hb_reduce);
E
Ebrahim Byagowi 已提交
383 384


B
Behdad Esfahbod 已提交
385
/* hb_zip() */
386

B
Behdad Esfahbod 已提交
387
template <typename A, typename B>
388
struct hb_zip_iter_t :
B
Behdad Esfahbod 已提交
389 390
  hb_iter_t<hb_zip_iter_t<A, B>,
	    hb_pair_t<typename A::item_t, typename B::item_t> >
B
Behdad Esfahbod 已提交
391
{
392
  hb_zip_iter_t () {}
393
  hb_zip_iter_t (const A& a, const B& b) : a (a), b (b) {}
B
Behdad Esfahbod 已提交
394 395

  typedef hb_pair_t<typename A::item_t, typename B::item_t> __item_t__;
396
  static constexpr bool is_random_access_iterator =
B
Behdad Esfahbod 已提交
397 398
    A::is_random_access_iterator &&
    B::is_random_access_iterator;
399
  static constexpr bool is_sorted_iterator =
B
Behdad Esfahbod 已提交
400 401
    A::is_sorted_iterator &&
    B::is_sorted_iterator;
B
Behdad Esfahbod 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414
  __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 已提交
415
struct
416 417 418 419
{
  template <typename A, typename B,
	    hb_enable_if (hb_is_iterable (A) && hb_is_iterable (B))>
  hb_zip_iter_t<hb_iter_t (A), hb_iter_t (B)>
420
  operator () (A& a, B &b) const
B
Behdad Esfahbod 已提交
421
  { return hb_zip_iter_t<hb_iter_t (A), hb_iter_t (B)> (hb_iter (a), hb_iter (b)); }
B
Behdad Esfahbod 已提交
422
} HB_FUNCOBJ (hb_zip);
B
Behdad Esfahbod 已提交
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
/* hb_enumerate */

template <typename Iter,
	 hb_enable_if (hb_is_iterator (Iter))>
struct hb_enumerate_iter_t :
  hb_iter_t<hb_enumerate_iter_t<Iter>,
	    hb_pair_t<unsigned, typename Iter::item_t> >
{
  hb_enumerate_iter_t (const Iter& it) : i (0), it (it) {}

  typedef hb_pair_t<unsigned, typename Iter::item_t> __item_t__;
  static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator;
  static constexpr bool is_sorted_iterator = true;
  __item_t__ __item__ () const { return __item_t__ (+i, *it); }
  __item_t__ __item_at__ (unsigned j) const { return __item_t__ (i + j, it[j]); }
  bool __more__ () const { return bool (it); }
  unsigned __len__ () const { return it.len (); }
  void __next__ () { ++i; ++it; }
  void __forward__ (unsigned n) { i += n; it += n; }
  void __prev__ () { --i; --it; }
  void __rewind__ (unsigned n) { i -= n; it -= n; }

  private:
  unsigned i;
  Iter it;
};
B
Behdad Esfahbod 已提交
450
struct
451 452 453 454 455 456
{
  template <typename Iterable,
	    hb_enable_if (hb_is_iterable (Iterable))>
  hb_enumerate_iter_t<hb_iter_t (Iterable)>
  operator () (Iterable& it) const
  { return hb_enumerate_iter_t<hb_iter_t (Iterable)> (hb_iter (it)); }
B
Behdad Esfahbod 已提交
457
} HB_FUNCOBJ (hb_enumerate);
458

B
Behdad Esfahbod 已提交
459 460 461 462 463
/* hb_apply() */

template <typename Appl>
struct hb_apply_t
{
464
  hb_apply_t (Appl a) : a (a) {}
B
Behdad Esfahbod 已提交
465 466 467 468 469 470 471

  template <typename Iter,
	    hb_enable_if (hb_is_iterator (Iter))>
  void
  operator () (Iter it) const
  {
    for (; it; ++it)
B
Behdad Esfahbod 已提交
472
      (void) hb_invoke (a, *it);
B
Behdad Esfahbod 已提交
473 474 475 476 477
  }

  private:
  Appl a;
};
B
Behdad Esfahbod 已提交
478
struct
B
Behdad Esfahbod 已提交
479 480 481 482 483 484 485 486
{
  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 已提交
487
} HB_FUNCOBJ (hb_apply);
B
Behdad Esfahbod 已提交
488

B
Behdad Esfahbod 已提交
489 490 491 492 493 494 495
/* hb_sink() */

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

496 497
  template <typename Iter,
	    hb_enable_if (hb_is_iterator (Iter))>
B
Behdad Esfahbod 已提交
498
  void
499
  operator () (Iter it) const
B
Behdad Esfahbod 已提交
500
  {
501
    for (; it; ++it)
B
Behdad Esfahbod 已提交
502 503 504 505 506 507
      s << *it;
  }

  private:
  Sink s;
};
B
Behdad Esfahbod 已提交
508
struct
509 510 511 512
{
  template <typename Sink> hb_sink_t<Sink>
  operator () (Sink&& s) const
  { return hb_sink_t<Sink> (s); }
513 514 515 516

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

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

B
Behdad Esfahbod 已提交
521
struct
B
Behdad Esfahbod 已提交
522 523 524 525 526 527 528 529 530
{
  template <typename Iter,
	    hb_enable_if (hb_is_iterator (Iter))>
  void
  operator () (Iter it) const
  {
    for (; it; ++it)
      (void) *it;
  }
B
Behdad Esfahbod 已提交
531
} HB_FUNCOBJ (hb_drain);
B
Behdad Esfahbod 已提交
532

B
Behdad Esfahbod 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
/* hb_unzip(): unzip and sink to two sinks. */

template <typename Sink1, typename Sink2>
struct hb_unzip_t
{
  hb_unzip_t (Sink1&& s1, Sink2&& s2) : s1 (s1), s2 (s2) {}

  template <typename Iter,
	    hb_enable_if (hb_is_iterator (Iter))>
  void
  operator () (Iter it) const
  {
    for (; it; ++it)
    {
      const auto &v = *it;
      s1 << v.first;
      s2 << v.second;
    }
  }

  private:
  Sink1 s1;
  Sink2 s2;
};
B
Behdad Esfahbod 已提交
557
struct
B
Behdad Esfahbod 已提交
558 559 560 561 562 563 564 565
{
  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 已提交
566
} HB_FUNCOBJ (hb_unzip);
B
Behdad Esfahbod 已提交
567 568


569 570
/* hb-all, hb-any, hb-none. */

B
Behdad Esfahbod 已提交
571
struct
572 573 574 575 576 577 578 579 580 581 582
{
  template <typename Iterable,
	    hb_enable_if (hb_is_iterable (Iterable))>
  bool
  operator () (Iterable&& c) const
  {
    for (auto it = hb_iter (c); it; ++it)
      if (!*it)
	return false;
    return true;
  }
B
Behdad Esfahbod 已提交
583 584
} HB_FUNCOBJ (hb_all);
struct
585 586 587 588 589 590 591
{
  template <typename Iterable,
	    hb_enable_if (hb_is_iterable (Iterable))>
  bool
  operator () (Iterable&& c) const
  {
    for (auto it = hb_iter (c); it; ++it)
592
      if (*it)
593 594 595
	return true;
    return false;
  }
B
Behdad Esfahbod 已提交
596 597
} HB_FUNCOBJ (hb_any);
struct
598 599 600 601 602 603 604
{
  template <typename Iterable,
	    hb_enable_if (hb_is_iterable (Iterable))>
  bool
  operator () (Iterable&& c) const
  {
    for (auto it = hb_iter (c); it; ++it)
605
      if (*it)
606 607 608
	return false;
    return true;
  }
B
Behdad Esfahbod 已提交
609
} HB_FUNCOBJ (hb_none);
610

B
Behdad Esfahbod 已提交
611 612 613 614 615 616 617 618 619
/*
 * 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)
{
B
Behdad Esfahbod 已提交
620
  for (auto i = hb_iter (c); i; i++)
B
Behdad Esfahbod 已提交
621
    *i = v;
B
Behdad Esfahbod 已提交
622 623
}

624 625
template <typename S, typename D>
inline void
B
Behdad Esfahbod 已提交
626
hb_copy (S&& is, D&& id)
B
Behdad Esfahbod 已提交
627
{
B
Behdad Esfahbod 已提交
628
  hb_iter (is) | hb_sink (id);
B
Behdad Esfahbod 已提交
629 630 631
}


632
#endif /* HB_ITER_HH */