mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Cleanup indexer code
This commit is contained in:
parent
072055d947
commit
e79d8872ac
1 changed files with 22 additions and 15 deletions
|
@ -31,25 +31,32 @@ module Spectator
|
||||||
getter example_count = 0
|
getter example_count = 0
|
||||||
|
|
||||||
def [](index : Int) : Example
|
def [](index : Int) : Example
|
||||||
offset = index
|
offset = check_bounds(index)
|
||||||
offset += example_count if offset < 0
|
find_nested(offset)
|
||||||
raise IndexError.new if offset < 0 || offset >= example_count
|
end
|
||||||
found = children.find do |child|
|
|
||||||
count = child.example_count
|
private def check_bounds(index)
|
||||||
if offset < count
|
if index < 0
|
||||||
true
|
raise IndexError.new if index < -example_count
|
||||||
else
|
index + example_count
|
||||||
offset -= count
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if found
|
|
||||||
found[offset]
|
|
||||||
else
|
else
|
||||||
raise IndexError.new
|
raise IndexError.new if index >= example_count
|
||||||
|
index
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def find_nested(index)
|
||||||
|
offset = index
|
||||||
|
child = children.each do |child|
|
||||||
|
count = child.example_count
|
||||||
|
break child if offset < count
|
||||||
|
offset -= count
|
||||||
|
end
|
||||||
|
# It should be impossible to get `nil` here,
|
||||||
|
# provided the bounds check and example counts are correct.
|
||||||
|
child.not_nil![offset]
|
||||||
|
end
|
||||||
|
|
||||||
def finished? : Bool
|
def finished? : Bool
|
||||||
children.all?(&.finished?)
|
children.all?(&.finished?)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue