diff --git a/include/tscore/List.h b/include/tscore/List.h index 25bfac5a498..db5a8bca23c 100644 --- a/include/tscore/List.h +++ b/include/tscore/List.h @@ -256,7 +256,94 @@ template struct DLL { } DLL() : head(nullptr) {} + + /// STL compliant iterator. + class iterator : public std::forward_iterator_tag + { + friend DLL; + using self_type = iterator; ///< Self reference type. + C *_spot = nullptr; ///< Current location in the list. + public: + /// STL compliance type definitions. + /// @{ + using size_type = size_t; + using value_type = C; + using pointer = C *; + using reference = C &; + /// @} + + iterator() = default; ///< Construct empty iteratgor. + + /// Dereference operator. + C &operator*(); + + /// Indirection operator. + C *operator->(); + + /// Pre-increment. + self_type &operator++(); + + /// Post-increment. + self_type operator++(int); + + /// Equality. + bool operator==(self_type const &that); + + /// Inequality. + bool operator!=(self_type const &that); + + protected: + /// Construct for a specific element. + iterator(C *spot) : _spot(spot) {} + }; + + iterator begin(); + iterator end(); + + class const_iterator : public std::forward_iterator_tag + { + friend DLL; + using self_type = const_iterator; + C *_spot = nullptr; ///< Current location in the list. + public: + /// STL compliance type definitions. + /// @{ + using size_type = size_t; + using value_type = C const; + using pointer = C const *; + using reference = C const &; + /// @} + + const_iterator() = default; ///< Construct empty iterator. + /// Convert from iterator to constant iterator. + const_iterator(iterator spot) : _spot(spot._spot) {} + + /// Dereference operator. + C const &operator*(); + + /// Indirection operator. + C const *operator->(); + + /// Pre-increment. + self_type &operator++(); + + /// Post-increment. + self_type operator++(int); + + /// Equality. + bool operator==(self_type const &that); + + /// Inequality. + bool operator!=(self_type const &that); + + protected: + const_iterator(C *spot) : _spot(spot) {} + }; + + const_iterator begin() const; + const_iterator end() const; }; + #define DList(_c, _f) DLL<_c, _c::Link##_##_f> #define DListM(_c, _m, _ml, _l) DLL<_c, _c::Link##_##_ml##_##_l> @@ -316,6 +403,124 @@ DLL::insert(C *e, C *after) prev(next(e)) = e; } +template +auto +DLL::begin() -> iterator +{ + return iterator{head}; +} + +template +auto +DLL::end() -> iterator +{ + return iterator{}; +} + +template +auto +DLL::begin() const -> const_iterator +{ + return const_iterator{head}; +} +template + +auto +DLL::end() const -> const_iterator +{ + return const_iterator{}; +} + +template +C & +DLL::iterator::operator*() +{ + return *_spot; +} + +template +C * +DLL::iterator::operator->() +{ + return _spot; +} + +template +auto +DLL::iterator::operator++() -> self_type & +{ + _spot = next(_spot); + return *this; +} + +template +auto +DLL::iterator::operator++(int) -> self_type +{ + self_type zret{*this}; + ++*this; + return zret; +} + +template +bool +DLL::iterator::operator==(DLL::iterator::self_type const &that) +{ + return _spot == that._spot; +} + +template +bool +DLL::iterator::operator!=(DLL::iterator::self_type const &that) +{ + return _spot != that._spot; +} + +template +C const & +DLL::const_iterator::operator*() +{ + return *_spot; +} + +template +C const * +DLL::const_iterator::operator->() +{ + return _spot; +} + +template +auto +DLL::const_iterator::operator++() -> self_type & +{ + _spot = next(_spot); + return *this; +} + +template +auto +DLL::const_iterator::operator++(int) -> self_type +{ + self_type zret{*this}; + ++*this; + return zret; +} + +template +bool +DLL::const_iterator::operator==(DLL::const_iterator::self_type const &that) +{ + return _spot == that._spot; +} + +template +bool +DLL::const_iterator::operator!=(DLL::const_iterator::self_type const &that) +{ + return _spot != that._spot; +} + // // List descriptor for queue of objects of type C. // diff --git a/include/tscore/Trie.h b/include/tscore/Trie.h index 67f5d938f42..d043eab6d71 100644 --- a/include/tscore/Trie.h +++ b/include/tscore/Trie.h @@ -53,6 +53,18 @@ template class Trie virtual ~Trie() { Clear(); } + using const_iterator = typename Queue::const_iterator; + const_iterator + begin() const + { + return m_value_list.begin(); + } + const_iterator + end() const + { + return m_value_list.end(); + } + private: static const int N_NODE_CHILDREN = 256;