Skip to content

Commit ff93159

Browse files
committed
Apply ugly read_coordinate optimizations.
1 parent 5da3653 commit ff93159

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

sc2reader/utils.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,18 +315,34 @@ def read_object_id(self):
315315
return self.read_int(endian=BIG_ENDIAN)
316316

317317
def read_coordinate(self):
318-
# Combine coordinate whole and fraction
319-
def _coord_to_float(coord):
320-
fraction = 0
321-
for mask,quotient in self.coord_convert:
322-
if (coord[1] & mask):
323-
fraction = fraction + quotient
324-
return coord[0] + fraction
325-
318+
# TODO: This seems unreasonably percise....
326319
# Read an x or y coordinate dimension
327320
def _coord_dimension():
328-
coord = self.read(bits=20)
329-
return _coord_to_float([coord[0], coord[1] << 4 | coord[2],])
321+
if self.bit_shift == 0:
322+
return (self.read_short() << 4 | self.shift(4))/4096.0
323+
324+
else:
325+
old_bit_shift = self.bit_shift
326+
new_bit_shift = (20 + old_bit_shift) % 8
327+
328+
# Get all the bytes at once
329+
hi_bits = self.shift(8 - old_bit_shift)
330+
block = struct.unpack('>H', self.read_basic(2))[0]
331+
332+
if old_bit_shift > 4:
333+
block = block << 8 | ord(self.read_basic(1))
334+
335+
number = (block >> 8) << old_bit_shift | (block & self.lo_masks[new_bit_shift])
336+
number += hi_bits << (16 + new_bit_shift)
337+
else:
338+
number = (block >> 8) << old_bit_shift | (block & self.lo_masks[new_bit_shift])
339+
number += hi_bits << (8 + new_bit_shift)
340+
341+
# Reset the shift
342+
self.last_byte = block & 0xFF
343+
self.bit_shift = new_bit_shift
344+
345+
return number/4096.0
330346

331347
# TODO?: Handle optional z dimension
332348
return (_coord_dimension(), _coord_dimension())

0 commit comments

Comments
 (0)