2022-11-30 14:28:27 +00:00
|
|
|
#!/bin/python3
|
|
|
|
|
|
|
|
import random
|
2022-12-07 14:25:48 +00:00
|
|
|
from bitarray import bitarray
|
|
|
|
from bitarray.util import zeros
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
class Block:
|
|
|
|
|
|
|
|
blockSize = 0
|
2022-12-07 14:25:48 +00:00
|
|
|
data = bitarray()
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
def __init__(self, size):
|
|
|
|
self.blockSize = size
|
2022-12-07 14:25:48 +00:00
|
|
|
self.data = zeros(self.blockSize*self.blockSize)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
def fill(self):
|
2022-12-07 17:44:02 +00:00
|
|
|
self.data.setall(1)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-20 09:09:54 +00:00
|
|
|
def merge(self, merged):
|
|
|
|
self.data |= merged.data
|
|
|
|
|
2022-12-07 14:10:35 +00:00
|
|
|
def getColumn(self, columnID):
|
2022-12-07 14:25:48 +00:00
|
|
|
return self.data[columnID::self.blockSize]
|
2022-12-07 14:10:35 +00:00
|
|
|
|
2022-12-20 09:09:54 +00:00
|
|
|
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
|
|
|
|
|
2022-12-07 14:10:35 +00:00
|
|
|
def getRow(self, rowID):
|
2022-12-07 14:25:48 +00:00
|
|
|
return self.data[rowID*self.blockSize:(rowID+1)*self.blockSize]
|
2022-12-07 14:10:35 +00:00
|
|
|
|
2022-12-20 09:09:54 +00:00
|
|
|
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)
|
|
|
|
|