python 实现bitmap,排序
Posted butter-007
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 实现bitmap,排序相关的知识,希望对你有一定的参考价值。
#python整数类型默认是有符号类型。可用位数是31位。
import math
class Bitmap(object):
def __init__(self, max):
self.size = math.ceil(max / 31)
self.array = [0 for i in range(self.size)]
def calcElemIndex(self, num):
return num // 31
def calcBitIndex(self, num):
return num % 31
def set(self, num):
elemIndex = self.calcElemIndex(num)
byteIndex = self.calcBitIndex(num)
elem = self.array[elemIndex]
self.array[elemIndex] = elem | (1 << byteIndex)#左移相当于乘以2的次方
def clean(self, num):
elemIndex = self.bitmap.calcElemIndex(num)
byteIndex = self.bitmap.calcBitIndex(num)
elem = self.array[elemIndex]
self.array[elemIndex] = elem & (~(1 << byteIndex))#清除改位,不影响其余位
def test(self, num):
elemIndex = self.calcElemIndex(num)
byteIndex = self.calcBitIndex(num)
elem = self.array[elemIndex]
if elem & (1 << byteIndex):
return True
return False
if __name__ == ‘__main__‘:
# bitmap = Bitmap(90)
# print("需要%d个元素。" % bitmap.size)
# print("存储在第%d个元素上的第%d位上面" % (bitmap.calcElemIndex(47), bitmap.calcBitIndex(47)))
max = 688
suffle_array = [0, 67, 1, 45, 187, 65, 58, 101, 33, 252, 688, 145, 254, 487]
result = []
bitmap = Bitmap(688)
for i in suffle_array:
bitmap.set(i)
for i in range(max + 1):
if bitmap.test(i):
result.append(i)
print(result)
以上是关于python 实现bitmap,排序的主要内容,如果未能解决你的问题,请参考以下文章
python: c_char_p指向的bitmap图像数据,通过c_char_Array最终赋值给PIL的Image对象