17 #ifndef MOMO_INCLUDE_GUARD_STDISH_SET
18 #define MOMO_INCLUDE_GUARD_STDISH_SET
20 #include "../TreeSet.h"
53 template<
typename TKey,
54 typename TLessFunc = std::less<TKey>,
55 typename TAllocator = std::allocator<TKey>,
56 typename TTreeSet = TreeSet<TKey, TreeTraitsStd<TKey, TLessFunc>, MemManagerStd<TAllocator>>>
60 typedef TTreeSet TreeSet;
97 template<
typename KeyArg>
98 struct IsValidKeyArg :
public TreeTraits::template IsValidKeyArg<KeyArg>
115 : mTreeSet(TreeTraits(), MemManager(alloc))
120 : mTreeSet(TreeTraits(lessFunc), MemManager(alloc))
124 template<
typename Iterator>
131 template<
typename Iterator>
134 :
set(lessFunc, alloc)
141 : mTreeSet(values, TreeTraits(), MemManager(alloc))
147 : mTreeSet(values, TreeTraits(lessFunc), MemManager(alloc))
151 #ifdef MOMO_HAS_CONTAINERS_RANGES
152 template<std::ranges::input_range Range>
153 requires std::convertible_to<std::ranges::range_reference_t<Range>,
value_type>
157 insert_range(std::forward<Range>(values));
160 template<std::ranges::input_range Range>
161 requires std::convertible_to<std::ranges::range_reference_t<Range>,
value_type>
162 set(std::from_range_t, Range&& values,
const key_compare& lessFunc,
164 :
set(lessFunc, alloc)
166 insert_range(std::forward<Range>(values));
168 #endif // MOMO_HAS_CONTAINERS_RANGES
171 : mTreeSet(std::move(right.mTreeSet))
176 noexcept(std::is_empty<allocator_type>::value)
177 : mTreeSet(pvCreateSet(
std::move(right), alloc))
182 : mTreeSet(right.mTreeSet)
187 : mTreeSet(right.mTreeSet, MemManager(alloc))
194 noexcept(std::is_empty<allocator_type>::value ||
195 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
199 bool propagate = std::is_empty<allocator_type>::value ||
200 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value;
202 mTreeSet = pvCreateSet(std::move(right), alloc);
211 bool propagate = std::is_empty<allocator_type>::value ||
212 std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value;
214 mTreeSet = TreeSet(right.mTreeSet, MemManager(alloc));
221 mTreeSet = TreeSet(values, mTreeSet.GetTreeTraits(), MemManager(
get_allocator()));
227 MOMO_ASSERT(std::allocator_traits<allocator_type>::propagate_on_container_swap::value
229 mTreeSet.Swap(right.mTreeSet);
249 return mTreeSet.GetBegin();
256 return mTreeSet.GetEnd();
297 return mTreeSet.GetTreeTraits().GetLessFunc();
307 return allocator_type(mTreeSet.GetMemManager().GetByteAllocator());
312 return std::allocator_traits<allocator_type>::max_size(
get_allocator());
317 return mTreeSet.GetCount();
322 return mTreeSet.IsEmpty();
332 return mTreeSet.Find(key);
337 template<
typename KeyArg>
341 return mTreeSet.Find(key);
350 return mTreeSet.GetKeyCount(key);
353 template<
typename KeyArg>
357 return mTreeSet.GetKeyCount(key);
362 return mTreeSet.ContainsKey(key);
365 template<
typename KeyArg>
369 return mTreeSet.ContainsKey(key);
374 return mTreeSet.GetLowerBound(key);
379 template<
typename KeyArg>
383 return mTreeSet.GetLowerBound(key);
392 return mTreeSet.GetUpperBound(key);
397 template<
typename KeyArg>
401 return mTreeSet.GetUpperBound(key);
413 if (iter ==
end() || mTreeSet.GetTreeTraits().IsLess(key, *iter))
414 return { iter, iter };
415 return { iter, std::next(iter) };
420 template<
typename KeyArg>
422 std::pair<const_iterator, const_iterator>>
equal_range(
const KeyArg& key)
const
439 if (!pvCheckHint(hint,
static_cast<const key_type&
>(value)))
440 return insert(std::move(value)).first;
441 return mTreeSet.Add(hint, std::move(value));
452 if (!pvCheckHint(hint, value))
453 return insert(value).first;
454 return mTreeSet.Add(hint, value);
462 std::move(NodeTypeProxy::GetExtractedItem(node)));
470 if (!pvCheckHint(hint, node.value()))
471 return insert(std::move(node)).position;
472 return mTreeSet.Add(hint, std::move(NodeTypeProxy::GetExtractedItem(node)));
475 template<
typename Iterator>
476 void insert(Iterator first, Iterator last)
478 pvInsertRange(first, last);
481 void insert(std::initializer_list<value_type> values)
483 mTreeSet.Insert(values);
486 #ifdef MOMO_HAS_CONTAINERS_RANGES
487 template<std::ranges::input_range Range>
488 requires std::convertible_to<std::ranges::range_reference_t<Range>,
value_type>
489 void insert_range(Range&& values)
491 pvInsertRange(std::ranges::begin(values), std::ranges::end(values));
493 #endif // MOMO_HAS_CONTAINERS_RANGES
495 template<
typename... ValueArgs>
496 std::pair<iterator, bool>
emplace(ValueArgs&&... valueArgs)
498 MemManager& memManager = mTreeSet.GetMemManager();
500 typedef typename TreeSet::ItemTraits::template Creator<ValueArgs...> ValueCreator;
501 extItem.
Create(ValueCreator(memManager, std::forward<ValueArgs>(valueArgs)...));
506 template<
typename ValueArg>
508 std::pair<iterator, bool>>
emplace(ValueArg&& valueArg)
511 static_cast<const key_type&
>(valueArg), std::forward<ValueArg>(valueArg));
515 template<
typename... ValueArgs>
518 MemManager& memManager = mTreeSet.GetMemManager();
520 typedef typename TreeSet::ItemTraits::template Creator<ValueArgs...> ValueCreator;
521 extItem.
Create(ValueCreator(memManager, std::forward<ValueArgs>(valueArgs)...));
522 if (!pvCheckHint(hint, extItem.
GetItem()))
523 return mTreeSet.Insert(std::move(extItem)).position;
524 return mTreeSet.Add(hint, std::move(extItem));
527 template<
typename ValueArg>
531 if (!pvCheckHint(hint,
static_cast<const key_type&
>(valueArg)))
532 return emplace(std::forward<ValueArg>(valueArg)).first;
533 return mTreeSet.AddVar(hint, std::forward<ValueArg>(valueArg));
538 return mTreeSet.Remove(where);
545 return mTreeSet.Remove(first, last);
550 return mTreeSet.Remove(key);
553 template<
typename ValueFilter>
556 return cont.mTreeSet.Remove(valueFilter);
570 template<
typename Set>
581 #ifdef MOMO_HAS_THREE_WAY_COMPARISON
582 friend auto operator<=>(
const set& left,
const set& right)
583 requires requires (
const_reference ref) { std::tie(ref) <=> std::tie(ref); }
586 {
return std::tie(value1) <=> std::tie(value2); };
587 return std::lexicographical_compare_three_way(left.begin(), left.end(),
588 right.begin(), right.end(), comp);
593 return std::lexicographical_compare(left.
begin(), left.
end(), right.
begin(), right.
end());
602 if (right.get_allocator() == alloc)
603 return std::move(right.mTreeSet);
604 TreeSet treeSet(right.mTreeSet.GetTreeTraits(), MemManager(alloc));
605 treeSet.MergeFrom(right.mTreeSet);
611 const TreeTraits& treeTraits = mTreeSet.GetTreeTraits();
613 : treeTraits.
IsLess(key1, key2);
618 if (hint !=
begin() && !pvIsOrdered(*std::prev(hint), key))
620 if (hint !=
end() && !pvIsOrdered(key, *hint))
629 template<
typename Iterator,
typename Sentinel>
631 void> pvInsertRange(Iterator
begin, Sentinel
end)
633 mTreeSet.Insert(std::move(
begin), std::move(
end));
636 template<
typename Iterator,
typename Sentinel>
638 void> pvInsertRange(Iterator
begin, Sentinel
end)
640 for (Iterator iter = std::move(
begin); iter !=
end; ++iter)
656 template<
typename TKey,
657 typename TLessFunc = std::less<TKey>,
658 typename TAllocator = std::allocator<TKey>,
659 typename TTreeSet = TreeSet<TKey, TreeTraitsStd<TKey, TLessFunc, true>,
660 MemManagerStd<TAllocator>>>
661 class multiset :
public set<TKey, TLessFunc, TAllocator, TTreeSet>
705 template<
typename... ValueArgs>
708 return Set::emplace(std::forward<ValueArgs>(valueArgs)...).first;
711 template<
typename ValueFilter>
714 return cont.get_nested_container().Remove(valueFilter);
718 #ifdef MOMO_HAS_DEDUCTION_GUIDES
720 #define MOMO_DECLARE_DEDUCTION_GUIDES(set) \
721 template<typename Iterator, \
722 typename Key = typename std::iterator_traits<Iterator>::value_type, \
723 typename Allocator = std::allocator<Key>, \
724 typename = internal::ordered_checker<Key, Allocator>> \
725 set(Iterator, Iterator, Allocator = Allocator()) \
726 -> set<Key, std::less<Key>, Allocator>; \
727 template<typename Iterator, typename LessFunc, \
728 typename Key = typename std::iterator_traits<Iterator>::value_type, \
729 typename Allocator = std::allocator<Key>, \
730 typename = internal::ordered_checker<Key, Allocator, LessFunc>> \
731 set(Iterator, Iterator, LessFunc, Allocator = Allocator()) \
732 -> set<Key, LessFunc, Allocator>; \
733 template<typename Key, \
734 typename Allocator = std::allocator<Key>, \
735 typename = internal::ordered_checker<Key, Allocator>> \
736 set(std::initializer_list<Key>, Allocator = Allocator()) \
737 -> set<Key, std::less<Key>, Allocator>; \
738 template<typename Key, typename LessFunc, \
739 typename Allocator = std::allocator<Key>, \
740 typename = internal::ordered_checker<Key, Allocator, LessFunc>> \
741 set(std::initializer_list<Key>, LessFunc, Allocator = Allocator()) \
742 -> set<Key, LessFunc, Allocator>;
744 MOMO_DECLARE_DEDUCTION_GUIDES(set)
745 MOMO_DECLARE_DEDUCTION_GUIDES(multiset)
747 #undef MOMO_DECLARE_DEDUCTION_GUIDES
749 #ifdef MOMO_HAS_CONTAINERS_RANGES
751 #define MOMO_DECLARE_DEDUCTION_GUIDES_RANGES(set) \
752 template<std::ranges::input_range Range, \
753 typename Key = std::ranges::range_value_t<Range>, \
754 typename Allocator = std::allocator<Key>, \
755 typename = internal::ordered_checker<Key, Allocator>> \
756 set(std::from_range_t, Range&&, Allocator = Allocator()) \
757 -> set<Key, std::less<Key>, Allocator>; \
758 template<std::ranges::input_range Range, typename LessFunc, \
759 typename Key = std::ranges::range_value_t<Range>, \
760 typename Allocator = std::allocator<Key>, \
761 typename = internal::ordered_checker<Key, Allocator, LessFunc>> \
762 set(std::from_range_t, Range&&, LessFunc, Allocator = Allocator()) \
763 -> set<Key, LessFunc, Allocator>;
765 MOMO_DECLARE_DEDUCTION_GUIDES_RANGES(set)
766 MOMO_DECLARE_DEDUCTION_GUIDES_RANGES(multiset)
768 #undef MOMO_DECLARE_DEDUCTION_GUIDES_RANGES
770 #endif // MOMO_HAS_CONTAINERS_RANGES
772 #endif // MOMO_HAS_DEDUCTION_GUIDES
778 #endif // MOMO_INCLUDE_GUARD_STDISH_SET