clientbot/tools/CBitSet.py

135 lines
4.2 KiB
Python
Raw Normal View History

2019-10-24 17:45:24 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# module CBitSet
#
# Copyright (C) 2019 AleaJactaEst
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class CBitSet:
def __init__(self, NL_BITLEN=32):
self.NumBits = 0
self.MaskLast = 0
self.NL_BITLEN = NL_BITLEN
self.data = self.resize(1024)
def resize(self, numBits):
self.data = [ 0 for _ in range(0, (numBits + self.NL_BITLEN - 1) // self.NL_BITLEN) ]
self.NumBits = numBits
nLastBits = self.NumBits & (self.NL_BITLEN-1)
if nLastBits == 0:
self.MaskLast = ~0
else:
self.MaskLast = (1 << nLastBits)-1
self.clearAll()
def clearData(self):
self.data = []
self.NumBits = 0
self.MaskLast = 0
def clearAll(self):
for i in range(0, len(self.data)):
self.data[i] = 0
def set(self, bitNumber, value):
#logging.getLogger('myLogger').debug("CBitSet::set %d %s" % (bitNumber, str(value)))
mask = bitNumber & (self.NL_BITLEN-1)
mask = 1 << mask
if value:
self.data[bitNumber >> 5] |= mask
else:
self.data[bitNumber >> 5] &= ~mask
def get(self, bitNumber):
mask= bitNumber&(self.NL_BITLEN-1);
mask= 1<<mask;
return self.data[bitNumber >> 5] & mask != 0
2020-11-11 15:33:08 +00:00
def setUint(self, srcValue, i):
self.data[i] = srcValue
2019-10-24 17:45:24 +00:00
def setBit(self, bitNumber):
self.set(bitNumber, True)
def clearBit(self, bitNumber):
self.set(bitNumber, False)
def __str__(self):
return '.'.join([hex(x) for x in self.data])
2020-11-11 15:33:08 +00:00
def showBitString(self):
return ''.join(["{0:032b}".format(x) for x in reversed(self.data)])
2019-10-24 17:45:24 +00:00
def writeSerial(self, msgout):
# v = 0 # currentVersion
# if v >= 0xff:
# msgout.pushUint8(0xff)
# msgout.pushUint8(v)
# else:
# msgout.pushUint8(v)
#log = logging.getLogger('myLogger')
#log.debug("CBitSet::writeSerial NumBits:%d len:%d" % (self.NumBits, len(self.data)))
currentVersion = 0
msgout.pushUint8(currentVersion)
msgout.pushUint32(self.NumBits)
# il est lié à 'self.NumBits' dommage que l'on envoie celui-la
msgout.pushUint32(len(self.data))
for x in self.data:
msgout.pushUint32(x)
def readSerial(self, msgin, name):
_ = msgin.readSint8(name+':currentVersion')
NumBits = msgin.readUint32(name+':NumBits')
Size = msgin.readUint32(name+':Size')
self.resize(NumBits)
for i in range(0, Size):
y = msgin.readUint32( name+':Data' )
self.data[i] = y
2020-11-11 15:33:08 +00:00
def readSerialExtra(self, msgin, nbBit, name):
needread = nbBit
print("----", nbBit, nbBit // 32 + 1)
self.resize(nbBit)
i = 0
while needread>32:
print("---<", nbBit, nbBit // 32 + 1, i, needread)
y = msgin.readUint32( name+' Data' + str(i))
self.data[i] = y
needread -= 32
i += 1
print(self.data)
if needread > 0:
print("--->", nbBit, nbBit // 32 + 1, i, needread)
y = msgin.readSerial( needread, name+' Data' + str(i), typeName="I" + str(needread) )
self.data[i] = y
2019-10-24 17:45:24 +00:00
def TestCBitSet():
cBitSet = CBitSet()
cBitSet.resize(1024)
cBitSet.set(1, True)
cBitSet.set(3, True)
cBitSet.set(2, False)
cBitSet.set(13, True)
cBitSet.set(128, True)
cBitSet.set(1023, True)
print(cBitSet)
print(cBitSet.get(3))
cBitSet.set(3, False)
print(cBitSet)
print(cBitSet.get(3))