2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSUNIT_STD_HASH_MAP_GCC_H
32 #define CDSUNIT_STD_HASH_MAP_GCC_H
34 #include <mutex> //unique_lock
35 #include <unordered_map>
39 template <typename Key, typename Value, typename Lock,
40 class Alloc = typename CDS_DEFAULT_ALLOCATOR::template rebind<std::pair<Key const, Value> >::other
43 : public std::unordered_map<
52 typedef std::unique_lock<Lock> scoped_lock;
53 typedef std::unordered_map<
60 typedef typename base_class::mapped_type value_type;
61 typedef size_t item_counter;
66 template <class Config>
67 StdHashMap( Config const& )
70 bool contains( const Key& key )
72 scoped_lock al( m_lock );
73 return base_class::find( key ) != base_class::end();
76 bool insert( const Key& key, const Value& val )
78 scoped_lock al( m_lock );
79 return base_class::insert( typename base_class::value_type(key, val)).second;
82 template <typename T, typename Func>
83 bool insert( const Key& key, const T& val, Func func )
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() ));
88 func( pRet.first->second, val );
94 template <typename T, typename Func>
95 std::pair<bool, bool> update( const T& key, Func func, bool /*bAllowInsert*/ = true )
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() ));
100 func( true, *pRet.first );
101 return std::make_pair( true, true );
104 func( false, *pRet.first );
105 return std::make_pair( true, false );
109 bool erase( const Key& key )
111 scoped_lock al( m_lock );
112 return base_class::erase( key ) != 0;
115 template <typename T, typename Func>
116 bool erase( const T& key, Func func )
118 scoped_lock al( m_lock );
119 typename base_class::iterator it = base_class::find( key );
120 if ( it != base_class::end() ) {
122 return base_class::erase( key ) != 0;
127 std::ostream& dump( std::ostream& stm ) { return stm; }
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;
137 #endif // #ifndef CDSUNIT_STD_HASH_MAP_GCC_H