[iter] Add .random_access()

This commit is contained in:
Behdad Esfahbod 2018-12-21 15:53:09 -05:00
parent 20f14b4aa6
commit 3dbe1e364c
2 changed files with 5 additions and 0 deletions

View File

@ -75,6 +75,7 @@ struct hb_iter_t
void prev () { thiz()->__prev__ (); }
void rewind (unsigned n) { thiz()->__rewind__ (n); }
unsigned len () const { return thiz()->__len__ (); }
bool random_access () const { return thiz()->__random_access__ (); }
/*
* Subclasses overrides:
@ -99,6 +100,9 @@ struct hb_iter_t
/* Population: Implement __len__() if known. */
unsigned __len__ () const
{ iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; }
/* Random access: Return true if len(), forward(), item_at() are fast. */
bool __random_access__ () const { return false; }
};

View File

@ -39,6 +39,7 @@ struct array_iter_t : hb_iter_t<array_iter_t<T>, T>
void __forward__ (unsigned n) { arr += n; }
void __rewind__ (unsigned n) { arr -= n; }
unsigned __len__ () const { return arr.len; }
bool __random_access__ () const { return true; }
private:
hb_array_t<T> arr;