Compiletime Table

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 13 12:26:09 PDT 2015


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.


More information about the Digitalmars-d-learn mailing list