shared: Finish HashSet and fixup HashMap.
Changes are pretty straightforward. The iterator is now const again, but can be passed to the hash table itself for removal of the current item. Change-Id: Ifd3c8096ffb187a183ca5963ed69a256562a524f Reviewed-on: https://review.haiku-os.org/c/1042 Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
committed by
waddlesplash
parent
eff1e73cef
commit
cc54b43e68
@@ -1,16 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2004-2009, Ingo Weinhold, [email protected].
|
* Copyright 2004-2009, Ingo Weinhold, [email protected].
|
||||||
|
* Copyright 2019, Haiku, Inc. All rights reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
#ifndef HASH_MAP_H
|
#ifndef HASH_MAP_H
|
||||||
#define HASH_MAP_H
|
#define HASH_MAP_H
|
||||||
|
|
||||||
//#include <Debug.h>
|
#include <OpenHashTable.h>
|
||||||
|
#include <Locker.h>
|
||||||
#include <util/OpenHashTable.h>
|
|
||||||
|
|
||||||
#include "AutoLocker.h"
|
#include "AutoLocker.h"
|
||||||
#include "Locker.h"
|
|
||||||
|
|
||||||
|
namespace BPrivate {
|
||||||
|
|
||||||
|
|
||||||
// HashMapElement
|
// HashMapElement
|
||||||
@@ -98,20 +100,6 @@ public:
|
|||||||
return Entry(fElement->fKey, fElement->fValue);
|
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)
|
Iterator& operator=(const Iterator& other)
|
||||||
{
|
{
|
||||||
fMap = other.fMap;
|
fMap = other.fMap;
|
||||||
@@ -121,7 +109,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Iterator(HashMap<Key, Value>* map)
|
Iterator(const HashMap<Key, Value>* map)
|
||||||
:
|
:
|
||||||
fMap(map),
|
fMap(map),
|
||||||
fIterator(map->fTable.GetIterator()),
|
fIterator(map->fTable.GetIterator()),
|
||||||
@@ -134,7 +122,7 @@ public:
|
|||||||
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> >
|
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> >
|
||||||
ElementTable;
|
ElementTable;
|
||||||
|
|
||||||
HashMap<Key, Value>* fMap;
|
const HashMap<Key, Value>* fMap;
|
||||||
typename ElementTable::Iterator fIterator;
|
typename ElementTable::Iterator fIterator;
|
||||||
Element* fElement;
|
Element* fElement;
|
||||||
};
|
};
|
||||||
@@ -146,14 +134,16 @@ public:
|
|||||||
|
|
||||||
status_t Put(const Key& key, const Value& value);
|
status_t Put(const Key& key, const Value& value);
|
||||||
Value Remove(const Key& key);
|
Value Remove(const Key& key);
|
||||||
|
Value Remove(Iterator& it);
|
||||||
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();
|
Iterator GetIterator() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> > ElementTable;
|
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> > ElementTable;
|
||||||
@@ -166,7 +156,7 @@ protected:
|
|||||||
|
|
||||||
|
|
||||||
// SynchronizedHashMap
|
// SynchronizedHashMap
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value, typename Locker = BLocker>
|
||||||
class SynchronizedHashMap : public Locker {
|
class SynchronizedHashMap : public Locker {
|
||||||
public:
|
public:
|
||||||
typedef typename HashMap<Key, Value>::Entry Entry;
|
typedef typename HashMap<Key, Value>::Entry Entry;
|
||||||
@@ -414,6 +404,25 @@ HashMap<Key, Value>::Remove(const Key& key)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Remove
|
||||||
|
template<typename Key, typename Value>
|
||||||
|
Value
|
||||||
|
HashMap<Key, Value>::Remove(Iterator& it)
|
||||||
|
{
|
||||||
|
Element* element = it.fElement;
|
||||||
|
if (element == NULL)
|
||||||
|
return Value();
|
||||||
|
|
||||||
|
Value value = element->fValue;
|
||||||
|
|
||||||
|
fTable.RemoveUnchecked(element);
|
||||||
|
delete element;
|
||||||
|
it.fElement = NULL;
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Clear
|
// Clear
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
void
|
void
|
||||||
@@ -440,6 +449,19 @@ HashMap<Key, Value>::Get(const Key& key) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Get
|
||||||
|
template<typename Key, typename Value>
|
||||||
|
bool
|
||||||
|
HashMap<Key, Value>::Get(const Key& key, Value*& _value) const
|
||||||
|
{
|
||||||
|
if (Element* element = fTable.Lookup(key)) {
|
||||||
|
_value = &element->fValue;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ContainsKey
|
// ContainsKey
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
bool
|
bool
|
||||||
@@ -461,10 +483,17 @@ HashMap<Key, Value>::Size() const
|
|||||||
// GetIterator
|
// GetIterator
|
||||||
template<typename Key, typename Value>
|
template<typename Key, typename Value>
|
||||||
typename HashMap<Key, Value>::Iterator
|
typename HashMap<Key, Value>::Iterator
|
||||||
HashMap<Key, Value>::GetIterator()
|
HashMap<Key, Value>::GetIterator() const
|
||||||
{
|
{
|
||||||
return Iterator(this);
|
return Iterator(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace BPrivate
|
||||||
|
|
||||||
|
using BPrivate::HashMap;
|
||||||
|
using BPrivate::HashKey32;
|
||||||
|
using BPrivate::HashKey64;
|
||||||
|
using BPrivate::HashKeyPointer;
|
||||||
|
using BPrivate::SynchronizedHashMap;
|
||||||
|
|
||||||
#endif // HASH_MAP_H
|
#endif // HASH_MAP_H
|
||||||
|
|||||||
@@ -1,36 +1,43 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2004-2009, Ingo Weinhold, [email protected].
|
* Copyright 2004-2009, Ingo Weinhold, [email protected].
|
||||||
|
* Copyright 2019, Haiku, Inc. All rights reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
#ifndef HASH_SET_H
|
#ifndef HASH_SET_H
|
||||||
#define HASH_SET_H
|
#define HASH_SET_H
|
||||||
|
|
||||||
#include <util/OpenHashTable.h>
|
#include <OpenHashTable.h>
|
||||||
|
#include <Locker.h>
|
||||||
|
|
||||||
#include "AutoLocker.h"
|
#include "AutoLocker.h"
|
||||||
#include "Locker.h"
|
|
||||||
|
|
||||||
|
namespace BPrivate {
|
||||||
|
|
||||||
|
|
||||||
// HashSetElement
|
// HashSetElement
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
class HashSetElement : public HashTableLink<HashSetElement<Key> > {
|
class HashSetElement {
|
||||||
private:
|
private:
|
||||||
typedef HashSetElement<Key> Element;
|
typedef HashSetElement<Key> Element;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HashSetElement()
|
HashSetElement()
|
||||||
:
|
:
|
||||||
fKey()
|
fKey(),
|
||||||
|
fNext(NULL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
HashSetElement(const Key& key)
|
HashSetElement(const Key& key)
|
||||||
:
|
:
|
||||||
fKey(key)
|
fKey(key),
|
||||||
|
fNext(NULL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Key fKey;
|
Key fKey;
|
||||||
|
HashSetElement* fNext;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -46,8 +53,8 @@ struct HashSetTableDefinition {
|
|||||||
{ return HashKey(value->fKey); }
|
{ return HashKey(value->fKey); }
|
||||||
bool Compare(const KeyType& key, const ValueType* value) const
|
bool Compare(const KeyType& key, const ValueType* value) const
|
||||||
{ return value->fKey == key; }
|
{ return value->fKey == key; }
|
||||||
HashTableLink<ValueType>* GetLink(ValueType* value) const
|
ValueType*& GetLink(ValueType* value) const
|
||||||
{ return value; }
|
{ return value->fNext; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -111,8 +118,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class HashMap<Key, Value>;
|
typedef BOpenHashTable<HashSetTableDefinition<Key> > ElementTable;
|
||||||
typedef OpenHashTable<HashSetTableDefinition<Key> > ElementTable;
|
|
||||||
|
|
||||||
HashSet<Key>* fSet;
|
HashSet<Key>* fSet;
|
||||||
ElementTable::Iterator fIterator;
|
ElementTable::Iterator fIterator;
|
||||||
@@ -137,7 +143,7 @@ public:
|
|||||||
Iterator GetIterator();
|
Iterator GetIterator();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
typedef OpenHashTable<HashSetTableDefinition<Key> > ElementTable;
|
typedef BOpenHashTable<HashSetTableDefinition<Key> > ElementTable;
|
||||||
typedef HashSetElement<Key> Element;
|
typedef HashSetElement<Key> Element;
|
||||||
friend class Iterator;
|
friend class Iterator;
|
||||||
|
|
||||||
@@ -147,7 +153,7 @@ protected:
|
|||||||
|
|
||||||
|
|
||||||
// SynchronizedHashSet
|
// SynchronizedHashSet
|
||||||
template<typename Key>
|
template<typename Key, typename Locker = BLocker>
|
||||||
class SynchronizedHashSet : public Locker {
|
class SynchronizedHashSet : public Locker {
|
||||||
public:
|
public:
|
||||||
typedef HashSet<Key>::Iterator Iterator;
|
typedef HashSet<Key>::Iterator Iterator;
|
||||||
@@ -284,7 +290,7 @@ HashSet<Key>::Remove(const Key& key)
|
|||||||
|
|
||||||
|
|
||||||
// Clear
|
// Clear
|
||||||
template<typename Key, typename Value>
|
template<typename Key>
|
||||||
void
|
void
|
||||||
HashSet<Key>::Clear()
|
HashSet<Key>::Clear()
|
||||||
{
|
{
|
||||||
@@ -315,6 +321,7 @@ HashSet<Key>::Size() const
|
|||||||
return fTable.CountElements();
|
return fTable.CountElements();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// GetIterator
|
// GetIterator
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
HashSet<Key>::Iterator
|
HashSet<Key>::Iterator
|
||||||
@@ -323,20 +330,8 @@ HashSet<Key>::GetIterator()
|
|||||||
return Iterator(this);
|
return Iterator(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// _FindElement
|
} // namespace BPrivate
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
using BPrivate::HashSet;
|
||||||
|
|
||||||
#endif // HASH_SET_H
|
#endif // HASH_SET_H
|
||||||
|
|||||||
Reference in New Issue
Block a user