成都创新互联网站制作重庆分公司

Python中怎么实现一个位图索引

Python中怎么实现一个位图索引,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联公司专注于微山企业网站建设,响应式网站建设,商城建设。微山网站建设公司,为微山等地区提供建站服务。全流程按需开发,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

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)

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


网站名称:Python中怎么实现一个位图索引
网页路径:http://cxhlcq.com/article/ihcicd.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部