2019-02-13 06:44:14 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2004-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
|
2019-02-13 06:46:28 +03:00
|
|
|
* Copyright 2019, Haiku, Inc. All rights reserved.
|
2019-02-13 06:44:14 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2010-01-15 00:49:16 +03:00
|
|
|
#ifndef HASH_SET_H
|
|
|
|
#define HASH_SET_H
|
|
|
|
|
2019-02-13 06:46:28 +03:00
|
|
|
#include <OpenHashTable.h>
|
|
|
|
#include <Locker.h>
|
2010-01-15 00:49:16 +03:00
|
|
|
|
|
|
|
#include "AutoLocker.h"
|
2019-02-13 06:46:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
namespace BPrivate {
|
2010-01-15 00:49:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
// HashSetElement
|
|
|
|
template<typename Key>
|
2019-02-13 06:46:28 +03:00
|
|
|
class HashSetElement {
|
2010-01-15 00:49:16 +03:00
|
|
|
private:
|
|
|
|
typedef HashSetElement<Key> Element;
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
public:
|
|
|
|
HashSetElement()
|
|
|
|
:
|
2019-02-13 06:46:28 +03:00
|
|
|
fKey(),
|
|
|
|
fNext(NULL)
|
2010-01-15 00:49:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
HashSetElement(const Key& key)
|
|
|
|
:
|
2019-02-13 06:46:28 +03:00
|
|
|
fKey(key),
|
|
|
|
fNext(NULL)
|
2010-01-15 00:49:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-13 06:46:28 +03:00
|
|
|
Key fKey;
|
|
|
|
HashSetElement* fNext;
|
2019-02-13 06:44:14 +03:00
|
|
|
};
|
2010-01-15 00:49:16 +03:00
|
|
|
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
// 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; }
|
2019-02-13 06:46:28 +03:00
|
|
|
ValueType*& GetLink(ValueType* value) const
|
|
|
|
{ return value->fNext; }
|
2010-01-15 00:49:16 +03:00
|
|
|
};
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// HashSet
|
|
|
|
template<typename Key>
|
|
|
|
class HashSet {
|
|
|
|
public:
|
|
|
|
class Iterator {
|
|
|
|
private:
|
|
|
|
typedef HashSetElement<Key> Element;
|
|
|
|
public:
|
|
|
|
Iterator(const Iterator& other)
|
2019-02-13 06:44:14 +03:00
|
|
|
:
|
|
|
|
fSet(other.fSet),
|
|
|
|
fIterator(other.fIterator),
|
|
|
|
fElement(other.fElement)
|
2010-01-15 00:49:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasNext() const
|
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
return fIterator.HasNext();
|
2010-01-15 00:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Key Next()
|
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
fElement = fIterator.Next();
|
|
|
|
if (fElement == NULL)
|
2010-01-15 00:49:16 +03:00
|
|
|
return Key();
|
2019-02-13 06:44:14 +03:00
|
|
|
|
|
|
|
return fElement->fKey;
|
2010-01-15 00:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator& operator=(const Iterator& other)
|
|
|
|
{
|
|
|
|
fSet = other.fSet;
|
2019-02-13 06:44:14 +03:00
|
|
|
fIterator = other.fIterator;
|
2010-01-15 00:49:16 +03:00
|
|
|
fElement = other.fElement;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-02-15 04:43:49 +03:00
|
|
|
Iterator(const HashSet<Key>* set)
|
2019-02-13 06:44:14 +03:00
|
|
|
:
|
|
|
|
fSet(set),
|
|
|
|
fIterator(set->fTable.GetIterator()),
|
|
|
|
fElement(NULL)
|
2010-01-15 00:49:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
private:
|
2019-02-13 06:46:28 +03:00
|
|
|
typedef BOpenHashTable<HashSetTableDefinition<Key> > ElementTable;
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2019-02-15 04:43:49 +03:00
|
|
|
const HashSet<Key>* fSet;
|
2019-02-15 03:57:42 +03:00
|
|
|
typename ElementTable::Iterator fIterator;
|
|
|
|
Element* fElement;
|
2010-01-15 00:49:16 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class HashSet<Key>;
|
|
|
|
};
|
|
|
|
|
|
|
|
HashSet();
|
|
|
|
~HashSet();
|
|
|
|
|
|
|
|
status_t InitCheck() const;
|
|
|
|
|
|
|
|
status_t Add(const Key& key);
|
|
|
|
bool Remove(const Key& key);
|
2019-02-15 04:43:49 +03:00
|
|
|
bool Remove(Iterator& it);
|
2010-01-15 00:49:16 +03:00
|
|
|
void Clear();
|
|
|
|
bool Contains(const Key& key) const;
|
|
|
|
|
|
|
|
int32 Size() const;
|
|
|
|
|
2019-02-15 04:43:49 +03:00
|
|
|
Iterator GetIterator() const;
|
2010-01-15 00:49:16 +03:00
|
|
|
|
|
|
|
protected:
|
2019-02-13 06:46:28 +03:00
|
|
|
typedef BOpenHashTable<HashSetTableDefinition<Key> > ElementTable;
|
2010-01-15 00:49:16 +03:00
|
|
|
typedef HashSetElement<Key> Element;
|
|
|
|
friend class Iterator;
|
|
|
|
|
|
|
|
protected:
|
2019-02-13 06:44:14 +03:00
|
|
|
ElementTable fTable;
|
2010-01-15 00:49:16 +03:00
|
|
|
};
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// SynchronizedHashSet
|
2019-02-13 06:46:28 +03:00
|
|
|
template<typename Key, typename Locker = BLocker>
|
2019-02-13 06:44:14 +03:00
|
|
|
class SynchronizedHashSet : public Locker {
|
2010-01-15 00:49:16 +03:00
|
|
|
public:
|
2019-02-15 03:57:42 +03:00
|
|
|
typedef typename HashSet<Key>::Iterator Iterator;
|
2010-01-15 00:49:16 +03:00
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
SynchronizedHashSet() : Locker("synchronized hash set") {}
|
2019-02-15 03:57:42 +03:00
|
|
|
~SynchronizedHashSet() { Locker::Lock(); }
|
2010-01-15 00:49:16 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
MapLocker locker(this);
|
|
|
|
fSet.Clear();
|
|
|
|
}
|
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
bool Contains(const Key& key) const
|
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
const Locker* lock = this;
|
|
|
|
MapLocker locker(const_cast<Locker*>(lock));
|
2010-01-15 00:49:16 +03:00
|
|
|
if (!locker.IsLocked())
|
|
|
|
return false;
|
|
|
|
return fSet.Contains(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 Size() const
|
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
const Locker* lock = this;
|
|
|
|
MapLocker locker(const_cast<Locker*>(lock));
|
2010-01-15 00:49:16 +03:00
|
|
|
return fSet.Size();
|
|
|
|
}
|
|
|
|
|
2019-02-15 04:43:49 +03:00
|
|
|
Iterator GetIterator() const
|
2010-01-15 00:49:16 +03:00
|
|
|
{
|
|
|
|
return fSet.GetIterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
// for debugging only
|
|
|
|
const HashSet<Key>& GetUnsynchronizedSet() const { return fSet; }
|
|
|
|
HashSet<Key>& GetUnsynchronizedSet() { return fSet; }
|
|
|
|
|
|
|
|
protected:
|
2019-02-13 06:44:14 +03:00
|
|
|
typedef AutoLocker<Locker> MapLocker;
|
2010-01-15 00:49:16 +03:00
|
|
|
|
|
|
|
HashSet<Key> fSet;
|
|
|
|
};
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// HashSet
|
|
|
|
|
|
|
|
// constructor
|
|
|
|
template<typename Key>
|
|
|
|
HashSet<Key>::HashSet()
|
2019-02-13 06:44:14 +03:00
|
|
|
:
|
|
|
|
fTable()
|
2010-01-15 00:49:16 +03:00
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
fTable.Init();
|
2010-01-15 00:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// destructor
|
|
|
|
template<typename Key>
|
|
|
|
HashSet<Key>::~HashSet()
|
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
Clear();
|
2010-01-15 00:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// InitCheck
|
|
|
|
template<typename Key>
|
|
|
|
status_t
|
|
|
|
HashSet<Key>::InitCheck() const
|
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
return (fTable.TableSize() > 0 ? B_OK : B_NO_MEMORY);
|
2010-01-15 00:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// Add
|
|
|
|
template<typename Key>
|
|
|
|
status_t
|
|
|
|
HashSet<Key>::Add(const Key& key)
|
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
Element* element = fTable.Lookup(key);
|
|
|
|
if (element) {
|
|
|
|
// already contains the value
|
2010-01-15 00:49:16 +03:00
|
|
|
return B_OK;
|
2019-02-13 06:44:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// does not contain the key yet: create an element and add it
|
|
|
|
element = new(std::nothrow) Element(key);
|
2010-01-15 00:49:16 +03:00
|
|
|
if (!element)
|
|
|
|
return B_NO_MEMORY;
|
2019-02-13 06:44:14 +03:00
|
|
|
|
|
|
|
status_t error = fTable.Insert(element);
|
|
|
|
if (error != B_OK)
|
|
|
|
delete element;
|
|
|
|
|
|
|
|
return error;
|
2010-01-15 00:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// Remove
|
|
|
|
template<typename Key>
|
|
|
|
bool
|
|
|
|
HashSet<Key>::Remove(const Key& key)
|
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
Element* element = fTable.Lookup(key);
|
|
|
|
if (element == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fTable.Remove(element);
|
|
|
|
delete element;
|
|
|
|
|
|
|
|
return true;
|
2010-01-15 00:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2019-02-15 04:43:49 +03:00
|
|
|
// Remove
|
|
|
|
template<typename Key>
|
|
|
|
bool
|
|
|
|
HashSet<Key>::Remove(Iterator& it)
|
|
|
|
{
|
|
|
|
Element* element = it.fElement;
|
|
|
|
if (element == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fTable.RemoveUnchecked(element);
|
|
|
|
delete element;
|
|
|
|
it.fElement = NULL;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// Clear
|
2019-02-13 06:46:28 +03:00
|
|
|
template<typename Key>
|
2010-01-15 00:49:16 +03:00
|
|
|
void
|
|
|
|
HashSet<Key>::Clear()
|
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
// clear the table and delete the elements
|
|
|
|
Element* element = fTable.Clear(true);
|
|
|
|
while (element != NULL) {
|
|
|
|
Element* next = element->fNext;
|
|
|
|
delete element;
|
|
|
|
element = next;
|
|
|
|
}
|
2010-01-15 00:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// Contains
|
|
|
|
template<typename Key>
|
|
|
|
bool
|
|
|
|
HashSet<Key>::Contains(const Key& key) const
|
|
|
|
{
|
2019-02-13 06:44:14 +03:00
|
|
|
return fTable.Lookup(key) != NULL;
|
2010-01-15 00:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 06:44:14 +03:00
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// Size
|
|
|
|
template<typename Key>
|
|
|
|
int32
|
|
|
|
HashSet<Key>::Size() const
|
|
|
|
{
|
|
|
|
return fTable.CountElements();
|
|
|
|
}
|
|
|
|
|
2019-02-13 06:46:28 +03:00
|
|
|
|
2010-01-15 00:49:16 +03:00
|
|
|
// GetIterator
|
|
|
|
template<typename Key>
|
2019-02-15 03:57:42 +03:00
|
|
|
typename HashSet<Key>::Iterator
|
2019-02-15 04:43:49 +03:00
|
|
|
HashSet<Key>::GetIterator() const
|
2010-01-15 00:49:16 +03:00
|
|
|
{
|
|
|
|
return Iterator(this);
|
|
|
|
}
|
|
|
|
|
2019-02-13 06:46:28 +03:00
|
|
|
} // namespace BPrivate
|
2010-01-15 00:49:16 +03:00
|
|
|
|
2019-02-13 06:46:28 +03:00
|
|
|
using BPrivate::HashSet;
|
2010-01-15 00:49:16 +03:00
|
|
|
|
|
|
|
#endif // HASH_SET_H
|