Fixed-size arrays and randomShuffle()

Ali Çehreli acehreli at yahoo.com
Thu May 3 07:02:00 PDT 2012


On 05/03/2012 06:55 AM, Vidar Wahlberg wrote:
 > On 2012-05-03 15:34, Ali Çehreli wrote:
 >> Fixed-length arrays are value types. 'a' is copied to randomShuffle() so
 >> its copy is shuffled. Passing a slice of the whole array works:
 >>
 >> randomShuffle(a[]);
 >
 > True, it is however still not exceptionally newbie (or perhaps even
 > user?) friendly (my question was more of "does it have to be this way?"
 > rather than "how do you do this?", even though I appreciate the answer
 > on how to do it).
 > Is it not possible for the compilator to let you know that what you're
 > doing doesn't make any sense?

Random shuffle can work on a fixed-length array and there is a way for 
the implementation to know:

import std.traits;
// ...
   __traits(isStaticArray, a)

That can be used in a template constraint.

 > A quick follow-up:
 > I've tried some various random number engines, but neither come even
 > close to the performance of whatever is used for Java's
 > "Collection.shuffle()" method. Perhaps someone can shed some light on 
this?

I have no idea with that one.

Ali



More information about the Digitalmars-d-learn mailing list