Loading...
Searching...
No Matches
hashMap.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_DATATYPE_HASHMAP_H
#define BALL_DATATYPE_HASHMAP_H
#ifndef BALL_COMMON_EXCEPTION_H
# include <BALL/COMMON/exception.h>
#endif
#ifndef BALL_DATATYPE_STRING_H
# include <BALL/DATATYPE/string.h>
#endif
#ifndef BALL_DATATYPE_TRIPLE_H
# include <BALL/DATATYPE/triple.h>
#endif
#ifndef BALL_DATATYPE_QUADRUPLE_H
# include <BALL/DATATYPE/quadruple.h>
#endif
#include <boost/unordered_map.hpp>
#include <boost/functional/hash.hpp>
namespace boost
{
template<>
struct hash<BALL::String>
{
size_t operator () (const BALL::String& s) const { return boost::hash<std::string>()(s); }
};
template <typename T1, typename T2, typename T3>
struct hash<BALL::Triple<T1, T2, T3> >
{
size_t operator () (const BALL::Triple<T1, T2, T3>& s) const
{
size_t hash = 0;
boost::hash_combine(hash, s.first);
boost::hash_combine(hash, s.second);
boost::hash_combine(hash, s.third);
return hash;
}
};
template <typename T1, typename T2, typename T3, typename T4>
struct hash<BALL::Quadruple<T1, T2, T3, T4> >
{
size_t operator () (const BALL::Quadruple<T1, T2, T3, T4>& s) const
{
size_t hash = 0;
boost::hash_combine(hash, s.first);
boost::hash_combine(hash, s.second);
boost::hash_combine(hash, s.third);
boost::hash_combine(hash, s.fourth);
return hash;
}
};
}
namespace BALL
{
template <class Key, class T>
class HashMap : public boost::unordered_map<Key, T>
{
public:
class IllegalKey : public Exception::GeneralException
{
public:
IllegalKey(const char* file, int line)
: Exception::GeneralException(file, line)
{
}
};
typedef boost::unordered_map<Key, T> Base;
typedef typename Base::value_type ValueType;
typedef Key KeyType;
typedef typename Base::value_type* PointerType;
typedef typename Base::iterator Iterator;
typedef typename Base::const_iterator ConstIterator;
inline bool has(const Key& key) const
{
return Base::find(key) != Base::end();
}
const T& operator [] (const Key& key) const;
T& operator [] (const Key& key);
bool operator == (const HashMap<Key, T>& rhs) const;
Size size() const { return Base::size(); }
};
//******************************************************************************************
// Implementations of template methods
//******************************************************************************************
template <class Key, class T>
const T& HashMap<Key, T>::operator [] (const Key& key) const
{
ConstIterator it = this->find(key);
if (it == Base::end())
{
throw IllegalKey(__FILE__, __LINE__);
}
else
{
return it->second;
}
}
template <class Key, class T>
bool HashMap<Key, T>::operator == (const HashMap<Key, T>& rhs) const
{
// No equality if sizes differ.
if (Base::size() != rhs.size())
{
return false;
}
// Equality if bothe have the same size and every element of lhs is
// is contained in lhs. Testing the other way round is obviously
// unnecessary.
ConstIterator it(Base::begin());
for (; it != Base::end(); ++it)
{
if (!rhs.has(it->first)) return false;
}
return true;
}
template <class Key, class T>
T& HashMap<Key, T>::operator [] (const Key& key)
{
return Base::operator[] (key);
}
} // namespace BALL
#endif // BALL_DATATYPE_HASHMAP_H
exception.h
BALL::Exception::GeneralException
Definition COMMON/exception.h:61
BALL::Exception::GeneralException::GeneralException
GeneralException()
Default constructor.
BALL::HashMap::IllegalKey
HashMap illegal key exception.
Definition hashMap.h:83
BALL::HashMap::IllegalKey::IllegalKey
IllegalKey(const char *file, int line)
Definition hashMap.h:85
BALL::HashMap
HashMap class based on the STL map (containing serveral convenience functions).
Definition hashMap.h:74
BALL::HashMap::operator[]
const T & operator[](const Key &key) const
Return a constant reference to the element whose key is key.
Definition hashMap.h:130
BALL::HashMap< LongSize, void * >::PointerType
Base::value_type * PointerType
Definition hashMap.h:96
BALL::HashMap::has
bool has(const Key &key) const
Test whether the map contains the given key.
Definition hashMap.h:102
BALL::HashMap< LongSize, void * >::ValueType
Base::value_type ValueType
Definition hashMap.h:94
BALL::HashMap< LongSize, void * >::Iterator
Base::iterator Iterator
Definition hashMap.h:97
BALL::HashMap::size
Size size() const
Definition hashMap.h:122
BALL::HashMap< LongSize, void * >::KeyType
LongSize KeyType
Definition hashMap.h:95
BALL::HashMap< LongSize, void * >::Base
boost::unordered_map< LongSize, void * > Base
Definition hashMap.h:93
BALL::HashMap< LongSize, void * >::ConstIterator
Base::const_iterator ConstIterator
Definition hashMap.h:98
BALL::HashMap::operator==
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
BALL::Quadruple
Definition quadruple.h:23
BALL::Quadruple::first
T1 first
Definition quadruple.h:124
BALL::Quadruple::fourth
T4 fourth
Definition quadruple.h:136
BALL::Quadruple::third
T3 third
Definition quadruple.h:132
BALL::Quadruple::second
T2 second
Definition quadruple.h:128
BALL::String
Definition string.h:57
BALL::Triple
Definition triple.h:25
BALL::Triple::second
T2 second
Definition triple.h:128
BALL::Triple::third
T3 third
Definition triple.h:132
BALL::Triple::first
T1 first
Definition triple.h:124
BALL::Exception
Definition COMMON/exception.h:33
BALL
Definition constants.h:13
BALL::Size
BALL_SIZE_TYPE Size
Definition COMMON/global.h:114
boost
Definition graphAlgorithms.h:27
quadruple.h
string.h
triple.h