Shadowrun: Awakened 29 September 2011 - Build 871
concurrent_unordered_map.h
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 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 "_concurrent_unordered_internal.h"
00036 
00037 namespace tbb
00038 {
00039 
00040 // Template class for hash compare
00041 template<typename Key>
00042 class tbb_hash
00043 {
00044 public:
00045     tbb_hash() {}
00046 
00047     size_t operator()(const Key& key) const
00048     {
00049         return tbb_hasher(key);
00050     }
00051 };
00052 
00053 namespace interface5 {
00054 
00055 // Template class for hash map traits
00056 template<typename Key, typename T, typename Hash_compare, typename Allocator, bool Allow_multimapping>
00057 class concurrent_unordered_map_traits
00058 {
00059 protected:
00060     typedef std::pair<const Key, T> value_type;
00061     typedef Key key_type;
00062     typedef Hash_compare hash_compare;
00063     typedef typename Allocator::template rebind<value_type>::other allocator_type;
00064     enum { allow_multimapping = Allow_multimapping };
00065 
00066     concurrent_unordered_map_traits() : my_hash_compare() {}
00067     concurrent_unordered_map_traits(const hash_compare& hc) : my_hash_compare(hc) {}
00068 
00069     class value_compare : public std::binary_function<value_type, value_type, bool>
00070     {
00071         friend class concurrent_unordered_map_traits<Key, T, Hash_compare, Allocator, Allow_multimapping>;
00072 
00073     public:
00074         bool operator()(const value_type& left, const value_type& right) const
00075         {
00076             return (my_hash_compare(left.first, right.first));
00077         }
00078 
00079         value_compare(const hash_compare& comparator) : my_hash_compare(comparator) {}
00080 
00081     protected:
00082         hash_compare my_hash_compare;    // the comparator predicate for keys
00083     };
00084 
00085     template<class Type1, class Type2>
00086     static const Key& get_key(const std::pair<Type1, Type2>& value) {
00087         return (value.first);
00088     }
00089 
00090     hash_compare my_hash_compare; // the comparator predicate for keys
00091 };
00092 
00093 template <typename Key, typename T, typename Hasher = tbb_hash<Key>, typename Key_equality = std::equal_to<Key>, typename Allocator = tbb::tbb_allocator<std::pair<const Key, T> > >
00094 class concurrent_unordered_map : public internal::concurrent_unordered_base< concurrent_unordered_map_traits<Key, T, internal::hash_compare<Key, Hasher, Key_equality>, Allocator, false> >
00095 {
00096     // Base type definitions
00097     typedef internal::hash_compare<Key, Hasher, Key_equality> hash_compare;
00098     typedef internal::concurrent_unordered_base< concurrent_unordered_map_traits<Key, T, hash_compare, Allocator, false> > base_type;
00099     typedef concurrent_unordered_map_traits<Key, T, internal::hash_compare<Key, Hasher, Key_equality>, Allocator, false> traits_type;
00100     using traits_type::my_hash_compare;
00101 #if __TBB_EXTRA_DEBUG
00102 public:
00103 #endif
00104     using traits_type::allow_multimapping;
00105 public:
00106     using base_type::end;
00107     using base_type::find;
00108     using base_type::insert;
00109 
00110     // Type definitions
00111     typedef Key key_type;
00112     typedef typename base_type::value_type value_type;
00113     typedef T mapped_type;
00114     typedef Hasher hasher;
00115     typedef Key_equality key_equal;
00116     typedef hash_compare key_compare;
00117 
00118     typedef typename base_type::allocator_type allocator_type;
00119     typedef typename base_type::pointer pointer;
00120     typedef typename base_type::const_pointer const_pointer;
00121     typedef typename base_type::reference reference;
00122     typedef typename base_type::const_reference const_reference;
00123 
00124     typedef typename base_type::size_type size_type;
00125     typedef typename base_type::difference_type difference_type;
00126 
00127     typedef typename base_type::iterator iterator;
00128     typedef typename base_type::const_iterator const_iterator;
00129     typedef typename base_type::iterator local_iterator;
00130     typedef typename base_type::const_iterator const_local_iterator;
00131 
00132     // Construction/destruction/copying
00133     explicit concurrent_unordered_map(size_type n_of_buckets = 8, const hasher& a_hasher = hasher(),
00134         const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
00135         : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
00136     {
00137     }
00138 
00139     concurrent_unordered_map(const Allocator& a) : base_type(8, key_compare(), a)
00140     {
00141     }
00142 
00143     template <typename Iterator>
00144     concurrent_unordered_map(Iterator first, Iterator last, size_type n_of_buckets = 8, const hasher& a_hasher = hasher(),
00145         const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
00146         : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
00147     {
00148         for (; first != last; ++first)
00149             base_type::insert(*first);
00150     }
00151 
00152     concurrent_unordered_map(const concurrent_unordered_map& table) : base_type(table)
00153     {
00154     }
00155 
00156     concurrent_unordered_map(const concurrent_unordered_map& table, const Allocator& a)
00157         : base_type(table, a)
00158     {
00159     }
00160 
00161     concurrent_unordered_map& operator=(const concurrent_unordered_map& table)
00162     {
00163         base_type::operator=(table);
00164         return (*this);
00165     }
00166 
00167     iterator unsafe_erase(const_iterator where)
00168     {
00169         return base_type::unsafe_erase(where);
00170     }
00171 
00172     size_type unsafe_erase(const key_type& key)
00173     {
00174         return base_type::unsafe_erase(key);
00175     }
00176 
00177     iterator unsafe_erase(const_iterator first, const_iterator last)
00178     {
00179         return base_type::unsafe_erase(first, last);
00180     }
00181 
00182     void swap(concurrent_unordered_map& table)
00183     {
00184         base_type::swap(table);
00185     }
00186 
00187     // Observers
00188     hasher hash_function() const
00189     {
00190         return my_hash_compare.my_hash_object;
00191     }
00192 
00193     key_equal key_eq() const
00194     {
00195         return my_hash_compare.my_key_compare_object;
00196     }
00197 
00198     mapped_type& operator[](const key_type& key)
00199     {
00200         iterator where = find(key);
00201 
00202         if (where == end())
00203         {
00204             where = insert(std::pair<key_type, mapped_type>(key, mapped_type())).first;
00205         }
00206 
00207         return ((*where).second);
00208     }
00209 
00210     mapped_type& at(const key_type& key)
00211     {
00212         iterator where = find(key);
00213 
00214         if (where == end())
00215         {
00216             tbb::internal::throw_exception(tbb::internal::eid_invalid_key);
00217         }
00218 
00219         return ((*where).second);
00220     }
00221 
00222     const mapped_type& at(const key_type& key) const
00223     {
00224         const_iterator where = find(key);
00225 
00226         if (where == end())
00227         {
00228             tbb::internal::throw_exception(tbb::internal::eid_invalid_key);
00229         }
00230 
00231         return ((*where).second);
00232     }
00233 };
00234 
00235 } // namespace interface5
00236 
00237 using interface5::concurrent_unordered_map;
00238 
00239 } // namespace tbb
00240 
00241 #endif// __TBB_concurrent_unordered_map_H

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net