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
|
||||
Reference in New Issue
Block a user