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
|
2024-02-07 19:13:27 +00:00
|
|
|
StrategyType* = enum
|
|
|
|
# Simplest approach:
|
|
|
|
# 0 => 0, 1, 2
|
|
|
|
# 1 => 3, 4, 5
|
|
|
|
# 2 => 6, 7, 8
|
|
|
|
LinearStrategy,
|
|
|
|
|
|
|
|
# Stepped indexing:
|
|
|
|
# 0 => 0, 3, 6
|
|
|
|
# 1 => 1, 4, 7
|
|
|
|
# 2 => 2, 5, 8
|
|
|
|
SteppedStrategy
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
# 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-02-07 19:13:27 +00:00
|
|
|
IndexingStrategy* = object
|
|
|
|
strategyType*: StrategyType
|
2024-01-08 22:52:46 +00:00
|
|
|
firstIndex*: int # Lowest index that can be returned
|
|
|
|
lastIndex*: int # Highest index that can be returned
|
2024-02-07 19:13:27 +00:00
|
|
|
iterations*: int # getIndices(iteration) will run from 0 ..< iterations
|
2024-01-08 22:52:46 +00:00
|
|
|
step*: int
|
|
|
|
|
2024-02-07 19:13:27 +00:00
|
|
|
func checkIteration(self: IndexingStrategy, iteration: int): void {.raises: [IndexingError].} =
|
|
|
|
if iteration >= self.iterations:
|
2024-01-15 16:45:04 +00:00
|
|
|
raise newException(
|
|
|
|
IndexingError,
|
2024-02-07 19:13:27 +00:00
|
|
|
"Indexing iteration can't be greater than or equal to iterations.")
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-06-10 22:47:29 +00:00
|
|
|
func getIter(first, last, step: int): Iter[int] =
|
|
|
|
{.cast(noSideEffect).}:
|
|
|
|
Iter[int].new(first, last, step)
|
2024-01-15 16:45:04 +00:00
|
|
|
|
2024-02-07 19:13:27 +00:00
|
|
|
func getLinearIndicies(
|
|
|
|
self: IndexingStrategy,
|
2024-01-15 16:45:04 +00:00
|
|
|
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-02-07 19:13:27 +00:00
|
|
|
func getSteppedIndicies(
|
|
|
|
self: IndexingStrategy,
|
2024-01-15 16:45:04 +00:00
|
|
|
iteration: int): Iter[int] {.raises: [IndexingError].} =
|
|
|
|
self.checkIteration(iteration)
|
|
|
|
|
|
|
|
let
|
|
|
|
first = self.firstIndex + iteration
|
|
|
|
last = self.lastIndex
|
|
|
|
|
2024-02-07 19:13:27 +00:00
|
|
|
getIter(first, last, self.iterations)
|
|
|
|
|
|
|
|
func getIndicies*(
|
|
|
|
self: IndexingStrategy,
|
|
|
|
iteration: int): Iter[int] {.raises: [IndexingError].} =
|
|
|
|
|
|
|
|
case self.strategyType
|
|
|
|
of StrategyType.LinearStrategy:
|
|
|
|
self.getLinearIndicies(iteration)
|
|
|
|
of StrategyType.SteppedStrategy:
|
|
|
|
self.getSteppedIndicies(iteration)
|
|
|
|
|
|
|
|
func init*(
|
|
|
|
strategy: StrategyType,
|
|
|
|
firstIndex, lastIndex, iterations: int): IndexingStrategy {.raises: [IndexingError].} =
|
2024-01-15 16:45:04 +00:00
|
|
|
|
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-02-07 19:13:27 +00:00
|
|
|
if iterations <= 0:
|
2024-01-15 16:45:04 +00:00
|
|
|
raise newException(
|
|
|
|
IndexingWrongIterationsError,
|
2024-02-07 19:13:27 +00:00
|
|
|
"iterations (" & $iterations & ") must be greater than zero.")
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-07 19:13:27 +00:00
|
|
|
IndexingStrategy(
|
|
|
|
strategyType: strategy,
|
2024-01-08 22:52:46 +00:00
|
|
|
firstIndex: firstIndex,
|
|
|
|
lastIndex: lastIndex,
|
2024-02-07 19:13:27 +00:00
|
|
|
iterations: iterations,
|
|
|
|
step: divUp((lastIndex - firstIndex), iterations))
|