Python中如何实现位图索引?
作者:SunnyJourney
时间:2023-05-10
浏览:0
代码如下:classBitmap(object):def__init__(self,max):self.size=self.calcElemIndex(max,True)self.array=[0foriinrange(self.size)]defcalcElemIndex(self,num,up=False):'''up为True则为向上取整,否则为向下取整'''ifup:returnint((num+31)/31)#向上取整returnnum/31def
本文目录
代码如下:
class Bitmap(object):
def __init__(self, max):
self.size = self.calcElemIndex(max, True)
self.array = [0 for i in range(self.size)]
def calcElemIndex(self, num, up=False):
'''up为True则为向上取整, 否则为向下取整'''
if up:
return int((num + 31 ) / 31) #向上取整
return num / 31
def calcBitIndex(self, num):
return num % 31
def set(self, num):
elemIndex = int(self.calcElemIndex(num))
byteIndex = self.calcBitIndex(num)
elem = self.array[elemIndex]
self.array[elemIndex] = elem | (1 << byteIndex)
def clean(self, i):
elemIndex = int(self.calcElemIndex(i))
byteIndex = self.calcBitIndex(i)
elem = self.array[elemIndex]
self.array[elemIndex] = elem & (~(1 << byteIndex))
def test(self, i):
elemIndex =int(self.calcElemIndex(i))
byteIndex = self.calcBitIndex(i)
if self.array[elemIndex] & (1 << byteIndex):
return True
return False
MAX = 879
suffle_array = [45, 2, 78, 35, 67, 90, 879, 0, 340, 123, 46]
result = []
bitmap = Bitmap(MAX)
for num in suffle_array:
bitmap.set(num)
for i in range(MAX + 1):
if bitmap.test(i):
result.append(i)
print ('原始数组为: %s' % suffle_array)
print ('排序后的数组为: %s' % result)
作者最新文章
AO3如何加入系列|作品归入现有系列教程
2026-11-18 17:43
俄罗斯Yandex官网入口及外贸查询工具
2026-11-16 17:53
Yandex官网入口及网页搜索使用方法
2026-11-10 18:27
免注册Word转PDF在哪里?Word转PDF手机电脑都能用
2026-11-08 15:29
xshell如何使用密钥连接服务器
2026-11-05 21:38
上一篇:
使用Python计算矩阵范数和行列式
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































