diff --git a/include/fenwick/fenwick.h b/include/fenwick/fenwick.h index 55736c1..eb23c05 100644 --- a/include/fenwick/fenwick.h +++ b/include/fenwick/fenwick.h @@ -65,12 +65,25 @@ class fenwick { iterator begin() noexcept { return iterator(*this, 0); }; const_iterator begin() const noexcept { return const_iterator(*this, 0); }; - iterator end() noexcept { return iterator(*this, size_ - 1); }; - const_iterator end() const noexcept { return const_iterator(*this, size_ - 1); }; + iterator end() noexcept { return iterator(*this, size_); }; + const_iterator end() const noexcept { return const_iterator(*this, size_); }; const_iterator cbegin() const noexcept { return begin(); } const_iterator cend() const noexcept { return end(); } + reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }; + const_reverse_iterator rbegin() const noexcept { + return const_reverse_iterator(cend()); + }; + + reverse_iterator rend() noexcept { return reverse_iterator(begin()); }; + const_reverse_iterator rend() const noexcept { + return const_reverse_iterator(cbegin()); + }; + + const_reverse_iterator crbegin() const noexcept { return rbegin(); } + const_reverse_iterator crend() const noexcept { return rend(); } + /* * Capacity */ diff --git a/test/iterator.cc b/test/iterator.cc index dd19607..262482d 100644 --- a/test/iterator.cc +++ b/test/iterator.cc @@ -13,6 +13,14 @@ TEST(IteratorTest, BeginEnd) { fenwick::const_iterator cit; cit = tree.cbegin(); cit = tree.cend(); + + fenwick::reverse_iterator rit; + rit = tree.rbegin(); + rit = tree.rend(); + + fenwick::const_reverse_iterator crit; + crit = tree.crbegin(); + crit = tree.crend(); } TEST(IteratorTest, Iterate) { @@ -28,10 +36,20 @@ TEST(IteratorTest, Iterate) { } c = 0; - for (auto it = tree.cbegin(); it != tree.cend(); it++, c++) { ASSERT_EQ(c, *it); } + + ASSERT_EQ(4, *tree.rbegin()); + c = 4; + for (auto it = tree.rbegin(); it != tree.rend(); it++, c--) { + ASSERT_EQ(c, *it); + } + + c = 4; + for (auto it = tree.crbegin(); it != tree.crend(); it++, c--) { + ASSERT_EQ(c, *it); + } } }