//更新整数集得编码,使编码升级为更大的整数并插入给定的值。 //注意value的值一定是比整数集的数都大或者都小,这样才有升级编码的意义 static intset *intsetUpgradeAndAdd(intset *is, int64_t value) { uint8_t curenc = intrev32ifbe(is->encoding); uint8_t newenc = _intsetValueEncoding(value); int length = intrev32ifbe(is->length); int prepend = value < 0 ? 1 : 0; /* First set new encoding and resize */ //将整数集的编码更新为新的编码后调用intsetResize重新分配内存 is->encoding = intrev32ifbe(newenc); is = intsetResize(is,intrev32ifbe(is->length)+1); /* Upgrade back-to-front so we don't overwrite values. * Note that the "prepend" variable is used to make sure we have an empty * space at either the beginning or the end of the intset. */ //调用_intsetGetEncoded(is,length,curenc)以旧的编码从整数集中取到数,设置到新的位置 //从后往前进行操作,这样才不会覆盖掉原来的数值。 //当value为负数是,prepend为1,这样第一个元素的位置就是value的,因为它一定是最小的值 //当value非负,prepend为0,这样最后一个元素的位置就是value的,因为它一定是最大的值 while(length--) _intsetSet(is,length+prepend,_intsetGetEncoded(is,length,curenc)); /* Set the value at the beginning or the end. */ if (prepend) _intsetSet(is,0,value); else _intsetSet(is,intrev32ifbe(is->length),value); is->length = intrev32ifbe(intrev32ifbe(is->length)+1); return is; }
/* * * 添加 value 并升级编码类型 * */ static intset *intsetUpgradeAndAdd(intset *is, int64_t value) { uint8_t curenc = intrev32ifbe(is->encoding); uint8_t newenc = _intsetValueEncoding(value); int length = intrev32ifbe(is->length); int prepend = value < 0 ? 1 : 0; /* First set new encoding and resize */ is->encoding = intrev32ifbe(newenc); // 设置 new encoding is = intsetResize(is,intrev32ifbe(is->length)+1); // 重新分配内存 /* Upgrade back-to-front so we don't overwrite values. * Note that the "prepend" variable is used to make sure we have an empty * space at either the beginning or the end of the intset. */ // 从后往前移动 while(length--) _intsetSet(is,length+prepend,_intsetGetEncoded(is,length,curenc)); /* Set the value at the beginning or the end. */ // 插入 val if (prepend) // value < 0 在 beginning 处插入 _intsetSet(is,0,value); else // value > 0 在 end 处插入 _intsetSet(is,intrev32ifbe(is->length),value); is->length = intrev32ifbe(intrev32ifbe(is->length)+1); return is; }
/* Upgrades the intset to a larger encoding and inserts the given integer. */ static intset *intsetUpgradeAndAdd(intset *is, int64_t value) { uint8_t curenc = intrev32ifbe(is->encoding); uint8_t newenc = _intsetValueEncoding(value); int length = intrev32ifbe(is->length); // value<0 头插,value>0 尾插 int prepend = value < 0 ? 1 : 0; // realloc /* First set new encoding and resize */ is->encoding = intrev32ifbe(newenc); is = intsetResize(is,intrev32ifbe(is->length)+1); // 逆向处理,防止数据被覆盖,一般的插入排序步骤 /* Upgrade back-to-front so we don't overwrite values. * Note that the "prepend" variable is used to make sure we have an empty * space at either the beginning or the end of the intset. */ while(length--) _intsetSet(is,length+prepend,_intsetGetEncoded(is,length,curenc)); // value<0 放在集合开头,否则放在集合末尾。 // 因为,此函数是对整数所占内存进行升级,意味着 value 不是在集合中最大就是最小! /* Set the value at the beginning or the end. */ if (prepend) _intsetSet(is,0,value); else _intsetSet(is,intrev32ifbe(is->length),value); // 更新 set size is->length = intrev32ifbe(intrev32ifbe(is->length)+1); return is; }
/* Upgrades the intset to a larger encoding and inserts the given integer. * * 根据值value所使用的编码方式,对整数集合的编码进行升级,并将值value添加到升级后的整数集合中。 * * 返回值:添加新元素之后的整数集合 * * T = O(N) */ static intset *intsetUpgradeAndAdd(intset *is, int64_t value) { // 当前的编码方式 uint8_t curenc = intrev32ifbe(is->encoding); // 新值所需的编码方式 uint8_t newenc = _intsetValueEncoding(value); // 当前集合的元素数量 int length = intrev32ifbe(is->length); // 根据value的值,决定是将它添加到底层数组的最前端还是最后端 // 注意,因为value的编码比集合原有的其他元素的编码都要大 // 所以value要么大于集合中的所有元素,要么小于集合中的所有元素 // 因此,value只能添加到底层数组的最前端或最后端 int prepend = value < 0 ? 1 : 0; /* First set new encoding and resize */ // 更新集合的编码方式 is->encoding = intrev32ifbe(newenc); // 根据新编码对集合(的底层数组)进行空间调整 // T = O(N) is = intsetResize(is,intrev32ifbe(is->length)+1); /* Upgrade back-to-front so we don't overwrite values. * Note that the "prepend" variable is used to make sure we have an empty * space at either the beginning or the end of the intset. */ // 根据集合原来的编码方式,从底层数组中取出集合元素 // 然后再将元素以新编码的方式添加到集合中 // 当完成了这个步骤以后,集合中所有原有的元素就完成了从旧编码到新编码的转换 // 因为新分配的空间都放在数组的后端,所以程序先从后端向前端移动元素 // 举个例子,假设原来有curenc编码的三个元素,它们在数组中排列如下: // | x | y | z | // 当程序对数组进行重分配之后,数组就被扩容了(符合?表示未使用的内存): // | x | y | z | ? | ? | ? | // 这时程序从数组后端开始,重新插入元素: // | x | y | z | ? | z | ? | // | x | y | y | z | ? | // | x | y | z | ? | // 最后,程序可以将新元素添加到最后?号标示的位置中: // | x | y | z | new | // 上面演示的是新元素比原来的所有元素都打的情况,也即是prepend==0 // 当新元素比原来的所有元素都小时(prepent==1),调整的过程如下: // | x | y | z | ? | ? | ? | // | x | y | z | ? | ? | z | // | x | y | z | ? | y | z | // | x | y | x | y | z | // 当添加新键时,原本的| x | y |的数据将被新值代替 // | new | x | y | z | // T = O(N) while(length--) _intsetSet(is,length+prepend,_intsetGetEncoded(is,length,curenc)); /* Set the value at the beginning or the end. */ // 设置新值,根据prepend的值来决定是添加到数组头还是数组尾 if (prepend) _intsetSet(is,0,value); else _intsetSet(is,intrev32ifbe(is->length),value); // 更新整数集合的元素数量 is->length = intrev32ifbe(intrev32ifbe(is->length)+1); return is; }
/* * 根据 value ,对 intset 所使用的编码方式进行升级,并扩容 intset * 最后将 value 插入到新 intset 中。 * * T = O(n) */ static intset *intsetUpgradeAndAdd(intset *is, int64_t value) { // 当前值的编码类型 uint8_t curenc = intrev32ifbe(is->encoding); // 新值的编码类型 uint8_t newenc = _intsetValueEncoding(value); // 元素数量 int length = intrev32ifbe(is->length); // 决定新值插入的位置(0 为头,1 为尾) int prepend = value < 0 ? 1 : 0; // 设置新编码,并根据新编码对 intset 进行扩容 is->encoding = intrev32ifbe(newenc); is = intsetResize(is,intrev32ifbe(is->length)+1); /* Upgrade back-to-front so we don't overwrite values. * Note that the "prepend" variable is used to make sure we have an empty * space at either the beginning or the end of the intset. */ // 从最后的元素开始进行重新插入 // 以新元素插入到最开始为例子,之前: // | 1 | 2 | 3 | // 之后: // | 1 | 2 | | 3 | 重插入 3 // | 1 | | 2 | 3 | 重插入 2 // | | 1 | 2 | 3 | 重插入 1 // | ?????? | 1 | 2 | 3 | ??? 预留给新元素的空位 // // "prepend" 是为插入新值而设置的索引偏移量 while(length--) _intsetSet(is,length+prepend,_intsetGetEncoded(is,length,curenc)); /* Set the value at the beginning or the end. */ if (prepend) _intsetSet(is,0,value); else _intsetSet(is,intrev32ifbe(is->length),value); // 更新 is 元素数量 is->length = intrev32ifbe(intrev32ifbe(is->length)+1); return is; }
/* Upgrades the intset to a larger encoding and inserts the given integer. */ static intset *intsetUpgradeAndAdd(intset *is, int64_t value) { uint8_t curenc = is->encoding; uint8_t newenc = _intsetValueEncoding(value); int length = is->length; int prepend = value < 0 ? 1 : 0; /* First set new encoding and resize */ is->encoding = newenc; is = intsetResize(is,is->length+1); /* Upgrade back-to-front so we don't overwrite values. * Note that the "prepend" variable is used to make sure we have an empty * space at either the beginning or the end of the intset. */ while(length--) _intsetSet(is,length+prepend,_intsetGetEncoded(is,length,curenc)); /* Set the value at the beginning or the end. */ if (prepend) _intsetSet(is,0,value); else _intsetSet(is,is->length,value); is->length++; return is; }
/* Return the value at pos, using the configured encoding. */ static int64_t _intsetGet(intset *is, int pos) { return _intsetGetEncoded(is,pos,is->encoding); }
/* Return the value at pos, using the configured encoding. */ int64_t _intsetGet(intset *is, int pos) { return _intsetGetEncoded(is,pos,intrev32ifbe(is->encoding)); }