das-research/DAS/block.py

54 lines
1.4 KiB
Python
Raw Normal View History

2022-11-30 14:28:27 +00:00
#!/bin/python3
import random
from bitarray import bitarray
from bitarray.util import zeros
2022-11-30 14:28:27 +00:00
class Block:
blockSize = 0
data = bitarray()
2022-11-30 14:28:27 +00:00
def __init__(self, size):
self.blockSize = size
self.data = zeros(self.blockSize*self.blockSize)
2022-11-30 14:28:27 +00:00
def fill(self):
self.data.setall(1)
2022-11-30 14:28:27 +00:00
def merge(self, merged):
self.data |= merged.data
def getColumn(self, columnID):
return self.data[columnID::self.blockSize]
def mergeColumn(self, columnID, column):
self.data[columnID::self.blockSize] |= column
def repairColumn(self, id):
success = self.data[id::self.blockSize].count(1)
if success >= self.blockSize/2:
self.data[id::self.blockSize] = 1
def getRow(self, rowID):
return self.data[rowID*self.blockSize:(rowID+1)*self.blockSize]
def mergeRow(self, rowID, row):
self.data[rowID*self.blockSize:(rowID+1)*self.blockSize] |= row
def repairRow(self, id):
success = self.data[id*self.blockSize:(id+1)*self.blockSize].count(1)
if success >= self.blockSize/2:
self.data[id*self.blockSize:(id+1)*self.blockSize] = 1
2022-11-30 14:28:27 +00:00
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)