Skip to content

Commit

Permalink
Add reverse iterator and const reverse iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryRLee committed Feb 9, 2019
1 parent f12ea00 commit 607f102
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
17 changes: 15 additions & 2 deletions include/fenwick/fenwick.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
20 changes: 19 additions & 1 deletion test/iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ TEST(IteratorTest, BeginEnd) {
fenwick<int>::const_iterator cit;
cit = tree.cbegin();
cit = tree.cend();

fenwick<int>::reverse_iterator rit;
rit = tree.rbegin();
rit = tree.rend();

fenwick<int>::const_reverse_iterator crit;
crit = tree.crbegin();
crit = tree.crend();
}

TEST(IteratorTest, Iterate) {
Expand All @@ -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);
}
}

}

0 comments on commit 607f102

Please sign in to comment.