PerfectHashByLeo/Test/Test.mq5

111 lines
3 KiB
MQL5
Raw Permalink Normal View History

2026-06-27 17:51:10 -05:00
//+------------------------------------------------------------------+
//| Test.mq5 |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "..\\Src\\Main.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CHasherFnv164 : public TSN::CPerfectHashHasher<string>
{
public:
CHasherFnv164(void) {}
~CHasherFnv164(void) {}
//---
ulong Hash(const string& v) override final
{
ulong h = 0xcbf29ce484222325;
for(uint i = 0; i < v.Length(); i++)
{
h ^= (uchar)v[i];
h *= 0x100000001b3;
}
return h;
}
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CHasherFinal : public TSN::CPerfectHashHasherWSeed<string>
{
public:
CHasherFinal(void) {}
~CHasherFinal(void) {}
//---
__forceinline ulong Hash(const string& v, const ulong seed) override final
{
ulong h = 0xcbf29ce484222325;
for(uint i = 0; i < v.Length(); i++)
{
h ^= (uchar)v[i];
h *= 0x100000001b3;
}
return h ^ seed;
}
};
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
const string keys[5] =
{
"hola",
"valor",
"si",
"como",
"no"
};
2026-06-27 19:17:07 -05:00
const double val[5] =
{
10.0,
20.0,
30.0,
40.0,
50.0
};
2026-06-27 17:51:10 -05:00
//---
TSN::CPerfectHashByLeo<string> pf;
pf.Hasher0(new CHasherFnv164());
pf.Hasher1(new CHasherFinal());
pf.DeleteHashers(true);
pf.InitAlg(4, 12);
2026-06-28 12:19:40 -05:00
//--- Inicio
2026-06-27 17:51:10 -05:00
ulong seed[];
2026-06-27 19:17:07 -05:00
double value[];
2026-06-28 12:19:40 -05:00
ArrayResize(value, pf.m_final_table_size); // Ajustamos
// ArrayInitialize(value, 0.00); // Valor por defecto invalido
2026-06-27 19:17:07 -05:00
pf.RunWValue(keys, val, seed, value);
2026-06-28 12:19:40 -05:00
// Check
2026-06-27 19:17:07 -05:00
ArrayPrint(value);
2026-06-27 17:51:10 -05:00
//---
const string key = "no";
int bucket = int(pf.Hasher0().Hash(key) % pf.m_buckets_size);
ulong d = seed[bucket];
int final_index = int(pf.Hasher1().Hash(key, d) % pf.m_final_table_size);
2026-06-27 19:17:07 -05:00
Print(value[final_index]);
2026-06-27 17:51:10 -05:00
}
//+------------------------------------------------------------------+
2026-06-27 19:17:07 -05:00