* Implemented C++20 contains() for associative containers as specified in P0458R2.

* Fixed serious bug in heterogeneous lookup functions (is_transparent was broken).
This commit is contained in:
Ion Gaztañaga
2018-08-13 15:36:00 +02:00
parent 1ad64316a4
commit 139db663a3
16 changed files with 220 additions and 16 deletions

View File

@@ -471,7 +471,7 @@ void launch_tests(const char *BoostContName, const char *StdContName)
extensions_time< BoostClass >(get_range_t::sorted_unique());
}
}catch(std::exception e){
}catch(std::exception &e){
std::cout << e.what();
}
}

View File

@@ -137,7 +137,7 @@ int main()
std::cout << "varray/std::vector total time comparison:";
compare_times(time_varray,time_standard_vector);
}catch(std::exception e){
}catch(std::exception &e){
std::cout << e.what();
}
return 0;

View File

@@ -1240,6 +1240,16 @@ use [*Boost.Container]? There are several reasons for that:
[section:release_notes Release Notes]
[section:release_notes_boost_1_69_00 Boost 1.69 Release]
* Implemented C++20 `contains()` for associative containers as specified in
[@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0458r2.html
P0458R2: Checking for Existence of an Element in Associative Containers].
* Fixed serious bug in heterogeneous lookup functions (is_transparent was broken).
[endsect]
[section:release_notes_boost_1_68_00 Boost 1.68 Release]
* Improved correctness of [classref boost::container::adaptive_pool adaptive_pool] and many parameters are now compile-time

View File

@@ -1201,6 +1201,15 @@ class flat_tree
return n;
}
BOOST_CONTAINER_FORCEINLINE bool contains(const key_type& x) const
{ return this->find(x) != this->cend(); }
template<typename K>
BOOST_CONTAINER_FORCEINLINE
typename dtl::enable_if_transparent<key_compare, K, bool>::type
contains(const K& x) const
{ return this->find(x) != this->cend(); }
template<class C2>
BOOST_CONTAINER_FORCEINLINE void merge_unique(flat_tree<Value, KeyOfValue, C2, AllocatorOrContainer>& source)
{

View File

@@ -76,19 +76,28 @@ struct select1st
{ return const_cast<type&>(x.first); }
};
template<typename T>
struct void_t { typedef void type; };
template <class T, class=void>
struct is_transparent
struct is_transparent_base
{
static const bool value = false;
};
template <class T>
struct is_transparent<T, typename T::is_transparent>
struct is_transparent_base<T, typename void_t<typename T::is_transparent>::type>
{
static const bool value = true;
};
template <typename C, typename K, typename R>
template <class T>
struct is_transparent
: is_transparent_base<T>
{};
template <typename C, class /*Dummy*/, typename R>
struct enable_if_transparent
: boost::move_detail::enable_if_c<dtl::is_transparent<C>::value, R>
{};

View File

@@ -1297,6 +1297,15 @@ class tree
count(const K& k) const
{ return size_type(this->icont().count(k, KeyNodeCompare(key_comp()))); }
BOOST_CONTAINER_FORCEINLINE bool contains(const key_type& x) const
{ return this->find(x) != this->cend(); }
template<typename K>
BOOST_CONTAINER_FORCEINLINE
typename dtl::enable_if_transparent<key_compare, K, bool>::type
contains(const K& x) const
{ return this->find(x) != this->cend(); }
BOOST_CONTAINER_FORCEINLINE iterator lower_bound(const key_type& k)
{ return iterator(this->icont().lower_bound(k, KeyNodeCompare(key_comp()))); }

View File

