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

相关推荐
INFINI Labs26 分钟前
Easy-Es 2.1.0-easysearch 版本发布
大数据·elasticsearch·搜索引擎·easysearch·easy-es
曹牧27 分钟前
Oracle:判断一个字符串出现次数
数据库·oracle
源代码•宸27 分钟前
Leetcode—620. 有趣的电影&&Q3. 有趣的电影【简单】
数据库·后端·mysql·算法·leetcode·职场和发展
百***787534 分钟前
Step-Audio-2 轻量化接入全流程详解
android·java·gpt·php·llama
快乐肚皮40 分钟前
MySQL递归CTE
java·数据库·mysql·递归表达式
廋到被风吹走44 分钟前
【Spring】DispatcherServlet解析
java·后端·spring
廋到被风吹走1 小时前
【Spring】PlatformTransactionManager详解
java·spring·wpf
2301_800256111 小时前
地理空间数据库中的CPU 和 I/O 开销
数据库·算法·oracle
Elseide艾思1 小时前
艾思政策数据库正式发布(1989年至今)
数据库