On Thursday, 13 August 2015 at 19:26:12 UTC, Adam D. Ruppe wrote:
On Thursday, 13 August 2015 at 19:13:55 UTC, D_Learner wrote:
I was wondering how I could change the code below such the
`bmBc` is computed at compile time .
It is currently not possible to build an associative array at
compile time and keep it as a runtime table due to the
implementation.
However, looking at your code, you don't need a full AA... the
key is a single char, right?
Do something like this:
int[128] bmBc = calculate(); // ....
And then a line like this should work to set it in the
calculate function:
bmBc[pattern[i]] = i-1;
Adjust this code to fit you:
int[128] a = calc();
enum string pattern = "abc";
int[128] calc() {
int[128] a; // local copy to work on as we build
a[pattern[0]] = 0;
return a; // return the completed table
}
This works because individual characters have a numeric value.
If they are all A-Z, you can easily fit them in a small array
and access them even faster than an associative array (doesn't
even have to be full size 128 if the only keys are the letters).
And these can be built at CTFE and stored in the program's data
segment.
Thanks, the 128 is fine since I was considering the whole ASCII
character set . I was a bit skeptical of writing the code in that
manor though since I have to still compare runtime and
compiletime implementation performance(benchmarks ). That is why
my code seemed to mimic templating . Maybe the question would
then be, how do I force this function to be fully executed at
compiletime and later make it be executed at runntime ?