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)
作者最新文章
CorelDRAW是免费软件吗?详解试用、订阅与一次性购买收费模式
2026-10-30 19:32
PDF怎么免费转成Word并保留内容?转换前后检查要点
2026-10-30 17:50
ArchiveofOurOwn小说快速入口链接
2026-10-30 10:30
冰点还原怎么安装 初始配置步骤
2026-10-28 13:38
漫蛙下载最新版本
2026-10-13 13:42
上一篇:
使用Python计算矩阵范数和行列式
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































