Skip to content

Commit

Permalink
Add nth(size_type index) method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tessil committed Jul 4, 2017
1 parent 0468aca commit 657c9e5
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/ordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,14 @@ class ordered_hash: private Hash, private KeyEqual {
return insert_impl(std::forward<K>(key), hash_key(key)).first.value();
}

iterator nth(size_type index) {
return iterator(m_values.begin() + index);
}

const_iterator nth(size_type index) const {
return const_iterator(m_values.cbegin() + index);
}

const_reference front() const {
return m_values.front();
}
Expand Down Expand Up @@ -1547,9 +1555,31 @@ class ordered_map {
/*
* Other
*/

/**
* Requires index <= size().
*
* Return an iterator to the element at index. Return end() if index == size().
*/
iterator nth(size_type index) { return m_ht.nth(index); }

/**
* @copydoc nth(size_type index)
*/
const_iterator nth(size_type index) const { return m_ht.nth(index); }


/**
* Return const_reference to the first element. Requires the container to not be empty.
*/
const_reference front() const { return m_ht.front(); }

/**
* Return const_reference to the last element. Requires the container to not be empty.
*/
const_reference back() const { return m_ht.back(); }


/**
* Only available if ValueTypeContainer is a std::vector. Same as calling 'values_container().data()'.
*/
Expand Down Expand Up @@ -1964,9 +1994,31 @@ class ordered_set {
/*
* Other
*/

/**
* Requires index <= size().
*
* Return an iterator to the element at index. Return end() if index == size().
*/
iterator nth(size_type index) { return m_ht.nth(index); }

/**
* @copydoc nth(size_type index)
*/
const_iterator nth(size_type index) const { return m_ht.nth(index); }


/**
* Return const_reference to the first element. Requires the container to not be empty.
*/
const_reference front() const { return m_ht.front(); }

/**
* Return const_reference to the last element. Requires the container to not be empty.
*/
const_reference back() const { return m_ht.back(); }


/**
* Only available if ValueTypeContainer is a std::vector. Same as calling 'values_container().data()'.
*/
Expand Down
39 changes: 39 additions & 0 deletions tests/ordered_map_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,45 @@ BOOST_AUTO_TEST_CASE(test_swap) {
BOOST_CHECK(map2 == (tsl::ordered_map<int64_t, int64_t>{{1, 10}, {8, 80}, {3, 30}}));
}

/**
* front(), back()
*/
BOOST_AUTO_TEST_CASE(test_front_back) {
tsl::ordered_map<int64_t, int64_t> map = {{1, 10}, {2, 20}};
map.insert({0, 0});

BOOST_CHECK(map.front() == (std::pair<int64_t, int64_t>(1, 10)));
BOOST_CHECK(map.back() == (std::pair<int64_t, int64_t>(0, 0)));


map.clear();
map.insert({3, 30});
BOOST_CHECK(map.front() == (std::pair<int64_t, int64_t>(3, 30)));
BOOST_CHECK(map.back() == (std::pair<int64_t, int64_t>(3, 30)));
}

/**
* nth()
*/
BOOST_AUTO_TEST_CASE(test_nth) {
tsl::ordered_map<int64_t, int64_t> map = {{1, 10}, {2, 20}};
map.insert({0, 0});

BOOST_CHECK(map.nth(0) != map.end());
BOOST_CHECK(*map.nth(0) == (std::pair<int64_t, int64_t>(1, 10)));

BOOST_CHECK(map.nth(1) != map.end());
BOOST_CHECK(*map.nth(1) == (std::pair<int64_t, int64_t>(2, 20)));

BOOST_CHECK(map.nth(2) != map.end());
BOOST_CHECK(*map.nth(2) == (std::pair<int64_t, int64_t>(0, 0)));

BOOST_CHECK(map.nth(3) == map.end());


map.clear();
BOOST_CHECK(map.nth(0) == map.end());
}

/**
* other
Expand Down

0 comments on commit 657c9e5

Please sign in to comment.