Ranges longer than size_t.max

Peter Alexander peter.alexander.au at gmail.com
Sat Dec 29 13:13:26 PST 2012


On Saturday, 29 December 2012 at 20:59:56 UTC, Stewart Gordon 
wrote:
> On 29/12/2012 01:18, Peter Alexander wrote:
>> My question now is what to do with really large ranges? For 
>> example, if
>> we have a range of permutations of the elements in a set, when 
>> the set
>> is larger than 21 the number of permutations is 21 factorial, 
>> which is
>> over 2^64. As far as I see, there are three options:
>
> They look like four to me.

It's also three, for large values of three :-)


> If we define a length that may overflow, then sooner or later 
> some code will be called on it that calls hasLength on the 
> range, finds it to be true, and then relies on length and 
> malfunctions because the value returned doesn't match the 
> actual length.

Using this logic, chain shouldn't have length either (it can 
overflow, as I demonstrated in the original post), but it does, 
and it should. There are many ranges in Phobos that may overflow 
in length.

The problem with permutations is that, for the kinds of 
permutations you are likely to use often, length will not 
overflow. It would be really useful to have length available when 
this is the case.



More information about the Digitalmars-d mailing list