block: handle 2 dimensions separately

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2023-07-11 12:48:56 +02:00
parent db5fd6c157
commit f21a9ddb01
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E
1 changed files with 29 additions and 24 deletions

View File

@ -7,10 +7,15 @@ from bitarray.util import zeros
class Block: class Block:
"""This class represents a block in the Ethereum blockchain.""" """This class represents a block in the Ethereum blockchain."""
def __init__(self, blockSize): def __init__(self, blockSizeR, blockSizeC=0):
"""Initialize the block with a data array of blocksize^2 zeros.""" """Initialize the block with a data array of blocksize^2 zeros.
self.blockSize = blockSize
self.data = zeros(self.blockSize*self.blockSize) BlockSizeR: row size
BlockSizeC: column size (i.e. number of rows)
"""
self.blockSizeR = blockSizeR
self.blockSizeC = blockSizeC if blockSizeC else blockSizeR
self.data = zeros(self.blockSizeR*self.blockSizeC)
def fill(self): def fill(self):
"""It fills the block data with ones.""" """It fills the block data with ones."""
@ -22,62 +27,62 @@ class Block:
def getSegment(self, rowID, columnID): def getSegment(self, rowID, columnID):
"""Check whether a segment is included""" """Check whether a segment is included"""
return self.data[rowID*self.blockSize + columnID] return self.data[rowID*self.blockSizeR + columnID]
def setSegment(self, rowID, columnID, value = 1): def setSegment(self, rowID, columnID, value = 1):
"""Set value for a segment (default 1)""" """Set value for a segment (default 1)"""
self.data[rowID*self.blockSize + columnID] = value self.data[rowID*self.blockSizeR + columnID] = value
def getColumn(self, columnID): def getColumn(self, columnID):
"""It returns the block column corresponding to columnID.""" """It returns the block column corresponding to columnID."""
return self.data[columnID::self.blockSize] return self.data[columnID::self.blockSizeR]
def mergeColumn(self, columnID, column): def mergeColumn(self, columnID, column):
"""It merges (OR) the existing column with the received one.""" """It merges (OR) the existing column with the received one."""
self.data[columnID::self.blockSize] |= column self.data[columnID::self.blockSizeR] |= column
def repairColumn(self, id): def repairColumn(self, id):
"""It repairs the entire column if it has at least blockSize/2 ones. """It repairs the entire column if it has at least blockSizeC/2 ones.
Returns: list of repaired segments Returns: list of repaired segments
""" """
line = self.data[id::self.blockSize] line = self.data[id::self.blockSizeR]
success = line.count(1) success = line.count(1)
if success >= self.blockSize/2: if success >= self.blockSizeC/2:
ret = ~line ret = ~line
self.data[id::self.blockSize] = 1 self.data[id::self.blockSizeR] = 1
else: else:
ret = zeros(self.blockSize) ret = zeros(self.blockSizeC)
return ret return ret
def getRow(self, rowID): def getRow(self, rowID):
"""It returns the block row corresponding to rowID.""" """It returns the block row corresponding to rowID."""
return self.data[rowID*self.blockSize:(rowID+1)*self.blockSize] return self.data[rowID*self.blockSizeR:(rowID+1)*self.blockSizeR]
def mergeRow(self, rowID, row): def mergeRow(self, rowID, row):
"""It merges (OR) the existing row with the received one.""" """It merges (OR) the existing row with the received one."""
self.data[rowID*self.blockSize:(rowID+1)*self.blockSize] |= row self.data[rowID*self.blockSizeR:(rowID+1)*self.blockSizeR] |= row
def repairRow(self, id): def repairRow(self, id):
"""It repairs the entire row if it has at least blockSize/2 ones. """It repairs the entire row if it has at least blockSizeR/2 ones.
Returns: list of repaired segments. Returns: list of repaired segments.
""" """
line = self.data[id*self.blockSize:(id+1)*self.blockSize] line = self.data[id*self.blockSizeR:(id+1)*self.blockSizeR]
success = line.count(1) success = line.count(1)
if success >= self.blockSize/2: if success >= self.blockSizeR/2:
ret = ~line ret = ~line
self.data[id*self.blockSize:(id+1)*self.blockSize] = 1 self.data[id*self.blockSizeR:(id+1)*self.blockSizeR] = 1
else: else:
ret = zeros(self.blockSize) ret = zeros(self.blockSizeR)
return ret return ret
def print(self): def print(self):
"""It prints the block in the terminal (outside of the logger rules)).""" """It prints the block in the terminal (outside of the logger rules))."""
dash = "-" * (self.blockSize+2) dash = "-" * (self.blockSizeR+2)
print(dash) print(dash)
for i in range(self.blockSize): for i in range(self.blockSizeC):
line = "|" line = "|"
for j in range(self.blockSize): for j in range(self.blockSizeR):
line += "%i" % self.data[(i*self.blockSize)+j] line += "%i" % self.data[(i*self.blockSizeR)+j]
print(line+"|") print(line+"|")
print(dash) print(dash)