当前位置: 首页 > news >正文

山西省建设厅网站 孙涛源码论坛网站

山西省建设厅网站 孙涛,源码论坛网站,网站筑云做关键词,vue访问wordpress文章目录 前言数据结构添加与删除操作 JDK中BitSet源码解析重要成员属性初始化添加数据清除数据获取数据size和length方法集合操作#xff1a;与、或、异或 前言 为什么称为bitmap#xff1f; bitmap不仅仅存储介质以及数据结构不同于hashmap#xff0c;存储的key和value也… 文章目录 前言数据结构添加与删除操作 JDK中BitSet源码解析重要成员属性初始化添加数据清除数据获取数据size和length方法集合操作与、或、异或 前言 为什么称为bitmap bitmap不仅仅存储介质以及数据结构不同于hashmap存储的key和value也不同。 bitmap的key是元素的indexvalue只有0或者1具体结构见下文。 数据结构 Bit-map的基本思想就是用一个bit位来标记某个元素对应的Value而Key即是该元素。由于采用了Bit为单位来存储数据因此可以很大程度上节省存储空间。 举例 key-value: bitmap[1] 1、bitmap[2]0 添加与删除操作 添加使用1和key所在位的value进行 或 删除使用1和key所在位的value进行 与 JDK中BitSet源码解析 位于java.util包中 重要成员属性 /** BitSets are packed into arrays of words. Currently a word is* a long, which consists of 64 bits, requiring 6 address bits.* The choice of word size is determined purely by performance concerns.* 采用long作为载体long有8个byte所以有一个long有64个bit64这个数字需要6个bit承载*/ private final static int ADDRESS_BITS_PER_WORD 6; // 每一个words里面的元素占有64位 private final static int BITS_PER_WORD 1 ADDRESS_BITS_PER_WORD; private final static int BIT_INDEX_MASK BITS_PER_WORD - 1; /* Used to shift left or right for a partial word mask */ private static final long WORD_MASK 0xffffffffffffffffL; /*** serialField bits long[]** The bits in this BitSet. The ith bit is stored in bits[i/64] at* bit position i % 64 (where bit position 0 refers to the least* significant bit and 63 refers to the most significant bit).*/ private static final ObjectStreamField[] serialPersistentFields {new ObjectStreamField(bits, long[].class), }; /*** The internal field corresponding to the serialField bits.* bitset的数据载体*/ private long[] words; /*** The number of words in the logical size of this BitSet.* 表示数组中最多使用的元素个数也就是最后一个不为 0 的元素的索引加 1比如[0,4,0,0]数组长度为 4但是最后一个不为 0 的元素是 1所以 wordsInUse 2*/ private transient int wordsInUse 0;初始化 创建一个 BitSet 对象时默认 words 的长度为 1并且 words[0] 0。当然也可以用户给定一个具体的容量大小如下代码 /** * BitSet.class * 创建一个能存储给定数据索引的 BitSet */ public BitSet(int nbits) {// 参数合法性判断if (nbits 0)throw new NegativeArraySizeException(nbits 0: nbits);// 调用 initWords 方法初始化initWords(nbits);sizeIsSticky true; }private void initWords(int nbits) {words new long[wordIndex(nbits-1) 1]; } // 得到 bitIndex 对应的 words 下标 private static int wordIndex(int bitIndex) {return bitIndex ADDRESS_BITS_PER_WORD; }添加数据 public void set(int bitIndex) {// 参数合法性检验if (bitIndex 0)throw new IndexOutOfBoundsException(bitIndex 0: bitIndex);// 得到对应的数组下标int wordIndex wordIndex(bitIndex);// 是否要扩容expandTo(wordIndex);// 修改数据words[wordIndex] | (1L bitIndex); // 参数检查checkInvariants(); } private void expandTo(int wordIndex) {int wordsRequired wordIndex1;if (wordsInUse wordsRequired) {// 扩容ensureCapacity(wordsRequired);wordsInUse wordsRequired;} } private void ensureCapacity(int wordsRequired) {if (words.length wordsRequired) {// Allocate larger of doubled size or required size// 基本上是扩容两倍int request Math.max(2 * words.length, wordsRequired);words Arrays.copyOf(words, request);sizeIsSticky false;}}注意这里的set(bitIndex)是让二进制的位置为1并不是让words数组的某一index为1. 扩容的逻辑是如果需要的长度大于数组的两倍则扩容到需要的长度。否则扩容位数组的两倍。 清除数据 public void clear(int bitIndex) {//...int wordIndex wordIndex(bitIndex);// 如果 wordIndex wordsInUse说明该索引要么不存在要么一定是 0 直接返回即可if (wordIndex wordsInUse)return;words[wordIndex] ~(1L bitIndex);recalculateWordsInUse();//... } // 修改完可能会引起 wordsInUse 的变化所以还要调用 recalculateWordsInUse() 重新计算 wordsInUse从后往前遍历直到遇到 words[i] ! 0修改 wordsInUse i1。 private void recalculateWordsInUse() {int i;for (i wordsInUse-1; i 0; i--)if (words[i] ! 0)break;wordsInUse i1; // The new logical size }获取数据 public boolean get(int bitIndex) {if (bitIndex 0)throw new IndexOutOfBoundsException(bitIndex 0: bitIndex);checkInvariants();int wordIndex wordIndex(bitIndex);return (wordIndex wordsInUse) ((words[wordIndex] (1L bitIndex)) ! 0); }size和length方法 /*** Returns the number of bits of space actually in use by this* {code BitSet} to represent bit values.* The maximum element in the set is the size - 1st element.** return the number of bits currently in this bit set*/ public int size() {return words.length * BITS_PER_WORD; }/*** Returns the logical size of this {code BitSet}: the index of* the highest set bit in the {code BitSet} plus one. Returns zero* if the {code BitSet} contains no set bits.* 最高非0位1** return the logical size of this {code BitSet}* since 1.2*/ public int length() {if (wordsInUse 0)return 0;return BITS_PER_WORD * (wordsInUse - 1) (BITS_PER_WORD - Long.numberOfLeadingZeros(words[wordsInUse - 1])); }size方法words数组的长度 * 64每个long的长度lenght方法最高位的1所在位置 1 示例 集合操作与、或、异或 集合操作还是很常用的具体不作说明了自行去看源码。 本文就到这里下一篇介绍它的高级应用。
http://www.zqtcl.cn/news/286584/

