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_MAP_GCC_H
32 #define CDSUNIT_STD_MAP_GCC_H
35 #include <mutex> //unique_lock
39 template <typename Key, typename Value, typename Lock,
40 class Alloc = typename CDS_DEFAULT_ALLOCATOR::template rebind<std::pair<Key const, Value> >::other
42 class StdMap: public std::map<Key, Value, std::less<Key>, Alloc>
45 typedef std::unique_lock<Lock> scoped_lock;
46 typedef std::map<Key, Value, std::less<Key>, Alloc> base_class;
48 typedef typename base_class::mapped_type value_type;
49 typedef typename base_class::value_type pair_type;
50 typedef size_t item_counter;
55 template <class Config>
56 StdMap( Config const& )
59 bool contains( const Key& key )
61 scoped_lock al( m_lock );
62 return base_class::find( key ) != base_class::end();
65 bool insert( const Key& key, const Value& val )
67 scoped_lock al( m_lock );
68 return base_class::insert( typename base_class::value_type(key, val)).second;
71 template <typename T, typename Func>
72 bool insert( const Key& key, const T& val, Func func )
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() ));
77 func( pRet.first->second, val );
83 template <typename T, typename Func>
84 std::pair<bool, bool> update( const T& key, Func func, bool /*bAllowInsert*/ = true )
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() ));
89 func( true, *pRet.first );
90 return std::make_pair( true, true );
93 func( false, *pRet.first );
94 return std::make_pair( true, false );
98 bool erase( const Key& key )
100 scoped_lock al( m_lock );
101 return base_class::erase( key ) != 0;
104 template <typename T, typename Func>
105 bool erase( const T& key, Func func )
107 scoped_lock al( m_lock );
108 typename base_class::iterator it = base_class::find( key );
109 if ( it != base_class::end() ) {
111 base_class::erase( it );
117 std::ostream& dump( std::ostream& stm ) { return stm; }
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;
127 #endif // #ifndef CDSUNIT_STD_MAP_GCC_H