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

77 lines
3 KiB
MQL5

//+------------------------------------------------------------------+
//| Ref.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_REF_MQH
#define FASTCOLLECTIONSBYLEO_COLHASH_HASHMAP_REF_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "Main.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#define CHashMapFastR(k, v) TSN::CHashMapFastRef<k, v, TSN::CGenericHash_##k>
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename TKey, typename TValue, typename TBase>
class CHashMapFastRef : public CHashMapFastBase<TKey, TValue, TBase>
{
public:
CHashMapFastRef(int initial_size = 128) : CHashMapFastBase<TKey, TValue, TBase>(initial_size) {}
~CHashMapFastRef(void) {}
//---
using CFastHashTableBase<CHashMapFastInternalPar<TKey, TValue>, TKey, TBase>::Add;
//---
__forceinline bool Add(const TKey& key, TValue& v);
bool TrySet(const TKey& key, TValue& value);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename TKey, typename TValue, typename TBase>
bool CHashMapFastRef::TrySet(const TKey& key, TValue& value)
{
//---
const int slot = Find(key);
if(slot == -1)
return false;
m_table[slot].value = value;
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename TKey, typename TValue, typename TBase>
__forceinline bool CHashMapFastRef::Add(const TKey& key, TValue& v)
{
if(Add(key))
{
m_table[m_last_created_pos].value = v;
return true;
}
return false;
}
}
//+------------------------------------------------------------------+
#endif // FASTCOLLECTIONSBYLEO_COLHASH_HASHMAP_REF_MQH