Initial commit

This commit is contained in:
user
2026-07-12 14:22:00 +04:00
commit 49ecd4784c
90 changed files with 6910 additions and 0 deletions
@@ -0,0 +1,22 @@
#include "convert.h"
BEGIN_NAMESPACE(BusinessLogic)
BEGIN_NAMESPACE(Convert)
size_t FromStdString::operator()(const std::array<char, 5> &value, size_t alphabetPower) const {
if (value.empty()) {
throw std::invalid_argument("undefined key");
}
size_t result = 0;
size_t counter = 0;
for (auto crbegin = value.crbegin(), crend = value.crend()
; crbegin != crend && *crbegin >= '!' && *crbegin <= '~'; ++crbegin, ++counter
) {
result += (*crbegin) * std::pow(alphabetPower, counter);
}
return result;
}
END_NAMESPACE // Convert
END_NAMESPACE // BusinessLogic
@@ -0,0 +1,24 @@
#ifndef CONVERT_H
#define CONVERT_H
#include "../businessLogic.h"
#include <string>
#include <stdexcept>
#include <limits>
#include <array>
#include <cmath>
BEGIN_NAMESPACE(BusinessLogic)
BEGIN_NAMESPACE(Convert)
class FromStdString
{
public:
size_t operator()(const std::array<char, 5> &, size_t) const;
};
END_NAMESPACE // Convert
END_NAMESPACE // BusinessLogic
#endif // CONVERT_H
@@ -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> &currentList = 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
@@ -0,0 +1,37 @@
#ifndef SINGLETON_H
#define SINGLETON_H
#include "../businessLogic.h"
#include <memory>
BEGIN_NAMESPACE(BusinessLogic)
BEGIN_NAMESPACE(Pattern)
template <typename T>
class Singleton {
private:
static std::unique_ptr<T> m_singleton;
public:
static const T &getInstance() {
if (m_singleton == nullptr) {
m_singleton = std::unique_ptr<T>(new T);
}
return m_singleton.operator*();
}
Singleton() = default;
Singleton(const Singleton &) = delete;
Singleton(Singleton &&) = delete;
Singleton &operator=(const Singleton &) = delete;
Singleton &operator=(Singleton &&) = delete;
};
template<typename T>
std::unique_ptr<T> Singleton<T>::m_singleton = nullptr;
END_NAMESPACE // Pattern
END_NAMESPACE // BusinessLogic
#endif // SINGLETON_H
@@ -0,0 +1,18 @@
#include "dispersion.h"
BEGIN_NAMESPACE(BusinessLogic)
BEGIN_NAMESPACE(Statistics)
long double Dispersion::operator()(const container_type &container, size_t size) const {
long double average = 0;
long double averageOfSquares = 0;
for(size_t i = 0; i < size; ++i) {
average += ( container[i] ) / static_cast<long double>(size);
averageOfSquares += ( container[i] ) * ( container[i] ) / static_cast<long double>(size);
}
return averageOfSquares - average * average;
}
END_NAMESPACE // Statistics
END_NAMESPACE // BusinessLogic
@@ -0,0 +1,25 @@
#ifndef DISPERSION_H
#define DISPERSION_H
#include "../businessLogic.h"
#include <vector>
#include <algorithm>
#include <functional>
BEGIN_NAMESPACE(BusinessLogic)
BEGIN_NAMESPACE(Statistics)
class Dispersion {
public: // Types
using value_type = size_t;
using container_type = std::vector<value_type>;
public:
long double operator()(const container_type &container, size_t size) const;
};
END_NAMESPACE // Statistics
END_NAMESPACE // BusinessLogic
#endif // DISPERSION_H
+102
View File
@@ -0,0 +1,102 @@
#ifndef BUSINESS_LOGIC_H
#define BUSINESS_LOGIC_H
/* Opening bracket */
#define OPEN {
/* Closing bracket */
#define CLOSE }
/* Begin of namespace */
#define BEGIN_NAMESPACE(name) \
namespace name OPEN
/* Begin of namespace */
#define END_NAMESPACE \
CLOSE
/* Declaring classes of factory method */
#define DFM_TYPES \
class Product; \
class Creator;
/* Factory method namespace declaration */
#define DFM_NAMESPACE(name) \
BEGIN_NAMESPACE(name) \
DFM_TYPES \
END_NAMESPACE
/* Factory method base cod declaration */
#define DFM_BASE \
DFM_NAMESPACE(Abstract) \
DFM_NAMESPACE(One) \
DFM_NAMESPACE(Family)
namespace BusinessLogic
{
namespace Hash
{
namespace Function
{
DFM_BASE
DFM_NAMESPACE(FNV1a)
DFM_NAMESPACE(Standart)
DFM_NAMESPACE(Equivalent)
namespace Pattern
{
DFM_NAMESPACE(FamilyToOne)
}
}
namespace Algorithm
{
DFM_BASE
DFM_NAMESPACE(LinearProbing)
DFM_NAMESPACE(QuadraticProbing)
DFM_NAMESPACE(PseudorandomProbing)
DFM_NAMESPACE(DoubleHashing)
}
namespace Factory
{
class Abstract;
class One;
class Family;
}
namespace Table /* Only string data */
{
class Abstract;
class OpenAddressing; // Direct address hash function
class Chains; // Hash function implemented by the chain method
namespace Pattern
{
class InterfaceSubscriber;
class Publisher;
}
}
}
namespace Pattern // Can be used for different classes
{
template<typename T>
class Singleton;
}
namespace Convert
{
class FromStdString;
}
namespace Statistics
{
class Dispersion;
}
}
#endif // BUSINESS_LOGIC_H
+197
View File
@@ -0,0 +1,197 @@
#include "hashHeaderFiles.h"
#include <iostream>
#include <string>
#include <ctime>
#include <initializer_list>
#include <tuple>
#include <array>
#include "Convert/convert.h"
using namespace BusinessLogic::Hash;
using namespace Function::Pattern;
using namespace Table::Pattern;
using factory_ptr = std::shared_ptr<const Factory::Abstract>;
class Subscriber : public InterfaceSubscriber {
private:
clock_t m_time;
size_t m_maxCollision;
size_t m_numberOfSuccesses;
size_t m_numberOfFailures;
size_t m_target;
long double m_lastCoefficients;
public:
Subscriber(size_t target)
: m_maxCollision(0)
, m_numberOfSuccesses(0)
, m_numberOfFailures(0)
, m_target(target)
, m_lastCoefficients(0)
{
}
void startTime() {
m_time = clock();
}
void endTime() {
m_time = clock() - m_time;
}
double lastCoefficient() const {
return m_lastCoefficients;
}
double workingTime() const {
return m_time / static_cast<double>(CLOCKS_PER_SEC);
}
size_t maxCollision() const {
return m_maxCollision;
}
auto numberOfSuccesses() const {
return m_numberOfSuccesses;
}
auto numberOfFailures() const {
return m_numberOfFailures;
}
virtual void successfulInsertion(const Table::Abstract *table, const std::array<char, 5> &, std::pair<size_t, size_t>, size_t numberOfCollisions) override {
m_maxCollision = {
numberOfCollisions > m_maxCollision
? numberOfCollisions
: m_maxCollision
};
++m_numberOfSuccesses;
if(m_numberOfSuccesses + m_numberOfFailures >= m_target) {
m_lastCoefficients = table->simpleUniformHashingCoefficient();
}
}
virtual void unsuccessfulInsertion(const Table::Abstract *, const std::array<char, 5> &) override {
++m_numberOfFailures;
}
virtual void filledInPart(const Table::Abstract *) override {
m_lastCoefficients = 0;
}
};
void runTable(std::unique_ptr<Table::Abstract> table, size_t target) {
/* 26 letters */
constexpr char firstChar = ' ';
constexpr char endChar = '~';
size_t progress = 0;
std::array<char, 5> currentString = { ' ', ' ', ' ', ' ', ' ' };
bool continueFlag = true;
while (continueFlag) {
if (++progress > target || not table->insert(currentString)) {
continueFlag = false;
}
currentString[4] < endChar ? ++currentString[4]
: (currentString[4] = firstChar, currentString[3] < endChar) ? ++currentString[3]
: (currentString[3] = firstChar, currentString[2] < endChar) ? ++currentString[2]
: (currentString[2] = firstChar, currentString[1] < endChar) ? ++currentString[1]
: (currentString[1] = firstChar, currentString[0] < endChar) ? ++currentString[0]
: (continueFlag = false);
}
}
int main(int /*argc*/, char **/*argv*/)
{
auto fnv1a = std::make_shared<Function::FNV1a::Creator>();
auto equivalent = std::make_shared<Function::Equivalent::Creator>();
auto standart = std::make_shared<Function::Standart::Creator>();
auto doubleHashing = std::make_shared<Algorithm::DoubleHashing::Creator>(0, 1);
auto pseudorandomProbing = std::make_shared<Algorithm::PseudorandomProbing::Creator>(0);
auto linearProbing = std::make_shared<Algorithm::LinearProbing::Creator>();
auto quadraticProbing = std::make_shared<Algorithm::QuadraticProbing::Creator>();
/* First Hash Function has a number 0, second - 1 */
const Factory::Abstract &fac1 { Factory::Family(fnv1a, doubleHashing) };
const Factory::Abstract &fac2 { Factory::Family(fnv1a, pseudorandomProbing) };
const Factory::Abstract &fac3 { Factory::One(equivalent, linearProbing) };
const Factory::Abstract &fac4 { Factory::One(equivalent, quadraticProbing) };
const Factory::Abstract &fac5 { Factory::One(standart, linearProbing) };
const Factory::Abstract &fac6 { Factory::One(standart, quadraticProbing) };
auto simpleFNV1a = std::make_shared<FamilyToOne::Creator>(fnv1a, 0);
const Factory::Abstract &fac7 { Factory::One(simpleFNV1a, linearProbing) };
const Factory::Abstract &fac8 { Factory::One(simpleFNV1a, quadraticProbing) };
/* 0x5 == 0b0101 */
auto complicatedFNV1a = std::make_shared<FamilyToOne::Creator>(fnv1a, 0x55555555);
const Factory::Abstract &fac9 { Factory::One(complicatedFNV1a, linearProbing) };
const Factory::Abstract &fac10 { Factory::One(complicatedFNV1a, quadraticProbing) };
/*
* Constants
*/
constexpr size_t numberOfInserts = (
//11
//101
//5'003
//50'021
//230'003
//456'959
//1'000'003
//5'000'011
//10'000'019
10'000'019
); // Must be simple - max is 10'371'957'246
constexpr size_t quantityOfTables = 14;
constexpr size_t numberOfBins = numberOfInserts / 10;
constexpr size_t alphabetPower = '~' - ' ' + 1;
constexpr double alpha = 0.9;
std::array<std::shared_ptr<Subscriber>, quantityOfTables> results;
for(std::shared_ptr<Subscriber> &current : results) {
current = std::make_shared<Subscriber>(static_cast<size_t>(numberOfInserts * alpha));
}
/*
* Constants
*/
std::array<std::unique_ptr<Table::Abstract>, quantityOfTables> tables {
std::unique_ptr<Table::Abstract>( new Table::OpenAddressing(numberOfInserts, fac1, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::OpenAddressing(numberOfInserts, fac2, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::OpenAddressing(numberOfInserts, fac3, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::OpenAddressing(numberOfInserts, fac4, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::OpenAddressing(numberOfInserts, fac5, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::OpenAddressing(numberOfInserts, fac6, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::OpenAddressing(numberOfInserts, fac7, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::OpenAddressing(numberOfInserts, fac8, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::OpenAddressing(numberOfInserts, fac9, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::OpenAddressing(numberOfInserts, fac10, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::Chains (numberOfBins, *equivalent, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::Chains (numberOfBins, *standart, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::Chains (numberOfBins, *simpleFNV1a, alphabetPower) ),
std::unique_ptr<Table::Abstract>( new Table::Chains (numberOfBins, *complicatedFNV1a, alphabetPower) )
};
std::cout << std::fixed;
std::cout.precision(10);
std::cout << "START!" << std::endl;
for(size_t i = 0; i < quantityOfTables; ++i) {
tables[i]->signTheObject( &results[i].operator*() );
results[i]->startTime();
runTable(std::move(tables[i]), numberOfInserts * alpha);
results[i]->endTime();
std::cout
<< results[i]->maxCollision() << ' '
<< std::sqrt(results[i]->lastCoefficient()) << ' '
<< results[i]->numberOfSuccesses() << '/'
<< alpha << '/'
<< numberOfInserts << ' '
<< results[i]->workingTime() << std::endl;
}
std::cout << "END!" << std::endl;
}
@@ -0,0 +1,32 @@
#ifndef HASH_HEADER_FILES_H
#define HASH_HEADER_FILES_H
#include "Statistics/dispersion.h"
#include "Hash/Algorithm/One/linearProbing.h"
#include "Hash/Algorithm/One/quadraticProbing.h"
#include "Hash/Algorithm/Family/pseudorandomProbing.h"
#include "Hash/Algorithm/Family/doubleHashing.h"
#include "Hash/Function/Family/fnv1a.h"
#include "Hash/Function/One/standart.h"
#include "Hash/Function/One/equivalent.h"
#include "Hash/Function/Pattern/familyToOne.h"
#include "Hash/Algorithm/One/creatorLinearProbing.h"
#include "Hash/Algorithm/One/creatorQuadraticProbing.h"
#include "Hash/Algorithm/Family/creatorPseudorandomProbing.h"
#include "Hash/Algorithm/Family/creatorDoubleHashing.h"
#include "Hash/Function/Family/creatorFnv1a.h"
#include "Hash/Function/One/creatorStandart.h"
#include "Hash/Function/One/creatorEquivalent.h"
#include "Hash/Function/Pattern/creatorFamilyToOne.h"
#include "Hash/Factory/one.h"
#include "Hash/Factory/family.h"
#include "Hash/Table/chains.h"
#include "Hash/Table/openAddress.h"
#endif // HASH_HEADER_FILES_H