SimPHash/Src/Hash/Str/FNV1a64.mqh

94 lines
2.8 KiB
MQL5
Raw Permalink Normal View History

2026-06-27 21:28:54 -05:00
//+------------------------------------------------------------------+
//| FNV1a64.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/"
#property strict
#ifndef SIMPHASH_SRC_HASH_STR_FNV1A64_MQH
#define SIMPHASH_SRC_HASH_STR_FNV1A64_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "..\\Def.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CHashFnv1A64 : public IHashGenerator
{
private:
ulong m_basis;
ulong m_prime_offset;
public:
CHashFnv1A64(void) {}
~CHashFnv1A64(void) {}
//---
bool Init(CYmlNode& node) override final
{
//---
CYmlNode basis = node["basis"];
if((basis.GetFlag()&TSN_YAML_VTYPE_ALL_STRING) != 0) // def
{
m_basis = FNV_OFFSET_BASIS;
}
else
{
m_basis = ulong(basis.ToInt(FNV_OFFSET_BASIS));
}
//---
CYmlNode prime = node["prime"];
if((prime.GetFlag()&TSN_YAML_VTYPE_ALL_STRING) != 0)
{
m_prime_offset = FNV_PRIME;
}
else
{
m_prime_offset = ulong(prime.ToInt(FNV_PRIME));
}
//---
return true;
}
//---
ulong Hash(const string& key) override final
{
const int len = StringLen(key);
ulong hash = m_basis;
FNV1a_64_AsM_Str(key, len, hash, m_prime_offset)
return hash;
}
//---
void AddIncludes(string& data) override final
{
data += "#include <TSN/Tables/AllHashes.mqh>\r\n";
}
//---
void BuildString(string& data) override final
{
data += "//---\r\n";
data += " const int len = StringLen(key);\r\n";
data += " ulong key_hash = " + string(m_basis) + "ULL;\r\n";
data += " FNV1a_64_AsM_Str(key, len, key_hash, " + string(m_prime_offset) + "ULL)\r\n";
data += "\r\n";
}
};
}
//+------------------------------------------------------------------+
#endif // SIMPHASH_SRC_HASH_STR_FNV1A64_MQH