Compile-Time std::map

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Sat Apr 28 05:31:02 PDT 2007


NN wrote:

> Jari-Matti Mäkelä Wrote:
> 
>> NN wrote:
>> > Jari-Matti Mäkelä Wrote:
>> >> NN wrote:
>> >> > I allways wanted to create std::map but in compile time.
>> >> > 
>> >> > The problem:
>> >> > I want to write smth like:
>> >> > int[int] x = {3:3,2:2,1:1}
>> >> > 
>> >> > But compiler will generate the following:
>> >> > int[int] x = {1:1,2:2,3:3}
>> >> > 
>> >> > Thus I can use binary search on x.
>> >> > 
>> >> > Is it possible to do in D ?
>> >> > Thanx.
>> >> 
>> >> Well, in case you didn't notice, DMD 1.014 now includes hash literals.
>> >> Assigning static hashes aren't yet supported directly, but you can do
>> >> simple cases like
>> >> 
>> >>   int val = ([1:11,2:22,3:33][1]);
>> >> 
>> 
>> > But it does not do what I want.
>> > I want to rearange the values in compile time.
>> 
>> Oh, sorry. If you just want to rearrange the array contents on compile
>> time, you can do it with a CTFE function or with tuples, then assign the
>> resulting tuple into an array.
> 
> OK.
> If i would write:
> int[int] a = f([2:2, 1:1, 3:3]);
> 
> Won't it create [2:2, 1:1, 3:3] and [1:1, 2:2, 3:3] ?
> Or the compiler will remove the first array ?

Can you please show some use cases. I'm not really sure what you are trying
to do here. :) Do you need to be able to add new keys at runtime? The
builtin associative array uses quite optimal data structures so don't need
to worry about them unless you need extreme performance.



More information about the Digitalmars-d mailing list