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