mirror of
https://port.numenaute.org/aleajactaest/clientbot.git
synced 2024-11-25 08:36:18 +00:00
110 lines
3.4 KiB
Python
110 lines
3.4 KiB
Python
#!/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
|
|
|
|
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])
|
|
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
|
|
|
|
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))
|