Migrated map-insfind-int stress test to gtest
[libcds.git] / tests / unit / map2 / std_map.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
29 */
30
31 #ifndef CDSUNIT_STD_MAP_GCC_H
32 #define CDSUNIT_STD_MAP_GCC_H
33
34 #include <map>
35 #include <mutex>    //unique_lock
36
37 namespace map2 {
38
39     template <typename Key, typename Value, typename Lock,
40         class Alloc = typename CDS_DEFAULT_ALLOCATOR::template rebind<std::pair<Key const, Value> >::other
41     >
42     class StdMap: public std::map<Key, Value, std::less<Key>, Alloc>
43     {
44         Lock m_lock;
45         typedef std::unique_lock<Lock> scoped_lock;
46         typedef std::map<Key, Value, std::less<Key>, Alloc> base_class;
47     public:
48         typedef typename base_class::mapped_type value_type;
49         typedef typename base_class::value_type  pair_type;
50         typedef size_t      item_counter;
51
52         StdMap()
53         {}
54
55         template <class Config>
56         StdMap( Config const& )
57         {}
58
59         bool contains( const Key& key )
60         {
61             scoped_lock al( m_lock );
62             return base_class::find( key ) != base_class::end();
63         }
64
65         bool insert( const Key& key, const Value& val )
66         {
67             scoped_lock al( m_lock );
68             return base_class::insert( typename base_class::value_type(key, val)).second;
69         }
70
71         template <typename T, typename Func>
72         bool insert( const Key& key, const T& val, Func func )
73         {
74             scoped_lock al( m_lock );
75             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( typename base_class::value_type(key, Value() ));
76             if ( pRet.second ) {
77                 func( pRet.first->second, val );
78                 return true;
79             }
80             return false;
81         }
82
83         template <typename T, typename Func>
84         std::pair<bool, bool> update( const T& key, Func func, bool /*bAllowInsert*/ = true )
85         {
86             scoped_lock al( m_lock );
87             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( typename base_class::value_type(key, Value() ));
88             if ( pRet.second ) {
89                 func( true, *pRet.first );
90                 return std::make_pair( true, true );
91             }
92             else {
93                 func( false, *pRet.first );
94                 return std::make_pair( true, false );
95             }
96         }
97
98         bool erase( const Key& key )
99         {
100             scoped_lock al( m_lock );
101             return base_class::erase( key ) != 0;
102         }
103
104         template <typename T, typename Func>
105         bool erase( const T& key, Func func )
106         {
107             scoped_lock al( m_lock );
108             typename base_class::iterator it = base_class::find( key );
109             if ( it != base_class::end() ) {
110                 func( (*it) );
111                 base_class::erase( it );
112                 return true;
113             }
114             return false;
115         }
116
117         std::ostream& dump( std::ostream& stm ) { return stm; }
118
119
120         // for testing
121         static CDS_CONSTEXPR bool const c_bExtractSupported = false;
122         static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
123         static CDS_CONSTEXPR bool const c_bEraseExactKey = false;
124     };
125 }   // namespace map
126
127 #endif  // #ifndef CDSUNIT_STD_MAP_GCC_H