RoaringBitmap 源码

当调用add方法时,先把x分成高16位和低16位。

">>> "是 Java 中的无符号右移操作符,表示将 x 的二进制表示向右移动 16 位

当x为 65535 ,二进制为1111111111111111,16个1,即丢掉右16位,左边补0,所以x >>> 16=0,

当x为 65536 ,二进制为10000000000000000,17位,右16个0,所以x >>> 16=1

java 复制代码
    public void add(int x) {
        char hb = Util.highbits(x);
        int i = this.highLowContainer.getIndex(hb);
        if (i >= 0) {
            this.highLowContainer.setContainerAtIndex(i, this.highLowContainer.getContainerAtIndex(i).add(Util.lowbits(x)));
        } else {
            ArrayContainer newac = new ArrayContainer();
            this.highLowContainer.insertNewKeyValueAt(-i - 1, hb, newac.add(Util.lowbits(x)));
        }

    }

 protected static char highbits(int x) {
        return (char)(x >>> 16);
    }

默认初始化4个容器(桶),key都为0,数字65535 高16位为0,放在0号桶中,65536高16位为1,放在1号桶中,同一个桶中顺序排列,如果新插入的数据不是桶中最大的,数组需要copy进行插入,如果是最大的,直接放到最后面的位置,如果桶中的数据容量大于4096,则转换成toBitmapContainer容器存储。

java 复制代码
 public Container add(char x) {
        if (this.cardinality == 0 || this.cardinality > 0 && x > this.content[this.cardinality - 1]) {
            if (this.cardinality >= 4096) {
                return this.toBitmapContainer().add(x);
            }

            if (this.cardinality >= this.content.length) {
                this.increaseCapacity();
            }

            this.content[this.cardinality++] = x;
        } else {
            int loc = Util.unsignedBinarySearch(this.content, 0, this.cardinality, x);
            if (loc < 0) {
                if (this.cardinality >= 4096) {
                    return this.toBitmapContainer().add(x);
                }

                if (this.cardinality >= this.content.length) {
                    this.increaseCapacity();
                }

                System.arraycopy(this.content, -loc - 1, this.content, -loc, this.cardinality + loc + 1);
                this.content[-loc - 1] = x;
                ++this.cardinality;
            }
        }

        return this;
    }

65535在0号桶,存的低16位,就是本身65535

65536在1号桶,存的低16位,16个0,即0

访问的时候,也是先计算高位获得桶,然后用低位来算是否包含

java 复制代码
    public boolean contains(int x) {
        char hb = Util.highbits(x);
        Container c = this.highLowContainer.getContainer(hb);
        return c != null && c.contains(Util.lowbits(x));
    }

参考:

https://blog.csdn.net/S_ZaiJiangHu/article/details/125656217

相关推荐
Su-RE2 分钟前
【ElasticSearch】text 和 keyword 类型区分
java·数据库·elasticsearch
武子康4 分钟前
Java-146 深入浅出 MongoDB 数据插入、批量写入、BSON 格式与逻辑查询and or not操作指南
java·开发语言·数据库·sql·mongodb·性能优化·nosql
虎子_layor9 分钟前
从0到1学习泛型
java
奥尔特星云大使11 分钟前
MySQL快速构建主从(基于GTID)
数据库·mysql·主从复制
小园子的小菜13 分钟前
MySQL ORDER BY 深度解析:索引排序规则与关键配置参数阈值
数据库·mysql
wxjlkh15 分钟前
Oracle Exadata一体机简介 1千多个W
数据库·oracle
vxtkjzxt88823 分钟前
手机群控软件在游戏运营中的行为模拟技术实践
大数据
泽虞30 分钟前
《Qt应用开发》笔记p3
linux·开发语言·数据库·c++·笔记·qt·面试
XXYBMOOO34 分钟前
如何自定义 Qt 日志处理并记录日志到文件
开发语言·数据库·qt
不剪发的Tony老师43 分钟前
PEV2:一款PostgreSQL执行计划可视化工具
数据库·postgresql