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:
Augustin Cavalier
2019-02-15 00:34:36 +00:00
committed by waddlesplash
parent eff1e73cef
commit cc54b43e68
2 changed files with 74 additions and 50 deletions
+52 -23
View File
@@ -1,16 +1,18 @@
/*
* Copyright 2004-2009, Ingo Weinhold, [email protected].
* Copyright 2019, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef HASH_MAP_H
#define HASH_MAP_H
//#include <Debug.h>
#include <util/OpenHashTable.h>
#include <OpenHashTable.h>
#include <Locker.h>
#include "AutoLocker.h"
#include "Locker.h"
namespace BPrivate {
// HashMapElement
@@ -98,20 +100,6 @@ public:
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;
@@ -121,7 +109,7 @@ public:
}
private:
Iterator(HashMap<Key, Value>* map)
Iterator(const HashMap<Key, Value>* map)
:
fMap(map),
fIterator(map->fTable.GetIterator()),
@@ -134,7 +122,7 @@ public:
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> >
ElementTable;
HashMap<Key, Value>* fMap;
const HashMap<Key, Value>* fMap;
typename ElementTable::Iterator fIterator;
Element* fElement;
};
@@ -146,14 +134,16 @@ public:
status_t Put(const Key& key, const Value& value);
Value Remove(const Key& key);
Value Remove(Iterator& it);
void Clear();
Value Get(const Key& key) const;
bool Get(const Key& key, Value*& _value) const;
bool ContainsKey(const Key& key) const;
int32 Size() const;
Iterator GetIterator();
Iterator GetIterator() const;
protected:
typedef BOpenHashTable<HashMapTableDefinition<Key, Value> > ElementTable;
@@ -166,7 +156,7 @@ protected:
// SynchronizedHashMap
template<typename Key, typename Value>
template<typename Key, typename Value, typename Locker = BLocker>
class SynchronizedHashMap : public Locker {
public:
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
template<typename Key, typename Value>
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
template<typename Key, typename Value>
bool
@@ -461,10 +483,17 @@ HashMap<Key, Value>::Size() const
// GetIterator
template<typename Key, typename Value>
typename HashMap<Key, Value>::Iterator
HashMap<Key, Value>::GetIterator()
HashMap<Key, Value>::GetIterator() const
{
return Iterator(this);
}
} // namespace BPrivate
using BPrivate::HashMap;
using BPrivate::HashKey32;
using BPrivate::HashKey64;
using BPrivate::HashKeyPointer;
using BPrivate::SynchronizedHashMap;
#endif // HASH_MAP_H
+22 -27
View File
@@ -1,36 +1,43 @@
/*
* Copyright 2004-2009, Ingo Weinhold, [email protected].
* Copyright 2019, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef HASH_SET_H
#define HASH_SET_H
#include <util/OpenHashTable.h>
#include <OpenHashTable.h>
#include <Locker.h>
#include "AutoLocker.h"
#include "Locker.h"
namespace BPrivate {
// HashSetElement
template<typename Key>
class HashSetElement : public HashTableLink<HashSetElement<Key> > {
class HashSetElement {
private:
typedef HashSetElement<Key> Element;
public:
HashSetElement()
:
fKey()
fKey(),
fNext(NULL)
{
}
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); }
bool Compare(const KeyType& key, const ValueType* value) const
{ return value->fKey == key; }
HashTableLink<ValueType>* GetLink(ValueType* value) const
{ return value; }
ValueType*& GetLink(ValueType* value) const
{ return value->fNext; }
};
@@ -111,8 +118,7 @@ public:
}
private:
friend class HashMap<Key, Value>;
typedef OpenHashTable<HashSetTableDefinition<Key> > ElementTable;
typedef BOpenHashTable<HashSetTableDefinition<Key> > ElementTable;
HashSet<Key>* fSet;
ElementTable::Iterator fIterator;
@@ -137,7 +143,7 @@ public:
Iterator GetIterator();
protected:
typedef OpenHashTable<HashSetTableDefinition<Key> > ElementTable;
typedef BOpenHashTable<HashSetTableDefinition<Key> > ElementTable;
typedef HashSetElement<Key> Element;
friend class Iterator;
@@ -147,7 +153,7 @@ protected:
// SynchronizedHashSet
template<typename Key>
template<typename Key, typename Locker = BLocker>
class SynchronizedHashSet : public Locker {
public:
typedef HashSet<Key>::Iterator Iterator;
@@ -284,7 +290,7 @@ HashSet<Key>::Remove(const Key& key)
// Clear
template<typename Key, typename Value>
template<typename Key>
void
HashSet<Key>::Clear()
{
@@ -315,6 +321,7 @@ HashSet<Key>::Size() const
return fTable.CountElements();
}
// GetIterator
template<typename Key>
HashSet<Key>::Iterator
@@ -323,20 +330,8 @@ 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;
}
} // namespace BPrivate
using BPrivate::HashSet;
#endif // HASH_SET_H