- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# concurrent_unordered_map.h

    00001 /*
    00002     Copyright 2005-2012 Intel Corporation.  All Rights Reserved.
    00003
    00004     This file is part of Threading Building Blocks.
    00005
    00006     Threading Building Blocks is free software; you can redistribute it
    00007     and/or modify it under the terms of the GNU General Public License
    00008     version 2 as published by the Free Software Foundation.
    00009
    00010     Threading Building Blocks is distributed in the hope that it will be
    00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
    00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013     GNU General Public License for more details.
    00014
    00015     You should have received a copy of the GNU General Public License
    00016     along with Threading Building Blocks; if not, write to the Free Software
    00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    00018
    00019     As a special exception, you may use this file as part of a free software
    00020     library without restriction.  Specifically, if other files instantiate
    00021     templates or use macros or inline functions from this file, or you compile
    00022     this file and link it with other files to produce an executable, this
    00023     file does not by itself cause the resulting executable to be covered by
    00024     the GNU General Public License.  This exception does not however
    00025     invalidate any other reasons why the executable file might be covered by
    00026     the GNU General Public License.
    00027 */
    00028
    00029 /* Container implementations in this header are based on PPL implementations
    00030    provided by Microsoft. */
    00031
    00032 #ifndef __TBB_concurrent_unordered_map_H
    00033 #define __TBB_concurrent_unordered_map_H
    00034
    00035 #include "internal/_concurrent_unordered_impl.h"
    00036
    00037 namespace tbb
    00038 {
    00039
    00040 namespace interface5 {
    00041
    00042 // Template class for hash map traits
    00043 template<typename Key, typename T, typename Hash_compare, typename Allocator, bool Allow_multimapping>
    00044 class concurrent_unordered_map_traits
    00045 {
    00046 protected:
    00047     typedef std::pair<const Key, T> value_type;
    00048     typedef Key key_type;
    00049     typedef Hash_compare hash_compare;
    00050     typedef typename Allocator::template rebind<value_type>::other allocator_type;
    00051     enum { allow_multimapping = Allow_multimapping };
    00052
    00053     concurrent_unordered_map_traits() : my_hash_compare() {}
    00054     concurrent_unordered_map_traits(const hash_compare& hc) : my_hash_compare(hc) {}
    00055
    00056     class value_compare : public std::binary_function<value_type, value_type, bool>
    00057     {
    00058         friend class concurrent_unordered_map_traits<Key, T, Hash_compare, Allocator, Allow_multimapping>;
    00059
    00060     public:
    00061         bool operator()(const value_type& left, const value_type& right) const
    00062         {
    00063             return (my_hash_compare(left.first, right.first));
    00064         }
    00065
    00066         value_compare(const hash_compare& comparator) : my_hash_compare(comparator) {}
    00067
    00068     protected:
    00069         hash_compare my_hash_compare;    // the comparator predicate for keys
    00070     };
    00071
    00072     template<class Type1, class Type2>
    00073     static const Key& get_key(const std::pair<Type1, Type2>& value) {
    00074         return (value.first);
    00075     }
    00076
    00077     hash_compare my_hash_compare; // the comparator predicate for keys
    00078 };
    00079
    00080 template <typename Key, typename T, typename Hasher = tbb::tbb_hash<Key>, typename Key_equality = std::equal_to<Key>, typename Allocator = tbb::tbb_allocator<std::pair<const Key, T> > >
    00081 class concurrent_unordered_map : public internal::concurrent_unordered_base< concurrent_unordered_map_traits<Key, T, internal::hash_compare<Key, Hasher, Key_equality>, Allocator, false> >
    00082 {
    00083     // Base type definitions
    00084     typedef internal::hash_compare<Key, Hasher, Key_equality> hash_compare;
    00085     typedef internal::concurrent_unordered_base< concurrent_unordered_map_traits<Key, T, hash_compare, Allocator, false> > base_type;
    00086     typedef concurrent_unordered_map_traits<Key, T, internal::hash_compare<Key, Hasher, Key_equality>, Allocator, false> traits_type;
    00087     using traits_type::my_hash_compare;
    00088 #if __TBB_EXTRA_DEBUG
    00089 public:
    00090 #endif
    00091     using traits_type::allow_multimapping;
    00092 public:
    00093     using base_type::end;
    00094     using base_type::find;
    00095     using base_type::insert;
    00096
    00097     // Type definitions
    00098     typedef Key key_type;
    00099     typedef typename base_type::value_type value_type;
    00100     typedef T mapped_type;
    00101     typedef Hasher hasher;
    00102     typedef Key_equality key_equal;
    00103     typedef hash_compare key_compare;
    00104
    00105     typedef typename base_type::allocator_type allocator_type;
    00106     typedef typename base_type::pointer pointer;
    00107     typedef typename base_type::const_pointer const_pointer;
    00108     typedef typename base_type::reference reference;
    00109     typedef typename base_type::const_reference const_reference;
    00110
    00111     typedef typename base_type::size_type size_type;
    00112     typedef typename base_type::difference_type difference_type;
    00113
    00114     typedef typename base_type::iterator iterator;
    00115     typedef typename base_type::const_iterator const_iterator;
    00116     typedef typename base_type::iterator local_iterator;
    00117     typedef typename base_type::const_iterator const_local_iterator;
    00118
    00119     // Construction/destruction/copying
    00120     explicit concurrent_unordered_map(size_type n_of_buckets = 8, const hasher& a_hasher = hasher(),
    00121         const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
    00122         : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
    00123     {
    00124     }
    00125
    00126     concurrent_unordered_map(const Allocator& a) : base_type(8, key_compare(), a)
    00127     {
    00128     }
    00129
    00130     template <typename Iterator>
    00131     concurrent_unordered_map(Iterator first, Iterator last, size_type n_of_buckets = 8, const hasher& a_hasher = hasher(),
    00132         const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
    00133         : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
    00134     {
    00135         for (; first != last; ++first)
    00136             base_type::insert(*first);
    00137     }
    00138
    00139     concurrent_unordered_map(const concurrent_unordered_map& table) : base_type(table)
    00140     {
    00141     }
    00142
    00143     concurrent_unordered_map(const concurrent_unordered_map& table, const Allocator& a)
    00144         : base_type(table, a)
    00145     {
    00146     }
    00147
    00148     concurrent_unordered_map& operator=(const concurrent_unordered_map& table)
    00149     {
    00150         base_type::operator=(table);
    00151         return (*this);
    00152     }
    00153
    00154     iterator unsafe_erase(const_iterator where)
    00155     {
    00156         return base_type::unsafe_erase(where);
    00157     }
    00158
    00159     size_type unsafe_erase(const key_type& key)
    00160     {
    00161         return base_type::unsafe_erase(key);
    00162     }
    00163
    00164     iterator unsafe_erase(const_iterator first, const_iterator last)
    00165     {
    00166         return base_type::unsafe_erase(first, last);
    00167     }
    00168
    00169     void swap(concurrent_unordered_map& table)
    00170     {
    00171         base_type::swap(table);
    00172     }
    00173
    00174     // Observers
    00175     hasher hash_function() const
    00176     {
    00177         return my_hash_compare.my_hash_object;
    00178     }
    00179
    00180     key_equal key_eq() const
    00181     {
    00182         return my_hash_compare.my_key_compare_object;
    00183     }
    00184
    00185     mapped_type& operator[](const key_type& key)
    00186     {
    00187         iterator where = find(key);
    00188
    00189         if (where == end())
    00190         {
    00191             where = insert(std::pair<key_type, mapped_type>(key, mapped_type())).first;
    00192         }
    00193
    00194         return ((*where).second);
    00195     }
    00196
    00197     mapped_type& at(const key_type& key)
    00198     {
    00199         iterator where = find(key);
    00200
    00201         if (where == end())
    00202         {
    00203             tbb::internal::throw_exception(tbb::internal::eid_invalid_key);
    00204         }
    00205
    00206         return ((*where).second);
    00207     }
    00208
    00209     const mapped_type& at(const key_type& key) const
    00210     {
    00211         const_iterator where = find(key);
    00212
    00213         if (where == end())
    00214         {
    00215             tbb::internal::throw_exception(tbb::internal::eid_invalid_key);
    00216         }
    00217
    00218         return ((*where).second);
    00219     }
    00220 };
    00221
    00222 } // namespace interface5
    00223
    00224 using interface5::concurrent_unordered_map;
    00225
    00226 } // namespace tbb
    00227
    00228 #endif// __TBB_concurrent_unordered_map_H

Copyright © 2005-2012 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.