FastCollections-FastHashMap/Src/ColHash/HashMap/Main.mqh
2026-07-21 11:09:28 -05:00

141 lines
5.4 KiB
MQL5

//+------------------------------------------------------------------+
//| Main.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/"
#property strict
#ifndef FASTCOLLECTIONSBYLEO_COLHASH_HASHMAP_MAIN_MQH
#define FASTCOLLECTIONSBYLEO_COLHASH_HASHMAP_MAIN_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "..\\HashBases\\Main.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#define CHashMapFastIterator(k, v) TSN::CHashMapFastIteratorInt<k, v, TSN::CGenericHash_##k>
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename TKey, typename TValue, typename TBase>
struct CHashMapFastIteratorInt;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename TKey, typename TValue>
struct CHashMapFastInternalPar
{
TKey key;
TValue value;
ulong hash;
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename TKey, typename TValue, typename TBase>
class CHashMapFastBase : public CFastHashTableBase<CHashMapFastInternalPar<TKey, TValue>, TKey, TBase>
{
public:
CHashMapFastBase(int initial_size = 128) { Init(initial_size); }
~CHashMapFastBase(void) {}
//---
bool TryGet(const TKey& key, TValue& out) const;
//---
int CopyTo(TKey& keys[], TValue& values[]) const;
//---
CHashMapFastIteratorInt<TKey, TValue, TBase> BeginIteration();
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename TKey, typename TValue, typename TBase>
int CHashMapFastBase::CopyTo(TKey& keys[], TValue& values[]) const
{
ArrayResize(keys, m_count);
ArrayResize(values, m_count);
int out_idx = 0;
for(int group = 0; group < m_num_groups; group++)
{
ulong occupied_mask = (m_meta[group] & MQLARTICLES_HASHMAP_MASK_HIGH_BIT) ^ MQLARTICLES_HASHMAP_MASK_HIGH_BIT;
while(occupied_mask != 0)
{
const ulong flag = TSNTABLES_SWAR_64_DEJAR_SOLO_MINUS_SIG_BYTE(occupied_mask);
const int slot = (group << 3) + TSNTABLES_SWAR_64_GET_BYTE(flag);
keys[out_idx] = m_table[slot].key;
values[out_idx] = m_table[slot].value;
out_idx++;
occupied_mask &= (occupied_mask - 1);
}
}
return out_idx;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename TKey, typename TValue, typename TBase>
bool CHashMapFastBase::TryGet(const TKey& key, TValue& out) const
{
//---
const int slot = Find(key);
if(slot == -1)
return false;
out = m_table[slot].value;
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename TKey, typename TValue, typename TBase>
CHashMapFastIteratorInt<TKey, TValue, TBase> CHashMapFastBase::BeginIteration()
{
return CHashMapFastIteratorInt<TKey, TValue, TBase>(&this);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename TKey, typename TValue, typename TBase>
struct CHashMapFastIteratorInt : public CFastHashTableIteratorBase<CHashMapFastBase<TKey, TValue, TBase>, TKey>
{
CHashMapFastIteratorInt(CHashMapFastBase<TKey, TValue, TBase>* ptr)
: CFastHashTableIteratorBase<CHashMapFastBase<TKey, TValue, TBase>, TKey>(ptr)
{
}
CHashMapFastIteratorInt() {}
//---
__forceinline TValue Val() const { return m_ctx.m_table[m_current_slot].value; }
};
}
//+------------------------------------------------------------------+
#endif // FASTCOLLECTIONSBYLEO_COLHASH_HASHMAP_MAIN_MQH