[iter] Make is_random_access_iterator a constant

We cannot rely on constexpr functions...
This commit is contained in:
Behdad Esfahbod 2018-12-30 20:51:31 -05:00
parent a685bfe8fc
commit 6cd96ba1ac
3 changed files with 6 additions and 9 deletions

View File

@ -53,6 +53,7 @@ struct hb_array_t :
* Iterator implementation. * Iterator implementation.
*/ */
typedef Type& __item_type__; typedef Type& __item_type__;
enum { is_random_access_iterator = true };
Type& __item_at__ (unsigned i) const Type& __item_at__ (unsigned i) const
{ {
if (unlikely (i >= length)) return CrapOrNull (Type); if (unlikely (i >= length)) return CrapOrNull (Type);
@ -72,7 +73,6 @@ struct hb_array_t :
length -= n; length -= n;
} }
unsigned __len__ () const { return length; } unsigned __len__ () const { return length; }
bool __random_access__ () const { return true; }
/* Extra operators. /* Extra operators.
*/ */

View File

@ -55,7 +55,8 @@ struct hb_iter_t
typedef Iter iter_t; typedef Iter iter_t;
typedef Item item_t; typedef Item item_t;
enum { item_size = hb_static_size (Item) }; enum { item_size = hb_static_size (Item) };
enum { is_iter = true }; enum { is_iterator = true };
enum { is_random_access_iterator = false };
private: private:
/* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */ /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
@ -80,7 +81,6 @@ struct hb_iter_t
iter_t operator ++ (int) { iter_t c (*thiz()); ++*thiz(); return c; } iter_t operator ++ (int) { iter_t c (*thiz()); ++*thiz(); return c; }
iter_t operator - (unsigned count) const { iter_t c (*thiz()); c -= count; return c; } iter_t operator - (unsigned count) const { iter_t c (*thiz()); c -= count; return c; }
iter_t operator -- (int) { iter_t c (*thiz()); --*thiz(); return c; } iter_t operator -- (int) { iter_t c (*thiz()); --*thiz(); return c; }
constexpr bool is_random_access () const { return thiz()->__random_access__ (); }
protected: protected:
hb_iter_t () {} hb_iter_t () {}
@ -92,7 +92,8 @@ struct hb_iter_t
using typename Name::iter_t; \ using typename Name::iter_t; \
using typename Name::item_t; \ using typename Name::item_t; \
using Name::item_size; \ using Name::item_size; \
using Name::is_iter; \ using Name::is_iterator; \
using Name::is_random_access_iterator; \
using Name::iter; \ using Name::iter; \
using Name::operator bool; \ using Name::operator bool; \
using Name::len; \ using Name::len; \
@ -105,7 +106,6 @@ struct hb_iter_t
using Name::operator --; \ using Name::operator --; \
using Name::operator +; \ using Name::operator +; \
using Name::operator -; \ using Name::operator -; \
using Name::is_random_access; \
static_assert (true, "") static_assert (true, "")
/* Base class for sorted iterators. Does not enforce anything. /* Base class for sorted iterators. Does not enforce anything.
@ -146,9 +146,6 @@ struct hb_iter_mixin_t
void __prev__ () { *thiz() -= 1; } void __prev__ () { *thiz() -= 1; }
void __rewind__ (unsigned n) { while (n--) --*thiz(); } void __rewind__ (unsigned n) { while (n--) --*thiz(); }
/* Random access: Implement if __item_at__(), __len__(), __forward__() are. */
constexpr bool __random_access__ () const { return false; }
protected: protected:
hb_iter_mixin_t () {} hb_iter_mixin_t () {}
hb_iter_mixin_t (const hb_iter_mixin_t &o HB_UNUSED) {} hb_iter_mixin_t (const hb_iter_mixin_t &o HB_UNUSED) {}

View File

@ -40,11 +40,11 @@ struct array_iter_t :
array_iter_t (hb_array_t<T> arr_) : arr (arr_) {} array_iter_t (hb_array_t<T> arr_) : arr (arr_) {}
typedef T& __item_type__; typedef T& __item_type__;
enum { is_random_access_iterator = true };
T& __item_at__ (unsigned i) const { return arr[i]; } T& __item_at__ (unsigned i) const { return arr[i]; }
void __forward__ (unsigned n) { arr += n; } void __forward__ (unsigned n) { arr += n; }
void __rewind__ (unsigned n) { arr -= n; } void __rewind__ (unsigned n) { arr -= n; }
unsigned __len__ () const { return arr.length; } unsigned __len__ () const { return arr.length; }
bool __random_access__ () const { return true; }
private: private:
hb_array_t<T> arr; hb_array_t<T> arr;