Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created August 13, 2019 15:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/1449be473961c3a25ef12beaee7c5b9b to your computer and use it in GitHub Desktop.
Save IISResetMe/1449be473961c3a25ef12beaee7c5b9b to your computer and use it in GitHub Desktop.
Lazy range generator for `[bigint]`
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