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

相关推荐
AI行业学习10 分钟前
CC-Switch v3.16.1 官方下载 | 安装配置详细教程【2026.6.10】
java·开发语言·vue.js·python·mysql·eclipse·html
cui178756830 分钟前
物业费收缴困局的破题之路:2026年社区商业逻辑的底层重构
大数据·数据库·人工智能
2501_9336707933 分钟前
大数据在校实训项目一般做什么类型内容
大数据
monsion1 小时前
Loop Engineering:你不再 prompt agent,而是设计 prompt agent 的系统
大数据·人工智能·prompt
不负岁月无痕1 小时前
C++ 模板核心内容与高频面试题汇总
java·开发语言·c++
Flittly1 小时前
【AgentScope Java新手村系列】(2)第一个Agent-基础对话
java·spring boot·spring·ai
是发财不是旺财1 小时前
Hermes 网关四层权限控制方案:让 AI Agent 安全地查数据库
数据库·安全·agent·openclaw·hermes
摇滚侠1 小时前
Spring MVC 不是一个单独的框架,是 Spring 框架的一个模块
java·spring·mvc
阿正的梦工坊1 小时前
【Rust】04-借用、引用与切片
java·数据库·rust
AOwhisky1 小时前
学习自测与解析:MySQL第五、六、七期核心知识点详解
运维·数据库·笔记·学习·mysql·云计算