Replace cds::ref/boost::ref with std::ref, remove cds::unref and cds/ref.h header
[libcds.git] / tests / unit / set2 / std_hash_set_vc9.h
1 //$$CDS-header$$
2
3 #ifndef __CDSUNIT_STD_HASH_SET_VC_H
4 #define __CDSUNIT_STD_HASH_SET_VC_H
5
6 #include <hash_set>
7
8 namespace set2 {
9
10     template <typename KeyVal, typename Hash, typename Less>
11     struct hash_less: public stdext::hash_compare< KeyVal, Less >
12     {
13         typedef stdext::hash_compare< KeyVal, Less> base_class;
14         size_t operator()(const KeyVal& kv) const
15         {
16             return Hash()(kv);
17         }
18
19         bool operator()(const KeyVal& kv1, const KeyVal& kv2) const
20         {
21             return base_class::operator()( kv1, kv2 );
22         }
23     };
24
25     template <typename Value, typename Hash, typename Less, typename EqualTo, typename Lock, class Alloc = CDS_DEFAULT_ALLOCATOR>
26     class StdHashSet: public stdext::hash_set<Value, hash_less<Value, Hash, Less >, Alloc>
27     {
28     public:
29         Lock m_lock;
30         typedef cds::lock::scoped_lock<Lock> AutoLock;
31         typedef stdext::hash_set<Value, hash_less<Value, Hash, Less >, Alloc> base_class;
32
33     public:
34         typedef typename base_class::value_type  pair_type;
35
36         StdHashSet( size_t nSetSize, size_t nLoadFactor )
37         {}
38
39         template <typename Key>
40         bool find( const Key& key )
41         {
42             AutoLock al( m_lock );
43             return base_class::find( value_type(key) ) != base_class::end();
44         }
45
46         template <typename Key>
47         bool insert( Key const& key )
48         {
49             AutoLock al( m_lock );
50             std::pair<base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
51             return pRet.second;
52         }
53
54         template <typename Key, typename Func>
55         bool insert( Key const& key, Func func )
56         {
57             AutoLock al( m_lock );
58             std::pair<base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
59             if ( pRet.second ) {
60                 func( *pRet.first );
61                 return true;
62             }
63             return false;
64         }
65
66         template <typename T, typename Func>
67         std::pair<bool, bool> ensure( const T& key, Func func )
68         {
69             AutoLock al( m_lock );
70             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
71             if ( pRet.second ) {
72                 func( true, *pRet.first, key );
73                 return std::make_pair( true, true );
74             }
75             else {
76                 func( false, *pRet.first, key );
77                 return std::make_pair( true, false );
78             }
79         }
80
81         template <typename Key>
82         bool erase( const Key& key )
83         {
84             AutoLock al( m_lock );
85             return base_class::erase( value_type(key) ) != 0;
86         }
87
88         template <typename T, typename Func>
89         bool erase( const T& key, Func func )
90         {
91             AutoLock al( m_lock );
92             base_class::iterator it = base_class::find( key );
93             if ( it != base_class::end() ) {
94                 func( *it );
95                 return base_class::erase( it ) != base_class::end();
96             }
97             return false;
98         }
99
100
101         std::ostream& dump( std::ostream& stm ) { return stm; }
102     };
103 }   // namespace set2
104
105 #endif  // #ifndef __CDSUNIT_STD_HASH_SET_VC_H