diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-20 16:20:45 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-20 16:20:45 +0100 |
commit | b1f91277a29ddac92479106543ecfd62ff99d152 (patch) | |
tree | 55aab26342b34c5d9e661f42077c480d444ef345 /src/hashiterator.h | |
parent | 6d42a8f99cbe98201a0d52ab276f6929b66cfe4f (diff) |
added hashiterator, this is going to be one of the implementation classes for the upcoming value iterator
Diffstat (limited to 'src/hashiterator.h')
-rw-r--r-- | src/hashiterator.h | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/hashiterator.h b/src/hashiterator.h new file mode 100644 index 0000000..c37c22b --- /dev/null +++ b/src/hashiterator.h @@ -0,0 +1,121 @@ +/** + * HashIterator.h + * + * This is an internal helper class that is used when iterating over a + * Php::Value object that holds a hash table (an array or an object that + * does not implement the Traversable interface - stl style. + * + * Thus, when you do c++ things like "for (auto &iter : value)", internally + * a ValueIterator object is being used. + * + * @author Emiel Bruijntjes + * @copyright 2014 Copernica BV + */ + +/** + * Forward declaration + */ +struct _hashtable; +struct bucket; + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition + */ +class HashIterator : public IteratorImpl +{ +public: + /** + * Constructor + * @param hashtable The hashtable to iterate over + * @param first Should it start on the first position? + */ + HashIterator(struct _hashtable *hashtable, bool first); + + /** + * Copy constructor + * @param that + */ + HashIterator(const ValueIterator &that); + + /** + * Destructor + */ + virtual ~HashIterator() {} + + /** + * Increment position (pre-increment) + * @return bool + */ + virtual bool increment() override; + + /** + * Decrement position (pre-decrement) + * @return bool + */ + virtual bool decrement() override; + + /** + * Compare with other iterator + * @param that + * @return bool + */ + virtual bool equals(const IteratorImpl *that) const override + { + // this always is a hash iterator + HashIterator *other = (HashIterator *)that; + + // compare the positions + return _position == other->_position; + } + + /** + * Derefecence, this returns a std::pair with the current key and value + * @return std::pair + */ + virtual const std::pair<Value,Value> ¤t() const override + { + return _current; + } + +private: + /** + * The hash table over which is being iterated + * @var HashTable + */ + struct _hashtable *_table = nullptr; + + /** + * The position in the hash table + * @var HashPosition + */ + struct bucket *_position = nullptr; + + /** + * The current key and value + * @var std::pair + */ + std::pair<Value,Value> _current; + + /** + * Read current key and value + * @return bool + */ + bool read(); + + /** + * Invalidate the iterator + * @return bool + */ + bool invalidate(); +}; + +/** + * End namespace + */ +} + |