Skip to content

Commit

Permalink
Issue #3, changes in the operator + of the iterator class (#4)
Browse files Browse the repository at this point in the history
* Add n + it operator in iterator class

* Remove it + it operator in the iterator class

* Change tab to space as indentation

* Add tests for arithmetic operations on RandomAccessIterator.
  • Loading branch information
HenryRLee authored and Tessil committed May 26, 2017
1 parent c9ae583 commit 22502db
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/ordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,15 @@ class ordered_hash {
friend bool operator>=(const ordered_iterator& lhs, const ordered_iterator& rhs) {
return lhs.m_iterator >= rhs.m_iterator;
}
friend difference_type operator+(const ordered_iterator& lhs, const ordered_iterator& rhs) {
return lhs.m_iterator + rhs.m_iterator;

friend ordered_iterator operator+(difference_type n, const ordered_iterator& it) {
return n + it.m_iterator;
}

friend difference_type operator-(const ordered_iterator& lhs, const ordered_iterator& rhs) {
return lhs.m_iterator - rhs.m_iterator;
}

private:
iterator m_iterator;
};
Expand Down
49 changes: 48 additions & 1 deletion tests/ordered_map_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,54 @@ BOOST_AUTO_TEST_CASE(test_reverse_iterator) {
}
}

BOOST_AUTO_TEST_CASE(test_iterator_arithmetic) {
tsl::ordered_map<int64_t, int64_t> map = {{1, 10}, {2, 20}, {3, 30},
{4, 40}, {5, 50}, {6, 60}};

tsl::ordered_map<int64_t, int64_t>::const_iterator it;
tsl::ordered_map<int64_t, int64_t>::const_iterator it2;

it = map.cbegin();
// it += n
it += 3;
BOOST_CHECK_EQUAL(it->second, 40);



// it + n
BOOST_CHECK_EQUAL((map.cbegin() + 3)->second, 40);
// n + it
BOOST_CHECK_EQUAL((3 + map.cbegin())->second, 40);



it = map.cbegin() + 4;
// it -= n
it -= 2;
BOOST_CHECK_EQUAL(it->second, 30);



// it - n
BOOST_CHECK_EQUAL((it - 1)->second, 20);



it = map.cbegin() + 2;
it2 = map.cbegin() + 4;
// it - it
BOOST_CHECK_EQUAL(it2 - it, 2);



// it[n]
BOOST_CHECK_EQUAL(map.cbegin()[2].second, 30);

it = map.cbegin() + 1;
// it[n]
BOOST_CHECK_EQUAL(it[2].second, 40);
}

/**
* operator=
*/
Expand Down Expand Up @@ -316,7 +364,6 @@ BOOST_AUTO_TEST_CASE(test_move) {
}



/**
* at
*/
Expand Down

0 comments on commit 22502db

Please sign in to comment.