Skip to content

Commit

Permalink
Use a bisect search to look for the previous suitable frame
Browse files Browse the repository at this point in the history
  • Loading branch information
pozitronik committed Mar 10, 2024
1 parent 75f4374 commit a2feef0
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions sinner/models/FrameDirectoryBuffer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import threading
from bisect import bisect_right
from pathlib import Path
from typing import List

Expand Down Expand Up @@ -79,15 +80,16 @@ def get_frame(self, index: int, return_previous: bool = True) -> NumberedFrame |
except Exception:
pass
elif return_previous:
for previous_number in range(index - 1, 0, -1):
if self.has_frame(previous_number):
previous_filename = str(previous_number).zfill(self.zfill_length) + '.png'
previous_file_path = os.path.join(self.path, previous_filename)
if path_exists(previous_file_path):
try:
return NumberedFrame(index, read_from_image(previous_file_path))
except Exception: # the file may exist but can be locked in another thread.
pass
previous_position = bisect_right(self._indices, index - 1)
if previous_position:
previous_index = self._indices[previous_position - 1]
previous_filename = str(previous_index).zfill(self.zfill_length) + '.png'
previous_file_path = os.path.join(self.path, previous_filename)
if path_exists(previous_file_path):
try:
return NumberedFrame(index, read_from_image(previous_file_path))
except Exception: # the file may exist but can be locked in another thread.
pass
return None

def has_frame(self, index: int) -> bool:
Expand Down

0 comments on commit a2feef0

Please sign in to comment.