3 #ifndef __CDSUNIT_STD_HASH_SET_VC_H
4 #define __CDSUNIT_STD_HASH_SET_VC_H
7 #include <mutex> //unique_lock
11 template <typename KeyVal, typename Hash, typename Less>
12 struct hash_less: public stdext::hash_compare< KeyVal, Less >
14 typedef stdext::hash_compare< KeyVal, Less> base_class;
15 size_t operator()(const KeyVal& kv) const
20 bool operator()(const KeyVal& kv1, const KeyVal& kv2) const
22 return base_class::operator()( kv1, kv2 );
26 template <typename Value, typename Hash, typename Less, typename EqualTo, typename Lock, class Alloc = CDS_DEFAULT_ALLOCATOR>
27 class StdHashSet: public stdext::hash_set<Value, hash_less<Value, Hash, Less >, Alloc>
31 typedef std::unique_lock<Lock> AutoLock;
32 typedef stdext::hash_set<Value, hash_less<Value, Hash, Less >, Alloc> base_class;
35 typedef typename base_class::value_type pair_type;
37 StdHashSet( size_t nSetSize, size_t nLoadFactor )
40 template <typename Key>
41 bool find( const Key& key )
43 AutoLock al( m_lock );
44 return base_class::find( value_type(key) ) != base_class::end();
47 template <typename Key>
48 bool insert( Key const& key )
50 AutoLock al( m_lock );
51 std::pair<base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
55 template <typename Key, typename Func>
56 bool insert( Key const& key, Func func )
58 AutoLock al( m_lock );
59 std::pair<base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
67 template <typename T, typename Func>
68 std::pair<bool, bool> ensure( const T& key, Func func )
70 AutoLock al( m_lock );
71 std::pair<typename base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
73 func( true, *pRet.first, key );
74 return std::make_pair( true, true );
77 func( false, *pRet.first, key );
78 return std::make_pair( true, false );
82 template <typename Key>
83 bool erase( const Key& key )
85 AutoLock al( m_lock );
86 return base_class::erase( value_type(key) ) != 0;
89 template <typename T, typename Func>
90 bool erase( const T& key, Func func )
92 AutoLock al( m_lock );
93 base_class::iterator it = base_class::find( key );
94 if ( it != base_class::end() ) {
96 return base_class::erase( it ) != base_class::end();
102 std::ostream& dump( std::ostream& stm ) { return stm; }
106 #endif // #ifndef __CDSUNIT_STD_HASH_SET_VC_H