GCS  0.2.3
gu_unordered.hpp
1 //
2 // Copyright (C) 2010 Codership Oy <info@codership.com>
3 //
4 
6 // @file gu_unordered.hpp unordered_[multi]map definition
7 //
8 // We still have environments where neither boost or std unordered
9 // stuff is available. Wrapper classes are provided for alternate
10 // implementations with standard semantics.
11 //
12 // For usage see either boost or tr1 specifications for unordered_[multi]map
13 //
14 
15 #ifndef GU_UNORDERED_HPP
16 #define GU_UNORDERED_HPP
17 
18 #if defined(HAVE_BOOST_UNORDERED_MAP_HPP)
19 #include <boost/unordered_map.hpp>
20 #include <boost/unordered_set.hpp>
21 #elif defined(HAVE_UNORDERED_MAP)
22 #include <unordered_map>
23 #include <unordered_set>
24 #elif defined(HAVE_TR1_UNORDERED_MAP)
25 #include <tr1/unordered_map>
26 #include <tr1/unordered_set>
27 #else
28 #error "no unordered map available"
29 #endif
30 
31 #include "gu_throw.hpp"
32 
33 namespace gu
34 {
35  template <typename K>
37  {
38  public:
39 #if defined(HAVE_BOOST_UNORDERED_MAP_HPP)
40  typedef boost::hash<K> Type;
41 #elif defined(HAVE_UNORDERED_MAP)
42  typedef std::hash<K> Type;
43 #elif defined(HAVE_TR1_UNORDERED_MAP)
44  typedef std::tr1::hash<K> Type;
45 #endif
46  size_t operator()(const K& k) const
47  {
48  return Type()(k);
49  }
50  };
51 
52 
53  template <typename K>
54  size_t HashValue(const K& key)
55  {
56  return UnorderedHash<K>()(key);
57  }
58 
59  template <typename K, typename H = UnorderedHash<K>,
60  class P = std::equal_to<K>,
61  class A = std::allocator<K> >
63  {
64 #if defined(HAVE_BOOST_UNORDERED_MAP_HPP)
65  typedef boost::unordered_set<K, H, P, A> type;
66 #elif defined(HAVE_UNORDERED_MAP)
67  typedef std::unordered_set<K, H, P, A> type;
68 #elif defined(HAVE_TR1_UNORDERED_MAP)
69  typedef std::tr1::unordered_set<K, H, P, A> type;
70 #endif
71  type impl_;
72  public:
73  typedef typename type::value_type value_type;
74  typedef typename type::iterator iterator;
75  typedef typename type::const_iterator const_iterator;
76 
77  UnorderedSet() : impl_() { }
78  explicit UnorderedSet(A a) : impl_(a) { }
79 
80  iterator begin() { return impl_.begin(); }
81  const_iterator begin() const { return impl_.begin(); }
82  iterator end() { return impl_.end(); }
83  const_iterator end() const { return impl_.end(); }
84  std::pair<iterator, bool> insert(const value_type& k)
85  { return impl_.insert(k); }
86  iterator insert_unique(const value_type& k)
87  {
88  std::pair<iterator, bool> ret(insert(k));
89  if (ret.second == false) gu_throw_fatal << "insert unique failed";
90  return ret.first;
91  }
92  iterator find(const K& key) { return impl_.find(key); }
93  const_iterator find(const K& key) const { return impl_.find(key); }
94  iterator erase(iterator i) { return impl_.erase(i); }
95  size_t size() const { return impl_.size(); }
96  bool empty() const { return impl_.empty(); }
97  void clear() { impl_.clear(); }
98  void rehash(size_t n) { impl_.rehash(n); }
99 #if defined(HAVE_UNORDERED_MAP)
100  void reserve(size_t n) { impl_.reserve(n); }
101 #endif
102  };
103 
104 
105  template <typename K, typename V, typename H = UnorderedHash<K>,
106  class P = std::equal_to<K>,
107  class A = std::allocator<std::pair<const K, V> > >
109  {
110 #if defined(HAVE_BOOST_UNORDERED_MAP_HPP)
111  typedef boost::unordered_map<K, V, H, P, A> type;
112 #elif defined(HAVE_UNORDERED_MAP)
113  typedef std::unordered_map<K, V, H, P, A> type;
114 #elif defined(HAVE_TR1_UNORDERED_MAP)
115  typedef std::tr1::unordered_map<K, V, H, P, A> type;
116 #endif
117  type impl_;
118  public:
119  typedef typename type::value_type value_type;
120  typedef typename type::iterator iterator;
121  typedef typename type::const_iterator const_iterator;
122 
123  UnorderedMap() : impl_() { }
124 
125  iterator begin() { return impl_.begin(); }
126  const_iterator begin() const { return impl_.begin(); }
127  iterator end() { return impl_.end(); }
128  const_iterator end() const { return impl_.end(); }
129  std::pair<iterator, bool> insert(const std::pair<K, V>& kv)
130  { return impl_.insert(kv); }
131  iterator insert_unique(const std::pair<K, V>& kv)
132  {
133  std::pair<iterator, bool> ret(insert(kv));
134  if (ret.second == false) gu_throw_fatal << "insert unique failed";
135  return ret.first;
136  }
137  iterator find(const K& key) { return impl_.find(key); }
138  const_iterator find(const K& key) const { return impl_.find(key); }
139  iterator erase(iterator i) { return impl_.erase(i); }
140  size_t size() const { return impl_.size(); }
141  bool empty() const { return impl_.empty(); }
142  void clear() { impl_.clear(); }
143  void rehash(size_t n) { impl_.rehash(n); }
144  };
145 
146  template <typename K, typename V, typename H = UnorderedHash<K> >
148  {
149 #if defined(HAVE_BOOST_UNORDERED_MAP_HPP)
150  typedef boost::unordered_multimap<K, V> type;
151 #elif defined(HAVE_UNORDERED_MAP)
152  typedef std::unordered_multimap<K, V> type;
153 #elif defined(HAVE_TR1_UNORDERED_MAP)
154  typedef std::tr1::unordered_multimap<K, V> type;
155 #endif
156  type impl_;
157  public:
158  typedef typename type::value_type value_type;
159  typedef typename type::iterator iterator;
160  typedef typename type::const_iterator const_iterator;
161 
162  UnorderedMultimap() : impl_() { }
163  void clear() { impl_.clear(); }
164 
165  iterator begin() { return impl_.begin(); }
166  const_iterator begin() const { return impl_.begin(); }
167  iterator end() { return impl_.end(); }
168  const_iterator end() const { return impl_.end(); }
169  iterator insert(const std::pair<K, V>& kv)
170  { return impl_.insert(kv); }
171  iterator find(const K& key) { return impl_.find(key); }
172  const_iterator find(const K& key) const { return impl_.find(key); }
173  std::pair<iterator, iterator>
174  equal_range(const K& key) { return impl_.equal_range(key); }
175  std::pair<const_iterator, const_iterator>
176  equal_range(const K& key) const
177  { return impl_.equal_range(key); }
178  void erase(iterator i) { impl_.erase(i); }
179  size_t size() const { return impl_.size(); }
180  bool empty() const { return impl_.empty(); }
181  };
182 }
183 
184 #endif // GU_UNORDERED_HPP
Definition: gu_unordered.hpp:62
Definition: gu_unordered.hpp:147
Definition: gu_unordered.hpp:108
Definition: gu_unordered.hpp:36