Skip to content

Commit

Permalink
Correct set::emplace and set::emplace_hint
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Dec 10, 2023
1 parent 1a335e6 commit de3fa86
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/momo/stdish/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,15 @@ class set
return { res.position, res.inserted };
}

template<typename ValueArg>
momo::internal::EnableIf<std::is_same<key_type, typename std::decay<ValueArg>::type>::value,
std::pair<iterator, bool>> emplace(ValueArg&& valueArg)
{
typename TreeSet::InsertResult res = mTreeSet.InsertVar(
static_cast<const key_type&>(valueArg), std::forward<ValueArg>(valueArg));
return { res.position, res.inserted };
}

template<typename... ValueArgs>
iterator emplace_hint(const_iterator hint, ValueArgs&&... valueArgs)
{
Expand All @@ -486,6 +495,15 @@ class set
return mTreeSet.Add(hint, std::move(extItem));
}

template<typename ValueArg>
momo::internal::EnableIf<std::is_same<key_type, typename std::decay<ValueArg>::type>::value,
iterator> emplace_hint(const_iterator hint, ValueArg&& valueArg)
{
if (!pvCheckHint(hint, static_cast<const key_type&>(valueArg)))
return emplace(std::forward<ValueArg>(valueArg)).first;
return mTreeSet.AddVar(hint, std::forward<ValueArg>(valueArg));
}

iterator erase(const_iterator where)
{
return mTreeSet.Remove(where);
Expand Down
4 changes: 4 additions & 0 deletions test/sources/libcxx/multiset/emplace_hint.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ void main()
assert(r == m.begin());
assert(m.size() == 1);
assert(*r == 2);
r = m.emplace_hint(m.cend(), M::value_type(1));
assert(r == m.begin());
assert(m.size() == 2);
assert(*r == 1);
}
#ifdef LIBCPP_TEST_MIN_ALLOCATOR
{
Expand Down

0 comments on commit de3fa86

Please sign in to comment.