TsnTables/Src/Bits/H.mqh
2026-09-19 22:49:12 -05:00

217 lines
7.1 KiB
MQL4

//+------------------------------------------------------------------+
//| H.mqh |
//| Copyright 2026, Niquel Mendoza |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza"
#property link "https://www.mql5.com"
#property strict
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#ifndef TSNTABES_SRC_BITS_H_MQH
#define TSNTABES_SRC_BITS_H_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// Clase statica de bick tricks del ecoistema
// aqui se ubican funciones de los bicktricts
// aqiu solo estaran las funciones "complejas, osea que requiern muchas lineas.. y no
// tendria mucho snrido o ensuciara la lecutra el usar #define \\\ extendido..
// las que no estan aqui (pro jeemplok CTZ) oi compact etc
// estan como defines (maxima velocida)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CBitTricks
{
public:
CBitTricks(void) {}
~CBitTricks(void) {}
//--- CLZ
static int CLZ(ulong x);
static int CLZ(long x);
static int CLZ(datetime x);
static int CLZ(color x);
static int CLZ(int x);
static int CLZ(uint x);
static int CLZ(ushort x);
static int CLZ(short x);
static int CLZ(char x);
static int CLZ(uchar x);
//--- Count digits binary
static int CountDigitsBinary(const ulong v);
static int CountDigitsBinary(const uint v);
static int CountDigitsBinary(const ushort v);
static int CountDigitsBinary(const uchar v);
//---
template <typename T>
static string ToBitsBE(T v);
//---
template <typename T>
static bool ByteHighNumberEstaPrendido(T x);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// Aqui iran las definciiones H (header)
// y tambien el cuerpo de alguans funciones que no reuqin explciacion o algo mas complejo tipo tablas..
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template <typename T>
static string CBitTricks::ToBitsBE(T v)
{
string str;
StringInit(str, ((sizeof(T) << 3)), '0'); // reservamos una vez...
for(int i = (sizeof(T) << 3) - 1 ; i >= 0; i--)
{
if((v & (1ULL << i)) != 0)
str.SetChar(i, '1');
}
return str;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// Al final probadno descrubri que esto es mas rapido uqe usar CLZ y luego encontrar eel indice y eso
// asi que lo usare...
template <typename T>
static bool CBitTricks::ByteHighNumberEstaPrendido(T x)
{
//---
if(x == 0)
return false;
//---
T valor = x;
int idx = 0;
if(valor >= 0x100000000)
{
idx += 4;
valor >>= 32;
}
if(valor >= 0x10000)
{
idx += 2;
valor >>= 16;
}
if(valor >= 0x100)
{
idx += 1;
}
//---
return (uchar((x >> (idx << 3))) & 0x80) != 0;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
static int CBitTricks::CountDigitsBinary(const ulong v)
{
if(v < 10000000000ULL)
{
if(v < 100000ULL)
{
if(v < 100ULL)
return (v < 10ULL) ? 1 : 2;
if(v < 1000ULL)
return 3;
return (v < 10000ULL) ? 4 : 5;
}
if(v < 10000000ULL)
return (v < 1000000ULL) ? 6 : 7;
if(v < 1000000000ULL)
return (v < 100000000ULL) ? 8 : 9;
return 10;
}
if(v < 1000000000000000ULL) // 11 a 15
{
if(v < 1000000000000ULL)
return (v < 100000000000ULL) ? 11 : 12;
if(v < 100000000000000ULL)
return (v < 10000000000000ULL) ? 13 : 14;
return 15;
}
if(v < 100000000000000000ULL)
return (v < 10000000000000000ULL) ? 16 : 17;
if(v < 10000000000000000000ULL)
return (v < 1000000000000000000ULL) ? 18 : 19;
return 20;
}
//+------------------------------------------------------------------+
static int CBitTricks::CountDigitsBinary(const uint v)
{
if(v < 100000U)
{
if(v < 100U)
return (v < 10U) ? 1 : 2;
if(v < 1000U)
return 3;
return (v < 10000U) ? 4 : 5;
}
if(v < 10000000U)
return (v < 1000000U) ? 6 : 7;
if(v < 1000000000U)
return (v < 100000000U) ? 8 : 9;
return 10;
}
//+------------------------------------------------------------------+
static int CBitTricks::CountDigitsBinary(const ushort v)
{
if(v < 100)
return (v < 10) ? 1 : 2;
if(v < 1000)
return 3;
return (v < 10000) ? 4 : 5;
}
//+------------------------------------------------------------------+
static int CBitTricks::CountDigitsBinary(const uchar v)
{
if(v < 10)
return 1;
if(v < 100)
return 2;
return 3;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// 1. Notas estuve trabadto de usar StrginSetLenght
// Y note que no funciaon pro lo visto esa funcion solo cambia el "integer"
// que reprensetar el len.. pero solo reduce no aumenta ni nada
// como truncar...
// 2. StringInit por su parte lo qeu hace es inicar y reservar memoria de verdad
// pero por lo visto hay un tema el \0 es un caracter qeu note que cuyando lo usabmos
// en cunficones "de bajo" nivel como SetChar o StringInit (donde trabajsmo diremate )
// con su buffer (no con una api "amigable" += "" o algo asi)
// hay que tener cuidado con el \0 dado qeu visualemte se corta el print
// (en += "\0") pero la logitud len sigue bien, si lo imrres luego en SetChar parece que el len solo apunta a la parte
// antes del \0 parece uqe string intemante no es un mescanis buffer* len si no mas
// se guia tambien del \0 pero es raro eso..
}
//+------------------------------------------------------------------+
#endif // TSNTABES_SRC_BITS_H_MQH