O(1) dictionaries

mandel oh at no.es
Tue Nov 13 06:58:57 PST 2007


Hi,

I have made a dictionary that way:
enum Word { Hello, Bye };
char[][Word] dict;

static this()
{
   dict = [
     Word.Hello : "Hello",
     Word.Bye : "Bye"
   ];
}

Now there are two problems I have with this:

- the dict is an AA, but an array would be much faster,
  but it is not possible to assign it via an array literal
  with a mixed order and supplied keys.
char[][Word.max] dict = [Word.Hello : "Hello", ...]

- in place initialisation with a literal is not possible.
  Instead, the use of "static this" makes the code look long winded.

Does anyone have some nice workaround for these limitations?

The current solution is long winded (ok, I can make the name shorter),
but a literal would be nicer much.
static this()
{
    english_dictionary[ Word.Hello] = "Hello";
    //[...]
}



More information about the Digitalmars-d mailing list