From 2c2e42d36313bdea73561d496132530d8be215fa Mon Sep 17 00:00:00 2001 From: benbierens Date: Fri, 1 Dec 2023 11:28:36 +0100 Subject: [PATCH] Swaps in indexing strategy into erasure. --- codex/erasure/erasure.nim | 14 ++++++++++-- codex/manifest.nim | 3 ++- codex/manifest/indexingstrategy.nim | 11 ++++++---- tests/codex/testindexingstrategy.nim | 33 ++++++++++++++++------------ 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/codex/erasure/erasure.nim b/codex/erasure/erasure.nim index 19013b4d..4e9bebf1 100644 --- a/codex/erasure/erasure.nim +++ b/codex/erasure/erasure.nim @@ -127,7 +127,12 @@ proc prepareEncodingData( ## let - indicies = toSeq(countup(step, params.rounded - 1, params.steps)) + strategy = SteppedIndexingStrategy.new( + firstIndex = 0, + lastIndex = params.rounded - 1, + numberOfIterations = params.steps + ) + indicies = strategy.getIndicies(step) pendingBlocksIter = self.getPendingBlocks(manifest, indicies.filterIt(it < manifest.blocksCount)) var resolved = 0 @@ -171,7 +176,12 @@ proc prepareDecodingData( ## let - indicies = toSeq(countup(step, encoded.blocksCount - 1, encoded.steps)) + strategy = SteppedIndexingStrategy.new( + firstIndex = 0, + lastIndex = encoded.blocksCount - 1, + numberOfIterations = encoded.steps + ) + indicies = strategy.getIndicies(step) pendingBlocksIter = self.getPendingBlocks(encoded, indicies) var diff --git a/codex/manifest.nim b/codex/manifest.nim index 3cd9219e..09d8624f 100644 --- a/codex/manifest.nim +++ b/codex/manifest.nim @@ -1,4 +1,5 @@ import ./manifest/coders import ./manifest/manifest +import ./manifest/indexingstrategy -export manifest, coders +export manifest, coders, indexingstrategy diff --git a/codex/manifest/indexingstrategy.nim b/codex/manifest/indexingstrategy.nim index a28adb11..69b3f8e8 100644 --- a/codex/manifest/indexingstrategy.nim +++ b/codex/manifest/indexingstrategy.nim @@ -1,4 +1,5 @@ import std/sequtils +import ../utils type # Representing a strategy for grouping indices (of blocks usually) @@ -8,6 +9,7 @@ type 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 @@ -35,15 +37,16 @@ proc new*(T: type IndexingStrategy, firstIndex, lastIndex, numberOfIterations: i T( firstIndex: firstIndex, lastIndex: lastIndex, - numberOfIterations: numberOfIterations + numberOfIterations: numberOfIterations, + step: divUp((lastIndex - firstIndex), numberOfIterations) ) method getIndicies*(self: LinearIndexingStrategy, iteration: int): seq[int] = self.assertIteration(iteration) let - first = self.firstIndex + iteration * self.numberOfIterations - last = min(first + self.numberOfIterations, self.lastIndex) + first = self.firstIndex + iteration * self.step + last = min(first + self.step, self.lastIndex) toSeq(countup(first, last - 1, 1)) method getIndicies*(self: SteppedIndexingStrategy, iteration: int): seq[int] = @@ -51,5 +54,5 @@ method getIndicies*(self: SteppedIndexingStrategy, iteration: int): seq[int] = let first = self.firstIndex + iteration - last = first + (self.numberOfIterations * self.numberOfIterations) + last = first + (self.step * self.numberOfIterations) toSeq(countup(first, last - 1, self.numberOfIterations)) diff --git a/tests/codex/testindexingstrategy.nim b/tests/codex/testindexingstrategy.nim index e5ee5c97..9b173685 100644 --- a/tests/codex/testindexingstrategy.nim +++ b/tests/codex/testindexingstrategy.nim @@ -10,28 +10,33 @@ for offset in @[0, 1, 100]: checksuite "Indexing strategies (Offset: " & $offset & ")": let firstIndex = 0 + offset - lastIndex = 9 + offset + lastIndex = 12 + offset nIters = 3 linear = LinearIndexingStrategy.new(firstIndex, lastIndex, nIters) stepped = SteppedIndexingStrategy.new(firstIndex, lastIndex, nIters) test "linear": check: - linear.getIndicies(0) == @[0, 1, 2].mapIt(it + offset) - linear.getIndicies(1) == @[3, 4, 5].mapIt(it + offset) - linear.getIndicies(2) == @[6, 7, 8].mapIt(it + offset) - - test "linear - oob": - expect AssertionDefect: - discard linear.getIndicies(3) + linear.getIndicies(0) == @[0, 1, 2, 3].mapIt(it + offset) + linear.getIndicies(1) == @[4, 5, 6, 7].mapIt(it + offset) + linear.getIndicies(2) == @[8, 9, 10, 11].mapIt(it + offset) test "stepped": check: - stepped.getIndicies(0) == @[0, 3, 6].mapIt(it + offset) - stepped.getIndicies(1) == @[1, 4, 7].mapIt(it + offset) - stepped.getIndicies(2) == @[2, 5, 8].mapIt(it + offset) + stepped.getIndicies(0) == @[0, 3, 6, 9].mapIt(it + offset) + stepped.getIndicies(1) == @[1, 4, 7, 10].mapIt(it + offset) + stepped.getIndicies(2) == @[2, 5, 8, 11].mapIt(it + offset) - test "stepped - oob": - expect AssertionDefect: - discard stepped.getIndicies(3) +checksuite "Indexing strategies - oob": + let + linear = LinearIndexingStrategy.new(0, 10, 3) + stepped = SteppedIndexingStrategy.new(0, 10, 3) + + test "linear": + expect AssertionDefect: + discard linear.getIndicies(3) + + test "stepped": + expect AssertionDefect: + discard stepped.getIndicies(3)