Initial commit
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
#ifndef SUBSCRIBER_H
|
||||
#define SUBSCRIBER_H
|
||||
|
||||
#include "../../../businessLogic.h"
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Table)
|
||||
BEGIN_NAMESPACE(Pattern)
|
||||
|
||||
class InterfaceSubscriber
|
||||
{
|
||||
public:
|
||||
virtual void successfulInsertion(const Table::Abstract *, const std::array<char, 5> &, std::pair<size_t, size_t>, size_t) = 0;
|
||||
virtual void unsuccessfulInsertion(const Table::Abstract *, const std::array<char, 5> &) = 0;
|
||||
};
|
||||
|
||||
class Publisher
|
||||
{
|
||||
private:
|
||||
std::vector<InterfaceSubscriber *> m_subscribers;
|
||||
|
||||
public:
|
||||
virtual void signTheObject(InterfaceSubscriber *subscriber) {
|
||||
if (subscriber not_eq nullptr) {
|
||||
m_subscribers.push_back(subscriber);
|
||||
}
|
||||
}
|
||||
virtual void unsubscribe(InterfaceSubscriber *subscriber) {
|
||||
if (subscriber not_eq nullptr) {
|
||||
m_subscribers.erase(std::find(m_subscribers.begin(), m_subscribers.end(), subscriber));
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void sendSuccessfulInsertion(const Table::Abstract *sender, const std::array<char, 5> &value, std::pair<size_t, size_t> index, size_t numberOfCollisions) const {
|
||||
for (decltype(auto) currentSubscriber : m_subscribers) {
|
||||
currentSubscriber->successfulInsertion(sender, value, index, numberOfCollisions);
|
||||
}
|
||||
}
|
||||
virtual void sendUnseccessInsertion(const Table::Abstract *sender, const std::array<char, 5> &value) const {
|
||||
for (decltype(auto) currentSubscriber : m_subscribers) {
|
||||
currentSubscriber->unsuccessfulInsertion(sender, value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Pattern
|
||||
END_NAMESPACE // Table
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // SUBSCRIBER_H
|
||||
@@ -0,0 +1,60 @@
|
||||
#ifndef ABSTRACT_HASH_TABLE_H
|
||||
#define ABSTRACT_HASH_TABLE_H
|
||||
|
||||
#include "../../businessLogic.h"
|
||||
#include "../../Statistics/dispersion.h"
|
||||
#include "Pattern/subscriber.h"
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <typeinfo>
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Table)
|
||||
|
||||
class Abstract : public Pattern::Publisher
|
||||
{
|
||||
public:
|
||||
using value_type = std::array<char, 5>;
|
||||
using collision_container = BusinessLogic::Statistics::Dispersion::container_type;
|
||||
|
||||
protected: // Members
|
||||
size_t m_numberOfBuckets;
|
||||
size_t m_alphabetPower;
|
||||
BusinessLogic::Statistics::Dispersion m_dispersion;
|
||||
|
||||
protected:
|
||||
Abstract(size_t alphabetPower)
|
||||
: m_numberOfBuckets(0)
|
||||
, m_alphabetPower(alphabetPower)
|
||||
{
|
||||
}
|
||||
|
||||
Abstract(Abstract &&) = default;
|
||||
Abstract &operator=(Abstract &&) = default;
|
||||
Abstract(const Abstract &) = delete;
|
||||
Abstract &operator=(const Abstract &) = delete;
|
||||
|
||||
public:
|
||||
virtual bool isExist(const value_type &) const = 0;
|
||||
virtual size_t size() const = 0;
|
||||
virtual bool insert(const value_type &value) = 0;
|
||||
virtual double simpleUniformHashingCoefficient() const = 0;
|
||||
size_t alphabetPower() const {
|
||||
return m_alphabetPower;
|
||||
}
|
||||
size_t numberOfBuckets() const {
|
||||
return m_numberOfBuckets;
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Table
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // ABSTRACT_HASH_TABLE_H
|
||||
@@ -0,0 +1,93 @@
|
||||
#ifndef CHAINS_HASH_TABLE_H
|
||||
#define CHAINS_HASH_TABLE_H
|
||||
|
||||
#include "abstract.h"
|
||||
#include "../../Convert/convert.h"
|
||||
#include "../Function/One/one.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Table)
|
||||
|
||||
class Chains final : public Abstract
|
||||
{
|
||||
public: // Types
|
||||
using container_type = std::vector<std::list<value_type>>;
|
||||
using OFP_type = Function::One::Product;
|
||||
using OFC_type = Function::One::Creator;
|
||||
|
||||
private:
|
||||
BusinessLogic::Convert::FromStdString m_toInteger;
|
||||
|
||||
public:
|
||||
container_type m_container;
|
||||
const OFP_type &m_function;
|
||||
|
||||
Chains(size_t size, const OFC_type &function, size_t alphabetPower = std::numeric_limits<char>::max())
|
||||
: Abstract(alphabetPower)
|
||||
, m_function(function.create())
|
||||
{
|
||||
if (size == 0) {
|
||||
size = 1;
|
||||
}
|
||||
/* The use of 'this' is mandatory */
|
||||
this->m_container.resize(size);
|
||||
}
|
||||
|
||||
private:
|
||||
bool isExist(const value_type &value, size_t index) const {
|
||||
const std::list<value_type> ¤tList = m_container[index];
|
||||
return std::find(currentList.cbegin(), currentList.cend(), value) not_eq currentList.cend();
|
||||
}
|
||||
|
||||
public:
|
||||
virtual bool isExist(const value_type &value) const override {
|
||||
size_t size = this->size();
|
||||
size_t index = m_function.getHash(m_toInteger(value, m_alphabetPower)) % size;
|
||||
return isExist(value, index);
|
||||
}
|
||||
auto row(size_t index) const {
|
||||
return m_container[index];
|
||||
}
|
||||
virtual size_t size() const override {
|
||||
return m_container.size();
|
||||
}
|
||||
virtual bool insert(const value_type &value) override {
|
||||
using namespace BusinessLogic::Statistics;
|
||||
|
||||
size_t index = m_function.getHash(m_toInteger(value, m_alphabetPower)) % this->size();
|
||||
if( isExist(value, index) ) {
|
||||
sendUnseccessInsertion(this, value);
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// List does not need a reserve
|
||||
m_container[index].push_back(value);
|
||||
}
|
||||
catch (const std::bad_alloc &) {
|
||||
sendUnseccessInsertion(this, value);
|
||||
return false;
|
||||
}
|
||||
++m_numberOfBuckets;
|
||||
|
||||
size_t listLength = m_container[index].size();
|
||||
sendSuccessfulInsertion(this, value, std::make_pair(index, listLength - 1), listLength - 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
virtual double simpleUniformHashingCoefficient() const override {
|
||||
size_t size = this->size();
|
||||
collision_container container(size);
|
||||
for(size_t i = 0; i < size; ++i) {
|
||||
container[i] = m_container[i].size();
|
||||
}
|
||||
return m_dispersion(container, size);
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Table
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // CHAINS_HASH_TABLE_H
|
||||
@@ -0,0 +1,82 @@
|
||||
#ifndef DIRECT_ADRESS_HASH_TABLE_H
|
||||
#define DIRECT_ADRESS_HASH_TABLE_H
|
||||
|
||||
#include "../Factory/abstract.h"
|
||||
#include "abstract.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Table)
|
||||
|
||||
class OpenAddressing final : public Abstract
|
||||
{
|
||||
public: // Types
|
||||
using container_type = std::vector<value_type>;
|
||||
using factory_type = Factory::Abstract;
|
||||
|
||||
private:
|
||||
using sample_type = BusinessLogic::Statistics::Dispersion::container_type;
|
||||
|
||||
private:
|
||||
friend factory_type::AAP_type;
|
||||
container_type m_container;
|
||||
collision_container m_numberOfCollision;
|
||||
std::unique_ptr<Algorithm::Abstract::Product> m_algorithm;
|
||||
|
||||
public:
|
||||
OpenAddressing(size_t size, const factory_type &factory, size_t alphabetPower = std::numeric_limits<char>::max())
|
||||
: Abstract(alphabetPower)
|
||||
, m_numberOfCollision(size, std::numeric_limits<size_t>::max())
|
||||
, m_algorithm(factory.getAlgorithm())
|
||||
{
|
||||
constexpr std::array<char, 5> nullValue = {'\0', '\0', '\0', '\0', '\0'};
|
||||
m_container.resize(size, nullValue);
|
||||
m_algorithm->setTable(&m_container);
|
||||
}
|
||||
|
||||
public:
|
||||
virtual bool isExist(const value_type &value) const override {
|
||||
size_t numberOfCollisions = 0;
|
||||
size_t index = (*m_algorithm).getIndex(value, numberOfCollisions, m_alphabetPower);
|
||||
return ( index != std::numeric_limits<size_t>::max() )
|
||||
|| ( (m_container.begin() + index).operator*() == value );
|
||||
}
|
||||
virtual size_t size() const override {
|
||||
return m_container.size();
|
||||
}
|
||||
virtual bool insert(const value_type &value) override {
|
||||
using namespace BusinessLogic::Statistics;
|
||||
|
||||
size_t size = m_container.size();
|
||||
if ( m_numberOfBuckets == size) {
|
||||
sendUnseccessInsertion(this, value);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t numberOfCollisions = 0;
|
||||
size_t index = (*m_algorithm).getIndex(value, numberOfCollisions, m_alphabetPower);
|
||||
if (index == std::numeric_limits<size_t>::max()
|
||||
|| (m_container.begin() + index).operator*() == value
|
||||
) {
|
||||
sendUnseccessInsertion(this, value);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_container[index] = value;
|
||||
m_numberOfCollision[m_numberOfBuckets] = numberOfCollisions;
|
||||
++m_numberOfBuckets;
|
||||
|
||||
sendSuccessfulInsertion(this, value, std::make_pair(index, 0), numberOfCollisions);
|
||||
|
||||
return true;
|
||||
}
|
||||
virtual double simpleUniformHashingCoefficient() const override {
|
||||
return m_dispersion(m_numberOfCollision, m_numberOfBuckets);
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Table
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // DIRECT_ADRESS_HASH_TABLE_H
|
||||
Reference in New Issue
Block a user