BigInt foreach loop
    Steven Schveighoffer via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Fri Aug  4 06:09:55 PDT 2017
    
    
  
On 8/4/17 8:49 AM, Q. Schroll wrote:
> One can do
>    BigInt n = returnsBigInt();
>    foreach (i; BigInt(0) .. n)
>    { .. }
> How is this implemented? The code for BigInt is very large and I didn't 
> find it.
> And is it more efficient than
>    for (BigInt i = 0; i < n; ++i)
>    { .. }
Any foreach range statement like this:
foreach(var; A .. B)
is treated as if you wrote:
for(auto var = A; var < B; ++var)
So it's pretty much exactly like what you wrote, just the initializer is 
different but the result is the same.
 > as incrementing is a costly operation?
Here is increment in bigint:
https://github.com/dlang/phobos/blob/master/std/bigint.d#L563
And addOrSubInt:
https://github.com/dlang/phobos/blob/master/std/internal/math/biguintcore.d#L508
I think there is room for improvement, as incrementing or decrementing 
by 1 is probably something that can be optimized.
-Steve
    
    
More information about the Digitalmars-d-learn
mailing list