Skip to content

Commit bda5ef5

Browse files
committed
implemented binary tree search to get the closest deltachunk by offset
1 parent 834f081 commit bda5ef5

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

fun.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,18 @@ def _closest_index(dcl, absofs):
165165
to the DeltaChunk with a target buffer absofs that equals or is greater than
166166
absofs.
167167
:note: global method for performance only, it belongs to DeltaChunkList"""
168-
# TODO: binary search !!
169-
for i,d in enumerate(dcl):
170-
if absofs < d.to:
171-
return i-1
172-
elif absofs == d.to:
173-
return i
168+
lo = 0
169+
hi = len(dcl)
170+
while lo < hi:
171+
mid = (lo + hi) / 2
172+
dc = dcl[mid]
173+
if dc.to > absofs:
174+
hi = mid
175+
elif dc.rbound() > absofs or dc.to == absofs:
176+
return mid
177+
else:
178+
lo = mid + 1
179+
# END handle bound
174180
# END for each delta absofs
175181
return len(dcl)-1
176182

test/test_pack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def test_pack(self):
130130
self._assert_pack_file(pack, version, size)
131131
# END for each pack to test
132132

133-
def test_pack_entity(self):
133+
def _test_pack_entity(self):
134134
for packinfo, indexinfo in ( (self.packfile_v2_1, self.packindexfile_v1),
135135
(self.packfile_v2_2, self.packindexfile_v2),
136136
(self.packfile_v2_3_ascii, self.packindexfile_v2_3_ascii)):

0 commit comments

Comments
 (0)