3 #ifndef __CDS_CONTAINER_STRIPED_SET_STD_HASH_SET_STD_ADAPTER_H
4 #define __CDS_CONTAINER_STRIPED_SET_STD_HASH_SET_STD_ADAPTER_H
6 #ifndef __CDS_CONTAINER_STRIPED_SET_STD_HASH_SET_ADAPTER_H
7 # error <cds/container/striped_set/std_hash_set.h> must be included instead of <cds/container/striped_set/std_hash_set_std.h> header
10 #include <cds/container/striped_set/adapter.h>
11 #include <unordered_set>
14 namespace cds { namespace container {
15 namespace striped_set {
17 // Copy policy for std::unordered_set
18 template <typename T, typename Hash, typename Pred, typename Alloc>
19 struct copy_item_policy< std::unordered_set< T, Hash, Pred, Alloc > >
21 typedef std::unordered_set< T, Hash, Pred, Alloc > set_type;
22 typedef typename set_type::iterator iterator;
24 void operator()( set_type& set, iterator itWhat )
26 set.insert( *itWhat );
30 template <typename T, typename Hash, typename Pred, typename Alloc>
31 struct swap_item_policy< std::unordered_set< T, Hash, Pred, Alloc > >: public copy_item_policy< std::unordered_set< T, Hash, Pred, Alloc > >
34 // Move policy for std::unordered_set
35 template <typename T, typename Hash, typename Pred, typename Alloc>
36 struct move_item_policy< std::unordered_set< T, Hash, Pred, Alloc > >
38 typedef std::unordered_set< T, Hash, Pred, Alloc > set_type;
39 typedef typename set_type::iterator iterator;
41 void operator()( set_type& set, iterator itWhat )
43 set.insert( std::move( *itWhat ) );
47 } // namespace striped_set
48 }} // namespace cds::container
50 namespace cds { namespace intrusive { namespace striped_set {
51 /// std::unordered_set adapter for hash set bucket
52 template <typename T, class Hash, class Pred, class Alloc, typename... Options>
53 class adapt< std::unordered_set<T, Hash, Pred, Alloc>, Options... >
56 typedef std::unordered_set<T, Hash, Pred, Alloc> container_type ; ///< underlying container type
59 /// Adapted container type
60 class adapted_container: public cds::container::striped_set::adapted_container
63 typedef typename container_type::value_type value_type ; ///< value type stored in the container
64 typedef typename container_type::iterator iterator ; ///< container iterator
65 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
67 static bool const has_find_with = false;
68 static bool const has_erase_with = false;
72 typedef typename cds::opt::select<
73 typename cds::opt::value<
74 typename cds::opt::find_option<
75 cds::opt::copy_policy< cds::container::striped_set::move_item >
79 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
80 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type> // not defined
81 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
91 template <typename Q, typename Func>
92 bool insert( const Q& val, Func f )
94 std::pair<iterator, bool> res = m_Set.insert( value_type(val) );
96 ::cds::unref(f)( const_cast<value_type&>(*res.first) );
100 template <typename... Args>
101 bool emplace( Args&&... args )
103 std::pair<iterator, bool> res = m_Set.emplace( std::forward<Args>(args)... );
107 template <typename Q, typename Func>
108 std::pair<bool, bool> ensure( const Q& val, Func func )
110 std::pair<iterator, bool> res = m_Set.insert( value_type(val) );
111 cds::unref(func)( res.second, const_cast<value_type&>(*res.first), val );
112 return std::make_pair( true, res.second );
115 template <typename Q, typename Func>
116 bool erase( const Q& key, Func f )
118 const_iterator it = m_Set.find( value_type(key) );
119 if ( it == m_Set.end() )
121 cds::unref(f)( const_cast<value_type&>(*it) );
126 template <typename Q, typename Func>
127 bool find( Q& val, Func f )
129 iterator it = m_Set.find( value_type(val) );
130 if ( it == m_Set.end() )
132 cds::unref(f)( const_cast<value_type&>(*it), val );
136 /// Clears the container
142 iterator begin() { return m_Set.begin(); }
143 const_iterator begin() const { return m_Set.begin(); }
144 iterator end() { return m_Set.end(); }
145 const_iterator end() const { return m_Set.end(); }
147 void move_item( adapted_container& /*from*/, iterator itWhat )
149 assert( m_Set.find( *itWhat ) == m_Set.end() );
150 copy_item()( m_Set, itWhat );
160 typedef adapted_container type ; ///< Result of \p adapt metafunction
162 }}} // namespace cds::intrusive::striped_set
167 #endif // #ifndef __CDS_CONTAINER_STRIPED_SET_STD_HASH_SET_STD_ADAPTER_H