TsnTables/Src/Swar/TableConstant.mq5
2026-06-18 07:20:48 -05:00

103 lines
3 KiB
MQL5

//+------------------------------------------------------------------+
//| TableConstant.mq5 |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/"
#property version "1.00"
#property strict
#property script_show_inputs
//---
input string InpTablaName = "g_table";
input string InpConstantName = "constante";
input string InpFileName = "Tables\\out.txt";
input bool InpCommonFlag = true; // Common
//---
#define NUM_VENTANA 6
#define LEN (1 << NUM_VENTANA)
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
int secuencias[LEN] = {0};
int usadas[LEN] = {0};
int pos = NUM_VENTANA - 1;
//---
int estado = 0;
for(int i = 0; i < LEN - (NUM_VENTANA - 1); i++)
{
int ventana = (estado << 1 | 1) & (LEN - 1);
if(!usadas[ventana])
{
secuencias[pos++] = 1;
usadas[ventana] = 1;
estado = ventana;
}
else
{
int ventana_con_0 = (estado << 1 | 0) & (LEN - 1);
secuencias[pos++] = 0;
usadas[ventana_con_0] = 1;
estado = ventana_con_0;
}
}
//---
printf("Secuencia: ");
string str = "";
for(int i = 0; i < LEN; i++)
str += string(secuencias[i]);
printf(str);
//---
uint64_t constante = 0;
for(int i = 0; i < LEN; i++)
{
constante = (constante << 1) | secuencias[i];
}
printf("Constante (hex): 0x%llX\n", constante);
//---
int tabla[LEN];
for(int i = 0; i < LEN; i++)
{
uint64_t bit_aislado = 1ULL << i;
int indice = int((bit_aislado * constante) >> 58);
tabla[indice] = i;
}
// Generar tabla
::ResetLastError();
const int fh = FileOpen(InpFileName, FILE_WRITE | FILE_TXT | FILE_ANSI | (InpCommonFlag ? FILE_COMMON : 0));
if(fh == INVALID_HANDLE)
{
PrintFormat("Error al abrir el archivo = %s, utlimo err = %d", InpFileName, ::GetLastError());
return;
}
FileWrite(fh, StringFormat("const ulong %s = %I64uULL;", InpConstantName, constante));
FileWrite(fh, StringFormat("const char %s[64] = {", InpTablaName));
for(int i = 0; i < LEN - 1; i++)
{
FileWrite(fh, StringFormat("%d,", tabla[i]));
}
FileWrite(fh, StringFormat("%d };", tabla[ LEN - 1]));
FileClose(fh);
}
//+------------------------------------------------------------------+
// 0000011111101111001110101110001101101001100101100001010100010010
// Ejemplo para i = 1
// 0000011111101111001110101110001101101001100101100001010100010010
// Se le aplica mul: 2 = (1 << 1) mul por 2 es igual a << 1
// 0000111111011110011101011100011011010011001011000010101000100100
// Dezplaza 58 bit qeuidan solo 6 bit (Este creo que seira la ventana 1)
// 000011