concurrent_unordered_map.h

00001 /*
00002     Copyright 2005-2013 Intel Corporation.  All Rights Reserved.
00003 
00004     The source code contained or described herein and all documents related
00005     to the source code ("Material") are owned by Intel Corporation or its
00006     suppliers or licensors.  Title to the Material remains with Intel
00007     Corporation or its suppliers and licensors.  The Material is protected
00008     by worldwide copyright laws and treaty provisions.  No part of the
00009     Material may be used, copied, reproduced, modified, published, uploaded,
00010     posted, transmitted, distributed, or disclosed in any way without
00011     Intel's prior express written permission.
00012 
00013     No license under any patent, copyright, trade secret or other
00014     intellectual property right is granted to or conferred upon you by
00015     disclosure or delivery of the Materials, either expressly, by
00016     implication, inducement, estoppel or otherwise.  Any license under such
00017     intellectual property rights must be express and approved by Intel in
00018     writing.
00019 */
00020 
00021 /* Container implementations in this header are based on PPL implementations
00022    provided by Microsoft. */
00023 
00024 #ifndef __TBB_concurrent_unordered_map_H
00025 #define __TBB_concurrent_unordered_map_H
00026 
00027 #include "internal/_concurrent_unordered_impl.h"
00028 
00029 namespace tbb
00030 {
00031 
00032 namespace interface5 {
00033 
00034 // Template class for hash map traits
00035 template<typename Key, typename T, typename Hash_compare, typename Allocator, bool Allow_multimapping>
00036 class concurrent_unordered_map_traits
00037 {
00038 protected:
00039     typedef std::pair<const Key, T> value_type;
00040     typedef Key key_type;
00041     typedef Hash_compare hash_compare;
00042     typedef typename Allocator::template rebind<value_type>::other allocator_type;
00043     enum { allow_multimapping = Allow_multimapping };
00044 
00045     concurrent_unordered_map_traits() : my_hash_compare() {}
00046     concurrent_unordered_map_traits(const hash_compare& hc) : my_hash_compare(hc) {}
00047 
00048     class value_compare : public std::binary_function<value_type, value_type, bool>
00049     {
00050         friend class concurrent_unordered_map_traits<Key, T, Hash_compare, Allocator, Allow_multimapping>;
00051 
00052     public:
00053         bool operator()(const value_type& left, const value_type& right) const
00054         {
00055             return (my_hash_compare(left.first, right.first));
00056         }
00057 
00058         value_compare(const hash_compare& comparator) : my_hash_compare(comparator) {}
00059 
00060     protected:
00061         hash_compare my_hash_compare;    // the comparator predicate for keys
00062     };
00063 
00064     template<class Type1, class Type2>
00065     static const Key& get_key(const std::pair<Type1, Type2>& value) {
00066         return (value.first);
00067     }
00068 
00069     hash_compare my_hash_compare; // the comparator predicate for keys
00070 };
00071 
00072 template <typename Key, typename T, typename Hasher = tbb::tbb_hash<Key>, typename Key_equality = std::equal_to<Key>,
00073          typename Allocator = tbb::tbb_allocator<std::pair<const Key, T> > >
00074 class concurrent_unordered_map :
00075     public internal::concurrent_unordered_base< concurrent_unordered_map_traits<Key, T,
00076     internal::hash_compare<Key, Hasher, Key_equality>, Allocator, false> >
00077 {
00078     // Base type definitions
00079     typedef internal::hash_compare<Key, Hasher, Key_equality> hash_compare;
00080     typedef concurrent_unordered_map_traits<Key, T, hash_compare, Allocator, false> traits_type;
00081     typedef internal::concurrent_unordered_base< traits_type > base_type;
00082     using traits_type::my_hash_compare;
00083 #if __TBB_EXTRA_DEBUG
00084 public:
00085 #endif
00086     using traits_type::allow_multimapping;
00087 public:
00088     using base_type::end;
00089     using base_type::find;
00090     using base_type::insert;
00091 
00092     // Type definitions
00093     typedef Key key_type;
00094     typedef typename base_type::value_type value_type;
00095     typedef T mapped_type;
00096     typedef Hasher hasher;
00097     typedef Key_equality key_equal;
00098     typedef hash_compare key_compare;
00099 
00100     typedef typename base_type::allocator_type allocator_type;
00101     typedef typename base_type::pointer pointer;
00102     typedef typename base_type::const_pointer const_pointer;
00103     typedef typename base_type::reference reference;
00104     typedef typename base_type::const_reference const_reference;
00105 
00106     typedef typename base_type::size_type size_type;
00107     typedef typename base_type::difference_type difference_type;
00108 
00109     typedef typename base_type::iterator iterator;
00110     typedef typename base_type::const_iterator const_iterator;
00111     typedef typename base_type::iterator local_iterator;
00112     typedef typename base_type::const_iterator const_local_iterator;
00113 
00114     // Construction/destruction/copying
00115     explicit concurrent_unordered_map(size_type n_of_buckets = 8,
00116         const hasher& _Hasher = hasher(), const key_equal& _Key_equality = key_equal(),
00117         const allocator_type& a = allocator_type())
00118         : base_type(n_of_buckets, key_compare(_Hasher, _Key_equality), a)
00119     {
00120     }
00121 
00122     concurrent_unordered_map(const Allocator& a) : base_type(8, key_compare(), a)
00123     {
00124     }
00125 
00126     template <typename Iterator>
00127     concurrent_unordered_map(Iterator first, Iterator last, size_type n_of_buckets = 8,
00128         const hasher& _Hasher = hasher(), const key_equal& _Key_equality = key_equal(),
00129         const allocator_type& a = allocator_type())
00130         : base_type(n_of_buckets, key_compare(_Hasher, _Key_equality), a)
00131     {
00132         for (; first != last; ++first)
00133             base_type::insert(*first);
00134     }
00135 
00136     concurrent_unordered_map(const concurrent_unordered_map& table) : base_type(table)
00137     {
00138     }
00139 
00140     concurrent_unordered_map(const concurrent_unordered_map& table, const Allocator& a)
00141         : base_type(table, a)
00142     {
00143     }
00144 
00145     concurrent_unordered_map& operator=(const concurrent_unordered_map& table)
00146     {
00147         base_type::operator=(table);
00148         return (*this);
00149     }
00150 
00151     iterator unsafe_erase(const_iterator where)
00152     {
00153         return base_type::unsafe_erase(where);
00154     }
00155 
00156     size_type unsafe_erase(const key_type& key)
00157     {
00158         return base_type::unsafe_erase(key);
00159     }
00160 
00161     iterator unsafe_erase(const_iterator first, const_iterator last)
00162     {
00163         return base_type::unsafe_erase(first, last);
00164     }
00165 
00166     void swap(concurrent_unordered_map& table)
00167     {
00168         base_type::swap(table);
00169     }
00170 
00171     // Observers
00172     hasher hash_function() const
00173     {
00174         return my_hash_compare.my_hash_object;
00175     }
00176 
00177     key_equal key_eq() const
00178     {
00179         return my_hash_compare.my_key_compare_object;
00180     }
00181 
00182     mapped_type& operator[](const key_type& key)
00183     {
00184         iterator where = find(key);
00185 
00186         if (where == end())
00187         {
00188             where = insert(std::pair<key_type, mapped_type>(key, mapped_type())).first;
00189         }
00190 
00191         return ((*where).second);
00192     }
00193 
00194     mapped_type& at(const key_type& key)
00195     {
00196         iterator where = find(key);
00197 
00198         if (where == end())
00199         {
00200             tbb::internal::throw_exception(tbb::internal::eid_invalid_key);
00201         }
00202 
00203         return ((*where).second);
00204     }
00205 
00206     const mapped_type& at(const key_type& key) const
00207     {
00208         const_iterator where = find(key);
00209 
00210         if (where == end())
00211         {
00212             tbb::internal::throw_exception(tbb::internal::eid_invalid_key);
00213         }
00214 
00215         return ((*where).second);
00216     }
00217 };
00218 
00219 template < typename Key, typename T, typename Hasher = tbb::tbb_hash<Key>, typename Key_equality = std::equal_to<Key>,
00220         typename Allocator = tbb::tbb_allocator<std::pair<const Key, T> > >
00221 class concurrent_unordered_multimap :
00222     public internal::concurrent_unordered_base< concurrent_unordered_map_traits< Key, T,
00223     internal::hash_compare<Key, Hasher, Key_equality>, Allocator, true> >
00224 {
00225     // Base type definitions
00226     typedef internal::hash_compare<Key, Hasher, Key_equality> hash_compare;
00227     typedef concurrent_unordered_map_traits<Key, T, hash_compare, Allocator, true> traits_type;
00228     typedef internal::concurrent_unordered_base< traits_type > base_type;
00229     using traits_type::my_hash_compare;
00230 #if __TBB_EXTRA_DEBUG
00231 public:
00232 #endif
00233     using traits_type::allow_multimapping;
00234 public:
00235     using base_type::end;
00236     using base_type::find;
00237     using base_type::insert;
00238 
00239     // Type definitions
00240     typedef Key key_type;
00241     typedef typename base_type::value_type value_type;
00242     typedef T mapped_type;
00243     typedef Hasher hasher;
00244     typedef Key_equality key_equal;
00245     typedef hash_compare key_compare;
00246 
00247     typedef typename base_type::allocator_type allocator_type;
00248     typedef typename base_type::pointer pointer;
00249     typedef typename base_type::const_pointer const_pointer;
00250     typedef typename base_type::reference reference;
00251     typedef typename base_type::const_reference const_reference;
00252 
00253     typedef typename base_type::size_type size_type;
00254     typedef typename base_type::difference_type difference_type;
00255 
00256     typedef typename base_type::iterator iterator;
00257     typedef typename base_type::const_iterator const_iterator;
00258     typedef typename base_type::iterator local_iterator;
00259     typedef typename base_type::const_iterator const_local_iterator;
00260 
00261     // Construction/destruction/copying
00262     explicit concurrent_unordered_multimap(size_type n_of_buckets = 8,
00263         const hasher& _Hasher = hasher(), const key_equal& _Key_equality = key_equal(),
00264         const allocator_type& a = allocator_type())
00265         : base_type(n_of_buckets, key_compare(_Hasher, _Key_equality), a)
00266     {
00267     }
00268 
00269     concurrent_unordered_multimap(const Allocator& a) : base_type(8, key_compare(), a)
00270     {
00271     }
00272 
00273     template <typename Iterator>
00274     concurrent_unordered_multimap(Iterator first, Iterator last, size_type n_of_buckets = 8,
00275         const hasher& _Hasher = hasher(), const key_equal& _Key_equality = key_equal(),
00276         const allocator_type& a = allocator_type())
00277         : base_type(n_of_buckets,key_compare(_Hasher,_Key_equality), a)
00278     {
00279         for (; first != last; ++first)
00280             base_type::insert(*first);
00281     }
00282 
00283     concurrent_unordered_multimap(const concurrent_unordered_multimap& table) : base_type(table)
00284     {
00285     }
00286 
00287     concurrent_unordered_multimap(const concurrent_unordered_multimap& table, const Allocator& a)
00288         : base_type(table, a)
00289     {
00290     }
00291 
00292     concurrent_unordered_multimap& operator=(const concurrent_unordered_multimap& table)
00293     {
00294         base_type::operator=(table);
00295         return (*this);
00296     }
00297 
00298     iterator unsafe_erase(const_iterator where)
00299     {
00300         return base_type::unsafe_erase(where);
00301     }
00302 
00303     size_type unsafe_erase(const key_type& key)
00304     {
00305         return base_type::unsafe_erase(key);
00306     }
00307 
00308     iterator unsafe_erase(const_iterator first, const_iterator last)
00309     {
00310         return base_type::unsafe_erase(first, last);
00311     }
00312 
00313     void swap(concurrent_unordered_multimap& table)
00314     {
00315         base_type::swap(table);
00316     }
00317 
00318     // Observers
00319     hasher hash_function() const
00320     {
00321         return my_hash_compare.my_hash_object;
00322     }
00323 
00324     key_equal key_eq() const
00325     {
00326         return my_hash_compare.my_key_compare_object;
00327     }
00328 };
00329 } // namespace interface5
00330 
00331 using interface5::concurrent_unordered_map;
00332 using interface5::concurrent_unordered_multimap;
00333 
00334 } // namespace tbb
00335 
00336 #endif// __TBB_concurrent_unordered_map_H

Copyright © 2005-2013 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.