mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2025-12-14 11:40:35 +07:00
insert_or_assign for std::sets
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include "meta_base/cvref_traits.hpp"
|
||||
#include "meta_base/fixed_function.hpp"
|
||||
#include "meta_base/hash_combiner.hpp"
|
||||
#include "meta_base/insert_or_assign.hpp"
|
||||
#include "meta_base/is_in_place_type.hpp"
|
||||
#include "meta_base/memory_buffer.hpp"
|
||||
#include "meta_base/noncopyable.hpp"
|
||||
|
||||
44
headers/meta.hpp/meta_base/insert_or_assign.hpp
Normal file
44
headers/meta.hpp/meta_base/insert_or_assign.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2021-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base.hpp"
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < typename Key, typename Compare, typename Allocator >
|
||||
typename std::set<Key, Compare, Allocator>::iterator
|
||||
insert_or_assign(std::set<Key, Compare, Allocator>& set,
|
||||
typename std::set<Key, Compare, Allocator>::value_type&& value)
|
||||
{
|
||||
auto&& [position, inserted] = set.insert(std::move(value));
|
||||
|
||||
if ( inserted ) {
|
||||
return position;
|
||||
}
|
||||
|
||||
auto node = set.extract(position++);
|
||||
node.value() = std::move(value);
|
||||
return set.insert(position, std::move(node));
|
||||
}
|
||||
|
||||
template < typename Key, typename Compare, typename Allocator >
|
||||
typename std::set<Key, Compare, Allocator>::iterator
|
||||
insert_or_assign(std::set<Key, Compare, Allocator>& set,
|
||||
const typename std::set<Key, Compare, Allocator>::value_type& value)
|
||||
{
|
||||
auto&& [position, inserted] = set.insert(value);
|
||||
|
||||
if ( inserted ) {
|
||||
return position;
|
||||
}
|
||||
|
||||
auto node = set.extract(position++);
|
||||
node.value() = value;
|
||||
return set.insert(position, std::move(node));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user