相关文章:

  • 网站横幅图片网页设计怎么创建站点
  • 网站建设页面设计图片开个送快餐网站怎么做
  • 北京免费网站建设模板下载南江县建设局网站
  • 温岭手机网站建设义乌市网站建设
  • 西安网站制作费用哪家装修公司比较好的
  • 硅谷网站开发薪酬wordpress热门吗
  • 红酒营销型网站建设天一建设网站
  • 做网站建设公司哪家好安徽省住房建设部官方网站
  • 网站被黑咋样的柳州正规网站制作公司哪家好
  • 莱芜网站开发代理四川网络推广服务
  • 应该知道的网站网站全网建设莱芜
  • 北京网站页设计制作广州专业网站改版
  • 重庆网站建设建站收费免费外链网盘
  • 做加盟代理的网站比较好的网页网站设计
  • 兴义网站开发企业标准备案平台官网
  • 蓝彩网络科技_齐齐哈尔微信营销_齐齐哈尔网站建设会员卡管理系统哪里买
  • 织梦门户网站做大后建个人免费网站用哪个
  • 深圳市建设管理中心西安官网seo
  • 网站开发工作方案自己做的网站怎么维护
  • 潍坊建设部门管理网站做网站如何接单
  • 定制高端网站建设设计建立的近义词
  • 企业网站建设进度邢台163官网
  • 17做网店网站池尾替代wordpress 搜索
  • 网站建设资料 优帮云商品分类标准
  • 鄂尔多斯 网站建设俐侎族网站建设背景
  • 佛山专业网站建设公司上海公司官网
  • 那里做网站好网站模板 登陆
  • 网站的服务器打不开wordpress 修改默认路径
  • 外贸网站做几种产品合肥网络公司哪个最好
  • 长乐区建设局网站一般通过什么渠道了解防灾减灾知识