hb-iter.hh 17.5 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(); }
B
Behdad Esfahbod 已提交
72
  explicit operator bool () const { return thiz()->__more__ (); }
73
  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 127

/* TODO Change to function-object. */

template <typename> struct hb_array_t;
128 129 130 131 132

static const struct
{
  template <typename T>
  hb_iter_t (T)
133
  operator () (T&& c) const
134 135 136 137 138 139 140 141 142 143 144 145 146
  { return c.iter (); }

  /* Specialization for C arrays. */

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

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

} hb_iter HB_UNUSED;
147 148


149
/* Mixin to fill in what the subclass doesn't provide. */
150
template <typename iter_t, typename item_t = typename iter_t::__item_t__>
B
Behdad Esfahbod 已提交
151
struct hb_iter_fallback_mixin_t
152 153 154 155 156 157
{
  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:
158

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

163
  /* Termination: Implement __more__(), or __len__() if random-access. */
164
  bool __more__ () const { return thiz()->len (); }
165 166
  unsigned __len__ () const
  { iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; }
167 168

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

B
Behdad Esfahbod 已提交
172
  /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
173 174
  void __prev__ () { *thiz() -= 1; }
  void __rewind__ (unsigned n) { while (n--) --*thiz(); }
B
Behdad Esfahbod 已提交
175

B
Behdad Esfahbod 已提交
176
  protected:
B
Behdad Esfahbod 已提交
177 178 179
  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) {}
180 181
};

182 183 184 185 186 187 188
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 已提交
189 190 191
  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) {}
192 193 194
  void operator = (const hb_iter_with_fallback_t &o HB_UNUSED) {}
};

B
Behdad Esfahbod 已提交
195 196 197 198
/*
 * Meta-programming predicates.
 */

199 200 201
/* hb_is_iterable() */

template<typename T, typename B>
B
Behdad Esfahbod 已提交
202 203
struct _hb_is_iterable
{ enum { value = false }; };
204
template<typename T>
205
struct _hb_is_iterable<T, hb_bool_tt<true || sizeof (hb_declval (T).iter ())> >
B
Behdad Esfahbod 已提交
206
{ enum { value = true }; };
B
Behdad Esfahbod 已提交
207

208
template<typename T>
B
Behdad Esfahbod 已提交
209 210
struct hb_is_iterable { enum { value = _hb_is_iterable<T, hb_true_t>::value }; };
#define hb_is_iterable(Iterable) hb_is_iterable<Iterable>::value
211

B
Behdad Esfahbod 已提交
212 213 214
/* TODO Add hb_is_iterable_of().
 * TODO Add random_access / sorted variants. */

B
Behdad Esfahbod 已提交
215

216
/* hb_is_iterator() / hb_is_random_access_iterator() / hb_is_sorted_iterator() */
217

218
template <typename Iter>
219
struct _hb_is_iterator_of
220
{
B
Behdad Esfahbod 已提交
221
  char operator () (...) { return 0; }
B
Behdad Esfahbod 已提交
222 223 224 225
  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; }
226 227
  static_assert (sizeof (char) != sizeof (int), "");
};
228
template<typename Iter, typename Item>
229 230 231 232
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)
233

234 235 236 237 238
#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)

239
#define hb_is_sorted_iterator_of(Iter, Item) \
240
  hb_is_iterator_of (Iter, Item) && Iter::is_sorted_iterator
241 242
#define hb_is_sorted_iterator(Iter) \
  hb_is_sorted_iterator_of (Iter, typename Iter::item_t)
243

B
Behdad Esfahbod 已提交
244

B
Behdad Esfahbod 已提交
245
/*
B
Behdad Esfahbod 已提交
246
 * Adaptors, combiners, etc.
B
Behdad Esfahbod 已提交
247
 */
248

249 250 251 252 253
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 已提交
254 255 256 257 258
/* 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 已提交
259 260
  hb_iter_t<hb_map_iter_t<Iter, Proj>,
	    decltype (hb_declval (Proj) (hb_declval (typename Iter::item_t)))>
B
Behdad Esfahbod 已提交
261
{
B
Behdad Esfahbod 已提交
262
  hb_map_iter_t (const Iter& it, Proj f) : it (it), f (f) {}
B
Behdad Esfahbod 已提交
263

264
  typedef decltype (hb_declval (Proj) (hb_declval (typename Iter::item_t))) __item_t__;
265
  static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator;
B
Behdad Esfahbod 已提交
266 267
  __item_t__ __item__ () const { return f (*it); }
  __item_t__ __item_at__ (unsigned i) const { return f (it[i]); }
268
  bool __more__ () const { return bool (it); }
B
Behdad Esfahbod 已提交
269 270 271 272 273 274 275 276 277 278
  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;
};
279

B
Behdad Esfahbod 已提交
280 281 282
template <typename Proj>
struct hb_map_iter_factory_t
{
283
  hb_map_iter_factory_t (Proj f) : f (f) {}
B
Behdad Esfahbod 已提交
284

285 286 287 288 289
  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 已提交
290 291 292 293

  private:
  Proj f;
};
294 295 296 297 298 299 300
static const struct
{
  template <typename Proj>
  hb_map_iter_factory_t<Proj>
  operator () (Proj&& f) const
  { return hb_map_iter_factory_t<Proj> (f); }
} hb_map HB_UNUSED;
B
Behdad Esfahbod 已提交
301

B
Behdad Esfahbod 已提交
302 303 304
template <typename Iter, typename Pred, typename Proj,
	 hb_enable_if (hb_is_iterator (Iter))>
struct hb_filter_iter_t :
305 306
  hb_iter_with_fallback_t<hb_filter_iter_t<Iter, Pred, Proj>,
			  typename Iter::item_t>
B
Behdad Esfahbod 已提交
307
{
B
Behdad Esfahbod 已提交
308
  hb_filter_iter_t (const Iter& it_, Pred p, Proj f) : it (it_), p (p), f (f)
B
Behdad Esfahbod 已提交
309 310 311
  { while (it && !p (f (*it))) ++it; }

  typedef typename Iter::item_t __item_t__;
312
  static constexpr bool is_sorted_iterator = Iter::is_sorted_iterator;
B
Behdad Esfahbod 已提交
313
  __item_t__ __item__ () const { return *it; }
314
  bool __more__ () const { return bool (it); }
B
Behdad Esfahbod 已提交
315 316 317 318 319 320 321 322 323 324 325
  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
{
326
  hb_filter_iter_factory_t (Pred p, Proj f) : p (p), f (f) {}
B
Behdad Esfahbod 已提交
327

328 329 330 331 332
  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 已提交
333 334 335 336 337

  private:
  Pred p;
  Proj f;
};
338 339 340 341 342 343 344 345
static const struct
{
  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); }
} hb_filter HB_UNUSED;
346

B
Behdad Esfahbod 已提交
347
/* hb_zip() */
348

