2024-01-08 22:52:46 +00:00
|
|
|
import std/sequtils
|
2024-01-15 16:45:04 +00:00
|
|
|
|
|
|
|
import ./errors
|
2024-01-08 22:52:46 +00:00
|
|
|
import ./utils
|
2024-01-15 16:45:04 +00:00
|
|
|
import ./utils/asynciter
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-15 16:45:04 +00:00
|
|
|
{.push raises: [].}
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
# Representing a strategy for grouping indices (of blocks usually)
|
|
|
|
# Given an interation-count as input, will produce a seq of
|
|
|
|
# selected indices.
|
2024-01-15 16:45:04 +00:00
|
|
|
|
|
|
|
IndexingError* = object of CodexError
|
|
|
|
IndexingWrongIndexError* = object of IndexingError
|
|
|
|
IndexingWrongIterationsError* = object of IndexingError
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
IndexingStrategy* = ref object of RootObj
|
|
|
|
firstIndex*: int # Lowest index that can be returned
|
|
|
|
lastIndex*: int # Highest index that can be returned
|
|
|
|
numberOfIterations*: int # getIndices(iteration) will run from 0 ..< numberOfIterations
|
|
|
|
step*: int
|
|
|
|
|
|
|
|
# Simplest approach:
|
|
|
|
# 0 => 0, 1, 2
|
|
|
|
# 1 => 3, 4, 5
|
|
|
|
# 2 => 6, 7, 8
|
|
|
|
LinearIndexingStrategy* = ref object of IndexingStrategy
|
|
|
|
|
|
|
|
# Stepped indexing:
|
|
|
|
# 0 => 0, 3, 6
|
|
|
|
# 1 => 1, 4, 7
|
|
|
|
# 2 => 2, 5, 8
|
|
|
|
SteppedIndexingStrategy* = ref object of IndexingStrategy
|
|
|
|
|
2024-01-15 16:45:04 +00:00
|
|
|
proc checkIteration(
|
|
|
|
self: IndexingStrategy,
|
|
|
|
iteration: int): void {.raises: [IndexingError].} =
|
2024-01-08 22:52:46 +00:00
|
|
|
if iteration >= self.numberOfIterations:
|
2024-01-15 16:45:04 +00:00
|
|
|
raise newException(
|
|
|
|
IndexingError,
|
|
|
|
"Indexing iteration can't be greater than or equal to numberOfIterations.")
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-11 21:13:55 +00:00
|
|
|
|
|
|
|
method getIndices*(
|
2024-01-15 16:45:04 +00:00
|
|
|
self: IndexingStrategy,
|
|
|
|
iteration: int): Iter[int] {.base, raises: [IndexingError].} =
|
2024-01-08 22:52:46 +00:00
|
|
|
raiseAssert("Not implemented")
|
|
|
|
|
2024-01-15 16:45:04 +00:00
|
|
|
proc getIter(first, last, step: int): Iter[int] =
|
|
|
|
var
|
|
|
|
finish = false
|
|
|
|
cur = first
|
|
|
|
proc get(): int =
|
|
|
|
result = cur
|
|
|
|
cur += step
|
|
|
|
|
|
|
|
if cur > last:
|
|
|
|
finish = true
|
|
|
|
|
|
|
|
proc isFinished(): bool =
|
|
|
|
finish
|
|
|
|
|
|
|
|
Iter.new(get, isFinished)
|
|
|
|
|
2024-01-16 20:51:30 +00:00
|
|
|
method getIndices*(
|
2024-01-15 16:45:04 +00:00
|
|
|
self: LinearIndexingStrategy,
|
|
|
|
iteration: int): Iter[int] {.raises: [IndexingError].} =
|
|
|
|
|
|
|
|
self.checkIteration(iteration)
|
|
|
|
|
|
|
|
let
|
|
|
|
first = self.firstIndex + iteration * (self.step + 1)
|
|
|
|
last = min(first + self.step, self.lastIndex)
|
|
|
|
|
|
|
|
getIter(first, last, 1)
|
|
|
|
|
2024-01-16 20:51:30 +00:00
|
|
|
method getIndices*(
|
2024-01-15 16:45:04 +00:00
|
|
|
self: SteppedIndexingStrategy,
|
|
|
|
iteration: int): Iter[int] {.raises: [IndexingError].} =
|
|
|
|
|
|
|
|
self.checkIteration(iteration)
|
|
|
|
|
|
|
|
let
|
|
|
|
first = self.firstIndex + iteration
|
|
|
|
last = self.lastIndex
|
|
|
|
|
|
|
|
getIter(first, last, self.numberOfIterations)
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type IndexingStrategy,
|
|
|
|
firstIndex, lastIndex, numberOfIterations: int): T {.raises: [IndexingError].} =
|
2024-01-08 22:52:46 +00:00
|
|
|
if firstIndex > lastIndex:
|
2024-01-15 16:45:04 +00:00
|
|
|
raise newException(
|
|
|
|
IndexingWrongIndexError,
|
|
|
|
"firstIndex (" & $firstIndex & ") can't be greater than lastIndex (" & $lastIndex & ")")
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
if numberOfIterations <= 0:
|
2024-01-15 16:45:04 +00:00
|
|
|
raise newException(
|
|
|
|
IndexingWrongIterationsError,
|
|
|
|
"numberOfIteration (" & $numberOfIterations & ") must be greater than zero.")
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
T(
|
|
|
|
firstIndex: firstIndex,
|
|
|
|
lastIndex: lastIndex,
|
|
|
|
numberOfIterations: numberOfIterations,
|
2024-01-17 19:24:34 +00:00
|
|
|
step: divUp((lastIndex - firstIndex), numberOfIterations))
|