Lib to generate perfect hashes
- MQL5 100%
| Имя файла | Текст последнего коммита | Дата последнего коммита |
|---|---|---|
| Src | ||
| Test | ||
| dependencies.json | ||
| LICENSE | ||
| PerfectHashByLeo.mqproj | ||
| README.md | ||
Perfect hash generator for MQL5, based on the bucket displacement (CHD-style) technique.
Given a fixed set of keys,
builds per-bucket seeds so lookups resolve in O(1) with zero collisions.
Main Features
CPerfectHashByLeo<TKey, THasher0, THasher1>: generic over the key type and over two pluggable hashers —THasher0buckets the keys,THasher1places them in the final table given a per-bucket seed. Any type/hasher pair works as long as it exposes a staticHash().- Bucket displacement algorithm: keys are grouped into buckets by
THasher0::Hash(key), buckets are sorted largest-first, and for each bucket a seeddis searched (0, 1, 2, ...up toMaxValSeed()) such thatTHasher1::Hash(key, d)lands every key in that bucket on a free, non-colliding slot of the final table. Run(): builds the perfect hash for keys only — returns the per-bucket seeds and the keys reordered into their final table positions.RunWValue<TValue>(): same algorithm, but also reorders a parallel array of values alongside the keys — this is the form used by BasesParserSLan to build JIT perfect-hash tables for JSON/YAML object keys.- Tunable via
InitAlg(size_keys, factor_f, elementos_por_bucket): controls the final table load factor and average bucket size, trading memory for a higher chance of finding a displacement seed quickly.
Usage example
// TSN MQH Wrapers Style
#include <TSN\\PHash\\Main.mqh>
class CHasherFnv164
{
public:
static ulong Hash(const string& v)
{
ulong h = 0xcbf29ce484222325;
for(uint i = 0; i < v.Length(); i++) { h ^= (uchar)v[i]; h *= 0x100000001b3; }
return h;
}
};
class CHasherFinal
{
public:
static ulong Hash(const string& v, const ulong seed)
{
ulong h = 0xcbf29ce484222325 ^ seed;
for(uint i = 0; i < v.Length(); i++) { h ^= (uchar)v[i]; h *= 0x100000001b3; }
return h;
}
};
void OnStart()
{
const string keys[5] = {"hola", "valor", "si", "como", "no"};
const double val[5] = {10.0, 20.0, 30.0, 40.0, 50.0};
TSN::CPerfectHashByLeo<string, CHasherFnv164, CHasherFinal> pf;
pf.InitAlg(5, 0.50, 2); // 5 keys, load factor 0.50, ~2 keys por bucket
pf.MaxValSeed(15000ULL);
string out_keys[];
ulong seed[];
double value[];
ArrayResize(value, pf.m_final_table_size);
ArrayResize(out_keys, pf.m_final_table_size);
if(pf.RunWValue(keys, val, seed, value, out_keys))
{
// Lookup: hashea con THasher0 para el bucket, toma su seed, hashea con THasher1
const int bucket = int(CHasherFnv164::Hash("valor") & pf.m_buckets_size_last);
const int final_index = int(CHasherFinal::Hash("valor", seed[bucket]) & pf.m_final_table_size_last);
Print(value[final_index]); // 20.0
}
}
Repository Structure
PerfectHashByLeo/
├── Src/ # CPerfectHashByLeo y definiciones internas
└── Test/ # Script de prueba/ejemplo de uso
Requirements
- See dependencies.json
- MetaTrader 5, build 5430+
Installation
cd "C:\Users\YOUR_USER\AppData\Roaming\MetaQuotes\Terminal\YOUR_ID\MQL5\Shared Projects"
tsndep install "https://forge.mql5.io/nique_372/PerfectHashByLeo.git"
Requires the tsndep package, available on PyPI. It automatically downloads and installs all declared dependencies.
Quick Start
1. Include the library:
// TSN MQH Wrapers Style
#include <TSN\\PHash\\Main.mqh>
2. Define two hashers (bucket + final) and run it:
TSN::CPerfectHashByLeo<string, CHasherFnv164, CHasherFinal> pf;
pf.InitAlg(size_keys, 0.70, 4);
pf.Run(keys, seeds, out_keys);
License
Read Full License By downloading or using this repository, you accept the license terms.
Contact
- Platform: MQL5 Community
- Profile: https://www.mql5.com/es/users/nique_372
- Articles: https://www.mql5.com/es/users/nique_372/publications