From b77e4e0127fa84e150e39ccd484fdcf49be18891 Mon Sep 17 00:00:00 2001 From: Jonny007-MKD <1-23-4-5@web.de> Date: Tue, 7 Jan 2014 18:05:55 +0100 Subject: [PATCH] Gray lookup table --- Hazard/Hazard/Tools.cpp | 21 +++++++++++++++++++-- Hazard/Hazard/Tools.h | 4 ++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Hazard/Hazard/Tools.cpp b/Hazard/Hazard/Tools.cpp index 42beb6e..919b47c 100644 --- a/Hazard/Hazard/Tools.cpp +++ b/Hazard/Hazard/Tools.cpp @@ -3,6 +3,10 @@ using namespace std; +extern uint numElements; + +uint* Tools::GrayToBinaryTable = NULL; + // convert the binary representation of x to a string with the specified length char* Tools::BinaryToChars(uint x, char length) { @@ -20,9 +24,22 @@ char* Tools::BinaryToChars(uint x, char length) return c; } -// converts a gray number to its binary representation -// yes, this is quite slow and we could generate a lookup table to speed it up. uint Tools::GrayToBinary(uint x) +{ + if (Tools::GrayToBinaryTable == NULL) + Tools::InitGrayToBinaryTable(); + return Tools::GrayToBinaryTable[x]; +} + +void Tools::InitGrayToBinaryTable() +{ + Tools::GrayToBinaryTable = new uint[numElements]; + for (uint i = 0; i < numElements; i++) + Tools::GrayToBinaryTable[i] = Tools::CalcGrayToBinary(i); +} + +// converts a gray number to its binary representation +uint Tools::CalcGrayToBinary(uint x) { uint x1 = x; char r = 0; // r = ceil(ld(x)) diff --git a/Hazard/Hazard/Tools.h b/Hazard/Hazard/Tools.h index e4ebca0..e084739 100644 --- a/Hazard/Hazard/Tools.h +++ b/Hazard/Hazard/Tools.h @@ -6,4 +6,8 @@ public: static char* Tools::BinaryToChars(uint x, char length); static uint GrayToBinary(uint x); +private: + static void InitGrayToBinaryTable(); + static uint* GrayToBinaryTable; + static uint Tools::CalcGrayToBinary(uint x); }; \ No newline at end of file