Created
August 13, 2019 15:38
-
-
Save IISResetMe/1449be473961c3a25ef12beaee7c5b9b to your computer and use it in GitHub Desktop.
Lazy range generator for `[bigint]`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BigIntRange : System.Collections.IEnumerable | |
{ | |
[bigint]$from | |
[bigint]$to | |
[bool]$Descending | |
BigIntRange([bigint]$from,[bigint]$to) | |
{ | |
$this.from = $from | |
$this.to = $to | |
} | |
[System.Collections.IEnumerator] | |
GetEnumerator() | |
{ | |
return [BigIntEnumerator]::new($this.from,$this.to) | |
} | |
} | |
class BigIntEnumerator : System.Collections.IEnumerator | |
{ | |
[bigint]$from | |
[bigint]$to | |
[bigint]$value | |
[bool]$Descending | |
BigIntEnumerator([bigint]$from,[bigint]$to) | |
{ | |
$this.Descending = $to -lt $from | |
$this.to = $to | |
$this.from = $from | |
$this.Reset() | |
} | |
[object] | |
get_Current() | |
{ | |
return $this.value | |
} | |
[bool] | |
MoveNext() | |
{ | |
if($this.Descending){ | |
if($this.value -le $this.to){ | |
return $false | |
} | |
} | |
else{ | |
if($this.value -ge $this.to){ | |
return $false | |
} | |
} | |
if($this.Descending){ | |
if($this.value -gt $this.to){ | |
$this.value -= 1 | |
} | |
} | |
else{ | |
if($this.value -lt $this.to){ | |
$this.value += 1 | |
} | |
} | |
return $true | |
} | |
[void] | |
Reset() | |
{ | |
if($this.Descending){ | |
$this.value = $this.from + 1 | |
} | |
else{ | |
$this.value = $this.from - 1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment