Skip to content

Commit

Permalink
Generalize insert_range
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Oct 19, 2024
1 parent 6eaea5f commit 1d807dc
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 45 deletions.
7 changes: 4 additions & 3 deletions include/momo/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,11 +576,12 @@ class HashMap
return { PositionProxy(res.position), res.inserted };
}

template<internal::conceptMapArgIterator<Key> ArgIterator>
size_t Insert(ArgIterator begin, ArgIterator end)
template<internal::conceptMapArgIterator<Key> ArgIterator,
internal::conceptSentinel<ArgIterator> ArgSentinel>
size_t Insert(ArgIterator begin, ArgSentinel end)
{
size_t initCount = GetCount();
for (ArgIterator iter = begin; iter != end; ++iter)
for (ArgIterator iter = std::move(begin); iter != end; ++iter)
{
auto pair = internal::MapArgReferencer<>::GetReferencePair(iter);
InsertVar(std::forward<decltype(pair.first)>(pair.first),
Expand Down
7 changes: 4 additions & 3 deletions include/momo/HashMultiMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,11 @@ class HashMultiMap
return AddVar(keyIter, value);
}

template<internal::conceptMapArgIterator<Key> ArgIterator>
void Add(ArgIterator begin, ArgIterator end)
template<internal::conceptMapArgIterator<Key> ArgIterator,
internal::conceptSentinel<ArgIterator> ArgSentinel>
void Add(ArgIterator begin, ArgSentinel end)
{
for (ArgIterator iter = begin; iter != end; ++iter)
for (ArgIterator iter = std::move(begin); iter != end; ++iter)
{
auto pair = internal::MapArgReferencer<>::GetReferencePair(iter);
AddVar(std::forward<decltype(pair.first)>(pair.first),
Expand Down
7 changes: 4 additions & 3 deletions include/momo/HashSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -831,11 +831,12 @@ class HashSet
FastMovableFunctor(std::move(itemCreator)));
}

template<internal::conceptSetArgIterator<Item> ArgIterator>
size_t Insert(ArgIterator begin, ArgIterator end)
template<internal::conceptSetArgIterator<Item> ArgIterator,
internal::conceptSentinel<ArgIterator> ArgSentinel>
size_t Insert(ArgIterator begin, ArgSentinel end)
{
size_t initCount = GetCount();
for (ArgIterator iter = begin; iter != end; ++iter)
for (ArgIterator iter = std::move(begin); iter != end; ++iter)
{
auto&& ref = *iter;
InsertVar(ItemTraits::GetKey(std::as_const(ref)), std::forward<decltype(ref)>(ref));
Expand Down
4 changes: 2 additions & 2 deletions include/momo/MapUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@ namespace internal
typename KeyArg = decltype((*std::declval<ArgIterator>()).key),
typename ValueArg = decltype((*std::declval<ArgIterator>()).value)>
requires std::is_reference_v<KeyArg> && std::is_reference_v<ValueArg>
static auto GetReferencePair(ArgIterator iter) noexcept
static auto GetReferencePair(const ArgIterator& iter) noexcept
requires allowKeyValue
{
return std::pair<KeyArg, ValueArg>((*iter).key, (*iter).value);
Expand All @@ -1406,7 +1406,7 @@ namespace internal
typename ValueArg = decltype((*std::declval<ArgIterator>()).second)>
requires std::is_reference_v<std::iter_reference_t<ArgIterator>> ||
(std::is_reference_v<KeyArg> && std::is_reference_v<ValueArg>)
static auto GetReferencePair(ArgIterator iter) noexcept
static auto GetReferencePair(const ArgIterator& iter) noexcept
{
return pvGetReferencePair<KeyArg, ValueArg>(*iter);
}
Expand Down
6 changes: 5 additions & 1 deletion include/momo/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,15 @@ namespace internal
typename std::iterator_traits<Iterator>::iterator_category>;

template<typename Iterator>
concept conceptInputIterator = conceptIterator<Iterator, std::input_iterator_tag>;
concept conceptInputIterator = std::input_iterator<Iterator>;

template<typename Iterator>
concept conceptRandomIterator = conceptIterator<Iterator, std::random_access_iterator_tag>;

template<typename Sentinel, typename Iterator>
concept conceptSentinel =
requires (Iterator begin, Sentinel end) { { begin != end } -> std::convertible_to<bool>; };

template<typename Object,
size_t maxSize = sizeof(void*)>
concept conceptSmallAndTriviallyCopyable =
Expand Down
28 changes: 17 additions & 11 deletions include/momo/stdish/unordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,7 @@ class unordered_map
template<momo::internal::conceptInputIterator Iterator>
void insert(Iterator first, Iterator last)
{
if constexpr (momo::internal::conceptMapArgIterator<Iterator, key_type, false>)
{
mHashMap.Insert(first, last);
}
else
{
for (Iterator iter = first; iter != last; ++iter)
insert(*iter);
}
pvInsertRange(first, last);
}

void insert(std::initializer_list<value_type> values)
Expand All @@ -601,8 +593,7 @@ class unordered_map
template<momo::internal::conceptCompatibleRange<value_type> Range>
void insert_range(Range&& values)
{
for (auto&& ref : values)
insert(std::forward<decltype(ref)>(ref));
pvInsertRange(std::ranges::begin(values), std::ranges::end(values));
}

std::pair<iterator, bool> emplace()
Expand Down Expand Up @@ -1006,6 +997,21 @@ class unordered_map
}
#endif // MOMO_USE_UNORDERED_HINT_ITERATORS

template<momo::internal::conceptInputIterator Iterator,
momo::internal::conceptSentinel<Iterator> Sentinel>
void pvInsertRange(Iterator begin, Sentinel end)
{
if constexpr (momo::internal::conceptMapArgIterator<Iterator, key_type, false>)
{
mHashMap.Insert(std::move(begin), std::move(end));
}
else
{
for (Iterator iter = std::move(begin); iter != end; ++iter)
insert(*iter);
}
}

template<typename RKey, typename MappedArg>
std::pair<iterator, bool> pvInsertOrAssign(RKey&& key, MappedArg&& mappedArg)
{
Expand Down
28 changes: 17 additions & 11 deletions include/momo/stdish/unordered_multimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,15 +520,7 @@ class unordered_multimap
template<momo::internal::conceptInputIterator Iterator>
void insert(Iterator first, Iterator last)
{
if constexpr (momo::internal::conceptMapArgIterator<Iterator, key_type, false>)
{
mHashMultiMap.Add(first, last);
}
else
{
for (Iterator iter = first; iter != last; ++iter)
insert(*iter);
}
pvInsertRange(first, last);
}

void insert(std::initializer_list<value_type> values)
Expand All @@ -539,8 +531,7 @@ class unordered_multimap
template<momo::internal::conceptCompatibleRange<value_type> Range>
void insert_range(Range&& values)
{
for (auto&& ref : values)
insert(std::forward<decltype(ref)>(ref));
pvInsertRange(std::ranges::begin(values), std::ranges::end(values));
}

iterator emplace()
Expand Down Expand Up @@ -765,6 +756,21 @@ class unordered_multimap
std::forward<RKey>(std::get<0>(key)), std::move(mappedCreator)));
}

template<momo::internal::conceptInputIterator Iterator,
momo::internal::conceptSentinel<Iterator> Sentinel>
void pvInsertRange(Iterator begin, Sentinel end)
{
if constexpr (momo::internal::conceptMapArgIterator<Iterator, key_type, false>)
{
mHashMultiMap.Add(std::move(begin), std::move(end));
}
else
{
for (Iterator iter = std::move(begin); iter != end; ++iter)
insert(*iter);
}
}

private:
HashMultiMap mHashMultiMap;
};
Expand Down
28 changes: 17 additions & 11 deletions include/momo/stdish/unordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,7 @@ class unordered_set
template<momo::internal::conceptInputIterator Iterator>
void insert(Iterator first, Iterator last)
{
if constexpr (momo::internal::conceptSetArgIterator<Iterator, value_type>)
{
mHashSet.Insert(first, last);
}
else
{
for (Iterator iter = first; iter != last; ++iter)
emplace(*iter);
}
pvInsertRange(first, last);
}

void insert(std::initializer_list<value_type> values)
Expand All @@ -513,8 +505,7 @@ class unordered_set
template<momo::internal::conceptCompatibleRange<value_type> Range>
void insert_range(Range&& values)
{
for (auto&& ref : values)
emplace(std::forward<decltype(ref)>(ref));
pvInsertRange(std::ranges::begin(values), std::ranges::end(values));
}

template<typename... ValueArgs>
Expand Down Expand Up @@ -697,6 +688,21 @@ class unordered_set
return hashSet;
}

template<momo::internal::conceptInputIterator Iterator,
momo::internal::conceptSentinel<Iterator> Sentinel>
void pvInsertRange(Iterator begin, Sentinel end)
{
if constexpr (momo::internal::conceptSetArgIterator<Iterator, value_type>)
{
mHashSet.Insert(std::move(begin), std::move(end));
}
else
{
for (Iterator iter = std::move(begin); iter != end; ++iter)
emplace(*iter);
}
}

private:
HashSet mHashSet;
};
Expand Down

0 comments on commit 1d807dc

Please sign in to comment.