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

相关推荐
哥哥还在IT中5 分钟前
Elasticsearch优化从入门到精通
大数据·elasticsearch·搜索引擎
向上的车轮8 分钟前
基于Java Spring Boot的云原生TodoList Demo 项目,验证云原生核心特性
java·spring boot·云原生
程序员清风10 分钟前
快手一面:为什么要求用Static来修饰ThreadLocal变量?
java·后端·面试
逍遥德11 分钟前
Java8 Comparator接口 和 List Steam 排序使用案例
java·spring boot·list·排序算法
Elastic 中国社区官方博客18 分钟前
使用 cloud-native Elasticsearch 与 ECK 运行
大数据·数据库·elasticsearch·搜索引擎·kubernetes·k8s·全文检索
前行的小黑炭29 分钟前
Android :如何快速让布局适配手机和平板?
android·java·kotlin
Mr_hwt_12330 分钟前
基于MyCat 中间件实现mysql集群读写分离与从库负载均衡教程(详细案例教程)
数据库·mysql·中间件·mysql集群
酷ku的森1 小时前
Redis中的Zset数据类型
数据库·redis·缓存
zhong liu bin1 小时前
MySQL数据库面试题整理
数据结构·数据库·mysql
_BugMan2 小时前
【IDEA】干活?一个IDEA即可,集成开发平台打造攻略
java·ide·intellij-idea