3 #ifndef CDSLIB_CONTAINER_STRIPED_SET_STD_HASH_SET_ADAPTER_H
4 #define CDSLIB_CONTAINER_STRIPED_SET_STD_HASH_SET_ADAPTER_H
6 #include <cds/container/striped_set/adapter.h>
7 #include <unordered_set>
10 namespace cds { namespace container {
11 namespace striped_set {
13 // Copy policy for std::unordered_set
14 template <typename T, typename Hash, typename Pred, typename Alloc>
15 struct copy_item_policy< std::unordered_set< T, Hash, Pred, Alloc > >
17 typedef std::unordered_set< T, Hash, Pred, Alloc > set_type;
18 typedef typename set_type::iterator iterator;
20 void operator()( set_type& set, iterator itWhat )
22 set.insert( *itWhat );
26 template <typename T, typename Hash, typename Pred, typename Alloc>
27 struct swap_item_policy< std::unordered_set< T, Hash, Pred, Alloc > >: public copy_item_policy< std::unordered_set< T, Hash, Pred, Alloc > >
30 // Move policy for std::unordered_set
31 template <typename T, typename Hash, typename Pred, typename Alloc>
32 struct move_item_policy< std::unordered_set< T, Hash, Pred, Alloc > >
34 typedef std::unordered_set< T, Hash, Pred, Alloc > set_type;
35 typedef typename set_type::iterator iterator;
37 void operator()( set_type& set, iterator itWhat )
39 set.insert( std::move( *itWhat ) );
43 } // namespace striped_set
44 }} // namespace cds::container
46 namespace cds { namespace intrusive { namespace striped_set {
47 /// std::unordered_set adapter for hash set bucket
48 template <typename T, class Hash, class Pred, class Alloc, typename... Options>
49 class adapt< std::unordered_set<T, Hash, Pred, Alloc>, Options... >
52 typedef std::unordered_set<T, Hash, Pred, Alloc> container_type ; ///< underlying container type
55 /// Adapted container type
56 class adapted_container: public cds::container::striped_set::adapted_container
59 typedef typename container_type::value_type value_type ; ///< value type stored in the container
60 typedef typename container_type::iterator iterator ; ///< container iterator
61 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
63 static bool const has_find_with = false;
64 static bool const has_erase_with = false;
68 typedef typename cds::opt::select<
69 typename cds::opt::value<
70 typename cds::opt::find_option<
71 cds::opt::copy_policy< cds::container::striped_set::move_item >
75 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
76 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type> // not defined
77 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
87 template <typename Q, typename Func>
88 bool insert( const Q& val, Func f )
90 std::pair<iterator, bool> res = m_Set.insert( value_type(val) );
92 f( const_cast<value_type&>(*res.first) );
96 template <typename... Args>
97 bool emplace( Args&&... args )
99 std::pair<iterator, bool> res = m_Set.emplace( std::forward<Args>(args)... );
103 template <typename Q, typename Func>
104 std::pair<bool, bool> update( const Q& val, Func func, bool bAllowInsert )
106 if ( bAllowInsert ) {
107 std::pair<iterator, bool> res = m_Set.insert( value_type(val) );
108 func( res.second, const_cast<value_type&>(*res.first), val );
109 return std::make_pair( true, res.second );
112 auto it = m_Set.find( value_type(val));
113 if ( it == m_Set.end() )
114 return std::make_pair( false, false );
116 func( false, const_cast<value_type&>(*it), val );
117 return std::make_pair( true, false );
121 template <typename Q, typename Func>
122 bool erase( const Q& key, Func f )
124 const_iterator it = m_Set.find( value_type(key) );
125 if ( it == m_Set.end() )
127 f( const_cast<value_type&>(*it) );
132 template <typename Q, typename Func>
133 bool find( Q& val, Func f )
135 iterator it = m_Set.find( value_type(val) );
136 if ( it == m_Set.end() )
138 f( const_cast<value_type&>(*it), val );
142 /// Clears the container
148 iterator begin() { return m_Set.begin(); }
149 const_iterator begin() const { return m_Set.begin(); }
150 iterator end() { return m_Set.end(); }
151 const_iterator end() const { return m_Set.end(); }
153 void move_item( adapted_container& /*from*/, iterator itWhat )
155 assert( m_Set.find( *itWhat ) == m_Set.end() );
156 copy_item()( m_Set, itWhat );
166 typedef adapted_container type ; ///< Result of \p adapt metafunction
168 }}} // namespace cds::intrusive::striped_set
173 #endif // #ifndef CDSLIB_CONTAINER_STRIPED_SET_STD_HASH_SET_ADAPTER_H