Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
The return type of functions that use hints should be consistent with…
Browse files Browse the repository at this point in the history
… std (#138)

* Fixes member functions of Table to be consistent with std.

Fixes member functions (that take hint) to return iterator instead of std::pair<iterator, bool>.
Fixed member functions are:
- emplace_hint
- insert
- try_emplace
- insert_or_assign

* Fixes tests for try_emplace and insert_or_assign
  • Loading branch information
acd1034 authored Dec 11, 2021
1 parent 5399432 commit 363753c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
27 changes: 14 additions & 13 deletions src/include/robin_hood.h
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,8 @@ class Table
}

template <typename... Args>
iterator emplace_hint(Args&&... args) {
iterator emplace_hint(const_iterator position, Args&&... args) {
(void)position;
return emplace(std::forward<Args>(args)...).first;
}

Expand All @@ -1836,16 +1837,16 @@ class Table
}

template <typename... Args>
std::pair<iterator, bool> try_emplace(const_iterator hint, const key_type& key,
iterator try_emplace(const_iterator hint, const key_type& key,
Args&&... args) {
(void)hint;
return try_emplace_impl(key, std::forward<Args>(args)...);
return try_emplace_impl(key, std::forward<Args>(args)...).first;
}

template <typename... Args>
std::pair<iterator, bool> try_emplace(const_iterator hint, key_type&& key, Args&&... args) {
iterator try_emplace(const_iterator hint, key_type&& key, Args&&... args) {
(void)hint;
return try_emplace_impl(std::move(key), std::forward<Args>(args)...);
return try_emplace_impl(std::move(key), std::forward<Args>(args)...).first;
}

template <typename Mapped>
Expand All @@ -1859,35 +1860,35 @@ class Table
}

template <typename Mapped>
std::pair<iterator, bool> insert_or_assign(const_iterator hint, const key_type& key,
iterator insert_or_assign(const_iterator hint, const key_type& key,
Mapped&& obj) {
(void)hint;
return insertOrAssignImpl(key, std::forward<Mapped>(obj));
return insertOrAssignImpl(key, std::forward<Mapped>(obj)).first;
}

template <typename Mapped>
std::pair<iterator, bool> insert_or_assign(const_iterator hint, key_type&& key, Mapped&& obj) {
iterator insert_or_assign(const_iterator hint, key_type&& key, Mapped&& obj) {
(void)hint;
return insertOrAssignImpl(std::move(key), std::forward<Mapped>(obj));
return insertOrAssignImpl(std::move(key), std::forward<Mapped>(obj)).first;
}

std::pair<iterator, bool> insert(const value_type& keyval) {
ROBIN_HOOD_TRACE(this)
return emplace(keyval);
}

std::pair<iterator, bool> insert(const_iterator hint, const value_type& keyval) {
iterator insert(const_iterator hint, const value_type& keyval) {
(void)hint;
return emplace(keyval);
return emplace(keyval).first;
}

std::pair<iterator, bool> insert(value_type&& keyval) {
return emplace(std::move(keyval));
}

std::pair<iterator, bool> insert(const_iterator hint, value_type&& keyval) {
iterator insert(const_iterator hint, value_type&& keyval) {
(void)hint;
return emplace(std::move(keyval));
return emplace(std::move(keyval)).first;
}

// Returns 1 if key is found, 0 otherwise.
Expand Down
10 changes: 4 additions & 6 deletions src/test/unit/unit_insert_or_assign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ TEST_CASE_TEMPLATE("insert_or_assign", Map,

key = "e";
value = "f";
ret = map.insert_or_assign(map.end(), key, value);
REQUIRE(ret.second);
REQUIRE(ret.first == map.find("e"));
auto pos = map.insert_or_assign(map.end(), key, value);
REQUIRE(pos == map.find("e"));
REQUIRE(map.size() == 3);
REQUIRE(map["e"] == "f");

ret = map.insert_or_assign(map.begin(), "e", "ff");
REQUIRE_FALSE(ret.second);
REQUIRE(ret.first == map.find("e"));
pos = map.insert_or_assign(map.begin(), "e", "ff");
REQUIRE(pos == map.find("e"));
REQUIRE(map.size() == 3);
REQUIRE(map["e"] == "ff");
}
14 changes: 6 additions & 8 deletions src/test/unit/unit_try_emplace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,16 @@ TEST_CASE_TEMPLATE("try_emplace", Map, robin_hood::unordered_flat_map<std::strin
REQUIRE(ret.first->second == RegularType(1U, "b"));
REQUIRE(map.size() == 2U);

ret = map.try_emplace(map.end(), "e", 67U, "f");
REQUIRE(ret.second);
REQUIRE(ret.first == map.find("e"));
REQUIRE(ret.first->second == RegularType(67U, "f"));
auto pos = map.try_emplace(map.end(), "e", 67U, "f");
REQUIRE(pos == map.find("e"));
REQUIRE(pos->second == RegularType(67U, "f"));
REQUIRE(map.size() == 3U);

key = "e";
RegularType value2(66U, "ff");
ret = map.try_emplace(map.begin(), key, value2);
REQUIRE_FALSE(ret.second);
REQUIRE(ret.first == map.find("e"));
REQUIRE(ret.first->second == RegularType(67U, "f"));
pos = map.try_emplace(map.begin(), key, value2);
REQUIRE(pos == map.find("e"));
REQUIRE(pos->second == RegularType(67U, "f"));
REQUIRE(map.size() == 3U);
}
#endif

0 comments on commit 363753c

Please sign in to comment.