nextPermutation: why possible for dchar but not for char?

Ivan Kazmenko gassa at mail.ru
Sat Dec 28 14:58:39 PST 2013


On Saturday, 28 December 2013 at 22:55:39 UTC, Ivan Kazmenko 
wrote:
> Another quick question, two of them.
>
> 1. This is a minimal example of trying the permutations of a 
> character array.
>
> -----
> import std.algorithm;
> void main () {
>     char [] a;
>     do { } while (nextPermutation(a));
> }
> -----
>
> This gives a compile error.  However, it works when I change 
> "char [] a" to "dchar [] a".  Why?
>
> I understand that permuting a char [] array might be wrong way 
> to go when dealing with Unicode.  But what if, at this point of 
> the program, I am sure I'm dealing with ASCII and just want 
> efficiency?  Should I convert to ubyte [] somehow - what's the 
> expected way then?  The "cast (ubyte [])" works, but "to!(ubyte 
> [])" fails at runtime, expecting a string representation of the 
> array, not its raw contents.
>
> 2. Why does nextPermutation hang up for empty arrays?  I 
> suppose that's a bug?
>
> Ivan Kazmenko.

Also, the example at
http://dlang.org/phobos/std_algorithm.html#nextPermutation
is wrong:
while (nextPermutation(a)) { }
should in fact be
do { } while (nextPermutation(a));
as above, or we miss the very first permutation.


More information about the Digitalmars-d-learn mailing list