rename lazy_list_ptb.h to lazy_list_dhp.h
[libcds.git] / tests / unit / map2 / std_map_vc.h
1 //$$CDS-header$$
2
3 #ifndef __CDSUNIT_STD_MAP_VC_H
4 #define __CDSUNIT_STD_MAP_VC_H
5
6 #include <map>
7 #include <functional>   // ref
8 #include <mutex>    //unique_lock
9
10 namespace map2 {
11     template <typename Key, typename Value, typename Lock, class Alloc = CDS_DEFAULT_ALLOCATOR>
12     class StdMap: public std::map<Key, Value, std::less<Key>, Alloc>
13     {
14         Lock m_lock;
15         typedef std::unique_lock<Lock> AutoLock;
16         typedef std::map<Key, Value, std::less<Key>, Alloc> base_class;
17     public:
18         typedef typename base_class::mapped_type value_type;
19         typedef typename base_class::value_type  pair_type;
20         typedef size_t      item_counter;
21
22         StdMap( size_t nMapSize, size_t nLoadFactor )
23         {}
24
25         bool find( const Key& key )
26         {
27             AutoLock al( m_lock );
28             return base_class::find( key ) != base_class::end();
29         }
30
31         bool insert( const Key& key, const Value& val )
32         {
33             AutoLock al( m_lock );
34             return base_class::insert( base_class::value_type(key, val)).second;
35         }
36
37         template <typename T, typename Func>
38         bool insert( const Key& key, const T& val, Func func )
39         {
40             AutoLock al( m_lock );
41             std::pair<base_class::iterator, bool> pRet = base_class::insert( base_class::value_type(key, Value() ));
42             if ( pRet.second ) {
43                 func( pRet.first->second, val );
44                 return true;
45             }
46             return false;
47         }
48
49         template <typename T, typename Func>
50         std::pair<bool, bool> ensure( const T& key, Func func )
51         {
52             AutoLock al( m_lock );
53             std::pair<base_class::iterator, bool> pRet = base_class::insert( base_class::value_type(key, Value() ));
54             if ( pRet.second ) {
55                 func( true, *pRet.first );
56                 return std::make_pair( true, true );
57             }
58             else {
59                 func( false, *pRet.first );
60                 return std::make_pair( true, false );
61             }
62         }
63
64         bool erase( const Key& key )
65         {
66             AutoLock al( m_lock );
67             return base_class::erase( key ) != 0;
68         }
69
70         template <typename T, typename Func>
71         bool erase( const T& key, Func func )
72         {
73             AutoLock al( m_lock );
74             base_class::iterator it = base_class::find( key );
75             if ( it != base_class::end() ) {
76                 func( *it );
77
78                 base_class::erase( it );
79                 return true;
80             }
81             return false;
82         }
83
84         std::ostream& dump( std::ostream& stm ) { return stm; }
85     };
86 }
87
88 #endif  // #ifndef __CDSUNIT_STD_MAP_VC_H