@@ -1340,6 +1340,24 @@ class flat_map
BOOST_CONTAINER_FORCEINLINE size_type count(const K& x) const
{ return static_cast<size_type>(m_flat_tree.find(x) != m_flat_tree.end()); }
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
bool contains(const key_type& x) const
{ return m_flat_tree.find(x) != m_flat_tree.end(); }
//! <b>Requires</b>: This overload is available only if
//! key_compare::is_transparent exists.
//!
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
template<typename K>
bool contains(const K& x) const
{ return m_flat_tree.find(x) != m_flat_tree.end(); }
//! <b>Returns</b>: An iterator pointing to the first element with key not less
//! than k, or a.end() if such an element is not found.
//!
@@ -2631,6 +2649,24 @@ class flat_multimap
BOOST_CONTAINER_FORCEINLINE size_type count(const K& x) const
{ return m_flat_tree.count(x); }
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
bool contains(const key_type& x) const
{ return m_flat_tree.find(x) != m_flat_tree.end(); }
//! <b>Requires</b>: This overload is available only if
//! key_compare::is_transparent exists.
//!
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
template<typename K>
bool contains(const K& x) const
{ return m_flat_tree.find(x) != m_flat_tree.end(); }
//! <b>Returns</b>: An iterator pointing to the first element with key not less
//! than k, or a.end() if such an element is not found.
//!

View File

@@ -912,6 +912,23 @@ class flat_set
{ return static_cast<size_type>(this->tree_t::find(x) != this->tree_t::cend()); }
#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
bool contains(const key_type& x) const;
//! <b>Requires</b>: This overload is available only if
//! key_compare::is_transparent exists.
//!
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
template<typename K>
bool contains(const K& x) const;
//! <b>Returns</b>: An iterator pointing to the first element with key not less
//! than k, or a.end() if such an element is not found.
//!
@@ -1685,6 +1702,13 @@ class flat_multiset
//! @copydoc ::boost::container::flat_set::count(const key_type& ) const
size_type count(const key_type& x) const;
//! @copydoc ::boost::container::flat_set::contains(const key_type& ) const
bool contains(const key_type& x) const;
//! @copydoc ::boost::container::flat_set::contains(const K& ) const
template<typename K>
bool contains(const K& x) const;
//! @copydoc ::boost::container::flat_set::lower_bound(const key_type& )
iterator lower_bound(const key_type& x);

View File

@@ -1119,6 +1119,22 @@ class map
#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
bool contains(const key_type& x) const;
//! <b>Requires</b>: This overload is available only if
//! key_compare::is_transparent exists.
//!
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
template<typename K>
bool contains(const K& x) const;
//! <b>Returns</b>: An iterator pointing to the first element with key not less
//! than k, or a.end() if such an element is not found.
//!
@@ -2017,6 +2033,22 @@ class multimap
template<typename K>
size_type count(const K& x) const;
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
bool contains(const key_type& x) const;
//! <b>Requires</b>: This overload is available only if
//! key_compare::is_transparent exists.
//!
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
template<typename K>
bool contains(const K& x) const;
//! <b>Returns</b>: An iterator pointing to the first element with key not less
//! than k, or a.end() if such an element is not found.
//!

View File

@@ -772,14 +772,24 @@ class set
BOOST_CONTAINER_FORCEINLINE size_type count(const K& x) const
{ return static_cast<size_type>(this->find(x) != this->cend()); }
//! <b>Returns</b>: The number of elements with key equivalent to x.
//!
//! <b>Complexity</b>: log(size())+count(k)
BOOST_CONTAINER_FORCEINLINE size_type count(const key_type& x)
{ return static_cast<size_type>(this->base_t::find(x) != this->base_t::end()); }
#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
bool contains(const key_type& x) const;
//! <b>Requires</b>: This overload is available only if
//! key_compare::is_transparent exists.
//!
//! <b>Returns</b>: Returns true if there is an element with key
//! equivalent to key in the container, otherwise false.
//!
//! <b>Complexity</b>: log(size()).
template<typename K>
bool contains(const K& x) const;
//! <b>Returns</b>: An iterator pointing to the first element with key not less
//! than k, or a.end() if such an element is not found.
//!
@@ -1461,6 +1471,13 @@ class multiset
template<typename K>
size_type count(const K& x) const;
//! @copydoc ::boost::container::set::contains(const key_type& ) const
bool contains(const key_type& x) const;
//! @copydoc ::boost::container::set::contains(const K& ) const
template<typename K>
bool contains(const K& x) const;
//! @copydoc ::boost::container::set::lower_bound(const key_type& )
iterator lower_bound(const key_type& x);

