O(1) dictionaries

renoX renosky at free.fr
Sat Nov 17 12:35:27 PST 2007


mandel a écrit :
> 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";
>     //[...]
> }

Quite some times ago, there was a thread on 'reflective enums' where 
Kevin Bealer, I and other tried to make better enums, you could use the 
code as a basis of your work.

Note that I don't think much of the results: D isn't flexible enough for 
this .. at least currently.

Regards,
renoX









More information about the Digitalmars-d mailing list