BALL 1.5.0
Loading...
Searching...
No Matches
hashMap.h
Go to the documentation of this file.
1// -*- Mode: C++; tab-width: 2; -*-
2// vi: set ts=2:
3//
4
5#ifndef BALL_DATATYPE_HASHMAP_H
6#define BALL_DATATYPE_HASHMAP_H
7
8#ifndef BALL_COMMON_EXCEPTION_H
10#endif
11
12#ifndef BALL_DATATYPE_STRING_H
13# include <BALL/DATATYPE/string.h>
14#endif
15
16#ifndef BALL_DATATYPE_TRIPLE_H
17# include <BALL/DATATYPE/triple.h>
18#endif
19
20#ifndef BALL_DATATYPE_QUADRUPLE_H
22#endif
23
24#include <boost/unordered_map.hpp>
25#include <boost/functional/hash.hpp>
26
27namespace boost
28{
29 template<>
30 struct hash<BALL::String>
31 {
32 size_t operator () (const BALL::String& s) const { return boost::hash<std::string>()(s); }
33 };
34
35 template <typename T1, typename T2, typename T3>
36 struct hash<BALL::Triple<T1, T2, T3> >
37 {
38 size_t operator () (const BALL::Triple<T1, T2, T3>& s) const
39 {
40 size_t hash = 0;
41 boost::hash_combine(hash, s.first);
42 boost::hash_combine(hash, s.second);
43 boost::hash_combine(hash, s.third);
44
45 return hash;
46 }
47 };
48
49 template <typename T1, typename T2, typename T3, typename T4>
50 struct hash<BALL::Quadruple<T1, T2, T3, T4> >
51 {
52 size_t operator () (const BALL::Quadruple<T1, T2, T3, T4>& s) const
53 {
54 size_t hash = 0;
55 boost::hash_combine(hash, s.first);
56 boost::hash_combine(hash, s.second);
57 boost::hash_combine(hash, s.third);
58 boost::hash_combine(hash, s.fourth);
59
60 return hash;
61 }
62 };
63}
64
65namespace BALL
66{
72 template <class Key, class T>
73 class HashMap : public boost::unordered_map<Key, T>
74 {
75 public:
76
83 {
84 public:
85 IllegalKey(const char* file, int line)
86 : Exception::GeneralException(file, line)
87 {
88 }
89 };
90
92
93 typedef boost::unordered_map<Key, T> Base;
94 typedef typename Base::value_type ValueType;
95 typedef Key KeyType;
96 typedef typename Base::value_type* PointerType;
97 typedef typename Base::iterator Iterator;
98 typedef typename Base::const_iterator ConstIterator;
100
102 inline bool has(const Key& key) const
103 {
104 return Base::find(key) != Base::end();
105 }
106
112 const T& operator [] (const Key& key) const;
113
115 T& operator [] (const Key& key);
116
118 bool operator == (const HashMap<Key, T>& rhs) const;
119
122 Size size() const { return Base::size(); }
123 };
124
125 //******************************************************************************************
126 // Implementations of template methods
127 //******************************************************************************************
128
129 template <class Key, class T>
130 const T& HashMap<Key, T>::operator [] (const Key& key) const
131 {
132 ConstIterator it = this->find(key);
133 if (it == Base::end())
134 {
135 throw IllegalKey(__FILE__, __LINE__);
136 }
137 else
138 {
139 return it->second;
140 }
141 }
142
143 template <class Key, class T>
145 {
146 // No equality if sizes differ.
147 if (Base::size() != rhs.size())
148 {
149 return false;
150 }
151
152 // Equality if bothe have the same size and every element of lhs is
153 // is contained in lhs. Testing the other way round is obviously
154 // unnecessary.
155 ConstIterator it(Base::begin());
156 for (; it != Base::end(); ++it)
157 {
158 if (!rhs.has(it->first)) return false;
159 }
160
161 return true;
162 }
163
164 template <class Key, class T>
166 {
167 return Base::operator[] (key);
168 }
169
170} // namespace BALL
171
172#endif // BALL_DATATYPE_HASHMAP_H
GeneralException()
Default constructor.
HashMap class based on the STL map (containing serveral convenience functions)
Definition hashMap.h:74
const T & operator[](const Key &key) const
Return a constant reference to the element whose key is key.
Definition hashMap.h:130
Base::value_type * PointerType
Definition hashMap.h:96
bool has(const Key &key) const
Test whether the map contains the given key.
Definition hashMap.h:102
Base::value_type ValueType
Definition hashMap.h:94
Base::iterator Iterator
Definition hashMap.h:97
Size size() const
Definition hashMap.h:122
boost::unordered_map< Key, T > Base
Definition hashMap.h:93
Base::const_iterator ConstIterator
Definition hashMap.h:98
bool operator==(const HashMap< Key, T > &rhs) const
Equality operator. Check whether two two hashmaps contain the same elements. O(n) runtime.
Definition hashMap.h:144
HashMap illegal key exception.
Definition hashMap.h:83
IllegalKey(const char *file, int line)
Definition hashMap.h:85