View File

@@ -454,6 +454,8 @@ struct get_real_stored_allocator<flat_multimap<Key, T, Compare, Allocator> >
bool test_heterogeneous_lookups()
{
BOOST_STATIC_ASSERT((dtl::is_transparent<less_transparent>::value));
BOOST_STATIC_ASSERT(!(dtl::is_transparent<std::less<int> >::value));
typedef flat_map<int, char, less_transparent> map_t;
typedef flat_multimap<int, char, less_transparent> mmap_t;
typedef map_t::value_type value_type;
@@ -498,6 +500,16 @@ bool test_heterogeneous_lookups()
if(cmmap1.count(find_me) != 2)
return false;
//contains
if(!map1.contains(find_me))
return false;
if(!cmap1.contains(find_me))
return false;
if(!mmap1.contains(find_me))
return false;
if(!cmmap1.contains(find_me))
return false;
//lower_bound
if(map1.lower_bound(find_me)->second != 'd')
return false;
@@ -709,4 +721,3 @@ int main()
}
#include <boost/container/detail/config_end.hpp>

View File

@@ -495,6 +495,16 @@ bool test_heterogeneous_lookups()
if(cmset1.count(find_me) != 2)
return false;
//contains
if(!set1.contains(find_me))
return false;
if(!cset1.contains(find_me))
return false;
if(!mset1.contains(find_me))
return false;
if(!cmset1.contains(find_me))
return false;
//lower_bound
if(*set1.lower_bound(find_me) != 2)
return false;

View File

@@ -366,6 +366,16 @@ bool test_heterogeneous_lookups()
if(cmmap1.count(find_me) != 2)
return false;
//contains
if(!map1.contains(find_me))
return false;
if(!cmap1.contains(find_me))
return false;
if(!mmap1.contains(find_me))
return false;
if(!cmmap1.contains(find_me))
return false;
//lower_bound
if(map1.lower_bound(find_me)->second != 'd')
return false;

View File

@@ -628,16 +628,24 @@ int map_test()
}
}
//Compare count with std containers
//Compare count/contains with std containers
for(int i = 0; i < MaxElem; ++i){
IntType k(i);
if(boostmap.count(k) != stdmap.count(i)){
return -1;
}
if(boostmap.contains(k) != (stdmap.find(i) != stdmap.end())){
return -1;
}
if(boostmultimap.count(k) != stdmultimap.count(i)){
return -1;
}
if(boostmultimap.contains(k) != (stdmultimap.find(i) != stdmultimap.end())){
return -1;
}
}
//Now do count exercise

View File

@@ -363,6 +363,16 @@ bool test_heterogeneous_lookups()
if(cmset1.count(find_me) != 2)
return false;
//contains
if(!set1.contains(find_me))
return false;
if(!cset1.contains(find_me))
return false;
if(!mset1.contains(find_me))
return false;
if(!cmset1.contains(find_me))
return false;
//lower_bound
if(*set1.lower_bound(find_me) != 2)
return false;

View File

@@ -580,11 +580,20 @@ int set_test ()
//Compare count with std containers
for(int i = 0; i < MaxElem; ++i){
IntType count_me(i);
if(boostset.count(count_me) != stdset.count(i)){
IntType k(i);
if(boostset.count(k) != stdset.count(i)){
return -1;
}
if(boostmultiset.count(count_me) != stdmultiset.count(i)){
if(boostset.contains(k) != (stdset.find(i) != stdset.end())){
return -1;
}
if(boostmultiset.count(k) != stdmultiset.count(i)){
return -1;
}
if(boostmultiset.contains(k) != (stdmultiset.find(i) != stdmultiset.end())){
return -1;
}
}