B
Behdad Esfahbod 已提交
349
template <typename A, typename B>
350
struct hb_zip_iter_t :
B
Behdad Esfahbod 已提交
351 352
  hb_iter_t<hb_zip_iter_t<A, B>,
	    hb_pair_t<typename A::item_t, typename B::item_t> >
B
Behdad Esfahbod 已提交
353
{
354
  hb_zip_iter_t () {}
355
  hb_zip_iter_t (const A& a, const B& b) : a (a), b (b) {}
B
Behdad Esfahbod 已提交
356 357

  typedef hb_pair_t<typename A::item_t, typename B::item_t> __item_t__;
358
  static constexpr bool is_random_access_iterator =
B
Behdad Esfahbod 已提交
359 360
    A::is_random_access_iterator &&
    B::is_random_access_iterator;
361
  static constexpr bool is_sorted_iterator =
B
Behdad Esfahbod 已提交
362 363
    A::is_sorted_iterator &&
    B::is_sorted_iterator;
B
Behdad Esfahbod 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376
  __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;
};
377 378 379 380 381
static const struct
{
  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)>
382
  operator () (A& a, B &b) const
B
Behdad Esfahbod 已提交
383
  { return hb_zip_iter_t<hb_iter_t (A), hb_iter_t (B)> (hb_iter (a), hb_iter (b)); }
384
} hb_zip HB_UNUSED;
B
Behdad Esfahbod 已提交
385

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
/* 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;
};
static const struct
{
  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)); }
} hb_enumerate HB_UNUSED;

B
Behdad Esfahbod 已提交
421 422 423 424 425
/* hb_apply() */

template <typename Appl>
struct hb_apply_t
{
426
  hb_apply_t (Appl a) : a (a) {}
B
Behdad Esfahbod 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

  template <typename Iter,
	    hb_enable_if (hb_is_iterator (Iter))>
  void
  operator () (Iter it) const
  {
    for (; it; ++it)
      a (*it);
  }

  private:
  Appl a;
};
static const struct
{
  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); }
} hb_apply HB_UNUSED;

B
Behdad Esfahbod 已提交
451 452 453 454 455 456 457
/* hb_sink() */

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

458 459
  template <typename Iter,
	    hb_enable_if (hb_is_iterator (Iter))>
B
Behdad Esfahbod 已提交
460
  void
461
  operator () (Iter it) const
B
Behdad Esfahbod 已提交
462
  {
463
    for (; it; ++it)
B
Behdad Esfahbod 已提交
464 465 466 467 468 469
      s << *it;
  }

  private:
  Sink s;
};
470 471 472 473 474
static const struct
{
  template <typename Sink> hb_sink_t<Sink>
  operator () (Sink&& s) const
  { return hb_sink_t<Sink> (s); }
475 476 477 478

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

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

B
Behdad Esfahbod 已提交
483 484 485 486 487 488 489 490 491 492 493
static const struct
{
  template <typename Iter,
	    hb_enable_if (hb_is_iterator (Iter))>
  void
  operator () (Iter it) const
  {
    for (; it; ++it)
      (void) *it;
  }
} hb_drain HB_UNUSED;
B
Behdad Esfahbod 已提交
494

B
Behdad Esfahbod 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
/* 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;
};
static const struct
{
  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); }
} hb_unzip HB_UNUSED;


531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
/* hb-all, hb-any, hb-none. */

static const struct
{
  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;
  }
} hb_all HB_UNUSED;

static const struct
{
  template <typename Iterable,
	    hb_enable_if (hb_is_iterable (Iterable))>
  bool
  operator () (Iterable&& c) const
  {
    for (auto it = hb_iter (c); it; ++it)
555
      if (*it)
556 557 558 559 560 561 562 563 564 565 566 567 568
	return true;
    return false;
  }
} hb_any HB_UNUSED;

static const struct
{
  template <typename Iterable,
	    hb_enable_if (hb_is_iterable (Iterable))>
  bool
  operator () (Iterable&& c) const
  {
    for (auto it = hb_iter (c); it; ++it)
569
      if (*it)
570 571 572 573 574
	return false;
    return true;
  }
} hb_none HB_UNUSED;

B
Behdad Esfahbod 已提交
575 576 577 578 579 580 581 582 583
/*
 * 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 已提交
584
  for (auto i = hb_iter (c); i; i++)
B
Behdad Esfahbod 已提交
585
    *i = v;
B
Behdad Esfahbod 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598
}

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;
}


599
#endif /* HB_ITER_HH */