add SparseBlock with selected rows and columns only

SparseBlock stores only selected rows and columns.
Initial implementation without checks and asserts.

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2023-03-22 11:31:57 +01:00
parent ba92840b2c
commit 8bcf601e88
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E
2 changed files with 89 additions and 2 deletions

View File

@ -81,3 +81,90 @@ class Block:
print(line+"|")
print(dash)
class SparseBlock:
def __init__(self, size, rows, columns):
self.blockSize = size
self.rows = {r: zeros(self.blockSize) for r in rows}
self.columns = {r: zeros(self.blockSize) for r in columns}
def fill(self):
for line in self.rows:
line.setall(1)
for line in self.columns:
line.setall(1)
def merge(self, merged):
for id in self.rows:
self.mergeRow(id, merged.getRow(id))
for id in self.columns:
self.mergeColumn(id, merged.getColumn(id))
def getSegment(self, rowID, columnID):
"""Check whether a segment is included"""
ret = 0
r = self.rows.get(rowID)
if r:
ret |= r[columnID]
c = self.columns.get(columnID)
if c:
ret |= c[rowID]
return ret
def setSegment(self, rowID, columnID, value = 1):
"""Set value for a segment (default 1)"""
r = self.rows.get(rowID)
if r:
r[columnID] = 1
c = self.columns.get(columnID)
if c:
c[rowID] = 1
def getColumn(self, id):
return self.columns[id]
def mergeColumn(self, id, column):
self.columns[id] |= column
for xid, line in self.rows.items():
line[id] |= self.columns[id][xid]
def repairColumn(self, id):
success = self.columns[id].count(1)
if success >= self.blockSize/2:
ret = ~self.columns[id]
self.columns[id].setall(1)
else:
ret = zeros(self.blockSize)
return ret
def getRow(self, id):
return self.rows[id]
def mergeRow(self, id, row):
self.rows[id] |= row
for xid, line in self.columns.items():
line[id] |= self.rows[id][xid]
def repairRow(self, id):
success = self.rows[id].count(1)
if success >= self.blockSize/2:
ret = ~self.rows[id]
self.rows[id].setall(1)
else:
ret = zeros(self.blockSize)
return ret
# def print(self):
# dash = "-" * (self.blockSize+2)
# print(dash)
# for i in range(self.blockSize):
# line = "|"
# for j in range(self.blockSize):
# line += "%i" % self.data[(i*self.blockSize)+j]
# print(line+"|")
# print(dash)

View File

@ -49,8 +49,6 @@ class Validator:
FORMAT = "%(levelname)s : %(entity)s : %(message)s"
self.ID = ID
self.format = {"entity": "Val "+str(self.ID)}
self.block = Block(self.shape.blockSize)
self.receivedBlock = Block(self.shape.blockSize)
self.receivedQueue = deque()
self.sendQueue = deque()
self.amIproposer = amIproposer
@ -73,6 +71,8 @@ class Validator:
self.columnIDs = columns if columns else unionOfSamples(range(self.shape.blockSize), self.shape.chi, self.vpn)
self.rowNeighbors = collections.defaultdict(dict)
self.columnNeighbors = collections.defaultdict(dict)
self.block = SparseBlock(shape.blockSize, self.rowIDs, self.columnIDs)
self.receivedBlock = SparseBlock(shape.blockSize, self.rowIDs, self.columnIDs)
#statistics
self.statsTxInSlot = 0