Added copyright and license
[libcds.git] / tests / unit / map2 / std_hash_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_HASH_MAP_GCC_H
32 #define CDSUNIT_STD_HASH_MAP_GCC_H
33
34 #include <mutex>    //unique_lock
35 #include <unordered_map>
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 StdHashMap
43         : public std::unordered_map<
44             Key, Value
45             , std::hash<Key>
46             , std::equal_to<Key>
47             , Alloc
48         >
49     {
50     public:
51         Lock m_lock;
52         typedef std::unique_lock<Lock> scoped_lock;
53         typedef std::unordered_map<
54             Key, Value
55             , std::hash<Key>
56             , std::equal_to<Key>
57             , Alloc
58         >   base_class;
59     public:
60         typedef typename base_class::mapped_type value_type;
61         typedef size_t      item_counter;
62
63         StdHashMap()
64         {}
65
66         template <class Config>
67         StdHashMap( Config const& )
68         {}
69
70         bool contains( const Key& key )
71         {
72             scoped_lock al( m_lock );
73             return base_class::find( key ) != base_class::end();
74         }
75
76         bool insert( const Key& key, const Value& val )
77         {
78             scoped_lock al( m_lock );
79             return base_class::insert( typename base_class::value_type(key, val)).second;
80         }
81
82         template <typename T, typename Func>
83         bool insert( const Key& key, const T& val, Func func )
84         {
85             scoped_lock al( m_lock );
86             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( typename base_class::value_type(key, Value() ));
87             if ( pRet.second ) {
88                 func( pRet.first->second, val );
89                 return true;
90             }
91             return false;
92         }
93
94         template <typename T, typename Func>
95         std::pair<bool, bool> update( const T& key, Func func, bool /*bAllowInsert*/ = true )
96         {
97             scoped_lock al( m_lock );
98             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( typename base_class::value_type( key, Value() ));
99             if ( pRet.second ) {
100                 func( true, *pRet.first );
101                 return std::make_pair( true, true );
102             }
103             else {
104                 func( false, *pRet.first );
105                 return std::make_pair( true, false );
106             }
107         }
108
109         bool erase( const Key& key )
110         {
111             scoped_lock al( m_lock );
112             return base_class::erase( key ) != 0;
113         }
114
115         template <typename T, typename Func>
116         bool erase( const T& key, Func func )
117         {
118             scoped_lock al( m_lock );
119             typename base_class::iterator it = base_class::find( key );
120             if ( it != base_class::end() ) {
121                 func( *it );
122                 return base_class::erase( key ) != 0;
123             }
124             return false;
125         }
126
127         std::ostream& dump( std::ostream& stm ) { return stm; }
128
129
130         // for testing
131         static CDS_CONSTEXPR bool const c_bExtractSupported = false;
132         static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
133         static CDS_CONSTEXPR bool const c_bEraseExactKey = false;
134     };
135 }   // namespace map2
136
137 #endif  // #ifndef CDSUNIT_STD_HASH_MAP_GCC_H