add custom operator=, extend insert functions

This commit is contained in:
2019-05-05 03:15:16 +07:00
parent 75bb71c1bd
commit b3fff2a573
4 changed files with 159 additions and 11 deletions

View File

@@ -133,6 +133,33 @@ namespace flat_hpp
insert(ilist);
}
flat_map(flat_map&& other)
: data_(std::move(other.data_))
, compare_(std::move(other.compare_)) {}
flat_map(const flat_map& other)
: data_(other.data_)
, compare_(other.compare_) {}
flat_map& operator=(flat_map&& other) {
if ( this != &other ) {
flat_map(std::move(other)).swap(*this);
}
return *this;
}
flat_map& operator=(const flat_map& other) {
if ( this != &other ) {
flat_map(other).swap(*this);
}
return *this;
}
flat_map& operator=(std::initializer_list<value_type> ilist) {
flat_map(ilist).swap(*this);
return *this;
}
allocator_type get_allocator() const {
return data_.get_allocator();
}
@@ -166,15 +193,21 @@ namespace flat_hpp
}
mapped_type& operator[](key_type&& key) {
return insert(value_type(std::move(key), mapped_type())).first->second;
const iterator iter = find(key);
return iter != end()
? iter->second
: emplace(std::move(key), mapped_type()).first->second;
}
mapped_type& operator[](const key_type& key) {
return insert(value_type(key, mapped_type())).first->second;
const iterator iter = find(key);
return iter != end()
? iter->second
: emplace(key, mapped_type()).first->second;
}
mapped_type& at(const key_type& key) {
const auto iter = find(key);
const iterator iter = find(key);
if ( iter != end() ) {
return iter->second;
}
@@ -182,13 +215,20 @@ namespace flat_hpp
}
const mapped_type& at(const key_type& key) const {
const auto iter = find(key);
const const_iterator iter = find(key);
if ( iter != end() ) {
return iter->second;
}
throw std::out_of_range("flat_map::at: key not found");
}
std::pair<iterator, bool> insert(value_type&& value) {
const iterator iter = lower_bound(value.first);
return iter == end() || compare_(value.first, iter->first)
? std::make_pair(data_.insert(iter, std::move(value)), true)
: std::make_pair(iter, false);
}
std::pair<iterator, bool> insert(const value_type& value) {
const iterator iter = lower_bound(value.first);
return iter == end() || compare_(value.first, iter->first)
@@ -196,13 +236,20 @@ namespace flat_hpp
: std::make_pair(iter, false);
}
iterator insert(const_iterator hint, const value_type& value) {
iterator insert(const_iterator hint, value_type&& value) {
return (hint == begin() || compare_((hint - 1)->first, value.first))
&& (hint == end() || compare_(value.first, hint->first))
? data_.insert(hint, std::move(value))
: insert(std::move(value)).first;
}
iterator insert(const_iterator hint, const value_type& value) {
return (hint == begin() || compare_((hint - 1)->first, value.first))
&& (hint == end() || compare_(value.first, hint->first))
? data_.insert(hint, value)
: insert(value).first;
}
template < typename InputIter >
void insert(InputIter first, InputIter last) {
while ( first != last ) {