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

相关推荐
daidaidaiyu2 小时前
一文学习 工作流开发 BPMN、 Flowable
java
l1t2 小时前
DeepSeek总结的 pg_regresql插件:真正可移植的 PostgreSQL 统计信息
数据库·postgresql
oradh2 小时前
Oracle 11.2.0.1版本升级至11.2.0.4_单机环境
数据库·oracle·oracle11g·oracle升级
l1t2 小时前
用docker安装测试crate数据库
数据库·docker·容器·cratedb
anzhxu3 小时前
QT数据库(三):QSqlQuery使用
数据库·qt·oracle
身如柳絮随风扬3 小时前
MySQL核心知识
数据库·mysql
德彪稳坐倒骑驴3 小时前
Oracle 11g安装
数据库·oracle
SuniaWang3 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
韩立学长3 小时前
Springboot校园跑腿业务系统0b7amk02(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
sheji34163 小时前
【开题答辩全过程】以 基于springboot的扶贫系统为例,包含答辩的问题和答案
java·spring boot·后端