2018-12-21 06:08:05 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Google, Inc.
|
2019-04-15 23:36:09 +02:00
|
|
|
* Copyright © 2019 Facebook, Inc.
|
2018-12-21 06:08:05 +01:00
|
|
|
*
|
|
|
|
* 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
|
2019-04-15 21:39:03 +02:00
|
|
|
* Facebook Author(s): Behdad Esfahbod
|
2018-12-21 06:08:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HB_ITER_HH
|
|
|
|
#define HB_ITER_HH
|
|
|
|
|
|
|
|
#include "hb.hh"
|
2019-04-16 16:45:20 +02:00
|
|
|
#include "hb-algs.hh"
|
2018-12-27 23:23:12 +01:00
|
|
|
#include "hb-meta.hh"
|
2018-12-21 06:08:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* 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.
|
2018-12-21 06:54:55 +01:00
|
|
|
* hb_iter_tator objects are small, light-weight, objects that can be
|
2018-12-21 06:08:05 +01:00
|
|
|
* copied by value. If the collection / object being iterated on
|
2018-12-21 06:54:55 +01:00
|
|
|
* is writable, then the iterator returns lvalues, otherwise it
|
2018-12-21 06:08:05 +01:00
|
|
|
* returns rvalues.
|
2019-05-07 04:57:15 +02:00
|
|
|
*
|
|
|
|
* 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()?
|
2018-12-21 06:08:05 +01:00
|
|
|
*/
|
|
|
|
|
2018-12-28 20:29:09 +01:00
|
|
|
/*
|
|
|
|
* Base classes for iterators.
|
|
|
|
*/
|
|
|
|
|
2018-12-21 06:54:55 +01:00
|
|
|
/* Base class for all iterators. */
|
2019-01-26 22:44:09 +01:00
|
|
|
template <typename iter_t, typename Item = typename iter_t::__item_t__>
|
2019-01-04 17:22:32 +01:00
|
|
|
struct hb_iter_t
|
2018-12-21 06:08:05 +01:00
|
|
|
{
|
2018-12-21 07:57:02 +01:00
|
|
|
typedef Item item_t;
|
2019-01-22 12:15:23 +01:00
|
|
|
static constexpr unsigned item_size = hb_static_size (Item);
|
2019-01-25 15:34:03 +01:00
|
|
|
static constexpr bool is_iterator = true;
|
|
|
|
static constexpr bool is_random_access_iterator = false;
|
2019-05-15 07:28:07 +02:00
|
|
|
static constexpr bool is_sorted_iterator = false;
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2018-12-22 00:09:45 +01:00
|
|
|
private:
|
|
|
|
/* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
|
2019-04-13 04:42:44 +02:00
|
|
|
const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
|
2019-08-24 15:27:14 +02:00
|
|
|
iter_t* thiz () { return static_cast< iter_t *> (this); }
|
2018-12-22 00:09:45 +01:00
|
|
|
public:
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2019-01-08 22:43:49 +01:00
|
|
|
/* TODO:
|
|
|
|
* Port operators below to use hb_enable_if to sniff which method implements
|
2019-01-29 22:39:19 +01:00
|
|
|
* an operator and use it, and remove hb_iter_fallback_mixin_t completely. */
|
2019-01-08 22:43:49 +01:00
|
|
|
|
2018-12-21 06:54:55 +01:00
|
|
|
/* Operators. */
|
2019-04-13 04:42:44 +02:00
|
|
|
iter_t iter () const { return *thiz(); }
|
|
|
|
iter_t operator + () const { return *thiz(); }
|
2019-05-07 04:57:15 +02:00
|
|
|
iter_t begin () const { return *thiz(); }
|
|
|
|
iter_t end () const { return thiz()->__end__ (); }
|
2019-04-13 04:42:44 +02:00
|
|
|
explicit operator bool () const { return thiz()->__more__ (); }
|
|
|
|
unsigned len () const { return thiz()->__len__ (); }
|
2019-01-31 01:03:16 +01:00
|
|
|
/* The following can only be enabled if item_t is reference type. Otherwise
|
2019-05-07 04:57:15 +02:00
|
|
|
* it will be returning pointer to temporary rvalue.
|
|
|
|
* TODO Use a wrapper return type to fix for non-reference type. */
|
2019-01-31 01:03:16 +01:00
|
|
|
template <typename T = item_t,
|
|
|
|
hb_enable_if (hb_is_reference (T))>
|
2019-04-16 22:45:53 +02:00
|
|
|
hb_remove_reference<item_t>* operator -> () const { return hb_addressof (**thiz()); }
|
2019-04-13 04:42:44 +02:00
|
|
|
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); }
|
2019-05-08 18:56:29 +02:00
|
|
|
iter_t& operator += (unsigned count) & { thiz()->__forward__ (count); return *thiz(); }
|
|
|
|
iter_t operator += (unsigned count) && { thiz()->__forward__ (count); return *thiz(); }
|
2019-05-07 23:09:00 +02:00
|
|
|
iter_t& operator ++ () & { thiz()->__next__ (); return *thiz(); }
|
|
|
|
iter_t operator ++ () && { thiz()->__next__ (); return *thiz(); }
|
2019-05-08 18:56:29 +02:00
|
|
|
iter_t& operator -= (unsigned count) & { thiz()->__rewind__ (count); return *thiz(); }
|
|
|
|
iter_t operator -= (unsigned count) && { thiz()->__rewind__ (count); return *thiz(); }
|
2019-05-07 23:09:00 +02:00
|
|
|
iter_t& operator -- () & { thiz()->__prev__ (); return *thiz(); }
|
|
|
|
iter_t operator -- () && { thiz()->__prev__ (); return *thiz(); }
|
2019-04-13 04:42:44 +02:00
|
|
|
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; }
|
2019-01-28 22:23:12 +01:00
|
|
|
template <typename T>
|
2019-05-08 18:56:29 +02:00
|
|
|
iter_t& operator >> (T &v) & { v = **thiz(); ++*thiz(); return *thiz(); }
|
2019-01-28 22:23:12 +01:00
|
|
|
template <typename T>
|
2019-05-08 18:56:29 +02:00
|
|
|
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(); }
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2018-12-22 00:09:45 +01:00
|
|
|
protected:
|
2019-05-15 23:19:20 +02:00
|
|
|
hb_iter_t () = default;
|
|
|
|
hb_iter_t (const hb_iter_t &o HB_UNUSED) = default;
|
|
|
|
hb_iter_t (hb_iter_t &&o HB_UNUSED) = default;
|
|
|
|
hb_iter_t& operator = (const hb_iter_t &o HB_UNUSED) = default;
|
|
|
|
hb_iter_t& operator = (hb_iter_t &&o HB_UNUSED) = default;
|
2018-12-22 00:09:45 +01:00
|
|
|
};
|
|
|
|
|
2018-12-27 19:29:51 +01:00
|
|
|
#define HB_ITER_USING(Name) \
|
2019-01-09 01:38:08 +01:00
|
|
|
using item_t = typename Name::item_t; \
|
2019-05-07 04:57:15 +02:00
|
|
|
using Name::begin; \
|
|
|
|
using Name::end; \
|
2018-12-28 22:29:48 +01:00
|
|
|
using Name::item_size; \
|
2018-12-31 02:51:31 +01:00
|
|
|
using Name::is_iterator; \
|
2018-12-27 19:29:51 +01:00
|
|
|
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 --; \
|
2018-12-30 08:11:03 +01:00
|
|
|
using Name::operator +; \
|
2018-12-27 19:29:51 +01:00
|
|
|
using Name::operator -; \
|
2019-01-29 22:44:39 +01:00
|
|
|
using Name::operator >>; \
|
|
|
|
using Name::operator <<; \
|
2018-12-27 19:29:51 +01:00
|
|
|
static_assert (true, "")
|
|
|
|
|
2019-05-08 21:25:34 +02:00
|
|
|
/* 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 ());
|
2019-01-27 00:50:54 +01:00
|
|
|
|
2019-01-27 01:03:56 +01:00
|
|
|
|
|
|
|
template <typename> struct hb_array_t;
|
2019-02-16 01:11:32 +01:00
|
|
|
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-02-16 01:11:32 +01:00
|
|
|
{
|
2019-05-08 21:25:34 +02:00
|
|
|
template <typename T> hb_iter_type<T>
|
2019-02-16 01:55:08 +01:00
|
|
|
operator () (T&& c) const
|
2019-05-08 20:00:18 +02:00
|
|
|
{ return hb_deref (hb_forward<T> (c)).iter (); }
|
2019-02-16 01:11:32 +01:00
|
|
|
|
|
|
|
/* Specialization for C arrays. */
|
|
|
|
|
|
|
|
template <typename Type> inline hb_array_t<Type>
|
2019-04-13 04:42:44 +02:00
|
|
|
operator () (Type *array, unsigned int length) const
|
2019-02-16 01:11:32 +01:00
|
|
|
{ return hb_array_t<Type> (array, length); }
|
|
|
|
|
|
|
|
template <typename Type, unsigned int length> hb_array_t<Type>
|
2019-04-13 04:42:44 +02:00
|
|
|
operator () (Type (&array)[length]) const
|
2019-02-16 01:11:32 +01:00
|
|
|
{ return hb_array_t<Type> (array, length); }
|
|
|
|
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_iter);
|
2019-01-27 01:03:56 +01:00
|
|
|
|
2018-12-22 00:09:45 +01:00
|
|
|
/* Mixin to fill in what the subclass doesn't provide. */
|
2019-01-09 08:48:35 +01:00
|
|
|
template <typename iter_t, typename item_t = typename iter_t::__item_t__>
|
2019-01-29 22:39:19 +01:00
|
|
|
struct hb_iter_fallback_mixin_t
|
2018-12-22 00:09:45 +01:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
/* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
|
2019-04-13 04:42:44 +02:00
|
|
|
const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
|
2019-08-24 15:27:14 +02:00
|
|
|
iter_t* thiz () { return static_cast< iter_t *> (this); }
|
2018-12-22 00:09:45 +01:00
|
|
|
public:
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2018-12-21 07:53:27 +01:00
|
|
|
/* Access: Implement __item__(), or __item_at__() if random-access. */
|
2019-04-13 04:42:44 +02:00
|
|
|
item_t __item__ () const { return (*thiz())[0]; }
|
|
|
|
item_t __item_at__ (unsigned i) const { return *(*thiz() + i); }
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2018-12-22 00:49:27 +01:00
|
|
|
/* Termination: Implement __more__(), or __len__() if random-access. */
|
2019-05-09 20:30:27 +02:00
|
|
|
bool __more__ () const { return bool (thiz()->len ()); }
|
2019-04-13 04:42:44 +02:00
|
|
|
unsigned __len__ () const
|
2019-05-13 22:53:06 +02:00
|
|
|
{ iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; } return l; }
|
2018-12-21 06:54:55 +01:00
|
|
|
|
|
|
|
/* Advancing: Implement __next__(), or __forward__() if random-access. */
|
2019-04-13 04:42:44 +02:00
|
|
|
void __next__ () { *thiz() += 1; }
|
2019-05-11 20:54:30 +02:00
|
|
|
void __forward__ (unsigned n) { while (*thiz() && n--) ++*thiz(); }
|
2018-12-21 06:54:55 +01:00
|
|
|
|
2018-12-21 07:17:35 +01:00
|
|
|
/* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
|
2019-04-13 04:42:44 +02:00
|
|
|
void __prev__ () { *thiz() -= 1; }
|
2019-05-11 20:54:30 +02:00
|
|
|
void __rewind__ (unsigned n) { while (*thiz() && n--) --*thiz(); }
|
2018-12-21 07:17:35 +01:00
|
|
|
|
2019-05-07 04:57:15 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2018-12-27 00:54:27 +01:00
|
|
|
protected:
|
2019-05-15 23:19:20 +02:00
|
|
|
hb_iter_fallback_mixin_t () = default;
|
|
|
|
hb_iter_fallback_mixin_t (const hb_iter_fallback_mixin_t &o HB_UNUSED) = default;
|
|
|
|
hb_iter_fallback_mixin_t (hb_iter_fallback_mixin_t &&o HB_UNUSED) = default;
|
|
|
|
hb_iter_fallback_mixin_t& operator = (const hb_iter_fallback_mixin_t &o HB_UNUSED) = default;
|
|
|
|
hb_iter_fallback_mixin_t& operator = (hb_iter_fallback_mixin_t &&o HB_UNUSED) = default;
|
2018-12-21 06:08:05 +01:00
|
|
|
};
|
|
|
|
|
2019-01-30 02:10:19 +01:00
|
|
|
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:
|
2019-05-15 23:19:20 +02:00
|
|
|
hb_iter_with_fallback_t () = default;
|
|
|
|
hb_iter_with_fallback_t (const hb_iter_with_fallback_t &o HB_UNUSED) = default;
|
|
|
|
hb_iter_with_fallback_t (hb_iter_with_fallback_t &&o HB_UNUSED) = default;
|
|
|
|
hb_iter_with_fallback_t& operator = (const hb_iter_with_fallback_t &o HB_UNUSED) = default;
|
|
|
|
hb_iter_with_fallback_t& operator = (hb_iter_with_fallback_t &&o HB_UNUSED) = default;
|
2019-01-30 02:10:19 +01:00
|
|
|
};
|
|
|
|
|
2018-12-28 20:29:09 +01:00
|
|
|
/*
|
|
|
|
* Meta-programming predicates.
|
|
|
|
*/
|
|
|
|
|
2019-05-10 01:58:28 +02:00
|
|
|
/* hb_is_iterator() / hb_is_iterator_of() */
|
|
|
|
|
|
|
|
template<typename Iter, typename Item>
|
|
|
|
struct hb_is_iterator_of
|
|
|
|
{
|
|
|
|
template <typename Item2 = Item>
|
2019-05-11 05:06:31 +02:00
|
|
|
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 *);
|
2019-05-10 01:58:28 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-12-28 22:29:48 +01:00
|
|
|
/* hb_is_iterable() */
|
|
|
|
|
2019-04-03 23:18:19 +02:00
|
|
|
template <typename T>
|
|
|
|
struct hb_is_iterable
|
|
|
|
{
|
|
|
|
private:
|
2019-04-22 21:25:11 +02:00
|
|
|
|
2019-04-03 23:18:19 +02:00
|
|
|
template <typename U>
|
2019-05-11 05:06:31 +02:00
|
|
|
static auto impl (hb_priority<1>) -> decltype (hb_declval (U).iter (), hb_true_type ());
|
2019-04-22 21:25:11 +02:00
|
|
|
|
2019-04-03 23:18:19 +02:00
|
|
|
template <typename>
|
2019-05-11 05:06:31 +02:00
|
|
|
static hb_false_type impl (hb_priority<0>);
|
2019-04-03 23:18:19 +02:00
|
|
|
|
|
|
|
public:
|
2019-05-10 01:58:28 +02:00
|
|
|
static constexpr bool value = decltype (impl<T> (hb_prioritize))::value;
|
2019-04-03 23:18:19 +02:00
|
|
|
};
|
2018-12-28 20:29:09 +01:00
|
|
|
#define hb_is_iterable(Iterable) hb_is_iterable<Iterable>::value
|
2018-12-21 06:08:05 +01:00
|
|
|
|
2019-05-10 01:58:28 +02:00
|
|
|
/* hb_is_source_of() / hb_is_sink_of() */
|
2019-01-08 21:57:01 +01:00
|
|
|
|
2019-05-10 01:58:28 +02:00
|
|
|
template<typename Iter, typename Item>
|
|
|
|
struct hb_is_source_of
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
template <typename Iter2 = Iter,
|
2019-05-11 06:25:07 +02:00
|
|
|
hb_enable_if (hb_is_convertible (typename Iter2::item_t, hb_add_lvalue_reference<hb_add_const<Item>>))>
|
2019-05-11 05:06:31 +02:00
|
|
|
static hb_true_type impl (hb_priority<2>);
|
2019-05-10 20:18:52 +02:00
|
|
|
template <typename Iter2 = Iter>
|
2019-05-11 05:06:31 +02:00
|
|
|
static auto impl (hb_priority<1>) -> decltype (hb_declval (Iter2) >> hb_declval (Item &), hb_true_type ());
|
|
|
|
static hb_false_type impl (hb_priority<0>);
|
2018-12-28 22:29:48 +01:00
|
|
|
|
2019-05-10 01:58:28 +02:00
|
|
|
public:
|
|
|
|
static constexpr bool value = decltype (impl (hb_prioritize))::value;
|
|
|
|
};
|
|
|
|
#define hb_is_source_of(Iter, Item) hb_is_source_of<Iter, Item>::value
|
2019-04-26 22:16:48 +02:00
|
|
|
|
2018-12-31 00:42:14 +01:00
|
|
|
template<typename Iter, typename Item>
|
2019-05-10 01:58:28 +02:00
|
|
|
struct hb_is_sink_of
|
|
|
|
{
|
|
|
|
private:
|
2019-05-10 20:18:52 +02:00
|
|
|
template <typename Iter2 = Iter,
|
2019-05-11 06:25:07 +02:00
|
|
|
hb_enable_if (hb_is_convertible (typename Iter2::item_t, hb_add_lvalue_reference<Item>))>
|
2019-05-11 05:06:31 +02:00
|
|
|
static hb_true_type impl (hb_priority<2>);
|
2019-05-10 20:18:52 +02:00
|
|
|
template <typename Iter2 = Iter>
|
2019-05-11 05:06:31 +02:00
|
|
|
static auto impl (hb_priority<1>) -> decltype (hb_declval (Iter2) << hb_declval (Item), hb_true_type ());
|
|
|
|
static hb_false_type impl (hb_priority<0>);
|
2018-12-28 22:29:48 +01:00
|
|
|
|
2019-05-10 01:58:28 +02:00
|
|
|
public:
|
|
|
|
static constexpr bool value = decltype (impl (hb_prioritize))::value;
|
|
|
|
};
|
|
|
|
#define hb_is_sink_of(Iter, Item) hb_is_sink_of<Iter, Item>::value
|
2019-01-08 21:53:02 +01:00
|
|
|
|
2019-05-10 01:58:28 +02:00
|
|
|
/* This is commonly used, so define: */
|
|
|
|
#define hb_is_sorted_source_of(Iter, Item) \
|
|
|
|
(hb_is_source_of(Iter, Item) && Iter::is_sorted_iterator)
|
2018-12-28 22:29:48 +01:00
|
|
|
|
2019-01-09 18:07:01 +01:00
|
|
|
|
2019-05-07 04:57:15 +02:00
|
|
|
/* Range-based 'for' for iterables. */
|
|
|
|
|
|
|
|
template <typename Iterable,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterable (Iterable))>
|
2019-05-07 04:57:15 +02:00
|
|
|
static inline auto begin (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).begin ())
|
|
|
|
|
|
|
|
template <typename Iterable,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterable (Iterable))>
|
2019-05-07 04:57:15 +02:00
|
|
|
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,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterable (Iterable))>
|
2019-05-07 04:57:15 +02:00
|
|
|
static inline auto begin (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).begin ())
|
|
|
|
|
|
|
|
template <typename Iterable,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterable (Iterable))>
|
2019-05-07 04:57:15 +02:00
|
|
|
static inline auto end (Iterable&& iterable) HB_AUTO_RETURN (hb_iter (iterable).end ())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-28 20:29:09 +01:00
|
|
|
/*
|
2019-01-09 18:07:01 +01:00
|
|
|
* Adaptors, combiners, etc.
|
2018-12-28 20:29:09 +01:00
|
|
|
*/
|
2018-12-21 23:21:19 +01:00
|
|
|
|
2019-01-09 21:35:45 +01:00
|
|
|
template <typename Lhs, typename Rhs,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterator (Lhs))>
|
2019-04-17 00:33:51 +02:00
|
|
|
static inline auto
|
2019-05-08 19:02:30 +02:00
|
|
|
operator | (Lhs&& lhs, Rhs&& rhs) HB_AUTO_RETURN (hb_forward<Rhs> (rhs) (hb_forward<Lhs> (lhs)))
|
2019-01-09 21:35:45 +01:00
|
|
|
|
2019-01-09 20:15:49 +01:00
|
|
|
/* hb_map(), hb_filter(), hb_reduce() */
|
|
|
|
|
2019-05-14 00:26:00 +02:00
|
|
|
enum class hb_function_sortedness_t {
|
2019-05-11 20:23:31 +02:00
|
|
|
NOT_SORTED,
|
|
|
|
RETAINS_SORTING,
|
|
|
|
SORTED,
|
|
|
|
};
|
|
|
|
|
2019-05-14 00:26:00 +02:00
|
|
|
template <typename Iter, typename Proj, hb_function_sortedness_t Sorted,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterator (Iter))>
|
2019-01-09 20:15:49 +01:00
|
|
|
struct hb_map_iter_t :
|
2019-05-11 20:23:31 +02:00
|
|
|
hb_iter_t<hb_map_iter_t<Iter, Proj, Sorted>,
|
2019-05-08 19:56:09 +02:00
|
|
|
decltype (hb_get (hb_declval (Proj), *hb_declval (Iter)))>
|
2019-01-09 20:15:49 +01:00
|
|
|
{
|
2019-05-07 09:05:37 +02:00
|
|
|
hb_map_iter_t (const Iter& it, Proj f_) : it (it), f (f_) {}
|
2019-01-09 20:15:49 +01:00
|
|
|
|
2019-05-08 19:56:09 +02:00
|
|
|
typedef decltype (hb_get (hb_declval (Proj), *hb_declval (Iter))) __item_t__;
|
2019-01-25 15:34:03 +01:00
|
|
|
static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator;
|
2019-05-15 07:28:07 +02:00
|
|
|
static constexpr bool is_sorted_iterator =
|
|
|
|
Sorted == hb_function_sortedness_t::SORTED ? true :
|
|
|
|
Sorted == hb_function_sortedness_t::RETAINS_SORTING ? Iter::is_sorted_iterator :
|
|
|
|
false;
|
2019-05-07 09:05:37 +02:00
|
|
|
__item_t__ __item__ () const { return hb_get (f.get (), *it); }
|
|
|
|
__item_t__ __item_at__ (unsigned i) const { return hb_get (f.get (), it[i]); }
|
2019-01-09 21:35:45 +01:00
|
|
|
bool __more__ () const { return bool (it); }
|
2019-01-09 20:15:49 +01:00
|
|
|
unsigned __len__ () const { return it.len (); }
|
|
|
|
void __next__ () { ++it; }
|
|
|
|
void __forward__ (unsigned n) { it += n; }
|
|
|
|
void __prev__ () { --it; }
|
|
|
|
void __rewind__ (unsigned n) { it -= n; }
|
2019-05-07 09:13:11 +02:00
|
|
|
hb_map_iter_t __end__ () const { return hb_map_iter_t (it.end (), f); }
|
2019-05-07 04:57:15 +02:00
|
|
|
bool operator != (const hb_map_iter_t& o) const
|
2019-05-16 00:14:26 +02:00
|
|
|
{ return it != o.it; }
|
2019-01-09 20:15:49 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Iter it;
|
2019-05-07 09:05:37 +02:00
|
|
|
hb_reference_wrapper<Proj> f;
|
2019-01-09 20:15:49 +01:00
|
|
|
};
|
2019-01-26 22:44:09 +01:00
|
|
|
|
2019-05-14 00:26:00 +02:00
|
|
|
template <typename Proj, hb_function_sortedness_t Sorted>
|
2019-01-09 20:15:49 +01:00
|
|
|
struct hb_map_iter_factory_t
|
|
|
|
{
|
2019-02-14 20:53:40 +01:00
|
|
|
hb_map_iter_factory_t (Proj f) : f (f) {}
|
2019-01-09 20:15:49 +01:00
|
|
|
|
2019-02-14 19:48:20 +01:00
|
|
|
template <typename Iter,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterator (Iter))>
|
2019-05-11 20:23:31 +02:00
|
|
|
hb_map_iter_t<Iter, Proj, Sorted>
|
2019-05-08 19:02:30 +02:00
|
|
|
operator () (Iter it)
|
2019-05-11 20:23:31 +02:00
|
|
|
{ return hb_map_iter_t<Iter, Proj, Sorted> (it, f); }
|
2019-01-09 20:15:49 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Proj f;
|
|
|
|
};
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-02-16 01:09:29 +01:00
|
|
|
{
|
|
|
|
template <typename Proj>
|
2019-05-14 00:26:00 +02:00
|
|
|
hb_map_iter_factory_t<Proj, hb_function_sortedness_t::NOT_SORTED>
|
2019-02-16 01:09:29 +01:00
|
|
|
operator () (Proj&& f) const
|
2019-05-14 00:26:00 +02:00
|
|
|
{ return hb_map_iter_factory_t<Proj, hb_function_sortedness_t::NOT_SORTED> (f); }
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_map);
|
2019-05-11 20:23:31 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
template <typename Proj>
|
2019-05-14 00:26:00 +02:00
|
|
|
hb_map_iter_factory_t<Proj, hb_function_sortedness_t::RETAINS_SORTING>
|
2019-05-11 20:23:31 +02:00
|
|
|
operator () (Proj&& f) const
|
2019-05-14 00:26:00 +02:00
|
|
|
{ return hb_map_iter_factory_t<Proj, hb_function_sortedness_t::RETAINS_SORTING> (f); }
|
2019-05-11 20:23:31 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_map_retains_sorting);
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
template <typename Proj>
|
2019-05-14 00:26:00 +02:00
|
|
|
hb_map_iter_factory_t<Proj, hb_function_sortedness_t::SORTED>
|
2019-05-11 20:23:31 +02:00
|
|
|
operator () (Proj&& f) const
|
2019-05-14 00:26:00 +02:00
|
|
|
{ return hb_map_iter_factory_t<Proj, hb_function_sortedness_t::SORTED> (f); }
|
2019-05-11 20:23:31 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_map_sorted);
|
2019-01-09 20:15:49 +01:00
|
|
|
|
2019-01-09 20:32:33 +01:00
|
|
|
template <typename Iter, typename Pred, typename Proj,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterator (Iter))>
|
2019-01-09 20:32:33 +01:00
|
|
|
struct hb_filter_iter_t :
|
2019-01-30 02:10:19 +01:00
|
|
|
hb_iter_with_fallback_t<hb_filter_iter_t<Iter, Pred, Proj>,
|
|
|
|
typename Iter::item_t>
|
2019-01-09 20:32:33 +01:00
|
|
|
{
|
2019-05-07 09:05:37 +02:00
|
|
|
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; }
|
2019-01-09 20:32:33 +01:00
|
|
|
|
|
|
|
typedef typename Iter::item_t __item_t__;
|
2019-05-15 07:28:07 +02:00
|
|
|
static constexpr bool is_sorted_iterator = Iter::is_sorted_iterator;
|
2019-01-09 20:32:33 +01:00
|
|
|
__item_t__ __item__ () const { return *it; }
|
2019-01-09 21:35:45 +01:00
|
|
|
bool __more__ () const { return bool (it); }
|
2019-05-07 09:05:37 +02:00
|
|
|
void __next__ () { do ++it; while (it && !hb_has (p.get (), hb_get (f.get (), *it))); }
|
2019-05-11 17:38:06 +02:00
|
|
|
void __prev__ () { do --it; while (it && !hb_has (p.get (), hb_get (f.get (), *it))); }
|
2019-05-07 09:13:11 +02:00
|
|
|
hb_filter_iter_t __end__ () const { return hb_filter_iter_t (it.end (), p, f); }
|
2019-05-07 04:57:15 +02:00
|
|
|
bool operator != (const hb_filter_iter_t& o) const
|
2019-05-16 00:14:26 +02:00
|
|
|
{ return it != o.it; }
|
2019-01-09 20:32:33 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Iter it;
|
2019-05-07 09:05:37 +02:00
|
|
|
hb_reference_wrapper<Pred> p;
|
|
|
|
hb_reference_wrapper<Proj> f;
|
2019-01-09 20:32:33 +01:00
|
|
|
};
|
|
|
|
template <typename Pred, typename Proj>
|
|
|
|
struct hb_filter_iter_factory_t
|
|
|
|
{
|
2019-02-14 20:53:40 +01:00
|
|
|
hb_filter_iter_factory_t (Pred p, Proj f) : p (p), f (f) {}
|
2019-01-09 20:32:33 +01:00
|
|
|
|
2019-02-14 19:49:31 +01:00
|
|
|
template <typename Iter,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterator (Iter))>
|
2019-02-14 19:49:31 +01:00
|
|
|
hb_filter_iter_t<Iter, Pred, Proj>
|
2019-05-08 19:02:30 +02:00
|
|
|
operator () (Iter it)
|
2019-02-14 19:49:31 +01:00
|
|
|
{ return hb_filter_iter_t<Iter, Pred, Proj> (it, p, f); }
|
2019-01-09 20:32:33 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Pred p;
|
|
|
|
Proj f;
|
|
|
|
};
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-02-14 20:10:13 +01:00
|
|
|
{
|
2019-05-08 18:33:09 +02:00
|
|
|
template <typename Pred = decltype ((hb_identity)),
|
2019-02-14 20:10:13 +01:00
|
|
|
typename Proj = decltype ((hb_identity))>
|
|
|
|
hb_filter_iter_factory_t<Pred, Proj>
|
2019-05-08 18:33:09 +02:00
|
|
|
operator () (Pred&& p = hb_identity, Proj&& f = hb_identity) const
|
2019-02-14 20:10:13 +01:00
|
|
|
{ return hb_filter_iter_factory_t<Pred, Proj> (p, f); }
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_filter);
|
2018-12-21 23:21:19 +01:00
|
|
|
|
2019-04-01 22:00:06 +02:00
|
|
|
template <typename Redu, typename InitT>
|
2019-03-31 10:11:58 +02:00
|
|
|
struct hb_reduce_t
|
|
|
|
{
|
2019-04-01 22:00:06 +02:00
|
|
|
hb_reduce_t (Redu r, InitT init_value) : r (r), init_value (init_value) {}
|
2019-03-31 10:11:58 +02:00
|
|
|
|
|
|
|
template <typename Iter,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterator (Iter)),
|
2019-07-29 05:55:50 +02:00
|
|
|
typename AccuT = hb_decay<decltype (hb_declval (Redu) (hb_declval (InitT), hb_declval (typename Iter::item_t)))>>
|
2019-04-01 22:00:06 +02:00
|
|
|
AccuT
|
2019-05-08 19:02:30 +02:00
|
|
|
operator () (Iter it)
|
2019-03-31 10:11:58 +02:00
|
|
|
{
|
2019-04-01 22:00:06 +02:00
|
|
|
AccuT value = init_value;
|
2019-03-31 10:11:58 +02:00
|
|
|
for (; it; ++it)
|
2019-04-01 22:00:06 +02:00
|
|
|
value = r (value, *it);
|
2019-03-31 10:11:58 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Redu r;
|
2019-04-01 22:00:06 +02:00
|
|
|
InitT init_value;
|
2019-03-31 10:11:58 +02:00
|
|
|
};
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-03-31 10:11:58 +02:00
|
|
|
{
|
2019-04-01 22:00:06 +02:00
|
|
|
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); }
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_reduce);
|
2019-03-31 10:11:58 +02:00
|
|
|
|
|
|
|
|
2019-01-09 18:07:01 +01:00
|
|
|
/* hb_zip() */
|
2018-12-21 23:21:19 +01:00
|
|
|
|
2019-01-09 08:57:16 +01:00
|
|
|
template <typename A, typename B>
|
2019-01-09 18:49:12 +01:00
|
|
|
struct hb_zip_iter_t :
|
2019-01-30 02:15:12 +01:00
|
|
|
hb_iter_t<hb_zip_iter_t<A, B>,
|
2019-04-30 22:05:10 +02:00
|
|
|
hb_pair_t<typename A::item_t, typename B::item_t>>
|
2019-01-09 08:57:16 +01:00
|
|
|
{
|
2019-01-09 18:49:12 +01:00
|
|
|
hb_zip_iter_t () {}
|
2019-02-16 01:05:36 +01:00
|
|
|
hb_zip_iter_t (const A& a, const B& b) : a (a), b (b) {}
|
2019-01-09 08:57:16 +01:00
|
|
|
|
|
|
|
typedef hb_pair_t<typename A::item_t, typename B::item_t> __item_t__;
|
2019-01-25 15:34:03 +01:00
|
|
|
static constexpr bool is_random_access_iterator =
|
2019-01-30 02:15:12 +01:00
|
|
|
A::is_random_access_iterator &&
|
|
|
|
B::is_random_access_iterator;
|
2019-05-15 07:28:07 +02:00
|
|
|
/* Note. The following categorization is only valid if A is strictly sorted,
|
|
|
|
* ie. does NOT have duplicates. Previously I tried to categorize sortedness
|
|
|
|
* more granularly, see commits:
|
|
|
|
*
|
|
|
|
* 513762849a683914fc266a17ddf38f133cccf072
|
|
|
|
* 4d3cf2adb669c345cc43832d11689271995e160a
|
|
|
|
*
|
|
|
|
* However, that was not enough, since hb_sorted_array_t, hb_sorted_vector_t,
|
|
|
|
* SortedArrayOf, etc all needed to be updated to add more variants. At that
|
|
|
|
* point I saw it not worth the effort, and instead we now deem all sorted
|
|
|
|
* collections as essentially strictly-sorted for the purposes of zip.
|
|
|
|
*
|
|
|
|
* The above assumption is not as bad as it sounds. Our "sorted" comes with
|
|
|
|
* no guarantees. It's just a contract, put in place to help you remember,
|
|
|
|
* and think about, whether an iterator you receive is expected to be
|
|
|
|
* sorted or not. As such, it's not perfect by definition, and should not
|
|
|
|
* be treated so. The inaccuracy here just errs in the direction of being
|
|
|
|
* more permissive, so your code compiles instead of erring on the side of
|
|
|
|
* marking your zipped iterator unsorted in which case your code won't
|
|
|
|
* compile.
|
|
|
|
*
|
|
|
|
* This semantical limitation does NOT affect logic in any other place I
|
|
|
|
* know of as of this writing.
|
|
|
|
*/
|
|
|
|
static constexpr bool is_sorted_iterator = A::is_sorted_iterator;
|
2019-05-14 00:36:14 +02:00
|
|
|
|
2019-01-09 08:57:16 +01:00
|
|
|
__item_t__ __item__ () const { return __item_t__ (*a, *b); }
|
|
|
|
__item_t__ __item_at__ (unsigned i) const { return __item_t__ (a[i], b[i]); }
|
2019-05-09 20:30:27 +02:00
|
|
|
bool __more__ () const { return bool (a) && bool (b); }
|
2019-05-08 05:54:31 +02:00
|
|
|
unsigned __len__ () const { return hb_min (a.len (), b.len ()); }
|
2019-01-09 08:57:16 +01:00
|
|
|
void __next__ () { ++a; ++b; }
|
|
|
|
void __forward__ (unsigned n) { a += n; b += n; }
|
|
|
|
void __prev__ () { --a; --b; }
|
|
|
|
void __rewind__ (unsigned n) { a -= n; b -= n; }
|
2019-05-07 04:57:15 +02:00
|
|
|
hb_zip_iter_t __end__ () const { return hb_zip_iter_t (a.end (), b.end ()); }
|
2019-05-16 00:14:26 +02:00
|
|
|
/* Note, we should stop if ANY of the iters reaches end. As such two compare
|
|
|
|
* unequal if both items are unequal, NOT if either is unequal. */
|
2019-05-07 04:57:15 +02:00
|
|
|
bool operator != (const hb_zip_iter_t& o) const
|
2019-05-09 20:30:31 +02:00
|
|
|
{ return a != o.a && b != o.b; }
|
2019-01-09 08:57:16 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
A a;
|
|
|
|
B b;
|
|
|
|
};
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-02-14 20:07:12 +01:00
|
|
|
{
|
|
|
|
template <typename A, typename B,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterable (A) && hb_is_iterable (B))>
|
2019-05-08 21:25:34 +02:00
|
|
|
hb_zip_iter_t<hb_iter_type<A>, hb_iter_type<B>>
|
2019-05-09 20:07:38 +02:00
|
|
|
operator () (A&& a, B&& b) const
|
2019-05-08 21:25:34 +02:00
|
|
|
{ return hb_zip_iter_t<hb_iter_type<A>, hb_iter_type<B>> (hb_iter (a), hb_iter (b)); }
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_zip);
|
2019-01-09 08:57:16 +01:00
|
|
|
|
2019-02-14 20:40:22 +01:00
|
|
|
/* hb_apply() */
|
|
|
|
|
|
|
|
template <typename Appl>
|
|
|
|
struct hb_apply_t
|
|
|
|
{
|
2019-02-14 20:53:40 +01:00
|
|
|
hb_apply_t (Appl a) : a (a) {}
|
2019-02-14 20:40:22 +01:00
|
|
|
|
|
|
|
template <typename Iter,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterator (Iter))>
|
2019-05-08 19:02:30 +02:00
|
|
|
void operator () (Iter it)
|
2019-02-14 20:40:22 +01:00
|
|
|
{
|
|
|
|
for (; it; ++it)
|
2019-04-16 16:50:22 +02:00
|
|
|
(void) hb_invoke (a, *it);
|
2019-02-14 20:40:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Appl a;
|
|
|
|
};
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-02-14 20:40:22 +01:00
|
|
|
{
|
|
|
|
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); }
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_apply);
|
2019-02-14 20:40:22 +01:00
|
|
|
|
2019-05-09 20:02:56 +02:00
|
|
|
/* hb_iota()/hb_range() */
|
2019-05-08 19:35:02 +02:00
|
|
|
|
|
|
|
template <typename T, typename S>
|
2019-05-09 20:02:56 +02:00
|
|
|
struct hb_counter_iter_t :
|
|
|
|
hb_iter_t<hb_counter_iter_t<T, S>, T>
|
2019-05-08 19:35:02 +02:00
|
|
|
{
|
2019-05-09 20:25:02 +02:00
|
|
|
hb_counter_iter_t (T start, T end_, S step) : v (start), end_ (end_for (start, end_, step)), step (step) {}
|
2019-05-08 19:35:02 +02:00
|
|
|
|
|
|
|
typedef T __item_t__;
|
|
|
|
static constexpr bool is_random_access_iterator = true;
|
2019-05-15 07:28:07 +02:00
|
|
|
static constexpr bool is_sorted_iterator = true;
|
2019-05-09 20:14:06 +02:00
|
|
|
__item_t__ __item__ () const { return +v; }
|
2019-05-08 19:35:02 +02:00
|
|
|
__item_t__ __item_at__ (unsigned j) const { return v + j * step; }
|
2019-05-09 19:46:49 +02:00
|
|
|
bool __more__ () const { return v != end_; }
|
2019-05-09 21:07:45 +02:00
|
|
|
unsigned __len__ () const { return !step ? UINT_MAX : (end_ - v) / step; }
|
2019-05-08 19:35:02 +02:00
|
|
|
void __next__ () { v += step; }
|
|
|
|
void __forward__ (unsigned n) { v += n * step; }
|
|
|
|
void __prev__ () { v -= step; }
|
|
|
|
void __rewind__ (unsigned n) { v -= n * step; }
|
2019-05-09 20:10:31 +02:00
|
|
|
hb_counter_iter_t __end__ () const { return hb_counter_iter_t (end_, end_, step); }
|
2019-05-09 20:02:56 +02:00
|
|
|
bool operator != (const hb_counter_iter_t& o) const
|
2019-05-16 00:14:26 +02:00
|
|
|
{ return v != o.v; }
|
2019-05-08 19:35:02 +02:00
|
|
|
|
|
|
|
private:
|
2019-05-09 20:25:02 +02:00
|
|
|
static inline T end_for (T start, T end_, S step)
|
2019-05-08 19:35:02 +02:00
|
|
|
{
|
2019-05-09 20:25:02 +02:00
|
|
|
if (!step)
|
|
|
|
return end_;
|
2019-05-09 19:46:49 +02:00
|
|
|
auto res = (end_ - start) % step;
|
2019-05-08 19:35:02 +02:00
|
|
|
if (!res)
|
2019-05-09 19:46:49 +02:00
|
|
|
return end_;
|
|
|
|
end_ += step - res;
|
|
|
|
return end_;
|
2019-05-08 19:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T v;
|
2019-05-09 19:46:49 +02:00
|
|
|
T end_;
|
2019-05-08 19:35:02 +02:00
|
|
|
S step;
|
|
|
|
};
|
|
|
|
struct
|
|
|
|
{
|
2019-05-09 20:02:56 +02:00
|
|
|
template <typename T = unsigned, typename S = unsigned> hb_counter_iter_t<T, S>
|
|
|
|
operator () (T start = 0u, S&& step = 1u) const
|
2019-05-09 20:20:41 +02:00
|
|
|
{ return hb_counter_iter_t<T, S> (start, step >= 0 ? hb_int_max (T) : hb_int_min (T), step); }
|
2019-05-09 20:02:56 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_iota);
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
template <typename T = unsigned> hb_counter_iter_t<T, unsigned>
|
2019-05-08 19:35:02 +02:00
|
|
|
operator () (T end = (unsigned) -1) const
|
2019-05-09 20:02:56 +02:00
|
|
|
{ return hb_counter_iter_t<T, unsigned> (0, end, 1u); }
|
2019-05-08 19:35:02 +02:00
|
|
|
|
2019-05-09 20:02:56 +02:00
|
|
|
template <typename T, typename S = unsigned> hb_counter_iter_t<T, S>
|
2019-05-08 19:35:02 +02:00
|
|
|
operator () (T start, T end, S&& step = 1u) const
|
2019-05-09 20:02:56 +02:00
|
|
|
{ return hb_counter_iter_t<T, S> (start, end, step); }
|
2019-05-08 19:35:02 +02:00
|
|
|
}
|
2019-05-09 20:02:56 +02:00
|
|
|
HB_FUNCOBJ (hb_range);
|
2019-05-08 19:35:02 +02:00
|
|
|
|
2019-05-09 20:07:38 +02:00
|
|
|
/* 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);
|
|
|
|
|
2019-05-08 19:35:02 +02:00
|
|
|
|
2019-02-14 19:39:58 +01:00
|
|
|
/* hb_sink() */
|
|
|
|
|
|
|
|
template <typename Sink>
|
|
|
|
struct hb_sink_t
|
|
|
|
{
|
2019-05-08 18:48:55 +02:00
|
|
|
hb_sink_t (Sink s) : s (s) {}
|
2019-02-14 19:39:58 +01:00
|
|
|
|
2019-02-14 19:51:02 +01:00
|
|
|
template <typename Iter,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterator (Iter))>
|
2019-05-08 19:02:30 +02:00
|
|
|
void operator () (Iter it)
|
2019-02-14 19:39:58 +01:00
|
|
|
{
|
2019-02-14 19:51:02 +01:00
|
|
|
for (; it; ++it)
|
2019-02-14 19:39:58 +01:00
|
|
|
s << *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Sink s;
|
|
|
|
};
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-02-14 20:00:10 +01:00
|
|
|
{
|
|
|
|
template <typename Sink> hb_sink_t<Sink>
|
|
|
|
operator () (Sink&& s) const
|
|
|
|
{ return hb_sink_t<Sink> (s); }
|
2019-02-14 20:03:29 +01:00
|
|
|
|
|
|
|
template <typename Sink> hb_sink_t<Sink&>
|
|
|
|
operator () (Sink *s) const
|
|
|
|
{ return hb_sink_t<Sink&> (*s); }
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_sink);
|
2019-02-14 19:39:58 +01:00
|
|
|
|
2019-03-30 05:36:49 +01:00
|
|
|
/* hb-drain: hb_sink to void / blackhole / /dev/null. */
|
|
|
|
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-02-14 23:04:05 +01:00
|
|
|
{
|
|
|
|
template <typename Iter,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterator (Iter))>
|
|
|
|
void operator () (Iter it) const
|
2019-02-14 23:04:05 +01:00
|
|
|
{
|
|
|
|
for (; it; ++it)
|
|
|
|
(void) *it;
|
|
|
|
}
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_drain);
|
2019-01-09 18:07:01 +01:00
|
|
|
|
2019-03-30 05:36:49 +01:00
|
|
|
/* hb_unzip(): unzip and sink to two sinks. */
|
|
|
|
|
|
|
|
template <typename Sink1, typename Sink2>
|
|
|
|
struct hb_unzip_t
|
|
|
|
{
|
2019-05-08 19:02:30 +02:00
|
|
|
hb_unzip_t (Sink1 s1, Sink2 s2) : s1 (s1), s2 (s2) {}
|
2019-03-30 05:36:49 +01:00
|
|
|
|
|
|
|
template <typename Iter,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterator (Iter))>
|
2019-05-08 19:02:30 +02:00
|
|
|
void operator () (Iter it)
|
2019-03-30 05:36:49 +01:00
|
|
|
{
|
|
|
|
for (; it; ++it)
|
|
|
|
{
|
|
|
|
const auto &v = *it;
|
|
|
|
s1 << v.first;
|
|
|
|
s2 << v.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Sink1 s1;
|
|
|
|
Sink2 s2;
|
|
|
|
};
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-03-30 05:36:49 +01:00
|
|
|
{
|
|
|
|
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); }
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_unzip);
|
2019-03-30 05:36:49 +01:00
|
|
|
|
|
|
|
|
2019-02-16 01:55:08 +01:00
|
|
|
/* hb-all, hb-any, hb-none. */
|
|
|
|
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-02-16 01:55:08 +01:00
|
|
|
{
|
|
|
|
template <typename Iterable,
|
2019-05-08 18:33:09 +02:00
|
|
|
typename Pred = decltype ((hb_identity)),
|
2019-05-08 07:52:43 +02:00
|
|
|
typename Proj = decltype ((hb_identity)),
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterable (Iterable))>
|
2019-05-08 07:52:43 +02:00
|
|
|
bool operator () (Iterable&& c,
|
2019-05-08 18:33:09 +02:00
|
|
|
Pred&& p = hb_identity,
|
2019-05-08 07:52:43 +02:00
|
|
|
Proj&& f = hb_identity) const
|
2019-02-16 01:55:08 +01:00
|
|
|
{
|
|
|
|
for (auto it = hb_iter (c); it; ++it)
|
2019-05-08 18:32:19 +02:00
|
|
|
if (!hb_match (hb_forward<Pred> (p), hb_get (hb_forward<Proj> (f), *it)))
|
2019-02-16 01:55:08 +01:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_all);
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-02-16 01:55:08 +01:00
|
|
|
{
|
|
|
|
template <typename Iterable,
|
2019-05-08 18:33:09 +02:00
|
|
|
typename Pred = decltype ((hb_identity)),
|
2019-05-08 07:52:43 +02:00
|
|
|
typename Proj = decltype ((hb_identity)),
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterable (Iterable))>
|
2019-05-08 07:52:43 +02:00
|
|
|
bool operator () (Iterable&& c,
|
2019-05-08 18:33:09 +02:00
|
|
|
Pred&& p = hb_identity,
|
2019-05-08 07:52:43 +02:00
|
|
|
Proj&& f = hb_identity) const
|
2019-02-16 01:55:08 +01:00
|
|
|
{
|
|
|
|
for (auto it = hb_iter (c); it; ++it)
|
2019-05-08 18:32:19 +02:00
|
|
|
if (hb_match (hb_forward<Pred> (p), hb_get (hb_forward<Proj> (f), *it)))
|
2019-02-16 01:55:08 +01:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_any);
|
2019-04-15 21:39:03 +02:00
|
|
|
struct
|
2019-02-16 01:55:08 +01:00
|
|
|
{
|
|
|
|
template <typename Iterable,
|
2019-05-08 18:33:09 +02:00
|
|
|
typename Pred = decltype ((hb_identity)),
|
2019-05-08 07:52:43 +02:00
|
|
|
typename Proj = decltype ((hb_identity)),
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterable (Iterable))>
|
2019-05-08 07:52:43 +02:00
|
|
|
bool operator () (Iterable&& c,
|
2019-05-08 18:33:09 +02:00
|
|
|
Pred&& p = hb_identity,
|
2019-05-08 07:52:43 +02:00
|
|
|
Proj&& f = hb_identity) const
|
2019-02-16 01:55:08 +01:00
|
|
|
{
|
|
|
|
for (auto it = hb_iter (c); it; ++it)
|
2019-05-08 18:32:19 +02:00
|
|
|
if (hb_match (hb_forward<Pred> (p), hb_get (hb_forward<Proj> (f), *it)))
|
2019-02-16 01:55:08 +01:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-08 01:53:03 +02:00
|
|
|
}
|
|
|
|
HB_FUNCOBJ (hb_none);
|
2019-02-16 01:55:08 +01:00
|
|
|
|
2019-01-09 18:07:01 +01:00
|
|
|
/*
|
|
|
|
* Algorithms operating on iterators.
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename C, typename V,
|
2019-05-08 06:39:20 +02:00
|
|
|
hb_requires (hb_is_iterable (C))>
|
2019-01-09 18:07:01 +01:00
|
|
|
inline void
|
|
|
|
hb_fill (C& c, const V &v)
|
|
|
|
{
|
2019-02-15 02:10:04 +01:00
|
|
|
for (auto i = hb_iter (c); i; i++)
|
2019-03-30 04:23:07 +01:00
|
|
|
*i = v;
|
2019-01-09 18:07:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-01 04:00:09 +02:00
|
|
|
template <typename S, typename D>
|
|
|
|
inline void
|
2019-04-01 04:17:07 +02:00
|
|
|
hb_copy (S&& is, D&& id)
|
2019-01-09 18:07:01 +01:00
|
|
|
{
|
2019-04-01 04:17:07 +02:00
|
|
|
hb_iter (is) | hb_sink (id);
|
2019-01-09 18:07:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-21 06:08:05 +01:00
|
|
|
#endif /* HB_ITER_HH */
|