Skip to content

Commit

Permalink
Have 'Segment.chunks' yield smaller 'Segment's
Browse files Browse the repository at this point in the history
  • Loading branch information
bessman committed Sep 18, 2023
1 parent b087870 commit b725bf8
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions bincopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,8 @@ class Segment:
"""

class Chunk(namedtuple("Chunk", ["address", "data"])):
_Chunk = namedtuple("Chunk", ["address", "data"])

def __len___(self):
return len(self.data)

_Chunk = Chunk

def __init__(self, minimum_address, maximum_address, data, word_size_bytes):
self.minimum_address = minimum_address
self.maximum_address = maximum_address
Expand Down Expand Up @@ -389,16 +384,20 @@ def chunks(self, size=32, alignment=1):

if chunk_offset != 0:
first_chunk_size = (alignment - chunk_offset)
yield self.Chunk(address // self._word_size_bytes,
data[:first_chunk_size])
yield Segment(address // self._word_size_bytes,
(address + size) // self._word_size_bytes,
data[:first_chunk_size],
self._word_size_bytes)
address += (first_chunk_size // self._word_size_bytes)
data = data[first_chunk_size:]
else:
first_chunk_size = 0

for offset in range(0, len(data), size):
yield self.Chunk((address + offset) // self._word_size_bytes,
data[offset:offset + size])
yield Segment((address + offset) // self._word_size_bytes,
(address + offset + size) // self._word_size_bytes,
data[offset:offset + size],
self._word_size_bytes)

def add_data(self, minimum_address, maximum_address, data, overwrite):
"""Add given data to this segment. The added data must be adjacent to
Expand Down

0 comments on commit b725bf8

Please sign in to comment.