roaringbitmap 源码解析(2) 底层三种容器containner对比

首先对比一下,roaringbitMap里的三种container

原文地址:http://blog.csdn.net/chenfenggang/article/details/74781791

> /**  * Simple bitset-like container.  */

bitmapcontainer 定义了1024个long型,每个long占用64位。这样可以表达1024×64 =short 长度 16位
比如:12, 将第0个long置为1L<<12位。一个long 最多表示63.
如果需要表示64就需要用[64/64] =1 用第1个long。的第一位表示。
所以任何一个short表示为 [x/64]=1L<

> /**  * Simple container made of an array of 16-bit integers  */

当数据量小于4096时采用 short数组所占用的空间还小一些。所以定义了第二种容器,ArrayContainer。
ArrayContainer 用short数组来存储数据。并且按需扩展空间。而且数据按照有序排列。
比如存储12: 定义一个short [0] =12;
再添加15 ,找的末尾位置, short[1] = 15;
再添加14 ,找的中间位置。 将先移动数字 short【2】=15,然后放入 short【1】=14
系统默认生成4个单位的长度short,当长度小于1024的时候空间成倍数扩展,当大于1024时 ,按25%扩展。
这中数据存储方式对小量数据比较优化。

第三种方式runContainer.也就是采用run-length算法。
代码中说明了原理

// that if you have the values 11,12,13,14,15, you store that as 11,4 where 4 means that beyond 11
// itself, there are
// 4 contiguous values that follows.
// Other example: e.g., 1, 10, 20,0, 31,2 would be a concise representation of 1, 2, ..., 11, 20,
// 31, 32, 33

如果是连续的数据。采用RunContainer是最好不过了。比如存储1到100,200到300. 只需要存储。1,99,200,99就可以了。
前面位置表示起始位置,后面表示连续出现次数。
所以在定义range的时候一般用runbitmap ,比较稀疏的数据也可以用种方式。

public static Container rangeOfOnes(final int start, final int last) {
  final int sizeAsArrayContainer = ArrayContainer.serializedSizeInBytes(last - start);
  final int sizeAsRunContainer = RunContainer.serializedSizeInBytes(1);
  Container answer =
      sizeAsRunContainer < sizeAsArrayContainer ? new RunContainer() : new ArrayContainer();
  answer = answer.iadd(start, last);
  return answer;
}

总之:roaringbitmap 在存储连续的时候用RunContainer()。否则会用ArrayContainer。当ArrayContainer数据满了,即大于4096时。转为BitmapContainer。

未完待续。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
微信抢红包是一种基于微信平台的互动红包游戏,用户可以通过微信发送红包给好友或者群聊,其他用户可以在规定时间内抢到红包,抢到的金额将直接转入用户的微信钱包中。 在Python中,可以使用selenium库模拟微信网页版的操作,实现自动抢红包的功能。具体步骤如下: 1. 安装selenium库和Chrome浏览器驱动。 2. 打开微信网页版,并使用扫码登录。 3. 进入指定的聊天窗口,找到红包消息并点击。 4. 在弹出的红包界面中,获取红包金额和“开”按钮的位置。 5. 点击“开”按钮,抢到红包后返回红包金额。 代码示例: ```python from selenium import webdriver import time # 打开Chrome浏览器 driver = webdriver.Chrome() # 打开微信网页版并扫码登录 driver.get('https://wx.qq.com/') time.sleep(10) # 进入指定的聊天窗口 chat_window = driver.find_element_by_xpath('//div[@title="好友昵称"]') chat_window.click() # 找到红包消息并点击 red_packet = driver.find_element_by_xpath('//div[@class="bubble_containner"]') red_packet.click() # 获取红包金额和“开”按钮的位置 red_packet_amount = driver.find_element_by_xpath('//div[@class="money_num"]') open_button = driver.find_element_by_xpath('//div[@class="open"]/a') # 点击“开”按钮,抢到红包后返回红包金额 open_button.click() time.sleep(1) red_packet_amount = red_packet_amount.text print('抢到红包,金额为:', red_packet_amount) # 关闭浏览器 driver.quit() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值