3 #ifndef CDSUNIT_SET_TYPE_STD_H
4 #define CDSUNIT_SET_TYPE_STD_H
6 #include <unordered_set>
8 #include <mutex> //unique_lock
10 #include "set2/set_type.h"
16 template <typename Value, typename Hash, typename Less, typename EqualTo, typename Lock,
17 class Alloc = typename CDS_DEFAULT_ALLOCATOR::template rebind<Value>::other
20 : public std::unordered_set<
29 typedef std::unique_lock<Lock> scoped_lock;
30 typedef std::unordered_set<
38 typedef typename base_class::value_type value_type;
40 template <class Config>
41 StdHashSet( Config const& )
44 template <typename Key>
45 bool find( const Key& key )
47 scoped_lock al( m_lock );
48 return base_class::find( value_type(key) ) != base_class::end();
51 template <typename Key>
52 bool insert( Key const& key )
54 scoped_lock al( m_lock );
55 std::pair<typename base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
59 template <typename Key, typename Func>
60 bool insert( Key const& key, Func func )
62 scoped_lock al( m_lock );
63 std::pair<typename base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
71 template <typename T, typename Func>
72 std::pair<bool, bool> ensure( const T& key, Func func )
74 scoped_lock al( m_lock );
75 std::pair<typename base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
77 func( true, *pRet.first, key );
78 return std::make_pair( true, true );
81 func( false, *pRet.first, key );
82 return std::make_pair( true, false );
86 template <typename Key>
87 bool erase( const Key& key )
89 scoped_lock al( m_lock );
90 return base_class::erase( value_type(key) ) != 0;
93 template <typename T, typename Func>
94 bool erase( const T& key, Func func )
96 scoped_lock al( m_lock );
97 typename base_class::iterator it = base_class::find( value_type(key) );
98 if ( it != base_class::end() ) {
100 return base_class::erase( it ) != base_class::end();
105 std::ostream& dump( std::ostream& stm ) { return stm; }
108 static CDS_CONSTEXPR bool const c_bExtractSupported = false;
109 static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
112 template <typename Value, typename Less, typename Lock,
113 class Alloc = typename CDS_DEFAULT_ALLOCATOR::template rebind<Value>::other
115 class StdSet: public std::set<Value, Less, Alloc>
118 typedef std::unique_lock<Lock> scoped_lock;
119 typedef std::set<Value, Less, Alloc> base_class;
121 typedef typename base_class::key_type value_type;
123 template <class Config>
124 StdSet( Config const& )
127 template <typename Key>
128 bool find( const Key& key )
131 scoped_lock al( m_lock );
132 return base_class::find( v ) != base_class::end();
135 bool insert( value_type const& v )
137 scoped_lock al( m_lock );
138 return base_class::insert( v ).second;
141 template <typename Key, typename Func>
142 bool insert( Key const& key, Func func )
144 scoped_lock al( m_lock );
145 std::pair<typename base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
153 template <typename T, typename Func>
154 std::pair<bool, bool> ensure( const T& key, Func func )
156 scoped_lock al( m_lock );
157 std::pair<typename base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
159 func( true, *pRet.first, key );
160 return std::make_pair( true, true );
163 func( false, *pRet.first, key );
164 return std::make_pair( true, false );
168 template <typename Key>
169 bool erase( const Key& key )
171 scoped_lock al( m_lock );
172 return base_class::erase( value_type(key) ) != 0;
175 template <typename T, typename Func>
176 bool erase( const T& key, Func func )
178 scoped_lock al( m_lock );
179 typename base_class::iterator it = base_class::find( value_type(key) );
180 if ( it != base_class::end() ) {
183 base_class::erase( it );
189 std::ostream& dump( std::ostream& stm ) { return stm; }
192 static CDS_CONSTEXPR bool const c_bExtractSupported = false;
193 static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
196 template <typename Key, typename Val>
197 struct set_type< tag_StdSet, Key, Val >: public set_type_base< Key, Val >
199 typedef set_type_base< Key, Val > base_class;
200 typedef typename base_class::key_val key_val;
201 typedef typename base_class::less less;
202 typedef typename base_class::hash hash;
203 typedef typename base_class::equal_to equal_to;
205 typedef StdSet< key_val, less, cds::sync::spin > StdSet_Spin;
206 typedef StdSet< key_val, less, std::mutex > StdSet_Mutex;
207 typedef StdSet< key_val, less, lock::NoLock> StdSet_NoLock;
209 typedef StdHashSet< key_val, hash, less, equal_to, cds::sync::spin > StdHashSet_Spin;
210 typedef StdHashSet< key_val, hash, less, equal_to, std::mutex > StdHashSet_Mutex;
211 typedef StdHashSet< key_val, hash, less, equal_to, lock::NoLock > StdHashSet_NoLock;
216 #endif // #ifndef CDSUNIT_SET_TYPE_STD_H