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