poco/Foundation/include/Poco/SimpleHashTable.h

420 lines
9.8 KiB
C
Raw Normal View History

2006-10-23 17:48:43 +02:00
//
// SimpleHashTable.h
//
2008-01-28 18:23:19 +01:00
// $Id: //poco/svn/Foundation/include/Poco/SimpleHashTable.h#2 $
2006-10-23 17:48:43 +02:00
//
// Library: Foundation
2006-11-17 17:21:51 +01:00
// Package: Hashing
2006-10-23 17:48:43 +02:00
// Module: SimpleHashTable
//
// Definition of the SimpleHashTable class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// 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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Foundation_SimpleHashTable_INCLUDED
#define Foundation_SimpleHashTable_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/HashFunction.h"
#include "Poco/HashStatistic.h"
#include <vector>
#include <map>
2006-11-17 17:21:51 +01:00
#include <cstddef>
#include <algorithm>
2006-10-23 17:48:43 +02:00
namespace Poco {
2006-11-17 17:21:51 +01:00
//@ deprecated
template <class Key, class Value, class KeyHashFunction = HashFunction<Key> >
2006-10-23 17:48:43 +02:00
class SimpleHashTable
/// A SimpleHashTable stores a key value pair that can be looked up via a hashed key.
///
/// In comparision to a HashTable, this class handles collisions by sequentially searching the next
/// free location. This also means that the maximum size of this table is limited, i.e. if the hash table
/// is full, it will throw an exception and that this class does not support remove operations.
/// On the plus side it is faster than the HashTable.
///
/// This class is NOT thread safe.
{
public:
class HashEntry
{
public:
2006-11-17 17:21:51 +01:00
Key key;
2006-10-23 17:48:43 +02:00
Value value;
2006-11-17 17:21:51 +01:00
HashEntry(const Key k, const Value v): key(k), value(v)
2006-10-23 17:48:43 +02:00
{
}
};
2006-11-17 17:21:51 +01:00
typedef std::vector<HashEntry*> HashTableVector;
2006-10-23 17:48:43 +02:00
2006-11-17 17:21:51 +01:00
SimpleHashTable(UInt32 capacity = 251): _entries(capacity, 0), _size(0), _capacity(capacity)
2006-10-23 17:48:43 +02:00
/// Creates the SimpleHashTable.
{
}
SimpleHashTable(const SimpleHashTable& ht):
_size(ht._size),
2006-11-17 17:21:51 +01:00
_capacity(ht._capacity)
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
_entries.reserve(ht._capacity);
for (typename HashTableVector::iterator it = ht._entries.begin(); it != ht._entries.end(); ++it)
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
if (*it)
_entries.push_back(new HashEntry(*it));
2006-10-23 17:48:43 +02:00
else
2006-11-17 17:21:51 +01:00
_entries.push_back(0);
2006-10-23 17:48:43 +02:00
}
}
~SimpleHashTable()
/// Destroys the SimpleHashTable.
{
clear();
}
SimpleHashTable& operator = (const SimpleHashTable& ht)
{
if (this != &ht)
{
2006-11-17 17:21:51 +01:00
SimpleHashTable tmp(ht);
swap(tmp);
2006-10-23 17:48:43 +02:00
}
return *this;
}
2006-11-17 17:21:51 +01:00
void swap(SimpleHashTable& ht)
{
using std::swap;
swap(_entries, ht._entries);
swap(_size, ht._size);
swap(_capacity, ht._capacity);
}
2006-10-23 17:48:43 +02:00
void clear()
{
2006-11-17 17:21:51 +01:00
for (typename HashTableVector::iterator it = _entries.begin(); it != _entries.end(); ++it)
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
delete *it;
*it = 0;
2006-10-23 17:48:43 +02:00
}
2006-11-17 17:21:51 +01:00
_size = 0;
2006-10-23 17:48:43 +02:00
}
UInt32 insert(const Key& key, const Value& value)
/// Returns the hash value of the inserted item.
/// Throws an exception if the entry was already inserted
{
UInt32 hsh = hash(key);
insertRaw(key, hsh, value);
return hsh;
}
2006-11-17 17:21:51 +01:00
Value& insertRaw(const Key& key, UInt32 hsh, const Value& value)
2006-10-23 17:48:43 +02:00
/// Returns the hash value of the inserted item.
/// Throws an exception if the entry was already inserted
{
2006-11-17 17:21:51 +01:00
UInt32 pos = hsh;
if (!_entries[pos])
_entries[pos] = new HashEntry(key, value);
2006-10-23 17:48:43 +02:00
else
{
UInt32 origHash = hsh;
2006-11-17 17:21:51 +01:00
while (_entries[hsh % _capacity])
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
if (_entries[hsh % _capacity]->key == key)
throw ExistsException();
if (hsh - origHash > _capacity)
2006-10-23 17:48:43 +02:00
throw PoolOverflowException("SimpleHashTable full");
hsh++;
}
2006-11-17 17:21:51 +01:00
pos = hsh % _capacity;
_entries[pos] = new HashEntry(key, value);
2006-10-23 17:48:43 +02:00
}
_size++;
2006-11-17 17:21:51 +01:00
return _entries[pos]->value;
2006-10-23 17:48:43 +02:00
}
UInt32 update(const Key& key, const Value& value)
/// Returns the hash value of the inserted item.
/// Replaces an existing entry if it finds one
{
UInt32 hsh = hash(key);
updateRaw(key, hsh, value);
return hsh;
}
void updateRaw(const Key& key, UInt32 hsh, const Value& value)
/// Returns the hash value of the inserted item.
/// Replaces an existing entry if it finds one
{
if (!_entries[hsh])
_entries[hsh] = new HashEntry(key, value);
else
{
UInt32 origHash = hsh;
2006-11-17 17:21:51 +01:00
while (_entries[hsh % _capacity])
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
if (_entries[hsh % _capacity]->key == key)
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
_entries[hsh % _capacity]->value = value;
2006-10-23 17:48:43 +02:00
return;
}
2006-11-17 17:21:51 +01:00
if (hsh - origHash > _capacity)
2006-10-23 17:48:43 +02:00
throw PoolOverflowException("SimpleHashTable full");
hsh++;
}
2006-11-17 17:21:51 +01:00
_entries[hsh % _capacity] = new HashEntry(key, value);
2006-10-23 17:48:43 +02:00
}
_size++;
}
UInt32 hash(const Key& key) const
{
2006-11-17 17:21:51 +01:00
return _hash(key, _capacity);
2006-10-23 17:48:43 +02:00
}
const Value& get(const Key& key) const
/// Throws an exception if the value does not exist
{
UInt32 hsh = hash(key);
return getRaw(key, hsh);
}
const Value& getRaw(const Key& key, UInt32 hsh) const
/// Throws an exception if the value does not exist
{
UInt32 origHash = hsh;
while (true)
{
2006-11-17 17:21:51 +01:00
if (_entries[hsh % _capacity])
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
if (_entries[hsh % _capacity]->key == key)
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
return _entries[hsh % _capacity]->value;
2006-10-23 17:48:43 +02:00
}
}
else
throw InvalidArgumentException("value not found");
2006-11-17 17:21:51 +01:00
if (hsh - origHash > _capacity)
2006-10-23 17:48:43 +02:00
throw InvalidArgumentException("value not found");
hsh++;
}
}
2006-11-17 17:21:51 +01:00
Value& get(const Key& key)
/// Throws an exception if the value does not exist
{
UInt32 hsh = hash(key);
return const_cast<Value&>(getRaw(key, hsh));
}
const Value& operator [] (const Key& key) const
{
return get(key);
}
Value& operator [] (const Key& key)
{
UInt32 hsh = hash(key);
UInt32 origHash = hsh;
while (true)
{
if (_entries[hsh % _capacity])
{
if (_entries[hsh % _capacity]->key == key)
{
return _entries[hsh % _capacity]->value;
}
}
else return insertRaw(key, hsh, Value());
if (hsh - origHash > _capacity)
return insertRaw(key, hsh, Value());
hsh++;
}
}
2006-10-23 17:48:43 +02:00
const Key& getKeyRaw(const Key& key, UInt32 hsh)
/// Throws an exception if the key does not exist. returns a reference to the internally
/// stored key. Useful when someone does an insert and wants for performance reason only to store
/// a pointer to the key in another collection
{
UInt32 origHash = hsh;
while (true)
{
2006-11-17 17:21:51 +01:00
if (_entries[hsh % _capacity])
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
if (_entries[hsh % _capacity]->key == key)
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
return _entries[hsh % _capacity]->key;
2006-10-23 17:48:43 +02:00
}
}
else
throw InvalidArgumentException("key not found");
2006-11-17 17:21:51 +01:00
if (hsh - origHash > _capacity)
2006-10-23 17:48:43 +02:00
throw InvalidArgumentException("key not found");
hsh++;
}
}
bool get(const Key& key, Value& v) const
/// Sets v to the found value, returns false if no value was found
{
UInt32 hsh = hash(key);
return getRaw(key, hsh, v);
}
bool getRaw(const Key& key, UInt32 hsh, Value& v) const
/// Sets v to the found value, returns false if no value was found
{
UInt32 origHash = hsh;
while (true)
{
2006-11-17 17:21:51 +01:00
if (_entries[hsh % _capacity])
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
if (_entries[hsh % _capacity]->key == key)
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
v = _entries[hsh % _capacity]->value;
2006-10-23 17:48:43 +02:00
return true;
}
}
else
return false;
2006-11-17 17:21:51 +01:00
if (hsh - origHash > _capacity)
2006-10-23 17:48:43 +02:00
return false;
hsh++;
}
}
2007-04-18 18:22:57 +02:00
bool exists(const Key& key) const
2006-10-23 17:48:43 +02:00
{
UInt32 hsh = hash(key);
return existsRaw(key, hsh);
}
2007-04-18 18:22:57 +02:00
bool existsRaw(const Key& key, UInt32 hsh) const
2006-10-23 17:48:43 +02:00
{
UInt32 origHash = hsh;
while (true)
{
2006-11-17 17:21:51 +01:00
if (_entries[hsh % _capacity])
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
if (_entries[hsh % _capacity]->key == key)
2006-10-23 17:48:43 +02:00
{
return true;
}
}
else
return false;
2006-11-17 17:21:51 +01:00
if (hsh - origHash > _capacity)
2006-10-23 17:48:43 +02:00
return false;
hsh++;
}
}
2007-04-25 16:39:24 +02:00
std::size_t size() const
2006-10-23 17:48:43 +02:00
/// Returns the number of elements already inserted into the SimpleHashTable
{
return _size;
}
2006-11-17 17:21:51 +01:00
UInt32 capacity() const
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
return _capacity;
2006-10-23 17:48:43 +02:00
}
void resize(UInt32 newSize)
/// Resizes the hashtable, rehashes all existing entries. Expensive!
{
2006-11-17 17:21:51 +01:00
if (_capacity != newSize)
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
SimpleHashTable tmp(newSize);
swap(tmp);
for (typename HashTableVector::const_iterator it = tmp._entries.begin(); it != tmp._entries.end(); ++it)
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
if (*it)
2006-10-23 17:48:43 +02:00
{
2006-11-17 17:21:51 +01:00
insertRaw((*it)->key, hash((*it)->key), (*it)->value);
2006-10-23 17:48:43 +02:00
}
}
}
}
HashStatistic currentState(bool details = false) const
/// Returns the current internal state
{
UInt32 numberOfEntries = (UInt32)_size;
UInt32 numZeroEntries = 0;
UInt32 maxEntriesPerHash = 0;
std::vector<UInt32> detailedEntriesPerHash;
#ifdef _DEBUG
UInt32 totalSize = 0;
#endif
2006-11-17 17:21:51 +01:00
for (int i=0; i < _capacity; ++i)
2006-10-23 17:48:43 +02:00
{
if (_entries[i])
{
maxEntriesPerHash = 1;
UInt32 size = 1;
if (details)
detailedEntriesPerHash.push_back(size);
#ifdef DEBUG
totalSize += size;
#endif
}
else
{
numZeroEntries++;
if (details)
detailedEntriesPerHash.push_back(0);
}
}
poco_assert_dbg(totalSize == numberOfEntries);
2006-11-17 17:21:51 +01:00
return HashStatistic(_capacity, numberOfEntries, numZeroEntries, maxEntriesPerHash, detailedEntriesPerHash);
2006-10-23 17:48:43 +02:00
}
private:
HashTableVector _entries;
2007-04-25 16:39:24 +02:00
std::size_t _size;
2006-11-17 17:21:51 +01:00
UInt32 _capacity;
KeyHashFunction _hash;
2006-10-23 17:48:43 +02:00
};
} // namespace Poco
#endif // Foundation_HashTable_INCLUDED