Initial commit
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
#ifndef CREATOR_ALGHORITHM_DOUBLE_HASHING_H
|
||||
#define CREATOR_ALGHORITHM_DOUBLE_HASHING_H
|
||||
|
||||
#include "doubleHashing.h"
|
||||
#include "creatorFamily.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(DoubleHashing)
|
||||
|
||||
class Creator : public Family::Creator
|
||||
{
|
||||
private:
|
||||
size_t m_firstHashFunction;
|
||||
size_t m_secondHashFunction;
|
||||
|
||||
public:
|
||||
Creator(size_t first, size_t second) : m_firstHashFunction(first), m_secondHashFunction(second)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual std::unique_ptr<Abstract::Product> create() const override {
|
||||
return std::unique_ptr<Abstract::Product>(new Product(m_firstHashFunction, m_secondHashFunction));
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // DoubleHashing
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_ALGHORITHM_DOUBLE_HASHING_H
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef CREATOR_ALGHORITHM_FAMILY_H
|
||||
#define CREATOR_ALGHORITHM_FAMILY_H
|
||||
|
||||
#include "../creatorAbstract.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(Family)
|
||||
|
||||
class Creator : public Abstract::Creator
|
||||
{
|
||||
protected:
|
||||
Creator()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Family
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_ALGHORITHM_FAMILY_H
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifndef CREATOR_ALGHORITHM_PSEUDORANDOM_PROBING_H
|
||||
#define CREATOR_ALGHORITHM_PSEUDORANDOM_PROBING_H
|
||||
|
||||
#include "pseudorandomProbing.h"
|
||||
#include "creatorFamily.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(PseudorandomProbing)
|
||||
|
||||
class Creator : public Family::Creator
|
||||
{
|
||||
private:
|
||||
size_t m_hashFunctionNumber;
|
||||
|
||||
public:
|
||||
Creator(size_t number) : m_hashFunctionNumber(number)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual std::unique_ptr<Abstract::Product> create() const override {
|
||||
return std::unique_ptr<Abstract::Product>(new Product(m_hashFunctionNumber));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
END_NAMESPACE // PseudorandomProbing
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_ALGHORITHM_PSEUDORANDOM_PROBING_H
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef ALGHORITHM_DOUBLE_HASHING_H
|
||||
#define ALGHORITHM_DOUBLE_HASHING_H
|
||||
|
||||
#include "family.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(DoubleHashing)
|
||||
|
||||
class Product final : public Family::Product
|
||||
{
|
||||
private:
|
||||
size_t m_firstHashFunction;
|
||||
size_t m_secondHashFunction;
|
||||
size_t m_multiplier;
|
||||
private:
|
||||
friend Creator;
|
||||
Product(size_t first, size_t second) : m_firstHashFunction(first), m_secondHashFunction(second)
|
||||
{
|
||||
if (first == second) {
|
||||
throw std::invalid_argument("hash functions must be different");
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual size_t getFirstCoefficient() override {
|
||||
m_multiplier = m_hashFunction->getHash(m_convertValue, m_firstHashFunction);
|
||||
return m_hashFunction->getHash(m_convertValue, m_firstHashFunction);
|
||||
}
|
||||
virtual size_t getSecondCoefficient(size_t counter) override {
|
||||
return counter * m_multiplier;
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // DoubleHashing
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !ALGHORITHM_DOUBLE_HASHING_H
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef ALGHORITHM_FAMILY_H
|
||||
#define ALGHORITHM_FAMILY_H
|
||||
|
||||
#include "../../Function/Family/family.h"
|
||||
#include "../abstract.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(Family)
|
||||
|
||||
class Product : public Abstract::Product
|
||||
{
|
||||
public:
|
||||
using function_type = Function::Family::Product;
|
||||
|
||||
protected:
|
||||
const function_type *m_hashFunction;
|
||||
|
||||
protected:
|
||||
Product() : m_hashFunction(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
void setHashFunction(const function_type &hashFunction) {
|
||||
m_hashFunction = &hashFunction;
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Family
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !ALGHORITHM_FAMILY_H
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef ALGHORITHM_PSEUDORANDOM_PROBING_H
|
||||
#define ALGHORITHM_PSEUDORANDOM_PROBING_H
|
||||
|
||||
#include "family.h"
|
||||
|
||||
#include <random>
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(PseudorandomProbing)
|
||||
|
||||
class Product final : public Family::Product
|
||||
{
|
||||
private:
|
||||
size_t m_hashFunctionNumber;
|
||||
size_t m_multiplier;
|
||||
|
||||
private:
|
||||
friend Creator;
|
||||
Product(size_t number) : m_hashFunctionNumber(number)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
// virtual bool isValidInput() const override {
|
||||
// return isPrimeNumber(size());
|
||||
// }
|
||||
virtual size_t getFirstCoefficient() override {
|
||||
m_multiplier = m_hashFunction->getHash(m_convertValue, m_hashFunctionNumber);
|
||||
srand(m_multiplier);
|
||||
return m_hashFunction->getHash(m_convertValue, m_hashFunctionNumber);
|
||||
}
|
||||
virtual size_t getSecondCoefficient(size_t counter) override {
|
||||
return counter * rand();
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // PseudorandomProbing
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !ALGHORITHM_PSEUDORANDOM_PROBING_H
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef CREATOR_ALGHORITHM_LINEAR_PROBING_H
|
||||
#define CREATOR_ALGHORITHM_LINEAR_PROBING_H
|
||||
|
||||
#include "linearProbing.h"
|
||||
#include "creatorOne.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(LinearProbing)
|
||||
|
||||
class Creator : public One::Creator
|
||||
{
|
||||
private:
|
||||
/* A number that is not mutually prime to the size will not apply */
|
||||
const size_t m_coefficient;
|
||||
|
||||
public:
|
||||
Creator(size_t coefficient = 1) : m_coefficient(coefficient)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual std::unique_ptr<Abstract::Product> create() const override {
|
||||
return std::unique_ptr<Abstract::Product>(new Product(m_coefficient));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
END_NAMESPACE // LinearProbing
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_ALGHORITHM_LINEAR_PROBING_H
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef CREATOR_ALGHORITHM_ONE_H
|
||||
#define CREATOR_ALGHORITHM_ONE_H
|
||||
|
||||
#include "../creatorAbstract.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(One)
|
||||
|
||||
class Creator : public Abstract::Creator
|
||||
{
|
||||
protected:
|
||||
Creator()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // One
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_ALGHORITHM_ONE_H
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef CREATOR_ALGHORITHM_QUADRATIC_PROBING_H
|
||||
#define CREATOR_ALGHORITHM_QUADRATIC_PROBING_H
|
||||
|
||||
#include "quadraticProbing.h"
|
||||
#include "creatorOne.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(QuadraticProbing)
|
||||
|
||||
class Creator : public One::Creator
|
||||
{
|
||||
public:
|
||||
Creator()
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual std::unique_ptr<Abstract::Product> create() const override {
|
||||
return std::unique_ptr<Abstract::Product>(new Product());
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // LinearProbing
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_ALGHORITHM_QUADRATIC_PROBING_H
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef ALGHORITHM_LINEAR_PROBING_H
|
||||
#define ALGHORITHM_LINEAR_PROBING_H
|
||||
|
||||
#include "one.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(LinearProbing)
|
||||
|
||||
class Product final : public One::Product
|
||||
{
|
||||
private:
|
||||
/* A number that is not mutually prime to the size will not apply */
|
||||
const size_t m_coefficient;
|
||||
|
||||
private:
|
||||
friend Creator;
|
||||
Product(size_t coefficient)
|
||||
: m_coefficient(coefficient == 0 ? 1 : coefficient)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
// virtual bool isValidInput() const override {
|
||||
// return gcd(m_coefficient, size()) == 1;
|
||||
// }
|
||||
virtual size_t getFirstCoefficient() override {
|
||||
return m_hashFunction->getHash(m_convertValue);
|
||||
}
|
||||
virtual size_t getSecondCoefficient(size_t counter) override {
|
||||
return counter * m_coefficient;
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // LinearProbing
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !ALGHORITHM_LINEAR_PROBING_H
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef ALGHORITHM_ONE_H
|
||||
#define ALGHORITHM_ONE_H
|
||||
|
||||
#include "../../Function/One/one.h"
|
||||
#include "../abstract.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(One)
|
||||
|
||||
class Product : public Abstract::Product
|
||||
{
|
||||
public:
|
||||
using function_type = Function::One::Product;
|
||||
|
||||
protected:
|
||||
const function_type *m_hashFunction;
|
||||
|
||||
protected:
|
||||
Product() : m_hashFunction(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
void setHashFunction(const function_type &hashFunction) {
|
||||
m_hashFunction = &hashFunction;
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // One
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !ALGHORITHM_ONE_H
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef ALGHORITHM_QUADRATIC_PROBING_H
|
||||
#define ALGHORITHM_QUADRATIC_PROBING_H
|
||||
|
||||
#include "one.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(QuadraticProbing)
|
||||
|
||||
class Product final : public One::Product
|
||||
{
|
||||
private:
|
||||
friend Creator;
|
||||
Product()
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
// virtual bool isValidInput() const override {
|
||||
// return isPrimeNumber(size());
|
||||
// }
|
||||
virtual size_t getFirstCoefficient() override {
|
||||
return m_hashFunction->getHash(m_convertValue);
|
||||
}
|
||||
virtual size_t getSecondCoefficient(size_t counter) override {
|
||||
return std::pow(size(), counter);
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // LinearProbing
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !ALGHORITHM_QUADRATIC_PROBING_H
|
||||
@@ -0,0 +1,101 @@
|
||||
#ifndef ALGHORITHM_ABSTRACT_H
|
||||
#define ALGHORITHM_ABSTRACT_H
|
||||
|
||||
#include "../../Convert/convert.h"
|
||||
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
#include <memory>
|
||||
#include <array>
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(Abstract)
|
||||
|
||||
class Product
|
||||
{
|
||||
public:
|
||||
using value_type = std::array<char, 5>;
|
||||
|
||||
private:
|
||||
const std::vector<value_type> *m_table;
|
||||
BusinessLogic::Convert::FromStdString m_toInteger;
|
||||
|
||||
protected:
|
||||
size_t m_convertValue;
|
||||
|
||||
protected:
|
||||
Product() : m_table(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
size_t gcd(size_t first, size_t second) const {
|
||||
while (first > 0 && second > 0) {
|
||||
first > second
|
||||
? first %= second
|
||||
: second %= first;
|
||||
}
|
||||
return first + second;
|
||||
}
|
||||
bool isPrimeNumber(size_t value) const {
|
||||
size_t size = static_cast<size_t>(sqrt(value));
|
||||
for (size_t i = 2; i <= size; i++) {
|
||||
if (value % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
size_t size() const {
|
||||
return m_table->size();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual bool isValidInput() const {
|
||||
return true;
|
||||
}
|
||||
virtual size_t getFirstCoefficient() = 0;
|
||||
virtual size_t getSecondCoefficient(size_t counter) = 0;
|
||||
|
||||
public:
|
||||
void setTable(decltype(m_table) table) {
|
||||
m_table = table;
|
||||
}
|
||||
|
||||
size_t getIndex(const value_type &value, size_t& numberOfCollisions, size_t alphabetPower) {
|
||||
if (not isValidInput()) {
|
||||
throw std::invalid_argument("the condition for using the algorithm is not met");
|
||||
}
|
||||
|
||||
size_t size = m_table->size();
|
||||
m_convertValue = m_toInteger(value, alphabetPower);
|
||||
// It is necessary to use some coefficient
|
||||
|
||||
numberOfCollisions = 0;
|
||||
decltype (m_table->cbegin()) currentIterator;
|
||||
|
||||
constexpr std::array<char, 5> nullValue = {'\0', '\0', '\0', '\0', '\0'};
|
||||
while (numberOfCollisions <= size) {
|
||||
currentIterator = {
|
||||
m_table->cbegin()
|
||||
+ ( getFirstCoefficient() + getSecondCoefficient(numberOfCollisions) ) % size
|
||||
};
|
||||
|
||||
if ( *currentIterator == nullValue || *currentIterator == value ) { // Free cell or value match
|
||||
return ( currentIterator - m_table->cbegin() );
|
||||
}
|
||||
++numberOfCollisions;
|
||||
}
|
||||
return std::numeric_limits<size_t>::max();
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Abstract
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !ALGHORITHM_ABSTRACT_H
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef CREATOR_ALGHORITHM_ABSTRACT_H
|
||||
#define CREATOR_ALGHORITHM_ABSTRACT_H
|
||||
|
||||
#include "../../../BusinessLogic/businessLogic.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Algorithm)
|
||||
BEGIN_NAMESPACE(Abstract)
|
||||
|
||||
class Creator
|
||||
{
|
||||
protected:
|
||||
Creator()
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual std::unique_ptr<Product> create() const = 0;
|
||||
};
|
||||
|
||||
END_NAMESPACE // Abstract
|
||||
END_NAMESPACE // Algorithm
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_ALGHORITHM_ABSTRACT_H
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef FACTORY_ABSTRACT_H
|
||||
#define FACTORY_ABSTRACT_H
|
||||
|
||||
#include "../Function/abstract.h"
|
||||
#include "../Algorithm/abstract.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Factory)
|
||||
|
||||
class Abstract
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* I used to use a constant link. This led to UB.
|
||||
*
|
||||
* https://eel.is/c++draft/class.temporary#6 (27.10.2021)
|
||||
*
|
||||
* The exceptions to this lifetime rule are:
|
||||
* (6.9) A temporary object bound to a reference parameter in a function call ([expr.call]) persists until the completion of the full-expression containing the call.
|
||||
*/
|
||||
using AFC_type = std::shared_ptr<Function::Abstract::Creator>;
|
||||
using AAC_type = std::shared_ptr<Algorithm::Abstract::Creator>;
|
||||
|
||||
protected:
|
||||
AFC_type m_functionCreator;
|
||||
AAC_type m_algorithmCreator;
|
||||
|
||||
protected:
|
||||
Abstract(AFC_type functionCreator, AAC_type algorithmCreator)
|
||||
: m_functionCreator(functionCreator), m_algorithmCreator(algorithmCreator)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
using AFP_type = Function::Abstract::Product;
|
||||
using AAP_type = Algorithm::Abstract::Product;
|
||||
|
||||
public:
|
||||
virtual const AFP_type &getFunction() const = 0;
|
||||
virtual std::unique_ptr<AAP_type> getAlgorithm() const = 0;
|
||||
};
|
||||
|
||||
END_NAMESPACE // Factory
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !FACTORY_ABSTRACT_H
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef FACTORY_FAMILY_H
|
||||
#define FACTORY_FAMILY_H
|
||||
|
||||
#include "../Factory/abstract.h"
|
||||
|
||||
#include "../Function/Family/family.h"
|
||||
#include "../Function/Family/creatorFamily.h"
|
||||
|
||||
#include "../Algorithm/Family/family.h"
|
||||
#include "../Algorithm/Family/creatorFamily.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Factory)
|
||||
|
||||
class Family final : public Abstract
|
||||
{
|
||||
public:
|
||||
using FFC_type = std::shared_ptr<Function::Family::Creator>;
|
||||
using FAC_type = std::shared_ptr<Algorithm::Family::Creator>;
|
||||
|
||||
using FFP_type = Function::Family::Product;
|
||||
using FAP_type = Algorithm::Family::Product;
|
||||
|
||||
public:
|
||||
Family(const FFC_type functionCreator, const FAC_type algorithmCreator)
|
||||
: Abstract(functionCreator, algorithmCreator)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual const FFP_type &getFunction() const override {
|
||||
return dynamic_cast<const FFP_type &>(m_functionCreator->create());
|
||||
}
|
||||
virtual std::unique_ptr<AAP_type> getAlgorithm() const override {
|
||||
std::unique_ptr<AAP_type> algorithm = m_algorithmCreator->create();
|
||||
dynamic_cast<FAP_type *>(algorithm.get())->setHashFunction(getFunction());
|
||||
return algorithm;
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Factory
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !FACTORY_FAMILY_H
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef FACTORY_ONE_H
|
||||
#define FACTORY_ONE_H
|
||||
|
||||
#include "../Factory/abstract.h"
|
||||
|
||||
#include "../Function/One/one.h"
|
||||
#include "../Function/One/creatorOne.h"
|
||||
|
||||
#include "../Algorithm/One/one.h"
|
||||
#include "../Algorithm/One/creatorOne.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Factory)
|
||||
|
||||
class One final : public Abstract
|
||||
{
|
||||
public:
|
||||
using OFC_type = std::shared_ptr<Function::One::Creator>;
|
||||
using OAC_type = std::shared_ptr<Algorithm::One::Creator>;
|
||||
|
||||
using OFP_type = Function::One::Product;
|
||||
using OAP_type = Algorithm::One::Product;
|
||||
|
||||
public:
|
||||
One(OFC_type functionCreator, OAC_type algorithmCreator)
|
||||
: Abstract(functionCreator, algorithmCreator)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual const OFP_type &getFunction() const override {
|
||||
return dynamic_cast<const OFP_type &>(m_functionCreator->create());
|
||||
}
|
||||
virtual std::unique_ptr<AAP_type> getAlgorithm() const override {
|
||||
std::unique_ptr<AAP_type> algorithm = m_algorithmCreator->create();
|
||||
dynamic_cast<OAP_type *>(algorithm.get())->setHashFunction(getFunction());
|
||||
return algorithm;
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Factory
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !FACTORY_ONE_H
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef CREATOR_FUNCTION_FAMILY_H
|
||||
#define CREATOR_FUNCTION_FAMILY_H
|
||||
|
||||
#include "../creatorAbstract.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(Family)
|
||||
|
||||
class Creator : public Abstract::Creator
|
||||
{
|
||||
protected:
|
||||
Creator()
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual const Product &create() const = 0;
|
||||
};
|
||||
|
||||
END_NAMESPACE // Family
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // CREATOR_FUNCTION_FAMILY_H
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef CREATOR_FUNCTION_FNV1a_H
|
||||
#define CREATOR_FUNCTION_FNV1a_H
|
||||
|
||||
#include "fnv1a.h"
|
||||
#include "creatorFamily.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(FNV1a)
|
||||
|
||||
class Creator : public Family::Creator
|
||||
{
|
||||
public:
|
||||
virtual const Product &create() const override {
|
||||
return Product::getInstance();
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // FNV1a
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_FUNCTION_FNV1a_H
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef FUNCTION_FAMILY_H
|
||||
#define FUNCTION_FAMILY_H
|
||||
|
||||
#include "../abstract.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(Family)
|
||||
|
||||
using function_type = std::function<size_t(size_t, size_t)>;
|
||||
|
||||
class Product : public Abstract::Product
|
||||
{
|
||||
protected:
|
||||
const function_type m_function;
|
||||
|
||||
protected:
|
||||
Product(const function_type& function) : m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
const function_type &to_std() const {
|
||||
return m_function;
|
||||
}
|
||||
size_t getHash(size_t first, size_t second) const {
|
||||
// First - the value that is hashed
|
||||
// Second - coefficient
|
||||
return m_function(first, second);
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Family
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // FUNCTION_FAMILY_H
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef FUNCTION_FNV1a_H
|
||||
#define FUNCTION_FNV1a_H
|
||||
|
||||
#include "family.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(FNV1a)
|
||||
|
||||
using Singleton = BusinessLogic::Pattern::Singleton<Product>;
|
||||
|
||||
class Product final : public Family::Product, public Singleton
|
||||
{
|
||||
private:
|
||||
const int32_t m_p;
|
||||
|
||||
private:
|
||||
friend const Product &Singleton::getInstance();
|
||||
Product()
|
||||
: Family::Product(
|
||||
[this](size_t value, size_t i) -> size_t {
|
||||
size_t result = (0x811C9DC5 xor i) xor static_cast<int32_t>(value);
|
||||
return static_cast<int32_t>(result * m_p);
|
||||
}
|
||||
)
|
||||
, m_p(0x01000193)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
using Singleton::getInstance;
|
||||
};
|
||||
|
||||
END_NAMESPACE // FNV1a
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !FUNCTION_FNV1a_H
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef CREATOR_FUNCTION_EQUIVALENT_H
|
||||
#define CREATOR_FUNCTION_EQUIVALENT_H
|
||||
|
||||
|
||||
#include "creatorOne.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(Equivalent)
|
||||
|
||||
class Creator : public One::Creator
|
||||
{
|
||||
public:
|
||||
virtual const Product &create() const override {
|
||||
return Product::getInstance();
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Equivalent
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_FUNCTION_EQUIVALENT_H
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef CREATOR_FUNCTION_ONE_H
|
||||
#define CREATOR_FUNCTION_ONE_H
|
||||
|
||||
#include "../creatorAbstract.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(One)
|
||||
|
||||
class Creator : public Abstract::Creator
|
||||
{
|
||||
protected:
|
||||
Creator()
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual const Product &create() const = 0;
|
||||
};
|
||||
|
||||
END_NAMESPACE // One
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_FUNCTION_ONE_H
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef CREATOR_FUNCTION_STANDART_H
|
||||
#define CREATOR_FUNCTION_STANDART_H
|
||||
|
||||
#include "creatorOne.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(Standart)
|
||||
|
||||
class Creator : public One::Creator
|
||||
{
|
||||
public:
|
||||
virtual const Product &create() const override {
|
||||
return Product::getInstance();
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // Standart
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_FUNCTION_FNV1a_H
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef FUNCTION_EQUIVALENT_H
|
||||
#define FUNCTION_EQUIVALENT_H
|
||||
|
||||
#include "one.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(Equivalent)
|
||||
|
||||
using Singleton = BusinessLogic::Pattern::Singleton<Product>;
|
||||
|
||||
class Product final : public One::Product, public Singleton
|
||||
{
|
||||
private:
|
||||
friend const Product &Singleton::getInstance();
|
||||
Product() :
|
||||
One::Product(
|
||||
[](size_t value) -> size_t {
|
||||
return value;
|
||||
}
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
using Singleton::getInstance;
|
||||
};
|
||||
|
||||
END_NAMESPACE // Equivalent
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !FUNCTION_EQUIVALENT_H
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef FUNCTION_ONE_H
|
||||
#define FUNCTION_ONE_H
|
||||
|
||||
#include "../abstract.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(One)
|
||||
|
||||
using function_type = std::function<size_t(size_t)>;
|
||||
|
||||
class Product : public Abstract::Product
|
||||
{
|
||||
protected:
|
||||
const function_type m_function;
|
||||
|
||||
protected:
|
||||
Product(const function_type& function) : m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
const function_type& to_std() const {
|
||||
return m_function;
|
||||
}
|
||||
size_t getHash(size_t value) const {
|
||||
return m_function(value);
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // One
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !FUNCTION_ONE_H
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef FUNCTION_STANDART_H
|
||||
#define FUNCTION_STANDART_H
|
||||
|
||||
#include "one.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(Standart)
|
||||
|
||||
using Singleton = BusinessLogic::Pattern::Singleton<Product>;
|
||||
|
||||
class Product final : public One::Product, public Singleton
|
||||
{
|
||||
private:
|
||||
std::hash<size_t> m_standartHashFunction;
|
||||
|
||||
private:
|
||||
friend const Product &Singleton::getInstance();
|
||||
Product()
|
||||
: One::Product(
|
||||
std::bind(&std::hash<size_t>::operator(), &m_standartHashFunction, std::placeholders::_1)
|
||||
)
|
||||
, m_standartHashFunction()
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
using Singleton::getInstance;
|
||||
};
|
||||
|
||||
END_NAMESPACE // Standart
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !FUNCTION_FNV1a_H
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef CREATOR_ADAPTER_FAMILY_TO_ONE_H
|
||||
#define CREATOR_ADAPTER_FAMILY_TO_ONE_H
|
||||
|
||||
#include "familyToOne.h"
|
||||
#include "../One/creatorOne.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(Pattern)
|
||||
BEGIN_NAMESPACE(FamilyToOne)
|
||||
|
||||
class Creator final : public One::Creator
|
||||
{
|
||||
private:
|
||||
Product m_product; // To support the interface
|
||||
|
||||
public:
|
||||
Creator(std::shared_ptr<Family::Creator> adaptee, size_t coefficient)
|
||||
: m_product(adaptee->create(), coefficient)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual const Product &create() const override {
|
||||
return m_product;
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // FamilyToOne
|
||||
END_NAMESPACE // Pattern
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_ADAPTER_FAMILY_TO_ONE_H
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef ADAPTER_FAMILY_TO_ONE_H
|
||||
#define ADAPTER_FAMILY_TO_ONE_H
|
||||
|
||||
#include "../Family/family.h"
|
||||
#include "../One/one.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(Pattern)
|
||||
BEGIN_NAMESPACE(FamilyToOne)
|
||||
|
||||
class Product final : public One::Product
|
||||
{
|
||||
public:
|
||||
Product(const Family::Product &adaptee, size_t coefficient)
|
||||
: One::Product(
|
||||
std::bind(adaptee.to_std(), std::placeholders::_1, coefficient)
|
||||
)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
END_NAMESPACE // FamilyToOne
|
||||
END_NAMESPACE // Pattern
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !ADAPTER_FAMILY_TO_ONE_H
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef FUNCTION_ABSTRACT_H
|
||||
#define FUNCTION_ABSTRACT_H
|
||||
|
||||
#include "../../businessLogic.h"
|
||||
#include "../../Pattern/singleton.h"
|
||||
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(Abstract)
|
||||
|
||||
class Product
|
||||
{
|
||||
protected:
|
||||
Product()
|
||||
{
|
||||
}
|
||||
/* With the virtual destructor, the compiler considers this class to be polymorphic */
|
||||
virtual ~Product() = default;
|
||||
};
|
||||
|
||||
END_NAMESPACE // Abstract
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !FUNCTION_ABSTRACT_H
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef CREATOR_FUNCTION_ABSTRACT_H
|
||||
#define CREATOR_FUNCTION_ABSTRACT_H
|
||||
|
||||
#include "../../businessLogic.h"
|
||||
|
||||
BEGIN_NAMESPACE(BusinessLogic)
|
||||
BEGIN_NAMESPACE(Hash)
|
||||
BEGIN_NAMESPACE(Function)
|
||||
BEGIN_NAMESPACE(Abstract)
|
||||
|
||||
class Creator
|
||||
{
|
||||
protected:
|
||||
Creator()
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual const Product &create() const = 0;
|
||||
};
|
||||
|
||||
END_NAMESPACE // Abstract
|
||||
END_NAMESPACE // Function
|
||||
END_NAMESPACE // Hash
|
||||
END_NAMESPACE // BusinessLogic
|
||||
|
||||
#endif // !CREATOR_FUNCTION_ABSTRACT_H
|
||||
@@ -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