shared: Merge BOpenHashTable in; remove OpenTracker's OpenHashTable.
The HashMap and HashSet classes are copied from userlandfs. The HashMap one works as-is as it's already used in userlandfs; the HashSet does not even compile yet. Change-Id: I1deabb54deb3f289e266794ce618948b60be58c0 Reviewed-on: https://review.haiku-os.org/c/1041 Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
committed by
waddlesplash
parent
4e29847d38
commit
eff1e73cef
+161
-172
@@ -1,75 +1,64 @@
|
|||||||
// HashMap.h
|
/*
|
||||||
//
|
* Copyright 2004-2009, Ingo Weinhold, [email protected].
|
||||||
// Copyright (c) 2004-2007, Ingo Weinhold ([email protected])
|
* Distributed under the terms of the MIT License.
|
||||||
//
|
*/
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
|
||||||
// to deal in the Software without restriction, including without limitation
|
|
||||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
// and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
// Software is furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
// DEALINGS IN THE SOFTWARE.
|
|
||||||
//
|
|
||||||
// Except as contained in this notice, the name of a copyright holder shall
|
|
||||||
// not be used in advertising or otherwise to promote the sale, use or other
|
|
||||||
// dealings in this Software without prior written authorization of the
|
|
||||||
// copyright holder.
|
|
||||||
|
|
||||||
#ifndef HASH_MAP_H
|
#ifndef HASH_MAP_H
|
||||||
#define HASH_MAP_H
|
#define HASH_MAP_H
|
||||||
|
|
||||||
|
//#include <Debug.h>
|
||||||
|
|
||||||
#include <Locker.h>
|
#include <util/OpenHashTable.h>
|
||||||
|
|
||||||
#include "AutoLocker.h"
|
#include "AutoLocker.h"
|
||||||
#include "OpenHashTable.h"
|
#include "Locker.h"
|
||||||
|
|
||||||
|
|
||||||
namespace BPrivate {
|
|
||||||
|
|
||||||
// HashMapElement
|
// HashMapElement
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
class HashMapElement : public OpenHashElement {
|
class HashMapElement {
|
||||||
private:
|
private:
|
||||||
typedef HashMapElement<Key, Value> Element;
|
typedef HashMapElement<Key, Value> Element;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
HashMapElement()
|
||||||
HashMapElement() : OpenHashElement(), fKey(), fValue()
|
:
|
||||||
|
fKey(),
|
||||||
|
fValue(),
|
||||||
|
fNext(NULL)
|
||||||
{
|
{
|
||||||
fNext = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline uint32 Hash() const
|
HashMapElement(const Key& key, const Value& value)
|
||||||
|
:
|
||||||
|
fKey(key),
|
||||||
|
fValue(value),
|
||||||
|
fNext(NULL)
|
||||||
{
|
{
|
||||||
return fKey.GetHashCode();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator==(const OpenHashElement &_element) const
|
Key fKey;
|
||||||
{
|
Value fValue;
|
||||||
const Element &element = static_cast<const Element&>(_element);
|
HashMapElement* fNext;
|
||||||
return (fKey == element.fKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Adopt(Element &element)
|
|
||||||
{
|
|
||||||
fKey = element.fKey;
|
|
||||||
fValue = element.fValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Key fKey;
|
|
||||||
Value fValue;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// 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; }
|
||||||
|
ValueType*& GetLink(ValueType* value) const
|
||||||
|
{ return value->fNext; }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// HashMap
|
// HashMap
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
class HashMap {
|
class HashMap {
|
||||||
@@ -90,87 +79,64 @@ public:
|
|||||||
Iterator(const Iterator& other)
|
Iterator(const Iterator& other)
|
||||||
:
|
:
|
||||||
fMap(other.fMap),
|
fMap(other.fMap),
|
||||||
fIndex(other.fIndex),
|
fIterator(other.fIterator),
|
||||||
fElement(other.fElement),
|
fElement(other.fElement)
|
||||||
fLastElement(other.fElement)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HasNext() const
|
bool HasNext() const
|
||||||
{
|
{
|
||||||
return fElement;
|
return fIterator.HasNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
Entry Next()
|
Entry Next()
|
||||||
{
|
{
|
||||||
if (!fElement)
|
fElement = fIterator.Next();
|
||||||
return Entry();
|
|
||||||
Entry result(fElement->fKey, fElement->fValue);
|
|
||||||
_FindNext();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Value* NextValue()
|
|
||||||
{
|
|
||||||
if (fElement == NULL)
|
if (fElement == NULL)
|
||||||
return NULL;
|
return Entry();
|
||||||
|
|
||||||
Value* value = &fElement->fValue;
|
return Entry(fElement->fKey, fElement->fValue);
|
||||||
_FindNext();
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Entry Remove()
|
Entry Remove()
|
||||||
{
|
{
|
||||||
if (!fLastElement)
|
if (fElement == NULL)
|
||||||
return Entry();
|
return Entry();
|
||||||
Entry result(fLastElement->fKey, fLastElement->fValue);
|
|
||||||
fMap->fTable.Remove(fLastElement, true);
|
Entry result(fElement->fKey, fElement->fValue);
|
||||||
fLastElement = NULL;
|
|
||||||
|
fMap->fTable.RemoveUnchecked(fElement);
|
||||||
|
delete fElement;
|
||||||
|
fElement = NULL;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator& operator=(const Iterator& other)
|
Iterator& operator=(const Iterator& other)
|
||||||
{
|
{
|
||||||
fMap = other.fMap;
|
fMap = other.fMap;
|
||||||
fIndex = other.fIndex;
|
fIterator = other.fIterator;
|
||||||
fElement = other.fElement;
|
fElement = other.fElement;
|
||||||
fLastElement = other.fLastElement;
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Iterator(const HashMap<Key, Value>* map)
|
Iterator(HashMap<Key, Value>* map)
|
||||||
:
|
:
|
||||||
fMap(const_cast<HashMap<Key, Value>*>(map)),
|
fMap(map),
|
||||||
fIndex(0),
|
fIterator(map->fTable.GetIterator()),
|
||||||
fElement(NULL),
|
fElement(NULL)
|
||||||
fLastElement(NULL)
|
|
||||||
{
|
{
|
||||||
// find first
|
|
||||||
_FindNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
void _FindNext()
|
|
||||||
{
|
|
||||||
fLastElement = fElement;
|
|
||||||
if (fElement && fElement->fNext >= 0) {
|
|
||||||
fElement = fMap->fTable.ElementAt(fElement->fNext);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fElement = NULL;
|
|
||||||
int32 arraySize = fMap->fTable.ArraySize();
|
|
||||||
for (; !fElement && fIndex < arraySize; fIndex++)
|
|
||||||
fElement = fMap->fTable.FindFirst(fIndex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class HashMap<Key, Value>;
|
friend class HashMap<Key, Value>;
|
||||||
|
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> >
|
||||||
|
ElementTable;
|
||||||
|
|
||||||
HashMap<Key, Value>* fMap;
|
HashMap<Key, Value>* fMap;
|
||||||
int32 fIndex;
|
typename ElementTable::Iterator fIterator;
|
||||||
Element* fElement;
|
Element* fElement;
|
||||||
Element* fLastElement;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
HashMap();
|
HashMap();
|
||||||
@@ -178,38 +144,35 @@ public:
|
|||||||
|
|
||||||
status_t InitCheck() const;
|
status_t InitCheck() const;
|
||||||
|
|
||||||
status_t Put(const Key& key, Value value);
|
status_t Put(const Key& key, const Value& value);
|
||||||
Value Remove(const Key& key);
|
Value Remove(const Key& key);
|
||||||
void Clear();
|
void Clear();
|
||||||
Value Get(const Key& key) const;
|
Value Get(const Key& key) const;
|
||||||
bool Get(const Key& key, Value*& _value) const;
|
|
||||||
|
|
||||||
bool ContainsKey(const Key& key) const;
|
bool ContainsKey(const Key& key) const;
|
||||||
|
|
||||||
int32 Size() const;
|
int32 Size() const;
|
||||||
|
|
||||||
Iterator GetIterator() const;
|
Iterator GetIterator();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> > ElementTable;
|
||||||
typedef HashMapElement<Key, Value> Element;
|
typedef HashMapElement<Key, Value> Element;
|
||||||
friend class Iterator;
|
friend class Iterator;
|
||||||
|
|
||||||
private:
|
|
||||||
Element *_FindElement(const Key& key) const;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
OpenHashElementArray<Element> fElementArray;
|
ElementTable fTable;
|
||||||
OpenHashTable<Element, OpenHashElementArray<Element> > fTable;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// SynchronizedHashMap
|
// SynchronizedHashMap
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
class SynchronizedHashMap : public BLocker {
|
class SynchronizedHashMap : public Locker {
|
||||||
public:
|
public:
|
||||||
typedef struct HashMap<Key, Value>::Entry Entry;
|
typedef typename HashMap<Key, Value>::Entry Entry;
|
||||||
typedef struct HashMap<Key, Value>::Iterator Iterator;
|
typedef typename HashMap<Key, Value>::Iterator Iterator;
|
||||||
|
|
||||||
SynchronizedHashMap() : BLocker("synchronized hash map") {}
|
SynchronizedHashMap() : Locker("synchronized hash map") {}
|
||||||
~SynchronizedHashMap() { Lock(); }
|
~SynchronizedHashMap() { Lock(); }
|
||||||
|
|
||||||
status_t InitCheck() const
|
status_t InitCheck() const
|
||||||
@@ -217,7 +180,7 @@ public:
|
|||||||
return fMap.InitCheck();
|
return fMap.InitCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
status_t Put(const Key& key, Value value)
|
status_t Put(const Key& key, const Value& value)
|
||||||
{
|
{
|
||||||
MapLocker locker(this);
|
MapLocker locker(this);
|
||||||
if (!locker.IsLocked())
|
if (!locker.IsLocked())
|
||||||
@@ -241,8 +204,8 @@ public:
|
|||||||
|
|
||||||
Value Get(const Key& key) const
|
Value Get(const Key& key) const
|
||||||
{
|
{
|
||||||
const BLocker* lock = this;
|
const Locker* lock = this;
|
||||||
MapLocker locker(const_cast<BLocker*>(lock));
|
MapLocker locker(const_cast<Locker*>(lock));
|
||||||
if (!locker.IsLocked())
|
if (!locker.IsLocked())
|
||||||
return Value();
|
return Value();
|
||||||
return fMap.Get(key);
|
return fMap.Get(key);
|
||||||
@@ -250,8 +213,8 @@ public:
|
|||||||
|
|
||||||
bool ContainsKey(const Key& key) const
|
bool ContainsKey(const Key& key) const
|
||||||
{
|
{
|
||||||
const BLocker* lock = this;
|
const Locker* lock = this;
|
||||||
MapLocker locker(const_cast<BLocker*>(lock));
|
MapLocker locker(const_cast<Locker*>(lock));
|
||||||
if (!locker.IsLocked())
|
if (!locker.IsLocked())
|
||||||
return false;
|
return false;
|
||||||
return fMap.ContainsKey(key);
|
return fMap.ContainsKey(key);
|
||||||
@@ -259,8 +222,8 @@ public:
|
|||||||
|
|
||||||
int32 Size() const
|
int32 Size() const
|
||||||
{
|
{
|
||||||
const BLocker* lock = this;
|
const Locker* lock = this;
|
||||||
MapLocker locker(const_cast<BLocker*>(lock));
|
MapLocker locker(const_cast<Locker*>(lock));
|
||||||
return fMap.Size();
|
return fMap.Size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,7 +237,7 @@ public:
|
|||||||
HashMap<Key, Value>& GetUnsynchronizedMap() { return fMap; }
|
HashMap<Key, Value>& GetUnsynchronizedMap() { return fMap; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
typedef AutoLocker<BLocker> MapLocker;
|
typedef AutoLocker<Locker> MapLocker;
|
||||||
|
|
||||||
HashMap<Key, Value> fMap;
|
HashMap<Key, Value> fMap;
|
||||||
};
|
};
|
||||||
@@ -342,104 +305,150 @@ struct HashKey64 {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// HashKeyPointer
|
||||||
|
template<typename Value>
|
||||||
|
struct HashKeyPointer {
|
||||||
|
HashKeyPointer() {}
|
||||||
|
HashKeyPointer(const Value& value) : value(value) {}
|
||||||
|
|
||||||
|
uint32 GetHashCode() const
|
||||||
|
{
|
||||||
|
#if __HAIKU_ARCH_BITS == 32
|
||||||
|
return (uint32)(addr_t)value;
|
||||||
|
#elif __HAIKU_ARCH_BITS == 64
|
||||||
|
uint64 v = (uint64)(addr_t)value;
|
||||||
|
return (uint32)(v >> 32) ^ (uint32)v;
|
||||||
|
#else
|
||||||
|
#error unknown bitness
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
HashKeyPointer<Value> operator=(const HashKeyPointer<Value>& other)
|
||||||
|
{
|
||||||
|
value = other.value;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const HashKeyPointer<Value>& other) const
|
||||||
|
{
|
||||||
|
return (value == other.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const HashKeyPointer<Value>& other) const
|
||||||
|
{
|
||||||
|
return (value != other.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Value value;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// HashMap
|
// HashMap
|
||||||
|
|
||||||
// constructor
|
// constructor
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
HashMap<Key, Value>::HashMap()
|
HashMap<Key, Value>::HashMap()
|
||||||
:
|
:
|
||||||
fElementArray(1000),
|
fTable()
|
||||||
fTable(1000, &fElementArray)
|
|
||||||
{
|
{
|
||||||
|
fTable.Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// destructor
|
// destructor
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
HashMap<Key, Value>::~HashMap()
|
HashMap<Key, Value>::~HashMap()
|
||||||
{
|
{
|
||||||
|
Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// InitCheck
|
// InitCheck
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
status_t
|
status_t
|
||||||
HashMap<Key, Value>::InitCheck() const
|
HashMap<Key, Value>::InitCheck() const
|
||||||
{
|
{
|
||||||
return (fTable.InitCheck() && fElementArray.InitCheck()
|
return (fTable.TableSize() > 0 ? B_OK : B_NO_MEMORY);
|
||||||
? B_OK : B_NO_MEMORY);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Put
|
// Put
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
status_t
|
status_t
|
||||||
HashMap<Key, Value>::Put(const Key& key, Value value)
|
HashMap<Key, Value>::Put(const Key& key, const Value& value)
|
||||||
{
|
{
|
||||||
Element* element = _FindElement(key);
|
Element* element = fTable.Lookup(key);
|
||||||
if (element) {
|
if (element) {
|
||||||
// already contains the key: just set the new value
|
// already contains the key: just set the new value
|
||||||
element->fValue = value;
|
element->fValue = value;
|
||||||
return B_OK;
|
return B_OK;
|
||||||
}
|
}
|
||||||
// does not contain the key yet: add an element
|
|
||||||
element = fTable.Add(key.GetHashCode());
|
// does not contain the key yet: create an element and add it
|
||||||
|
element = new(std::nothrow) Element(key, value);
|
||||||
if (!element)
|
if (!element)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
element->fKey = key;
|
|
||||||
element->fValue = value;
|
status_t error = fTable.Insert(element);
|
||||||
return B_OK;
|
if (error != B_OK)
|
||||||
|
delete element;
|
||||||
|
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Remove
|
// Remove
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
Value
|
Value
|
||||||
HashMap<Key, Value>::Remove(const Key& key)
|
HashMap<Key, Value>::Remove(const Key& key)
|
||||||
{
|
{
|
||||||
Value value = Value();
|
Element* element = fTable.Lookup(key);
|
||||||
if (Element* element = _FindElement(key)) {
|
if (element == NULL)
|
||||||
value = element->fValue;
|
return Value();
|
||||||
fTable.Remove(element);
|
|
||||||
}
|
fTable.Remove(element);
|
||||||
|
Value value = element->fValue;
|
||||||
|
delete element;
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Clear
|
// Clear
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
void
|
void
|
||||||
HashMap<Key, Value>::Clear()
|
HashMap<Key, Value>::Clear()
|
||||||
{
|
{
|
||||||
fTable.RemoveAll();
|
// clear the table and delete the elements
|
||||||
|
Element* element = fTable.Clear(true);
|
||||||
|
while (element != NULL) {
|
||||||
|
Element* next = element->fNext;
|
||||||
|
delete element;
|
||||||
|
element = next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get
|
// Get
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
Value
|
Value
|
||||||
HashMap<Key, Value>::Get(const Key& key) const
|
HashMap<Key, Value>::Get(const Key& key) const
|
||||||
{
|
{
|
||||||
if (Element* element = _FindElement(key))
|
if (Element* element = fTable.Lookup(key))
|
||||||
return element->fValue;
|
return element->fValue;
|
||||||
return Value();
|
return Value();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
bool
|
|
||||||
HashMap<Key, Value>::Get(const Key& key, Value*& _value) const
|
|
||||||
{
|
|
||||||
if (Element* element = _FindElement(key)) {
|
|
||||||
_value = &element->fValue;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ContainsKey
|
// ContainsKey
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
bool
|
bool
|
||||||
HashMap<Key, Value>::ContainsKey(const Key& key) const
|
HashMap<Key, Value>::ContainsKey(const Key& key) const
|
||||||
{
|
{
|
||||||
return _FindElement(key);
|
return fTable.Lookup(key) != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Size
|
// Size
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
int32
|
int32
|
||||||
@@ -448,34 +457,14 @@ HashMap<Key, Value>::Size() const
|
|||||||
return fTable.CountElements();
|
return fTable.CountElements();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// GetIterator
|
// GetIterator
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
class HashMap<Key, Value>::Iterator
|
typename HashMap<Key, Value>::Iterator
|
||||||
HashMap<Key, Value>::GetIterator() const
|
HashMap<Key, Value>::GetIterator()
|
||||||
{
|
{
|
||||||
return Iterator(this);
|
return Iterator(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// _FindElement
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
typename HashMap<Key, Value>::Element *
|
|
||||||
HashMap<Key, Value>::_FindElement(const Key& key) const
|
|
||||||
{
|
|
||||||
Element* element = fTable.FindFirst(key.GetHashCode());
|
|
||||||
while (element && element->fKey != key) {
|
|
||||||
if (element->fNext >= 0)
|
|
||||||
element = fTable.ElementAt(element->fNext);
|
|
||||||
else
|
|
||||||
element = NULL;
|
|
||||||
}
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace BPrivate
|
|
||||||
|
|
||||||
using BPrivate::HashMap;
|
|
||||||
using BPrivate::HashKey32;
|
|
||||||
using BPrivate::HashKey64;
|
|
||||||
using BPrivate::SynchronizedHashMap;
|
|
||||||
|
|
||||||
#endif // HASH_MAP_H
|
#endif // HASH_MAP_H
|
||||||
|
|||||||
+123
-123
@@ -1,72 +1,56 @@
|
|||||||
// HashSet.h
|
/*
|
||||||
//
|
* Copyright 2004-2009, Ingo Weinhold, [email protected].
|
||||||
// Copyright (c) 2004, Ingo Weinhold ([email protected])
|
* Distributed under the terms of the MIT License.
|
||||||
//
|
*/
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
|
||||||
// to deal in the Software without restriction, including without limitation
|
|
||||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
// and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
// Software is furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
// DEALINGS IN THE SOFTWARE.
|
|
||||||
//
|
|
||||||
// Except as contained in this notice, the name of a copyright holder shall
|
|
||||||
// not be used in advertising or otherwise to promote the sale, use or other
|
|
||||||
// dealings in this Software without prior written authorization of the
|
|
||||||
// copyright holder.
|
|
||||||
|
|
||||||
#ifndef HASH_SET_H
|
#ifndef HASH_SET_H
|
||||||
#define HASH_SET_H
|
#define HASH_SET_H
|
||||||
|
|
||||||
#include <Locker.h>
|
#include <util/OpenHashTable.h>
|
||||||
|
|
||||||
#include "AutoLocker.h"
|
#include "AutoLocker.h"
|
||||||
#include "OpenHashTable.h"
|
#include "Locker.h"
|
||||||
|
|
||||||
|
|
||||||
namespace BPrivate {
|
|
||||||
|
|
||||||
// HashSetElement
|
// HashSetElement
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
class HashSetElement : public OpenHashElement {
|
class HashSetElement : public HashTableLink<HashSetElement<Key> > {
|
||||||
private:
|
private:
|
||||||
typedef HashSetElement<Key> Element;
|
typedef HashSetElement<Key> Element;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
HashSetElement()
|
||||||
HashSetElement() : OpenHashElement(), fKey()
|
:
|
||||||
|
fKey()
|
||||||
{
|
{
|
||||||
fNext = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline uint32 Hash() const
|
HashSetElement(const Key& key)
|
||||||
|
:
|
||||||
|
fKey(key)
|
||||||
{
|
{
|
||||||
return fKey.GetHashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool operator==(const OpenHashElement &_element) const
|
|
||||||
{
|
|
||||||
const Element &element = static_cast<const Element&>(_element);
|
|
||||||
return (fKey == element.fKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Adopt(Element &element)
|
|
||||||
{
|
|
||||||
fKey = element.fKey;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Key fKey;
|
Key fKey;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// HashSetTableDefinition
|
||||||
|
template<typename Key>
|
||||||
|
struct HashSetTableDefinition {
|
||||||
|
typedef Key KeyType;
|
||||||
|
typedef HashSetElement<Key> 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; }
|
||||||
|
HashTableLink<ValueType>* GetLink(ValueType* value) const
|
||||||
|
{ return value; }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// HashSet
|
// HashSet
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
class HashSet {
|
class HashSet {
|
||||||
@@ -76,76 +60,66 @@ public:
|
|||||||
typedef HashSetElement<Key> Element;
|
typedef HashSetElement<Key> Element;
|
||||||
public:
|
public:
|
||||||
Iterator(const Iterator& other)
|
Iterator(const Iterator& other)
|
||||||
: fSet(other.fSet),
|
:
|
||||||
fIndex(other.fIndex),
|
fSet(other.fSet),
|
||||||
fElement(other.fElement),
|
fIterator(other.fIterator),
|
||||||
fLastElement(other.fElement)
|
fElement(other.fElement)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HasNext() const
|
bool HasNext() const
|
||||||
{
|
{
|
||||||
return fElement;
|
return fIterator.HasNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
Key Next()
|
Key Next()
|
||||||
{
|
{
|
||||||
if (!fElement)
|
fElement = fIterator.Next();
|
||||||
|
if (fElement == NULL)
|
||||||
return Key();
|
return Key();
|
||||||
Key result(fElement->fKey);
|
|
||||||
_FindNext();
|
return fElement->fKey;
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Remove()
|
bool Remove()
|
||||||
{
|
{
|
||||||
if (!fLastElement)
|
if (fElement == NULL)
|
||||||
return false;
|
return false;
|
||||||
fSet->fTable.Remove(fLastElement);
|
|
||||||
fLastElement = NULL;
|
fSet->fTable.RemoveUnchecked(fElement);
|
||||||
|
delete fElement;
|
||||||
|
fElement = NULL;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator& operator=(const Iterator& other)
|
Iterator& operator=(const Iterator& other)
|
||||||
{
|
{
|
||||||
fSet = other.fSet;
|
fSet = other.fSet;
|
||||||
fIndex = other.fIndex;
|
fIterator = other.fIterator;
|
||||||
fElement = other.fElement;
|
fElement = other.fElement;
|
||||||
fLastElement = other.fLastElement;
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Iterator(HashSet<Key>* map)
|
Iterator(HashSet<Key>* set)
|
||||||
: fSet(map),
|
:
|
||||||
fIndex(0),
|
fSet(set),
|
||||||
fElement(NULL),
|
fIterator(set->fTable.GetIterator()),
|
||||||
fLastElement(NULL)
|
fElement(NULL)
|
||||||
{
|
{
|
||||||
// find first
|
|
||||||
_FindNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
void _FindNext()
|
|
||||||
{
|
|
||||||
fLastElement = fElement;
|
|
||||||
if (fElement && fElement->fNext >= 0) {
|
|
||||||
fElement = fSet->fTable.ElementAt(fElement->fNext);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fElement = NULL;
|
|
||||||
int32 arraySize = fSet->fTable.ArraySize();
|
|
||||||
for (; !fElement && fIndex < arraySize; fIndex++)
|
|
||||||
fElement = fSet->fTable.FindFirst(fIndex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class HashSet<Key>;
|
friend class HashMap<Key, Value>;
|
||||||
|
typedef OpenHashTable<HashSetTableDefinition<Key> > ElementTable;
|
||||||
|
|
||||||
HashSet<Key>* fSet;
|
HashSet<Key>* fSet;
|
||||||
int32 fIndex;
|
ElementTable::Iterator fIterator;
|
||||||
Element* fElement;
|
Element* fElement;
|
||||||
Element* fLastElement;
|
|
||||||
|
private:
|
||||||
|
friend class HashSet<Key>;
|
||||||
};
|
};
|
||||||
|
|
||||||
HashSet();
|
HashSet();
|
||||||
@@ -159,29 +133,26 @@ public:
|
|||||||
bool Contains(const Key& key) const;
|
bool Contains(const Key& key) const;
|
||||||
|
|
||||||
int32 Size() const;
|
int32 Size() const;
|
||||||
bool IsEmpty() const { return Size() == 0; }
|
|
||||||
|
|
||||||
Iterator GetIterator();
|
Iterator GetIterator();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
typedef OpenHashTable<HashSetTableDefinition<Key> > ElementTable;
|
||||||
typedef HashSetElement<Key> Element;
|
typedef HashSetElement<Key> Element;
|
||||||
friend class Iterator;
|
friend class Iterator;
|
||||||
|
|
||||||
private:
|
|
||||||
Element *_FindElement(const Key& key) const;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
OpenHashElementArray<Element> fElementArray;
|
ElementTable fTable;
|
||||||
OpenHashTable<Element, OpenHashElementArray<Element> > fTable;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// SynchronizedHashSet
|
// SynchronizedHashSet
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
class SynchronizedHashSet : public BLocker {
|
class SynchronizedHashSet : public Locker {
|
||||||
public:
|
public:
|
||||||
typedef typename HashSet<Key>::Iterator Iterator;
|
typedef HashSet<Key>::Iterator Iterator;
|
||||||
|
|
||||||
SynchronizedHashSet() : BLocker("synchronized hash set") {}
|
SynchronizedHashSet() : Locker("synchronized hash set") {}
|
||||||
~SynchronizedHashSet() { Lock(); }
|
~SynchronizedHashSet() { Lock(); }
|
||||||
|
|
||||||
status_t InitCheck() const
|
status_t InitCheck() const
|
||||||
@@ -205,10 +176,16 @@ public:
|
|||||||
return fSet.Remove(key);
|
return fSet.Remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Clear()
|
||||||
|
{
|
||||||
|
MapLocker locker(this);
|
||||||
|
fSet.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
bool Contains(const Key& key) const
|
bool Contains(const Key& key) const
|
||||||
{
|
{
|
||||||
const BLocker* lock = this;
|
const Locker* lock = this;
|
||||||
MapLocker locker(const_cast<BLocker*>(lock));
|
MapLocker locker(const_cast<Locker*>(lock));
|
||||||
if (!locker.IsLocked())
|
if (!locker.IsLocked())
|
||||||
return false;
|
return false;
|
||||||
return fSet.Contains(key);
|
return fSet.Contains(key);
|
||||||
@@ -216,8 +193,8 @@ public:
|
|||||||
|
|
||||||
int32 Size() const
|
int32 Size() const
|
||||||
{
|
{
|
||||||
const BLocker* lock = this;
|
const Locker* lock = this;
|
||||||
MapLocker locker(const_cast<BLocker*>(lock));
|
MapLocker locker(const_cast<Locker*>(lock));
|
||||||
return fSet.Size();
|
return fSet.Size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,78 +208,105 @@ public:
|
|||||||
HashSet<Key>& GetUnsynchronizedSet() { return fSet; }
|
HashSet<Key>& GetUnsynchronizedSet() { return fSet; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
typedef AutoLocker<BLocker> MapLocker;
|
typedef AutoLocker<Locker> MapLocker;
|
||||||
|
|
||||||
HashSet<Key> fSet;
|
HashSet<Key> fSet;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// HashSet
|
// HashSet
|
||||||
|
|
||||||
// constructor
|
// constructor
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
HashSet<Key>::HashSet()
|
HashSet<Key>::HashSet()
|
||||||
: fElementArray(1000),
|
:
|
||||||
fTable(1000, &fElementArray)
|
fTable()
|
||||||
{
|
{
|
||||||
|
fTable.Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// destructor
|
// destructor
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
HashSet<Key>::~HashSet()
|
HashSet<Key>::~HashSet()
|
||||||
{
|
{
|
||||||
|
Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// InitCheck
|
// InitCheck
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
status_t
|
status_t
|
||||||
HashSet<Key>::InitCheck() const
|
HashSet<Key>::InitCheck() const
|
||||||
{
|
{
|
||||||
return (fTable.InitCheck() && fElementArray.InitCheck()
|
return (fTable.TableSize() > 0 ? B_OK : B_NO_MEMORY);
|
||||||
? B_OK : B_NO_MEMORY);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Add
|
// Add
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
status_t
|
status_t
|
||||||
HashSet<Key>::Add(const Key& key)
|
HashSet<Key>::Add(const Key& key)
|
||||||
{
|
{
|
||||||
if (Contains(key))
|
Element* element = fTable.Lookup(key);
|
||||||
|
if (element) {
|
||||||
|
// already contains the value
|
||||||
return B_OK;
|
return B_OK;
|
||||||
Element* element = fTable.Add(key.GetHashCode());
|
}
|
||||||
|
|
||||||
|
// does not contain the key yet: create an element and add it
|
||||||
|
element = new(std::nothrow) Element(key);
|
||||||
if (!element)
|
if (!element)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
element->fKey = key;
|
|
||||||
return B_OK;
|
status_t error = fTable.Insert(element);
|
||||||
|
if (error != B_OK)
|
||||||
|
delete element;
|
||||||
|
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Remove
|
// Remove
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
bool
|
bool
|
||||||
HashSet<Key>::Remove(const Key& key)
|
HashSet<Key>::Remove(const Key& key)
|
||||||
{
|
{
|
||||||
if (Element* element = _FindElement(key)) {
|
Element* element = fTable.Lookup(key);
|
||||||
fTable.Remove(element);
|
if (element == NULL)
|
||||||
return true;
|
return false;
|
||||||
}
|
|
||||||
return false;
|
fTable.Remove(element);
|
||||||
|
delete element;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Clear
|
// Clear
|
||||||
template<typename Key>
|
template<typename Key, typename Value>
|
||||||
void
|
void
|
||||||
HashSet<Key>::Clear()
|
HashSet<Key>::Clear()
|
||||||
{
|
{
|
||||||
fTable.RemoveAll();
|
// clear the table and delete the elements
|
||||||
|
Element* element = fTable.Clear(true);
|
||||||
|
while (element != NULL) {
|
||||||
|
Element* next = element->fNext;
|
||||||
|
delete element;
|
||||||
|
element = next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Contains
|
// Contains
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
bool
|
bool
|
||||||
HashSet<Key>::Contains(const Key& key) const
|
HashSet<Key>::Contains(const Key& key) const
|
||||||
{
|
{
|
||||||
return _FindElement(key);
|
return fTable.Lookup(key) != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Size
|
// Size
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
int32
|
int32
|
||||||
@@ -313,7 +317,7 @@ HashSet<Key>::Size() const
|
|||||||
|
|
||||||
// GetIterator
|
// GetIterator
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
typename HashSet<Key>::Iterator
|
HashSet<Key>::Iterator
|
||||||
HashSet<Key>::GetIterator()
|
HashSet<Key>::GetIterator()
|
||||||
{
|
{
|
||||||
return Iterator(this);
|
return Iterator(this);
|
||||||
@@ -321,7 +325,7 @@ HashSet<Key>::GetIterator()
|
|||||||
|
|
||||||
// _FindElement
|
// _FindElement
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
HashSetElement<Key> *
|
HashSet<Key>::Element *
|
||||||
HashSet<Key>::_FindElement(const Key& key) const
|
HashSet<Key>::_FindElement(const Key& key) const
|
||||||
{
|
{
|
||||||
Element* element = fTable.FindFirst(key.GetHashCode());
|
Element* element = fTable.FindFirst(key.GetHashCode());
|
||||||
@@ -334,9 +338,5 @@ HashSet<Key>::_FindElement(const Key& key) const
|
|||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace BPrivate
|
|
||||||
|
|
||||||
using BPrivate::HashSet;
|
|
||||||
using BPrivate::SynchronizedHashSet;
|
|
||||||
|
|
||||||
#endif // HASH_SET_H
|
#endif // HASH_SET_H
|
||||||
|
|||||||
@@ -1,514 +1 @@
|
|||||||
/*
|
#include <../kernel/util/OpenHashTable.h>
|
||||||
Open Tracker License
|
|
||||||
|
|
||||||
Terms and Conditions
|
|
||||||
|
|
||||||
Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
||||||
this software and associated documentation files (the "Software"), to deal in
|
|
||||||
the Software without restriction, including without limitation the rights to
|
|
||||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
||||||
of the Software, and to permit persons to whom the Software is furnished to do
|
|
||||||
so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice applies to all licensees
|
|
||||||
and shall be included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
||||||
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
|
|
||||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
Except as contained in this notice, the name of Be Incorporated shall not be
|
|
||||||
used in advertising or otherwise to promote the sale, use or other dealings in
|
|
||||||
this Software without prior written authorization from Be Incorporated.
|
|
||||||
|
|
||||||
Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
|
|
||||||
of Be Incorporated in the United States and other countries. Other brand product
|
|
||||||
names are registered trademarks or trademarks of their respective holders.
|
|
||||||
All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// bonefish:
|
|
||||||
// * removed need for exceptions
|
|
||||||
// * fixed warnings
|
|
||||||
// * implemented rehashing
|
|
||||||
// * added RemoveAll()
|
|
||||||
// TODO:
|
|
||||||
// * shrinking of element vectors
|
|
||||||
|
|
||||||
// Hash table with open addresssing
|
|
||||||
|
|
||||||
#ifndef __OPEN_HASH_TABLE__
|
|
||||||
#define __OPEN_HASH_TABLE__
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <new>
|
|
||||||
|
|
||||||
// don't include <Debug.h>
|
|
||||||
#ifndef _OPEN_HASH_TABLE_ASSERT
|
|
||||||
# define _OPEN_HASH_TABLE_ASSERT(E) (void)0
|
|
||||||
#endif
|
|
||||||
#ifndef _OPEN_HASH_TABLE_TRESPASS
|
|
||||||
# define _OPEN_HASH_TABLE_TRESPASS() (void)0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace BPrivate {
|
|
||||||
|
|
||||||
template <class Element>
|
|
||||||
class ElementVector {
|
|
||||||
// element vector for OpenHashTable needs to implement this
|
|
||||||
// interface
|
|
||||||
public:
|
|
||||||
Element &At(int32 index);
|
|
||||||
Element *Add();
|
|
||||||
int32 IndexOf(const Element &) const;
|
|
||||||
void Remove(int32 index);
|
|
||||||
};
|
|
||||||
|
|
||||||
class OpenHashElement {
|
|
||||||
public:
|
|
||||||
uint32 Hash() const;
|
|
||||||
bool operator==(const OpenHashElement &) const;
|
|
||||||
void Adopt(OpenHashElement &);
|
|
||||||
// low overhead copy, original element is in undefined state
|
|
||||||
// after call (calls Adopt on BString members, etc.)
|
|
||||||
int32 fNext;
|
|
||||||
};
|
|
||||||
|
|
||||||
const uint32 kPrimes [] = {
|
|
||||||
509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139,
|
|
||||||
524287, 1048573, 2097143, 4194301, 8388593, 16777213, 33554393, 67108859,
|
|
||||||
134217689, 268435399, 536870909, 1073741789, 2147483647, 0
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class Element, class ElementVec = ElementVector<Element> >
|
|
||||||
class OpenHashTable {
|
|
||||||
public:
|
|
||||||
OpenHashTable(int32 minSize, ElementVec *elementVector = 0,
|
|
||||||
float maxLoadFactor = 0.8);
|
|
||||||
// it is up to the subclass of OpenHashTable to supply
|
|
||||||
// elementVector
|
|
||||||
~OpenHashTable();
|
|
||||||
|
|
||||||
bool InitCheck() const;
|
|
||||||
|
|
||||||
void SetElementVector(ElementVec *elementVector);
|
|
||||||
|
|
||||||
Element *FindFirst(uint32 elementHash) const;
|
|
||||||
Element *Add(uint32 elementHash);
|
|
||||||
|
|
||||||
void Remove(Element *element, bool dontRehash = false);
|
|
||||||
void RemoveAll();
|
|
||||||
|
|
||||||
// when calling Add, any outstanding element pointer may become
|
|
||||||
// invalid; to deal with this, get the element index and restore
|
|
||||||
// it after the add
|
|
||||||
int32 ElementIndex(const Element *) const;
|
|
||||||
Element *ElementAt(int32 index) const;
|
|
||||||
|
|
||||||
int32 ArraySize() const;
|
|
||||||
int32 VectorSize() const;
|
|
||||||
int32 CountElements() const;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
static int32 OptimalSize(int32 minSize);
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool _RehashIfNeeded();
|
|
||||||
bool _Rehash();
|
|
||||||
|
|
||||||
int32 fArraySize;
|
|
||||||
int32 fInitialSize;
|
|
||||||
int32 fElementCount;
|
|
||||||
int32 *fHashArray;
|
|
||||||
ElementVec *fElementVector;
|
|
||||||
float fMaxLoadFactor;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class Element>
|
|
||||||
class OpenHashElementArray : public ElementVector<Element> {
|
|
||||||
// this is a straightforward implementation of an element vector
|
|
||||||
// deleting is handled by linking deleted elements into a free list
|
|
||||||
// the vector never shrinks
|
|
||||||
public:
|
|
||||||
OpenHashElementArray(int32 initialSize);
|
|
||||||
~OpenHashElementArray();
|
|
||||||
|
|
||||||
bool InitCheck() const;
|
|
||||||
|
|
||||||
Element &At(int32 index);
|
|
||||||
const Element &At(int32 index) const;
|
|
||||||
Element *Add(const Element &);
|
|
||||||
Element *Add();
|
|
||||||
void Remove(int32 index);
|
|
||||||
int32 IndexOf(const Element &) const;
|
|
||||||
int32 Size() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
Element *fData;
|
|
||||||
int32 fSize;
|
|
||||||
int32 fNextFree;
|
|
||||||
int32 fNextDeleted;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
OpenHashTable<Element, ElementVec>::OpenHashTable(int32 minSize,
|
|
||||||
ElementVec *elementVector, float maxLoadFactor)
|
|
||||||
: fArraySize(OptimalSize(minSize)),
|
|
||||||
fInitialSize(fArraySize),
|
|
||||||
fElementCount(0),
|
|
||||||
fElementVector(elementVector),
|
|
||||||
fMaxLoadFactor(maxLoadFactor)
|
|
||||||
{
|
|
||||||
// sanity check the maximal load factor
|
|
||||||
if (fMaxLoadFactor < 0.5)
|
|
||||||
fMaxLoadFactor = 0.5;
|
|
||||||
// allocate and init the array
|
|
||||||
fHashArray = (int32*)calloc(fArraySize, sizeof(int32));
|
|
||||||
if (fHashArray) {
|
|
||||||
for (int32 index = 0; index < fArraySize; index++)
|
|
||||||
fHashArray[index] = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
OpenHashTable<Element, ElementVec>::~OpenHashTable()
|
|
||||||
{
|
|
||||||
RemoveAll();
|
|
||||||
free(fHashArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
bool
|
|
||||||
OpenHashTable<Element, ElementVec>::InitCheck() const
|
|
||||||
{
|
|
||||||
return (fHashArray && fElementVector);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
int32
|
|
||||||
OpenHashTable<Element, ElementVec>::OptimalSize(int32 minSize)
|
|
||||||
{
|
|
||||||
for (int32 index = 0; ; index++)
|
|
||||||
if (!kPrimes[index] || kPrimes[index] >= (uint32)minSize)
|
|
||||||
return (int32)kPrimes[index];
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
Element *
|
|
||||||
OpenHashTable<Element, ElementVec>::FindFirst(uint32 hash) const
|
|
||||||
{
|
|
||||||
_OPEN_HASH_TABLE_ASSERT(fElementVector);
|
|
||||||
hash %= fArraySize;
|
|
||||||
if (fHashArray[hash] < 0)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return &fElementVector->At(fHashArray[hash]);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
int32
|
|
||||||
OpenHashTable<Element, ElementVec>::ElementIndex(const Element *element) const
|
|
||||||
{
|
|
||||||
return fElementVector->IndexOf(*element);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
Element *
|
|
||||||
OpenHashTable<Element, ElementVec>::ElementAt(int32 index) const
|
|
||||||
{
|
|
||||||
return &fElementVector->At(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
int32
|
|
||||||
OpenHashTable<Element, ElementVec>::ArraySize() const
|
|
||||||
{
|
|
||||||
return fArraySize;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
int32
|
|
||||||
OpenHashTable<Element, ElementVec>::VectorSize() const
|
|
||||||
{
|
|
||||||
return fElementVector->Size();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
int32
|
|
||||||
OpenHashTable<Element, ElementVec>::CountElements() const
|
|
||||||
{
|
|
||||||
return fElementCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
Element *
|
|
||||||
OpenHashTable<Element, ElementVec>::Add(uint32 hash)
|
|
||||||
{
|
|
||||||
_OPEN_HASH_TABLE_ASSERT(fElementVector);
|
|
||||||
_RehashIfNeeded();
|
|
||||||
hash %= fArraySize;
|
|
||||||
Element *result = fElementVector->Add();
|
|
||||||
if (result) {
|
|
||||||
result->fNext = fHashArray[hash];
|
|
||||||
fHashArray[hash] = fElementVector->IndexOf(*result);
|
|
||||||
fElementCount++;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
void
|
|
||||||
OpenHashTable<Element, ElementVec>::Remove(Element *element, bool dontRehash)
|
|
||||||
{
|
|
||||||
if (!dontRehash)
|
|
||||||
_RehashIfNeeded();
|
|
||||||
uint32 hash = element->Hash() % fArraySize;
|
|
||||||
int32 next = fHashArray[hash];
|
|
||||||
_OPEN_HASH_TABLE_ASSERT(next >= 0);
|
|
||||||
|
|
||||||
if (&fElementVector->At(next) == element) {
|
|
||||||
fHashArray[hash] = element->fNext;
|
|
||||||
fElementVector->Remove(next);
|
|
||||||
fElementCount--;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int32 index = next; index >= 0; ) {
|
|
||||||
// look for an existing match in table
|
|
||||||
next = fElementVector->At(index).fNext;
|
|
||||||
if (next < 0) {
|
|
||||||
_OPEN_HASH_TABLE_TRESPASS();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (&fElementVector->At(next) == element) {
|
|
||||||
fElementVector->At(index).fNext = element->fNext;
|
|
||||||
fElementVector->Remove(next);
|
|
||||||
fElementCount--;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
index = next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
void
|
|
||||||
OpenHashTable<Element, ElementVec>::RemoveAll()
|
|
||||||
{
|
|
||||||
for (int32 i = 0; fElementCount > 0 && i < fArraySize; i++) {
|
|
||||||
int32 index = fHashArray[i];
|
|
||||||
while (index >= 0) {
|
|
||||||
Element* element = &fElementVector->At(index);
|
|
||||||
int32 next = element->fNext;
|
|
||||||
fElementVector->Remove(index);
|
|
||||||
fElementCount--;
|
|
||||||
index = next;
|
|
||||||
}
|
|
||||||
fHashArray[i] = -1;
|
|
||||||
}
|
|
||||||
_RehashIfNeeded();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
void
|
|
||||||
OpenHashTable<Element, ElementVec>::SetElementVector(ElementVec *elementVector)
|
|
||||||
{
|
|
||||||
fElementVector = elementVector;
|
|
||||||
}
|
|
||||||
|
|
||||||
// _RehashIfNeeded
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
bool
|
|
||||||
OpenHashTable<Element, ElementVec>::_RehashIfNeeded()
|
|
||||||
{
|
|
||||||
// The load factor range [fMaxLoadFactor / 3, fMaxLoadFactor] is fine,
|
|
||||||
// I think. After rehashing the load factor will be about
|
|
||||||
// fMaxLoadFactor * 2 / 3, respectively fMaxLoadFactor / 2.
|
|
||||||
float loadFactor = (float)fElementCount / (float)fArraySize;
|
|
||||||
if (loadFactor > fMaxLoadFactor
|
|
||||||
|| (fArraySize > fInitialSize && loadFactor < fMaxLoadFactor / 3)) {
|
|
||||||
return _Rehash();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// _Rehash
|
|
||||||
template<class Element, class ElementVec>
|
|
||||||
bool
|
|
||||||
OpenHashTable<Element, ElementVec>::_Rehash()
|
|
||||||
{
|
|
||||||
bool result = true;
|
|
||||||
int32 newSize = int32(fElementCount * 1.73 * fMaxLoadFactor);
|
|
||||||
newSize = (fInitialSize > newSize ? fInitialSize : newSize);
|
|
||||||
if (newSize != fArraySize) {
|
|
||||||
// allocate a new array
|
|
||||||
int32 *newHashArray = (int32*)calloc(newSize, sizeof(int32));
|
|
||||||
if (newHashArray) {
|
|
||||||
// init the new hash array
|
|
||||||
for (int32 index = 0; index < newSize; index++)
|
|
||||||
newHashArray[index] = -1;
|
|
||||||
// iterate through all elements and put them into the new
|
|
||||||
// hash array
|
|
||||||
for (int i = 0; i < fArraySize; i++) {
|
|
||||||
int32 index = fHashArray[i];
|
|
||||||
while (index >= 0) {
|
|
||||||
// insert the element in the new array
|
|
||||||
Element &element = fElementVector->At(index);
|
|
||||||
int32 next = element.fNext;
|
|
||||||
uint32 hash = (element.Hash() % newSize);
|
|
||||||
element.fNext = newHashArray[hash];
|
|
||||||
newHashArray[hash] = index;
|
|
||||||
// next element in old list
|
|
||||||
index = next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// delete the old array and set the new one
|
|
||||||
free(fHashArray);
|
|
||||||
fHashArray = newHashArray;
|
|
||||||
fArraySize = newSize;
|
|
||||||
} else
|
|
||||||
result = false;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<class Element>
|
|
||||||
OpenHashElementArray<Element>::OpenHashElementArray(int32 initialSize)
|
|
||||||
: fSize(initialSize),
|
|
||||||
fNextFree(0),
|
|
||||||
fNextDeleted(-1)
|
|
||||||
{
|
|
||||||
fData = (Element*)calloc((size_t)initialSize, sizeof(Element));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element>
|
|
||||||
OpenHashElementArray<Element>::~OpenHashElementArray()
|
|
||||||
{
|
|
||||||
free(fData);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element>
|
|
||||||
bool
|
|
||||||
OpenHashElementArray<Element>::InitCheck() const
|
|
||||||
{
|
|
||||||
return fData;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element>
|
|
||||||
Element &
|
|
||||||
OpenHashElementArray<Element>::At(int32 index)
|
|
||||||
{
|
|
||||||
_OPEN_HASH_TABLE_ASSERT(index < fSize);
|
|
||||||
return fData[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element>
|
|
||||||
const Element &
|
|
||||||
OpenHashElementArray<Element>::At(int32 index) const
|
|
||||||
{
|
|
||||||
_OPEN_HASH_TABLE_ASSERT(index < fSize);
|
|
||||||
return fData[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element>
|
|
||||||
int32
|
|
||||||
OpenHashElementArray<Element>::IndexOf(const Element &element) const
|
|
||||||
{
|
|
||||||
int32 result = &element - fData;
|
|
||||||
if (result < 0 || result > fSize)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element>
|
|
||||||
int32
|
|
||||||
OpenHashElementArray<Element>::Size() const
|
|
||||||
{
|
|
||||||
return fSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<class Element>
|
|
||||||
Element *
|
|
||||||
OpenHashElementArray<Element>::Add(const Element &newElement)
|
|
||||||
{
|
|
||||||
Element *element = Add();
|
|
||||||
if (element)
|
|
||||||
element->Adopt(newElement);
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if DEBUG
|
|
||||||
const int32 kGrowChunk = 10;
|
|
||||||
#else
|
|
||||||
const int32 kGrowChunk = 1024;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
template<class Element>
|
|
||||||
Element *
|
|
||||||
OpenHashElementArray<Element>::Add()
|
|
||||||
{
|
|
||||||
int32 index = fNextFree;
|
|
||||||
if (fNextDeleted >= 0) {
|
|
||||||
index = fNextDeleted;
|
|
||||||
fNextDeleted = At(index).fNext;
|
|
||||||
} else if (fNextFree >= fSize - 1) {
|
|
||||||
int32 newSize = fSize + kGrowChunk;
|
|
||||||
/*
|
|
||||||
Element *newData = (Element *)calloc((size_t)newSize , sizeof(Element));
|
|
||||||
if (!newData)
|
|
||||||
return NULL;
|
|
||||||
memcpy(newData, fData, fSize * sizeof(Element));
|
|
||||||
free(fData);
|
|
||||||
*/
|
|
||||||
Element *newData = (Element*)realloc(fData,
|
|
||||||
(size_t)newSize * sizeof(Element));
|
|
||||||
if (!newData)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
fData = newData;
|
|
||||||
fSize = newSize;
|
|
||||||
index = fNextFree;
|
|
||||||
fNextFree++;
|
|
||||||
} else
|
|
||||||
fNextFree++;
|
|
||||||
|
|
||||||
new (&At(index)) Element;
|
|
||||||
// call placement new to initialize the element properly
|
|
||||||
_OPEN_HASH_TABLE_ASSERT(At(index).fNext == -1);
|
|
||||||
|
|
||||||
return &At(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Element>
|
|
||||||
void
|
|
||||||
OpenHashElementArray<Element>::Remove(int32 index)
|
|
||||||
{
|
|
||||||
// delete by chaining empty elements in a single linked
|
|
||||||
// list, reusing the next field
|
|
||||||
_OPEN_HASH_TABLE_ASSERT(index < fSize);
|
|
||||||
At(index).~Element();
|
|
||||||
// call the destructor explicitly to destroy the element
|
|
||||||
// properly
|
|
||||||
At(index).fNext = fNextDeleted;
|
|
||||||
fNextDeleted = index;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace BPrivate
|
|
||||||
|
|
||||||
using BPrivate::OpenHashTable;
|
|
||||||
|
|
||||||
#endif // __OPEN_HASH_TABLE__
|
|
||||||
|
|||||||
@@ -1,470 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2004-2009, Ingo Weinhold, [email protected].
|
|
||||||
* Distributed under the terms of the MIT License.
|
|
||||||
*/
|
|
||||||
#ifndef HASH_MAP_H
|
|
||||||
#define HASH_MAP_H
|
|
||||||
|
|
||||||
//#include <Debug.h>
|
|
||||||
|
|
||||||
#include <util/OpenHashTable.h>
|
|
||||||
|
|
||||||
#include "AutoLocker.h"
|
|
||||||
#include "Locker.h"
|
|
||||||
|
|
||||||
|
|
||||||
// HashMapElement
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
class HashMapElement {
|
|
||||||
private:
|
|
||||||
typedef HashMapElement<Key, Value> Element;
|
|
||||||
|
|
||||||
public:
|
|
||||||
HashMapElement()
|
|
||||||
:
|
|
||||||
fKey(),
|
|
||||||
fValue(),
|
|
||||||
fNext(NULL)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMapElement(const Key& key, const Value& value)
|
|
||||||
:
|
|
||||||
fKey(key),
|
|
||||||
fValue(value),
|
|
||||||
fNext(NULL)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Key fKey;
|
|
||||||
Value fValue;
|
|
||||||
HashMapElement* fNext;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// 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; }
|
|
||||||
ValueType*& GetLink(ValueType* value) const
|
|
||||||
{ return value->fNext; }
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
:
|
|
||||||
fMap(other.fMap),
|
|
||||||
fIterator(other.fIterator),
|
|
||||||
fElement(other.fElement)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HasNext() const
|
|
||||||
{
|
|
||||||
return fIterator.HasNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
Entry Next()
|
|
||||||
{
|
|
||||||
fElement = fIterator.Next();
|
|
||||||
if (fElement == NULL)
|
|
||||||
return Entry();
|
|
||||||
|
|
||||||
return Entry(fElement->fKey, fElement->fValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
Entry Remove()
|
|
||||||
{
|
|
||||||
if (fElement == NULL)
|
|
||||||
return Entry();
|
|
||||||
|
|
||||||
Entry result(fElement->fKey, fElement->fValue);
|
|
||||||
|
|
||||||
fMap->fTable.RemoveUnchecked(fElement);
|
|
||||||
delete fElement;
|
|
||||||
fElement = NULL;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator& operator=(const Iterator& other)
|
|
||||||
{
|
|
||||||
fMap = other.fMap;
|
|
||||||
fIterator = other.fIterator;
|
|
||||||
fElement = other.fElement;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
Iterator(HashMap<Key, Value>* map)
|
|
||||||
:
|
|
||||||
fMap(map),
|
|
||||||
fIterator(map->fTable.GetIterator()),
|
|
||||||
fElement(NULL)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class HashMap<Key, Value>;
|
|
||||||
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> >
|
|
||||||
ElementTable;
|
|
||||||
|
|
||||||
HashMap<Key, Value>* fMap;
|
|
||||||
typename ElementTable::Iterator fIterator;
|
|
||||||
Element* fElement;
|
|
||||||
};
|
|
||||||
|
|
||||||
HashMap();
|
|
||||||
~HashMap();
|
|
||||||
|
|
||||||
status_t InitCheck() const;
|
|
||||||
|
|
||||||
status_t Put(const Key& key, const Value& value);
|
|
||||||
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:
|
|
||||||
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> > ElementTable;
|
|
||||||
typedef HashMapElement<Key, Value> Element;
|
|
||||||
friend class Iterator;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
ElementTable fTable;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// SynchronizedHashMap
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
class SynchronizedHashMap : public Locker {
|
|
||||||
public:
|
|
||||||
typedef typename HashMap<Key, Value>::Entry Entry;
|
|
||||||
typedef typename HashMap<Key, Value>::Iterator Iterator;
|
|
||||||
|
|
||||||
SynchronizedHashMap() : Locker("synchronized hash map") {}
|
|
||||||
~SynchronizedHashMap() { Lock(); }
|
|
||||||
|
|
||||||
status_t InitCheck() const
|
|
||||||
{
|
|
||||||
return fMap.InitCheck();
|
|
||||||
}
|
|
||||||
|
|
||||||
status_t Put(const Key& key, const Value& value)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// HashKeyPointer
|
|
||||||
template<typename Value>
|
|
||||||
struct HashKeyPointer {
|
|
||||||
HashKeyPointer() {}
|
|
||||||
HashKeyPointer(const Value& value) : value(value) {}
|
|
||||||
|
|
||||||
uint32 GetHashCode() const
|
|
||||||
{
|
|
||||||
#if __HAIKU_ARCH_BITS == 32
|
|
||||||
return (uint32)(addr_t)value;
|
|
||||||
#elif __HAIKU_ARCH_BITS == 64
|
|
||||||
uint64 v = (uint64)(addr_t)value;
|
|
||||||
return (uint32)(v >> 32) ^ (uint32)v;
|
|
||||||
#else
|
|
||||||
#error unknown bitness
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
HashKeyPointer<Value> operator=(const HashKeyPointer<Value>& other)
|
|
||||||
{
|
|
||||||
value = other.value;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const HashKeyPointer<Value>& other) const
|
|
||||||
{
|
|
||||||
return (value == other.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator!=(const HashKeyPointer<Value>& other) const
|
|
||||||
{
|
|
||||||
return (value != other.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
Value value;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// HashMap
|
|
||||||
|
|
||||||
// constructor
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
HashMap<Key, Value>::HashMap()
|
|
||||||
:
|
|
||||||
fTable()
|
|
||||||
{
|
|
||||||
fTable.Init();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// destructor
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
HashMap<Key, Value>::~HashMap()
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// InitCheck
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
status_t
|
|
||||||
HashMap<Key, Value>::InitCheck() const
|
|
||||||
{
|
|
||||||
return (fTable.TableSize() > 0 ? B_OK : B_NO_MEMORY);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Put
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
status_t
|
|
||||||
HashMap<Key, Value>::Put(const Key& key, const Value& value)
|
|
||||||
{
|
|
||||||
Element* element = fTable.Lookup(key);
|
|
||||||
if (element) {
|
|
||||||
// already contains the key: just set the new value
|
|
||||||
element->fValue = value;
|
|
||||||
return B_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
// does not contain the key yet: create an element and add it
|
|
||||||
element = new(std::nothrow) Element(key, value);
|
|
||||||
if (!element)
|
|
||||||
return B_NO_MEMORY;
|
|
||||||
|
|
||||||
status_t error = fTable.Insert(element);
|
|
||||||
if (error != B_OK)
|
|
||||||
delete element;
|
|
||||||
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Remove
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
Value
|
|
||||||
HashMap<Key, Value>::Remove(const Key& key)
|
|
||||||
{
|
|
||||||
Element* element = fTable.Lookup(key);
|
|
||||||
if (element == NULL)
|
|
||||||
return Value();
|
|
||||||
|
|
||||||
fTable.Remove(element);
|
|
||||||
Value value = element->fValue;
|
|
||||||
delete element;
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Clear
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
void
|
|
||||||
HashMap<Key, Value>::Clear()
|
|
||||||
{
|
|
||||||
// clear the table and delete the elements
|
|
||||||
Element* element = fTable.Clear(true);
|
|
||||||
while (element != NULL) {
|
|
||||||
Element* next = element->fNext;
|
|
||||||
delete element;
|
|
||||||
element = next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Get
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
Value
|
|
||||||
HashMap<Key, Value>::Get(const Key& key) const
|
|
||||||
{
|
|
||||||
if (Element* element = fTable.Lookup(key))
|
|
||||||
return element->fValue;
|
|
||||||
return Value();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ContainsKey
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
bool
|
|
||||||
HashMap<Key, Value>::ContainsKey(const Key& key) const
|
|
||||||
{
|
|
||||||
return fTable.Lookup(key) != NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Size
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
int32
|
|
||||||
HashMap<Key, Value>::Size() const
|
|
||||||
{
|
|
||||||
return fTable.CountElements();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// GetIterator
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
typename HashMap<Key, Value>::Iterator
|
|
||||||
HashMap<Key, Value>::GetIterator()
|
|
||||||
{
|
|
||||||
return Iterator(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif // HASH_MAP_H
|
|
||||||
@@ -1,342 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2004-2009, Ingo Weinhold, [email protected].
|
|
||||||
* Distributed under the terms of the MIT License.
|
|
||||||
*/
|
|
||||||
#ifndef HASH_SET_H
|
|
||||||
#define HASH_SET_H
|
|
||||||
|
|
||||||
#include <util/OpenHashTable.h>
|
|
||||||
|
|
||||||
#include "AutoLocker.h"
|
|
||||||
#include "Locker.h"
|
|
||||||
|
|
||||||
|
|
||||||
// HashSetElement
|
|
||||||
template<typename Key>
|
|
||||||
class HashSetElement : public HashTableLink<HashSetElement<Key> > {
|
|
||||||
private:
|
|
||||||
typedef HashSetElement<Key> Element;
|
|
||||||
|
|
||||||
public:
|
|
||||||
HashSetElement()
|
|
||||||
:
|
|
||||||
fKey()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
HashSetElement(const Key& key)
|
|
||||||
:
|
|
||||||
fKey(key)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Key fKey;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// HashSetTableDefinition
|
|
||||||
template<typename Key>
|
|
||||||
struct HashSetTableDefinition {
|
|
||||||
typedef Key KeyType;
|
|
||||||
typedef HashSetElement<Key> 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; }
|
|
||||||
HashTableLink<ValueType>* GetLink(ValueType* value) const
|
|
||||||
{ return value; }
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// HashSet
|
|
||||||
template<typename Key>
|
|
||||||
class HashSet {
|
|
||||||
public:
|
|
||||||
class Iterator {
|
|
||||||
private:
|
|
||||||
typedef HashSetElement<Key> Element;
|
|
||||||
public:
|
|
||||||
Iterator(const Iterator& other)
|
|
||||||
:
|
|
||||||
fSet(other.fSet),
|
|
||||||
fIterator(other.fIterator),
|
|
||||||
fElement(other.fElement)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HasNext() const
|
|
||||||
{
|
|
||||||
return fIterator.HasNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
Key Next()
|
|
||||||
{
|
|
||||||
fElement = fIterator.Next();
|
|
||||||
if (fElement == NULL)
|
|
||||||
return Key();
|
|
||||||
|
|
||||||
return fElement->fKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Remove()
|
|
||||||
{
|
|
||||||
if (fElement == NULL)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
fSet->fTable.RemoveUnchecked(fElement);
|
|
||||||
delete fElement;
|
|
||||||
fElement = NULL;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator& operator=(const Iterator& other)
|
|
||||||
{
|
|
||||||
fSet = other.fSet;
|
|
||||||
fIterator = other.fIterator;
|
|
||||||
fElement = other.fElement;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
Iterator(HashSet<Key>* set)
|
|
||||||
:
|
|
||||||
fSet(set),
|
|
||||||
fIterator(set->fTable.GetIterator()),
|
|
||||||
fElement(NULL)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class HashMap<Key, Value>;
|
|
||||||
typedef OpenHashTable<HashSetTableDefinition<Key> > ElementTable;
|
|
||||||
|
|
||||||
HashSet<Key>* fSet;
|
|
||||||
ElementTable::Iterator fIterator;
|
|
||||||
Element* fElement;
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class HashSet<Key>;
|
|
||||||
};
|
|
||||||
|
|
||||||
HashSet();
|
|
||||||
~HashSet();
|
|
||||||
|
|
||||||
status_t InitCheck() const;
|
|
||||||
|
|
||||||
status_t Add(const Key& key);
|
|
||||||
bool Remove(const Key& key);
|
|
||||||
void Clear();
|
|
||||||
bool Contains(const Key& key) const;
|
|
||||||
|
|
||||||
int32 Size() const;
|
|
||||||
|
|
||||||
Iterator GetIterator();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
typedef OpenHashTable<HashSetTableDefinition<Key> > ElementTable;
|
|
||||||
typedef HashSetElement<Key> Element;
|
|
||||||
friend class Iterator;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
ElementTable fTable;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// SynchronizedHashSet
|
|
||||||
template<typename Key>
|
|
||||||
class SynchronizedHashSet : public Locker {
|
|
||||||
public:
|
|
||||||
typedef HashSet<Key>::Iterator Iterator;
|
|
||||||
|
|
||||||
SynchronizedHashSet() : Locker("synchronized hash set") {}
|
|
||||||
~SynchronizedHashSet() { Lock(); }
|
|
||||||
|
|
||||||
status_t InitCheck() const
|
|
||||||
{
|
|
||||||
return fSet.InitCheck();
|
|
||||||
}
|
|
||||||
|
|
||||||
status_t Add(const Key& key)
|
|
||||||
{
|
|
||||||
MapLocker locker(this);
|
|
||||||
if (!locker.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
return fSet.Add(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Remove(const Key& key)
|
|
||||||
{
|
|
||||||
MapLocker locker(this);
|
|
||||||
if (!locker.IsLocked())
|
|
||||||
return false;
|
|
||||||
return fSet.Remove(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Clear()
|
|
||||||
{
|
|
||||||
MapLocker locker(this);
|
|
||||||
fSet.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Contains(const Key& key) const
|
|
||||||
{
|
|
||||||
const Locker* lock = this;
|
|
||||||
MapLocker locker(const_cast<Locker*>(lock));
|
|
||||||
if (!locker.IsLocked())
|
|
||||||
return false;
|
|
||||||
return fSet.Contains(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
int32 Size() const
|
|
||||||
{
|
|
||||||
const Locker* lock = this;
|
|
||||||
MapLocker locker(const_cast<Locker*>(lock));
|
|
||||||
return fSet.Size();
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator GetIterator()
|
|
||||||
{
|
|
||||||
return fSet.GetIterator();
|
|
||||||
}
|
|
||||||
|
|
||||||
// for debugging only
|
|
||||||
const HashSet<Key>& GetUnsynchronizedSet() const { return fSet; }
|
|
||||||
HashSet<Key>& GetUnsynchronizedSet() { return fSet; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
typedef AutoLocker<Locker> MapLocker;
|
|
||||||
|
|
||||||
HashSet<Key> fSet;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// HashSet
|
|
||||||
|
|
||||||
// constructor
|
|
||||||
template<typename Key>
|
|
||||||
HashSet<Key>::HashSet()
|
|
||||||
:
|
|
||||||
fTable()
|
|
||||||
{
|
|
||||||
fTable.Init();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// destructor
|
|
||||||
template<typename Key>
|
|
||||||
HashSet<Key>::~HashSet()
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// InitCheck
|
|
||||||
template<typename Key>
|
|
||||||
status_t
|
|
||||||
HashSet<Key>::InitCheck() const
|
|
||||||
{
|
|
||||||
return (fTable.TableSize() > 0 ? B_OK : B_NO_MEMORY);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Add
|
|
||||||
template<typename Key>
|
|
||||||
status_t
|
|
||||||
HashSet<Key>::Add(const Key& key)
|
|
||||||
{
|
|
||||||
Element* element = fTable.Lookup(key);
|
|
||||||
if (element) {
|
|
||||||
// already contains the value
|
|
||||||
return B_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
// does not contain the key yet: create an element and add it
|
|
||||||
element = new(std::nothrow) Element(key);
|
|
||||||
if (!element)
|
|
||||||
return B_NO_MEMORY;
|
|
||||||
|
|
||||||
status_t error = fTable.Insert(element);
|
|
||||||
if (error != B_OK)
|
|
||||||
delete element;
|
|
||||||
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Remove
|
|
||||||
template<typename Key>
|
|
||||||
bool
|
|
||||||
HashSet<Key>::Remove(const Key& key)
|
|
||||||
{
|
|
||||||
Element* element = fTable.Lookup(key);
|
|
||||||
if (element == NULL)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
fTable.Remove(element);
|
|
||||||
delete element;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Clear
|
|
||||||
template<typename Key, typename Value>
|
|
||||||
void
|
|
||||||
HashSet<Key>::Clear()
|
|
||||||
{
|
|
||||||
// clear the table and delete the elements
|
|
||||||
Element* element = fTable.Clear(true);
|
|
||||||
while (element != NULL) {
|
|
||||||
Element* next = element->fNext;
|
|
||||||
delete element;
|
|
||||||
element = next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Contains
|
|
||||||
template<typename Key>
|
|
||||||
bool
|
|
||||||
HashSet<Key>::Contains(const Key& key) const
|
|
||||||
{
|
|
||||||
return fTable.Lookup(key) != NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Size
|
|
||||||
template<typename Key>
|
|
||||||
int32
|
|
||||||
HashSet<Key>::Size() const
|
|
||||||
{
|
|
||||||
return fTable.CountElements();
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetIterator
|
|
||||||
template<typename Key>
|
|
||||||
HashSet<Key>::Iterator
|
|
||||||
HashSet<Key>::GetIterator()
|
|
||||||
{
|
|
||||||
return Iterator(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
// _FindElement
|
|
||||||
template<typename Key>
|
|
||||||
HashSet<Key>::Element *
|
|
||||||
HashSet<Key>::_FindElement(const Key& key) const
|
|
||||||
{
|
|
||||||
Element* element = fTable.FindFirst(key.GetHashCode());
|
|
||||||
while (element && element->fKey != key) {
|
|
||||||
if (element->fNext >= 0)
|
|
||||||
element = fTable.ElementAt(element->fNext);
|
|
||||||
else
|
|
||||||
element = NULL;
|
|
||||||
}
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif // HASH_SET_H
|
|
||||||
@@ -0,0 +1,514 @@
|
|||||||
|
/*
|
||||||
|
Open Tracker License
|
||||||
|
|
||||||
|
Terms and Conditions
|
||||||
|
|
||||||
|
Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice applies to all licensees
|
||||||
|
and shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||||
|
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
Except as contained in this notice, the name of Be Incorporated shall not be
|
||||||
|
used in advertising or otherwise to promote the sale, use or other dealings in
|
||||||
|
this Software without prior written authorization from Be Incorporated.
|
||||||
|
|
||||||
|
Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
|
||||||
|
of Be Incorporated in the United States and other countries. Other brand product
|
||||||
|
names are registered trademarks or trademarks of their respective holders.
|
||||||
|
All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// bonefish:
|
||||||
|
// * removed need for exceptions
|
||||||
|
// * fixed warnings
|
||||||
|
// * implemented rehashing
|
||||||
|
// * added RemoveAll()
|
||||||
|
// TODO:
|
||||||
|
// * shrinking of element vectors
|
||||||
|
|
||||||
|
// Hash table with open addresssing
|
||||||
|
|
||||||
|
#ifndef __OPEN_HASH_TABLE__
|
||||||
|
#define __OPEN_HASH_TABLE__
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <new>
|
||||||
|
|
||||||
|
// don't include <Debug.h>
|
||||||
|
#ifndef _OPEN_HASH_TABLE_ASSERT
|
||||||
|
# define _OPEN_HASH_TABLE_ASSERT(E) (void)0
|
||||||
|
#endif
|
||||||
|
#ifndef _OPEN_HASH_TABLE_TRESPASS
|
||||||
|
# define _OPEN_HASH_TABLE_TRESPASS() (void)0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace BPrivate {
|
||||||
|
|
||||||
|
template <class Element>
|
||||||
|
class ElementVector {
|
||||||
|
// element vector for OpenHashTable needs to implement this
|
||||||
|
// interface
|
||||||
|
public:
|
||||||
|
Element &At(int32 index);
|
||||||
|
Element *Add();
|
||||||
|
int32 IndexOf(const Element &) const;
|
||||||
|
void Remove(int32 index);
|
||||||
|
};
|
||||||
|
|
||||||
|
class OpenHashElement {
|
||||||
|
public:
|
||||||
|
uint32 Hash() const;
|
||||||
|
bool operator==(const OpenHashElement &) const;
|
||||||
|
void Adopt(OpenHashElement &);
|
||||||
|
// low overhead copy, original element is in undefined state
|
||||||
|
// after call (calls Adopt on BString members, etc.)
|
||||||
|
int32 fNext;
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint32 kPrimes [] = {
|
||||||
|
509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139,
|
||||||
|
524287, 1048573, 2097143, 4194301, 8388593, 16777213, 33554393, 67108859,
|
||||||
|
134217689, 268435399, 536870909, 1073741789, 2147483647, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class Element, class ElementVec = ElementVector<Element> >
|
||||||
|
class OpenHashTable {
|
||||||
|
public:
|
||||||
|
OpenHashTable(int32 minSize, ElementVec *elementVector = 0,
|
||||||
|
float maxLoadFactor = 0.8);
|
||||||
|
// it is up to the subclass of OpenHashTable to supply
|
||||||
|
// elementVector
|
||||||
|
~OpenHashTable();
|
||||||
|
|
||||||
|
bool InitCheck() const;
|
||||||
|
|
||||||
|
void SetElementVector(ElementVec *elementVector);
|
||||||
|
|
||||||
|
Element *FindFirst(uint32 elementHash) const;
|
||||||
|
Element *Add(uint32 elementHash);
|
||||||
|
|
||||||
|
void Remove(Element *element, bool dontRehash = false);
|
||||||
|
void RemoveAll();
|
||||||
|
|
||||||
|
// when calling Add, any outstanding element pointer may become
|
||||||
|
// invalid; to deal with this, get the element index and restore
|
||||||
|
// it after the add
|
||||||
|
int32 ElementIndex(const Element *) const;
|
||||||
|
Element *ElementAt(int32 index) const;
|
||||||
|
|
||||||
|
int32 ArraySize() const;
|
||||||
|
int32 VectorSize() const;
|
||||||
|
int32 CountElements() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static int32 OptimalSize(int32 minSize);
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool _RehashIfNeeded();
|
||||||
|
bool _Rehash();
|
||||||
|
|
||||||
|
int32 fArraySize;
|
||||||
|
int32 fInitialSize;
|
||||||
|
int32 fElementCount;
|
||||||
|
int32 *fHashArray;
|
||||||
|
ElementVec *fElementVector;
|
||||||
|
float fMaxLoadFactor;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class Element>
|
||||||
|
class OpenHashElementArray : public ElementVector<Element> {
|
||||||
|
// this is a straightforward implementation of an element vector
|
||||||
|
// deleting is handled by linking deleted elements into a free list
|
||||||
|
// the vector never shrinks
|
||||||
|
public:
|
||||||
|
OpenHashElementArray(int32 initialSize);
|
||||||
|
~OpenHashElementArray();
|
||||||
|
|
||||||
|
bool InitCheck() const;
|
||||||
|
|
||||||
|
Element &At(int32 index);
|
||||||
|
const Element &At(int32 index) const;
|
||||||
|
Element *Add(const Element &);
|
||||||
|
Element *Add();
|
||||||
|
void Remove(int32 index);
|
||||||
|
int32 IndexOf(const Element &) const;
|
||||||
|
int32 Size() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Element *fData;
|
||||||
|
int32 fSize;
|
||||||
|
int32 fNextFree;
|
||||||
|
int32 fNextDeleted;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
OpenHashTable<Element, ElementVec>::OpenHashTable(int32 minSize,
|
||||||
|
ElementVec *elementVector, float maxLoadFactor)
|
||||||
|
: fArraySize(OptimalSize(minSize)),
|
||||||
|
fInitialSize(fArraySize),
|
||||||
|
fElementCount(0),
|
||||||
|
fElementVector(elementVector),
|
||||||
|
fMaxLoadFactor(maxLoadFactor)
|
||||||
|
{
|
||||||
|
// sanity check the maximal load factor
|
||||||
|
if (fMaxLoadFactor < 0.5)
|
||||||
|
fMaxLoadFactor = 0.5;
|
||||||
|
// allocate and init the array
|
||||||
|
fHashArray = (int32*)calloc(fArraySize, sizeof(int32));
|
||||||
|
if (fHashArray) {
|
||||||
|
for (int32 index = 0; index < fArraySize; index++)
|
||||||
|
fHashArray[index] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
OpenHashTable<Element, ElementVec>::~OpenHashTable()
|
||||||
|
{
|
||||||
|
RemoveAll();
|
||||||
|
free(fHashArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
bool
|
||||||
|
OpenHashTable<Element, ElementVec>::InitCheck() const
|
||||||
|
{
|
||||||
|
return (fHashArray && fElementVector);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
int32
|
||||||
|
OpenHashTable<Element, ElementVec>::OptimalSize(int32 minSize)
|
||||||
|
{
|
||||||
|
for (int32 index = 0; ; index++)
|
||||||
|
if (!kPrimes[index] || kPrimes[index] >= (uint32)minSize)
|
||||||
|
return (int32)kPrimes[index];
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
Element *
|
||||||
|
OpenHashTable<Element, ElementVec>::FindFirst(uint32 hash) const
|
||||||
|
{
|
||||||
|
_OPEN_HASH_TABLE_ASSERT(fElementVector);
|
||||||
|
hash %= fArraySize;
|
||||||
|
if (fHashArray[hash] < 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return &fElementVector->At(fHashArray[hash]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
int32
|
||||||
|
OpenHashTable<Element, ElementVec>::ElementIndex(const Element *element) const
|
||||||
|
{
|
||||||
|
return fElementVector->IndexOf(*element);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
Element *
|
||||||
|
OpenHashTable<Element, ElementVec>::ElementAt(int32 index) const
|
||||||
|
{
|
||||||
|
return &fElementVector->At(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
int32
|
||||||
|
OpenHashTable<Element, ElementVec>::ArraySize() const
|
||||||
|
{
|
||||||
|
return fArraySize;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
int32
|
||||||
|
OpenHashTable<Element, ElementVec>::VectorSize() const
|
||||||
|
{
|
||||||
|
return fElementVector->Size();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
int32
|
||||||
|
OpenHashTable<Element, ElementVec>::CountElements() const
|
||||||
|
{
|
||||||
|
return fElementCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
Element *
|
||||||
|
OpenHashTable<Element, ElementVec>::Add(uint32 hash)
|
||||||
|
{
|
||||||
|
_OPEN_HASH_TABLE_ASSERT(fElementVector);
|
||||||
|
_RehashIfNeeded();
|
||||||
|
hash %= fArraySize;
|
||||||
|
Element *result = fElementVector->Add();
|
||||||
|
if (result) {
|
||||||
|
result->fNext = fHashArray[hash];
|
||||||
|
fHashArray[hash] = fElementVector->IndexOf(*result);
|
||||||
|
fElementCount++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
void
|
||||||
|
OpenHashTable<Element, ElementVec>::Remove(Element *element, bool dontRehash)
|
||||||
|
{
|
||||||
|
if (!dontRehash)
|
||||||
|
_RehashIfNeeded();
|
||||||
|
uint32 hash = element->Hash() % fArraySize;
|
||||||
|
int32 next = fHashArray[hash];
|
||||||
|
_OPEN_HASH_TABLE_ASSERT(next >= 0);
|
||||||
|
|
||||||
|
if (&fElementVector->At(next) == element) {
|
||||||
|
fHashArray[hash] = element->fNext;
|
||||||
|
fElementVector->Remove(next);
|
||||||
|
fElementCount--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int32 index = next; index >= 0; ) {
|
||||||
|
// look for an existing match in table
|
||||||
|
next = fElementVector->At(index).fNext;
|
||||||
|
if (next < 0) {
|
||||||
|
_OPEN_HASH_TABLE_TRESPASS();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (&fElementVector->At(next) == element) {
|
||||||
|
fElementVector->At(index).fNext = element->fNext;
|
||||||
|
fElementVector->Remove(next);
|
||||||
|
fElementCount--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
index = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
void
|
||||||
|
OpenHashTable<Element, ElementVec>::RemoveAll()
|
||||||
|
{
|
||||||
|
for (int32 i = 0; fElementCount > 0 && i < fArraySize; i++) {
|
||||||
|
int32 index = fHashArray[i];
|
||||||
|
while (index >= 0) {
|
||||||
|
Element* element = &fElementVector->At(index);
|
||||||
|
int32 next = element->fNext;
|
||||||
|
fElementVector->Remove(index);
|
||||||
|
fElementCount--;
|
||||||
|
index = next;
|
||||||
|
}
|
||||||
|
fHashArray[i] = -1;
|
||||||
|
}
|
||||||
|
_RehashIfNeeded();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
void
|
||||||
|
OpenHashTable<Element, ElementVec>::SetElementVector(ElementVec *elementVector)
|
||||||
|
{
|
||||||
|
fElementVector = elementVector;
|
||||||
|
}
|
||||||
|
|
||||||
|
// _RehashIfNeeded
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
bool
|
||||||
|
OpenHashTable<Element, ElementVec>::_RehashIfNeeded()
|
||||||
|
{
|
||||||
|
// The load factor range [fMaxLoadFactor / 3, fMaxLoadFactor] is fine,
|
||||||
|
// I think. After rehashing the load factor will be about
|
||||||
|
// fMaxLoadFactor * 2 / 3, respectively fMaxLoadFactor / 2.
|
||||||
|
float loadFactor = (float)fElementCount / (float)fArraySize;
|
||||||
|
if (loadFactor > fMaxLoadFactor
|
||||||
|
|| (fArraySize > fInitialSize && loadFactor < fMaxLoadFactor / 3)) {
|
||||||
|
return _Rehash();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// _Rehash
|
||||||
|
template<class Element, class ElementVec>
|
||||||
|
bool
|
||||||
|
OpenHashTable<Element, ElementVec>::_Rehash()
|
||||||
|
{
|
||||||
|
bool result = true;
|
||||||
|
int32 newSize = int32(fElementCount * 1.73 * fMaxLoadFactor);
|
||||||
|
newSize = (fInitialSize > newSize ? fInitialSize : newSize);
|
||||||
|
if (newSize != fArraySize) {
|
||||||
|
// allocate a new array
|
||||||
|
int32 *newHashArray = (int32*)calloc(newSize, sizeof(int32));
|
||||||
|
if (newHashArray) {
|
||||||
|
// init the new hash array
|
||||||
|
for (int32 index = 0; index < newSize; index++)
|
||||||
|
newHashArray[index] = -1;
|
||||||
|
// iterate through all elements and put them into the new
|
||||||
|
// hash array
|
||||||
|
for (int i = 0; i < fArraySize; i++) {
|
||||||
|
int32 index = fHashArray[i];
|
||||||
|
while (index >= 0) {
|
||||||
|
// insert the element in the new array
|
||||||
|
Element &element = fElementVector->At(index);
|
||||||
|
int32 next = element.fNext;
|
||||||
|
uint32 hash = (element.Hash() % newSize);
|
||||||
|
element.fNext = newHashArray[hash];
|
||||||
|
newHashArray[hash] = index;
|
||||||
|
// next element in old list
|
||||||
|
index = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// delete the old array and set the new one
|
||||||
|
free(fHashArray);
|
||||||
|
fHashArray = newHashArray;
|
||||||
|
fArraySize = newSize;
|
||||||
|
} else
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class Element>
|
||||||
|
OpenHashElementArray<Element>::OpenHashElementArray(int32 initialSize)
|
||||||
|
: fSize(initialSize),
|
||||||
|
fNextFree(0),
|
||||||
|
fNextDeleted(-1)
|
||||||
|
{
|
||||||
|
fData = (Element*)calloc((size_t)initialSize, sizeof(Element));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element>
|
||||||
|
OpenHashElementArray<Element>::~OpenHashElementArray()
|
||||||
|
{
|
||||||
|
free(fData);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element>
|
||||||
|
bool
|
||||||
|
OpenHashElementArray<Element>::InitCheck() const
|
||||||
|
{
|
||||||
|
return fData;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element>
|
||||||
|
Element &
|
||||||
|
OpenHashElementArray<Element>::At(int32 index)
|
||||||
|
{
|
||||||
|
_OPEN_HASH_TABLE_ASSERT(index < fSize);
|
||||||
|
return fData[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element>
|
||||||
|
const Element &
|
||||||
|
OpenHashElementArray<Element>::At(int32 index) const
|
||||||
|
{
|
||||||
|
_OPEN_HASH_TABLE_ASSERT(index < fSize);
|
||||||
|
return fData[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element>
|
||||||
|
int32
|
||||||
|
OpenHashElementArray<Element>::IndexOf(const Element &element) const
|
||||||
|
{
|
||||||
|
int32 result = &element - fData;
|
||||||
|
if (result < 0 || result > fSize)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element>
|
||||||
|
int32
|
||||||
|
OpenHashElementArray<Element>::Size() const
|
||||||
|
{
|
||||||
|
return fSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class Element>
|
||||||
|
Element *
|
||||||
|
OpenHashElementArray<Element>::Add(const Element &newElement)
|
||||||
|
{
|
||||||
|
Element *element = Add();
|
||||||
|
if (element)
|
||||||
|
element->Adopt(newElement);
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
const int32 kGrowChunk = 10;
|
||||||
|
#else
|
||||||
|
const int32 kGrowChunk = 1024;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template<class Element>
|
||||||
|
Element *
|
||||||
|
OpenHashElementArray<Element>::Add()
|
||||||
|
{
|
||||||
|
int32 index = fNextFree;
|
||||||
|
if (fNextDeleted >= 0) {
|
||||||
|
index = fNextDeleted;
|
||||||
|
fNextDeleted = At(index).fNext;
|
||||||
|
} else if (fNextFree >= fSize - 1) {
|
||||||
|
int32 newSize = fSize + kGrowChunk;
|
||||||
|
/*
|
||||||
|
Element *newData = (Element *)calloc((size_t)newSize , sizeof(Element));
|
||||||
|
if (!newData)
|
||||||
|
return NULL;
|
||||||
|
memcpy(newData, fData, fSize * sizeof(Element));
|
||||||
|
free(fData);
|
||||||
|
*/
|
||||||
|
Element *newData = (Element*)realloc(fData,
|
||||||
|
(size_t)newSize * sizeof(Element));
|
||||||
|
if (!newData)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
fData = newData;
|
||||||
|
fSize = newSize;
|
||||||
|
index = fNextFree;
|
||||||
|
fNextFree++;
|
||||||
|
} else
|
||||||
|
fNextFree++;
|
||||||
|
|
||||||
|
new (&At(index)) Element;
|
||||||
|
// call placement new to initialize the element properly
|
||||||
|
_OPEN_HASH_TABLE_ASSERT(At(index).fNext == -1);
|
||||||
|
|
||||||
|
return &At(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Element>
|
||||||
|
void
|
||||||
|
OpenHashElementArray<Element>::Remove(int32 index)
|
||||||
|
{
|
||||||
|
// delete by chaining empty elements in a single linked
|
||||||
|
// list, reusing the next field
|
||||||
|
_OPEN_HASH_TABLE_ASSERT(index < fSize);
|
||||||
|
At(index).~Element();
|
||||||
|
// call the destructor explicitly to destroy the element
|
||||||
|
// properly
|
||||||
|
At(index).fNext = fNextDeleted;
|
||||||
|
fNextDeleted = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace BPrivate
|
||||||
|
|
||||||
|
using BPrivate::OpenHashTable;
|
||||||
|
|
||||||
|
#endif // __OPEN_HASH_TABLE__
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
#if FS_SHELL
|
#if FS_SHELL
|
||||||
# include "fssh_api_wrapper.h"
|
# include "fssh_api_wrapper.h"
|
||||||
|
|
||||||
# include "KOpenHashTable.h"
|
# include "OpenHashTable.h"
|
||||||
# include "list.h"
|
# include "list.h"
|
||||||
#else
|
#else
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
|
|||||||
Reference in New Issue
Block a user