代码如下:
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 / 31def calcBitIndex(self, num):return num % 31def 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 Truereturn FalseMAX = 879suffle_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)
登录后复制
以上就是Python如何实现一个位图索引的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2235335.html