2009-03-08 00:21:10 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2004-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2007-02-24 03:30:19 +03:00
|
|
|
#ifndef HASH_MAP_H
|
|
|
|
#define HASH_MAP_H
|
|
|
|
|
|
|
|
//#include <Debug.h>
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
#include <util/OpenHashTable.h>
|
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
#include "AutoLocker.h"
|
|
|
|
#include "Locker.h"
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
|
|
|
|
// HashMapElement
|
|
|
|
template<typename Key, typename Value>
|
2009-07-27 04:39:12 +04:00
|
|
|
class HashMapElement {
|
2007-02-24 03:30:19 +03:00
|
|
|
private:
|
|
|
|
typedef HashMapElement<Key, Value> Element;
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
public:
|
|
|
|
HashMapElement()
|
|
|
|
:
|
|
|
|
fKey(),
|
|
|
|
fValue()
|
2007-02-24 03:30:19 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
HashMapElement(const Key& key, const Value& value)
|
|
|
|
:
|
|
|
|
fKey(key),
|
|
|
|
fValue(value)
|
2007-02-24 03:30:19 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
Key fKey;
|
|
|
|
Value fValue;
|
|
|
|
HashMapElement* fNext;
|
2007-02-24 03:30:19 +03:00
|
|
|
};
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
|
|
|
// HashMapTableDefinition
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
struct HashMapTableDefinition {
|
|
|
|
typedef Key KeyType;
|
|
|
|
typedef HashMapElement<Key, Value> ValueType;
|
|
|
|
|
|
|
|
size_t HashKey(const KeyType& key) const
|
|
|
|
{ return key.GetHashCode(); }
|
|
|
|
size_t Hash(const ValueType* value) const
|
|
|
|
{ return HashKey(value->fKey); }
|
|
|
|
bool Compare(const KeyType& key, const ValueType* value) const
|
|
|
|
{ return value->fKey == key; }
|
2009-07-27 04:39:12 +04:00
|
|
|
ValueType*& GetLink(ValueType* value) const
|
|
|
|
{ return value->fNext; }
|
2009-03-08 00:21:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// HashMap
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
class HashMap {
|
|
|
|
public:
|
|
|
|
class Entry {
|
|
|
|
public:
|
|
|
|
Entry() {}
|
|
|
|
Entry(const Key& key, Value value) : key(key), value(value) {}
|
|
|
|
|
|
|
|
Key key;
|
|
|
|
Value value;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Iterator {
|
|
|
|
private:
|
|
|
|
typedef HashMapElement<Key, Value> Element;
|
|
|
|
public:
|
|
|
|
Iterator(const Iterator& other)
|
2009-03-08 00:21:10 +03:00
|
|
|
:
|
|
|
|
fMap(other.fMap),
|
|
|
|
fIterator(other.fIterator),
|
|
|
|
fElement(other.fElement)
|
2007-02-24 03:30:19 +03:00
|
|
|
{
|
|
|
|
}
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
bool HasNext() const
|
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
return fIterator.HasNext();
|
2007-02-24 03:30:19 +03:00
|
|
|
}
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
Entry Next()
|
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
fElement = fIterator.Next();
|
|
|
|
if (fElement == NULL)
|
2007-02-24 03:30:19 +03:00
|
|
|
return Entry();
|
2009-03-08 00:21:10 +03:00
|
|
|
|
|
|
|
return Entry(fElement->fKey, fElement->fValue);
|
2007-02-24 03:30:19 +03:00
|
|
|
}
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
Entry Remove()
|
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
if (fElement == NULL)
|
2007-02-24 03:30:19 +03:00
|
|
|
return Entry();
|
2009-03-08 00:21:10 +03:00
|
|
|
|
|
|
|
Entry result(fElement->fKey, fElement->fValue);
|
|
|
|
|
|
|
|
fMap->fTable.RemoveUnchecked(fElement);
|
|
|
|
delete fElement;
|
|
|
|
fElement = NULL;
|
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
return result;
|
|
|
|
}
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
Iterator& operator=(const Iterator& other)
|
|
|
|
{
|
|
|
|
fMap = other.fMap;
|
2009-03-08 00:21:10 +03:00
|
|
|
fIterator = other.fIterator;
|
2007-02-24 03:30:19 +03:00
|
|
|
fElement = other.fElement;
|
|
|
|
return *this;
|
|
|
|
}
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
private:
|
|
|
|
Iterator(HashMap<Key, Value>* map)
|
2009-03-08 00:21:10 +03:00
|
|
|
:
|
|
|
|
fMap(map),
|
|
|
|
fIterator(map->fTable.GetIterator()),
|
|
|
|
fElement(NULL)
|
2007-02-24 03:30:19 +03:00
|
|
|
{
|
|
|
|
}
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
private:
|
|
|
|
friend class HashMap<Key, Value>;
|
2009-07-27 04:39:12 +04:00
|
|
|
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> >
|
|
|
|
ElementTable;
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2009-03-19 01:43:47 +03:00
|
|
|
HashMap<Key, Value>* fMap;
|
|
|
|
typename ElementTable::Iterator fIterator;
|
|
|
|
Element* fElement;
|
2007-02-24 03:30:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
HashMap();
|
|
|
|
~HashMap();
|
|
|
|
|
|
|
|
status_t InitCheck() const;
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
status_t Put(const Key& key, const Value& value);
|
2007-02-24 03:30:19 +03:00
|
|
|
Value Remove(const Key& key);
|
|
|
|
void Clear();
|
|
|
|
Value Get(const Key& key) const;
|
|
|
|
|
|
|
|
bool ContainsKey(const Key& key) const;
|
|
|
|
|
|
|
|
int32 Size() const;
|
|
|
|
|
|
|
|
Iterator GetIterator();
|
|
|
|
|
|
|
|
protected:
|
2009-07-27 04:39:12 +04:00
|
|
|
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> > ElementTable;
|
2007-02-24 03:30:19 +03:00
|
|
|
typedef HashMapElement<Key, Value> Element;
|
|
|
|
friend class Iterator;
|
|
|
|
|
|
|
|
protected:
|
2009-03-08 00:21:10 +03:00
|
|
|
ElementTable fTable;
|
2007-02-24 03:30:19 +03:00
|
|
|
};
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// SynchronizedHashMap
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
class SynchronizedHashMap : public Locker {
|
|
|
|
public:
|
2007-03-04 11:26:45 +03:00
|
|
|
typedef typename HashMap<Key, Value>::Entry Entry;
|
|
|
|
typedef typename HashMap<Key, Value>::Iterator Iterator;
|
2007-02-24 03:30:19 +03:00
|
|
|
|
|
|
|
SynchronizedHashMap() : Locker("synchronized hash map") {}
|
|
|
|
~SynchronizedHashMap() { Lock(); }
|
|
|
|
|
|
|
|
status_t InitCheck() const
|
|
|
|
{
|
|
|
|
return fMap.InitCheck();
|
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
status_t Put(const Key& key, const Value& value)
|
2007-02-24 03:30:19 +03:00
|
|
|
{
|
|
|
|
MapLocker locker(this);
|
|
|
|
if (!locker.IsLocked())
|
|
|
|
return B_ERROR;
|
|
|
|
return fMap.Put(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Remove(const Key& key)
|
|
|
|
{
|
|
|
|
MapLocker locker(this);
|
|
|
|
if (!locker.IsLocked())
|
|
|
|
return Value();
|
|
|
|
return fMap.Remove(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
MapLocker locker(this);
|
|
|
|
return fMap.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Get(const Key& key) const
|
|
|
|
{
|
|
|
|
const Locker* lock = this;
|
|
|
|
MapLocker locker(const_cast<Locker*>(lock));
|
|
|
|
if (!locker.IsLocked())
|
|
|
|
return Value();
|
|
|
|
return fMap.Get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContainsKey(const Key& key) const
|
|
|
|
{
|
|
|
|
const Locker* lock = this;
|
|
|
|
MapLocker locker(const_cast<Locker*>(lock));
|
|
|
|
if (!locker.IsLocked())
|
|
|
|
return false;
|
|
|
|
return fMap.ContainsKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 Size() const
|
|
|
|
{
|
|
|
|
const Locker* lock = this;
|
|
|
|
MapLocker locker(const_cast<Locker*>(lock));
|
|
|
|
return fMap.Size();
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator GetIterator()
|
|
|
|
{
|
|
|
|
return fMap.GetIterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
// for debugging only
|
|
|
|
const HashMap<Key, Value>& GetUnsynchronizedMap() const { return fMap; }
|
|
|
|
HashMap<Key, Value>& GetUnsynchronizedMap() { return fMap; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef AutoLocker<Locker> MapLocker;
|
|
|
|
|
|
|
|
HashMap<Key, Value> fMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
// HashKey32
|
|
|
|
template<typename Value>
|
|
|
|
struct HashKey32 {
|
|
|
|
HashKey32() {}
|
|
|
|
HashKey32(const Value& value) : value(value) {}
|
|
|
|
|
|
|
|
uint32 GetHashCode() const
|
|
|
|
{
|
|
|
|
return (uint32)value;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashKey32<Value> operator=(const HashKey32<Value>& other)
|
|
|
|
{
|
|
|
|
value = other.value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const HashKey32<Value>& other) const
|
|
|
|
{
|
|
|
|
return (value == other.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const HashKey32<Value>& other) const
|
|
|
|
{
|
|
|
|
return (value != other.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// HashKey64
|
|
|
|
template<typename Value>
|
|
|
|
struct HashKey64 {
|
|
|
|
HashKey64() {}
|
|
|
|
HashKey64(const Value& value) : value(value) {}
|
|
|
|
|
|
|
|
uint32 GetHashCode() const
|
|
|
|
{
|
|
|
|
uint64 v = (uint64)value;
|
|
|
|
return (uint32)(v >> 32) ^ (uint32)v;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashKey64<Value> operator=(const HashKey64<Value>& other)
|
|
|
|
{
|
|
|
|
value = other.value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const HashKey64<Value>& other) const
|
|
|
|
{
|
|
|
|
return (value == other.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const HashKey64<Value>& other) const
|
|
|
|
{
|
|
|
|
return (value != other.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// HashMap
|
|
|
|
|
|
|
|
// constructor
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
HashMap<Key, Value>::HashMap()
|
2009-03-08 00:21:10 +03:00
|
|
|
:
|
|
|
|
fTable()
|
2007-02-24 03:30:19 +03:00
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
fTable.Init();
|
2007-02-24 03:30:19 +03:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// destructor
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
HashMap<Key, Value>::~HashMap()
|
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
Clear();
|
2007-02-24 03:30:19 +03:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// InitCheck
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
status_t
|
|
|
|
HashMap<Key, Value>::InitCheck() const
|
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
return (fTable.TableSize() > 0 ? B_OK : B_NO_MEMORY);
|
2007-02-24 03:30:19 +03:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// Put
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
status_t
|
2009-03-08 00:21:10 +03:00
|
|
|
HashMap<Key, Value>::Put(const Key& key, const Value& value)
|
2007-02-24 03:30:19 +03:00
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
Element* element = fTable.Lookup(key);
|
2007-02-24 03:30:19 +03:00
|
|
|
if (element) {
|
|
|
|
// already contains the key: just set the new value
|
|
|
|
element->fValue = value;
|
|
|
|
return B_OK;
|
|
|
|
}
|
2009-03-08 00:21:10 +03:00
|
|
|
|
|
|
|
// does not contain the key yet: create an element and add it
|
|
|
|
element = new(std::nothrow) Element(key, value);
|
2007-02-24 03:30:19 +03:00
|
|
|
if (!element)
|
|
|
|
return B_NO_MEMORY;
|
2009-03-08 00:21:10 +03:00
|
|
|
|
|
|
|
status_t error = fTable.Insert(element);
|
|
|
|
if (error != B_OK)
|
|
|
|
delete element;
|
|
|
|
|
|
|
|
return error;
|
2007-02-24 03:30:19 +03:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// Remove
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
Value
|
|
|
|
HashMap<Key, Value>::Remove(const Key& key)
|
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
Element* element = fTable.Lookup(key);
|
|
|
|
if (element == NULL)
|
|
|
|
return Value();
|
|
|
|
|
|
|
|
fTable.Remove(element);
|
|
|
|
Value value = element->fValue;
|
|
|
|
delete element;
|
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// Clear
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
void
|
|
|
|
HashMap<Key, Value>::Clear()
|
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
// clear the table and delete the elements
|
|
|
|
Element* element = fTable.Clear(true);
|
|
|
|
while (element != NULL) {
|
|
|
|
Element* next = element->fNext;
|
|
|
|
delete element;
|
|
|
|
element = next;
|
|
|
|
}
|
2007-02-24 03:30:19 +03:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// Get
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
Value
|
|
|
|
HashMap<Key, Value>::Get(const Key& key) const
|
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
if (Element* element = fTable.Lookup(key))
|
2007-02-24 03:30:19 +03:00
|
|
|
return element->fValue;
|
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// ContainsKey
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
bool
|
|
|
|
HashMap<Key, Value>::ContainsKey(const Key& key) const
|
|
|
|
{
|
2009-03-08 00:21:10 +03:00
|
|
|
return fTable.Lookup(key) != NULL;
|
2007-02-24 03:30:19 +03:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// Size
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
int32
|
|
|
|
HashMap<Key, Value>::Size() const
|
|
|
|
{
|
|
|
|
return fTable.CountElements();
|
|
|
|
}
|
|
|
|
|
2009-03-08 00:21:10 +03:00
|
|
|
|
2007-02-24 03:30:19 +03:00
|
|
|
// GetIterator
|
|
|
|
template<typename Key, typename Value>
|
2007-03-04 11:26:45 +03:00
|
|
|
typename HashMap<Key, Value>::Iterator
|
2007-02-24 03:30:19 +03:00
|
|
|
HashMap<Key, Value>::GetIterator()
|
|
|
|
{
|
|
|
|
return Iterator(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // HASH_MAP_H
|