Use emplace instead of insert in the backend as it's more appropriate.

[SVN r52394]
This commit is contained in:
Daniel James
2009-04-14 17:23:51 +00:00
parent a07e4c5810
commit ac2409627a
3 changed files with 41 additions and 41 deletions

View File

@@ -1604,67 +1604,67 @@ namespace boost {
// if hash function throws, basic exception safety // if hash function throws, basic exception safety
// strong otherwise // strong otherwise
iterator_base insert(value_type const& v) iterator_base emplace(value_type const& v)
{ {
// Create the node before rehashing in case it throws an // Create the node before rehashing in case it throws an
// exception (need strong safety in such a case). // exception (need strong safety in such a case).
node_constructor a(data_.allocators_); node_constructor a(data_.allocators_);
a.construct(v); a.construct(v);
return insert_impl(a); return emplace_impl(a);
} }
// Insert (equivalent key containers) // Insert (equivalent key containers)
// if hash function throws, basic exception safety // if hash function throws, basic exception safety
// strong otherwise // strong otherwise
iterator_base insert_hint(iterator_base const& it, value_type const& v) iterator_base emplace_hint(iterator_base const& it, value_type const& v)
{ {
// Create the node before rehashing in case it throws an // Create the node before rehashing in case it throws an
// exception (need strong safety in such a case). // exception (need strong safety in such a case).
node_constructor a(data_.allocators_); node_constructor a(data_.allocators_);
a.construct(v); a.construct(v);
return insert_hint_impl(it, a); return emplace_hint_impl(it, a);
} }
#else #else
// Insert (equivalent key containers) // Emplace (equivalent key containers)
// (I'm using an overloaded insert for both 'insert' and 'emplace') // (I'm using an overloaded emplace for both 'insert' and 'emplace')
// if hash function throws, basic exception safety // if hash function throws, basic exception safety
// strong otherwise // strong otherwise
template <class... Args> template <class... Args>
iterator_base insert(Args&&... args) iterator_base emplace(Args&&... args)
{ {
// Create the node before rehashing in case it throws an // Create the node before rehashing in case it throws an
// exception (need strong safety in such a case). // exception (need strong safety in such a case).
node_constructor a(data_.allocators_); node_constructor a(data_.allocators_);
a.construct(std::forward<Args>(args)...); a.construct(std::forward<Args>(args)...);
return insert_impl(a); return emplace_impl(a);
} }
// Insert (equivalent key containers) // Insert (equivalent key containers)
// (I'm using an overloaded insert for both 'insert' and 'emplace') // (I'm using an overloaded emplace for both 'insert' and 'emplace')
// if hash function throws, basic exception safety // if hash function throws, basic exception safety
// strong otherwise // strong otherwise
template <class... Args> template <class... Args>
iterator_base insert_hint(iterator_base const& it, Args&&... args) iterator_base emplace_hint(iterator_base const& it, Args&&... args)
{ {
// Create the node before rehashing in case it throws an // Create the node before rehashing in case it throws an
// exception (need strong safety in such a case). // exception (need strong safety in such a case).
node_constructor a(data_.allocators_); node_constructor a(data_.allocators_);
a.construct(std::forward<Args>(args)...); a.construct(std::forward<Args>(args)...);
return insert_hint_impl(it, a); return emplace_hint_impl(it, a);
} }
#endif #endif
iterator_base insert_impl(node_constructor& a) iterator_base emplace_impl(node_constructor& a)
{ {
key_type const& k = extract_key(a.get()->value()); key_type const& k = extract_key(a.get()->value());
size_type hash_value = hash_function()(k); size_type hash_value = hash_function()(k);
@@ -1685,13 +1685,13 @@ namespace boost {
); );
} }
iterator_base insert_hint_impl(iterator_base const& it, node_constructor& a) iterator_base emplace_hint_impl(iterator_base const& it, node_constructor& a)
{ {
// equal can throw, but with no effects // equal can throw, but with no effects
if (it == data_.end() || !equal(extract_key(a.get()->value()), *it)) { if (it == data_.end() || !equal(extract_key(a.get()->value()), *it)) {
// Use the standard insert if the iterator doesn't point // Use the standard insert if the iterator doesn't point
// to a matching key. // to a matching key.
return insert_impl(a); return emplace_impl(a);
} }
else { else {
// Find the first node in the group - so that the node // Find the first node in the group - so that the node
@@ -1724,7 +1724,7 @@ namespace boost {
{ {
size_type distance = unordered_detail::distance(i, j); size_type distance = unordered_detail::distance(i, j);
if(distance == 1) { if(distance == 1) {
insert(*i); emplace(*i);
} }
else { else {
// Only require basic exception safety here // Only require basic exception safety here
@@ -1754,7 +1754,7 @@ namespace boost {
{ {
// If only inserting 1 element, get the required // If only inserting 1 element, get the required
// safety since insert is only called once. // safety since insert is only called once.
for (; i != j; ++i) insert(*i); for (; i != j; ++i) emplace(*i);
} }
public: public:
@@ -1809,7 +1809,7 @@ namespace boost {
// if hash function throws, basic exception safety // if hash function throws, basic exception safety
// strong otherwise // strong otherwise
std::pair<iterator_base, bool> insert(value_type const& v) std::pair<iterator_base, bool> emplace(value_type const& v)
{ {
// No side effects in this initial code // No side effects in this initial code
key_type const& k = extract_key(v); key_type const& k = extract_key(v);
@@ -1849,12 +1849,12 @@ namespace boost {
// if hash function throws, basic exception safety // if hash function throws, basic exception safety
// strong otherwise // strong otherwise
iterator_base insert_hint(iterator_base const& it, value_type const& v) iterator_base emplace_hint(iterator_base const& it, value_type const& v)
{ {
if(it != data_.end() && equal(extract_key(v), *it)) if(it != data_.end() && equal(extract_key(v), *it))
return it; return it;
else else
return insert(v).first; return emplace(v).first;
} }
#else #else
@@ -1869,15 +1869,15 @@ namespace boost {
// if hash function throws, basic exception safety // if hash function throws, basic exception safety
// strong otherwise // strong otherwise
template<typename... Args> template<typename... Args>
std::pair<iterator_base, bool> insert(Args&&... args) std::pair<iterator_base, bool> emplace(Args&&... args)
{ {
return insert_impl( return emplace_impl(
extract_key(std::forward<Args>(args)...), extract_key(std::forward<Args>(args)...),
std::forward<Args>(args)...); std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
std::pair<iterator_base, bool> insert_impl(key_type const& k, Args&&... args) std::pair<iterator_base, bool> emplace_impl(key_type const& k, Args&&... args)
{ {
// No side effects in this initial code // No side effects in this initial code
size_type hash_value = hash_function()(k); size_type hash_value = hash_function()(k);
@@ -1911,7 +1911,7 @@ namespace boost {
} }
template<typename... Args> template<typename... Args>
std::pair<iterator_base, bool> insert_impl(no_key, Args&&... args) std::pair<iterator_base, bool> emplace_impl(no_key, Args&&... args)
{ {
// Construct the node regardless - in order to get the key. // Construct the node regardless - in order to get the key.
// It will be discarded if it isn't used // It will be discarded if it isn't used
@@ -1947,10 +1947,10 @@ namespace boost {
// if hash function throws, basic exception safety // if hash function throws, basic exception safety
// strong otherwise // strong otherwise
template<typename... Args> template<typename... Args>
iterator_base insert_hint(iterator_base const&, Args&&... args) iterator_base emplace_hint(iterator_base const&, Args&&... args)
{ {
// Life is complicated - just call the normal implementation. // Life is complicated - just call the normal implementation.
return insert(std::forward<Args>(args)...).first; return emplace(std::forward<Args>(args)...).first;
} }
#endif #endif

View File

@@ -224,25 +224,25 @@ namespace boost
std::pair<iterator, bool> emplace(Args&&... args) std::pair<iterator, bool> emplace(Args&&... args)
{ {
return boost::unordered_detail::pair_cast<iterator, bool>( return boost::unordered_detail::pair_cast<iterator, bool>(
base.insert(std::forward<Args>(args)...)); base.emplace(std::forward<Args>(args)...));
} }
template <class... Args> template <class... Args>
iterator emplace_hint(const_iterator hint, Args&&... args) iterator emplace_hint(const_iterator hint, Args&&... args)
{ {
return iterator(base.insert_hint(get(hint), std::forward<Args>(args)...)); return iterator(base.emplace_hint(get(hint), std::forward<Args>(args)...));
} }
#endif #endif
std::pair<iterator, bool> insert(const value_type& obj) std::pair<iterator, bool> insert(const value_type& obj)
{ {
return boost::unordered_detail::pair_cast<iterator, bool>( return boost::unordered_detail::pair_cast<iterator, bool>(
base.insert(obj)); base.emplace(obj));
} }
iterator insert(const_iterator hint, const value_type& obj) iterator insert(const_iterator hint, const value_type& obj)
{ {
return iterator(base.insert_hint(get(hint), obj)); return iterator(base.emplace_hint(get(hint), obj));
} }
template <class InputIterator> template <class InputIterator>
@@ -630,24 +630,24 @@ namespace boost
template <class... Args> template <class... Args>
iterator emplace(Args&&... args) iterator emplace(Args&&... args)
{ {
return iterator(base.insert(std::forward<Args>(args)...)); return iterator(base.emplace(std::forward<Args>(args)...));
} }
template <class... Args> template <class... Args>
iterator emplace_hint(const_iterator hint, Args&&... args) iterator emplace_hint(const_iterator hint, Args&&... args)
{ {
return iterator(base.insert_hint(get(hint), std::forward<Args>(args)...)); return iterator(base.emplace_hint(get(hint), std::forward<Args>(args)...));
} }
#endif #endif
iterator insert(const value_type& obj) iterator insert(const value_type& obj)
{ {
return iterator(base.insert(obj)); return iterator(base.emplace(obj));
} }
iterator insert(const_iterator hint, const value_type& obj) iterator insert(const_iterator hint, const value_type& obj)
{ {
return iterator(base.insert_hint(get(hint), obj)); return iterator(base.emplace_hint(get(hint), obj));
} }
template <class InputIterator> template <class InputIterator>

View File

@@ -222,26 +222,26 @@ namespace boost
std::pair<iterator, bool> emplace(Args&&... args) std::pair<iterator, bool> emplace(Args&&... args)
{ {
return boost::unordered_detail::pair_cast<iterator, bool>( return boost::unordered_detail::pair_cast<iterator, bool>(
base.insert(std::forward<Args>(args)...)); base.emplace(std::forward<Args>(args)...));
} }
template <class... Args> template <class... Args>
iterator emplace_hint(const_iterator hint, Args&&... args) iterator emplace_hint(const_iterator hint, Args&&... args)
{ {
return iterator( return iterator(
base.insert_hint(get(hint), std::forward<Args>(args)...)); base.emplace_hint(get(hint), std::forward<Args>(args)...));
} }
#endif #endif
std::pair<iterator, bool> insert(const value_type& obj) std::pair<iterator, bool> insert(const value_type& obj)
{ {
return boost::unordered_detail::pair_cast<iterator, bool>( return boost::unordered_detail::pair_cast<iterator, bool>(
base.insert(obj)); base.emplace(obj));
} }
iterator insert(const_iterator hint, const value_type& obj) iterator insert(const_iterator hint, const value_type& obj)
{ {
return iterator(base.insert_hint(get(hint), obj)); return iterator(base.emplace_hint(get(hint), obj));
} }
template <class InputIterator> template <class InputIterator>
@@ -599,24 +599,24 @@ namespace boost
template <class... Args> template <class... Args>
iterator emplace(Args&&... args) iterator emplace(Args&&... args)
{ {
return iterator(base.insert(std::forward<Args>(args)...)); return iterator(base.emplace(std::forward<Args>(args)...));
} }
template <class... Args> template <class... Args>
iterator emplace_hint(const_iterator hint, Args&&... args) iterator emplace_hint(const_iterator hint, Args&&... args)
{ {
return iterator(base.insert_hint(get(hint), std::forward<Args>(args)...)); return iterator(base.emplace_hint(get(hint), std::forward<Args>(args)...));
} }
#endif #endif
iterator insert(const value_type& obj) iterator insert(const value_type& obj)
{ {
return iterator(base.insert(obj)); return iterator(base.emplace(obj));
} }
iterator insert(const_iterator hint, const value_type& obj) iterator insert(const_iterator hint, const value_type& obj)
{ {
return iterator(base.insert_hint(get(hint), obj)); return iterator(base.emplace_hint(get(hint), obj));
} }
template <class InputIterator> template <class InputIterator>