Lib to generate perfect hashes
Найти файл
Файлы репозитория (в начале последний коммит)
Имя файла Текст последнего коммита Дата последнего коммита
Nique_372 cfa177aba3
2026-08-01 18:33:10 -05:00
Src 2026-07-31 12:06:51 -05:00
Test 2026-07-28 21:41:05 -05:00
dependencies.json new files added 2026-06-27 19:17:07 -05:00
LICENSE new files added 2026-06-27 19:17:07 -05:00
PerfectHashByLeo.mqproj Generated by MQL5 Wizard 2026-06-27 15:09:34 -05:00
README.md 2026-08-01 18:33:10 -05:00

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 — THasher0 buckets the keys, THasher1 places them in the final table given a per-bucket seed. Any type/hasher pair works as long as it exposes a static Hash().
  • Bucket displacement algorithm: keys are grouped into buckets by THasher0::Hash(key), buckets are sorted largest-first, and for each bucket a seed d is searched (0, 1, 2, ... up to MaxValSeed()) such that THasher1::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


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