//更新整数集得编码,使编码升级为更大的整数并插入给定的值。 //注意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; }
/* Insert an integer in the intset */ intset *intsetAdd(intset *is, int64_t value, uint8_t *success) { uint8_t valenc = _intsetValueEncoding(value); uint32_t pos; if (success) *success = 1; /* Upgrade encoding if necessary. If we need to upgrade, we know that * this value should be either appended (if > 0) or prepended (if < 0), * because it lies outside the range of existing values. */ if (valenc > is->encoding) { /* This always succeeds, so we don't need to curry *success. */ return intsetUpgradeAndAdd(is,value); } else { /* Abort if the value is already present in the set. * This call will populate "pos" with the right position to insert * the value when it cannot be found. */ if (intsetSearch(is,value,&pos)) { if (success) *success = 0; return is; } is = intsetResize(is,is->length+1); if (pos < is->length) intsetMoveTail(is,pos,pos+1); } _intsetSet(is,pos,value); is->length++; 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; }
/* Insert an integer in the intset */ intset *intsetAdd(intset *is, int64_t value, uint8_t *success) { uint8_t valenc = _intsetValueEncoding(value); uint32_t pos; if (success) *success = 1; /* Upgrade encoding if necessary. If we need to upgrade, we know that * this value should be either appended (if > 0) or prepended (if < 0), * because it lies outside the range of existing values. */ // 需要插入整数的所需内存超出了原有集合整数的范围,即内存类型不同, // 则升级整数类型 if (valenc > intrev32ifbe(is->encoding)) { /* This always succeeds, so we don't need to curry *success. */ return intsetUpgradeAndAdd(is,value); // 正常,分配内存,插入 } else { // intset 内部不允许重复 /* Abort if the value is already present in the set. * This call will populate "pos" with the right position to insert * the value when it cannot be found. */ if (intsetSearch(is,value,&pos)) { if (success) *success = 0; return is; } // realloc is = intsetResize(is,intrev32ifbe(is->length)+1); // 迁移内存,腾出空间给新的数据。intsetMoveTail() 完成内存迁移工作 if (pos < intrev32ifbe(is->length)) intsetMoveTail(is,pos,pos+1); } // 在腾出的空间中设置新的数据 _intsetSet(is,pos,value); // 更新 intset size is->length = intrev32ifbe(intrev32ifbe(is->length)+1); return is; }
/* * 将 value 添加到集合中 * * 如果元素已经存在, *success 被设置为 0 , * 如果元素添加成功, *success 被设置为 1 。 * * T = O(n) */ intset *intsetAdd(intset *is, int64_t value, uint8_t *success) { uint8_t valenc = _intsetValueEncoding(value); uint32_t pos; if (success) *success = 1; /* Upgrade encoding if necessary. If we need to upgrade, we know that * this value should be either appended (if > 0) or prepended (if < 0), * because it lies outside the range of existing values. */ // 如果有需要,进行升级并插入新值 if (valenc > intrev32ifbe(is->encoding)) { /* This always succeeds, so we don't need to curry *success. */ return intsetUpgradeAndAdd(is,value); } else { /* Abort if the value is already present in the set. * This call will populate "pos" with the right position to insert * the value when it cannot be found. */ // 如果值已经存在,那么直接返回 // 如果不存在,那么设置 *pos 设置为新元素添加的位置 if (intsetSearch(is,value,&pos)) { if (success) *success = 0; return is; } // 扩张 is ,准备添加新元素 is = intsetResize(is,intrev32ifbe(is->length)+1); // 如果 pos 不是数组中最后一个位置, // 那么对数组中的原有元素进行移动 if (pos < intrev32ifbe(is->length)) intsetMoveTail(is,pos,pos+1); } // 添加新元素 _intsetSet(is,pos,value); // 更新元素数量 is->length = intrev32ifbe(intrev32ifbe(is->length)+1); return is; }
//将value插入到整数集中 intset *intsetAdd(intset *is, int64_t value, uint8_t *success) { uint8_t valenc = _intsetValueEncoding(value); uint32_t pos; if (success) *success = 1; /* Upgrade encoding if necessary. If we need to upgrade, we know that * this value should be either appended (if > 0) or prepended (if < 0), * because it lies outside the range of existing values. */ if (valenc > intrev32ifbe(is->encoding)) { //如果表示value所需编码比现整数集的编码要大,升级整数集并将元素插入 /* This always succeeds, so we don't need to curry *success. */ return intsetUpgradeAndAdd(is,value); } else { /* Abort if the value is already present in the set. * This call will populate "pos" with the right position to insert * the value when it cannot be found. */ //调用intsetSearch查找元素,如果已经存在则返回0 if (intsetSearch(is,value,&pos)) { if (success) *success = 0; return is; } //扩大内存空间 is = intsetResize(is,intrev32ifbe(is->length)+1); //将原来从pos到末尾的元素都向后移动一位 if (pos < intrev32ifbe(is->length)) intsetMoveTail(is,pos,pos+1); } //将value设到pos位上 _intsetSet(is,pos,value); //更新元素个数 is->length = intrev32ifbe(intrev32ifbe(is->length)+1); return is; }
/* Insert an integer in the intset * * 尝试将元素value添加到整数集合中。 * * *success的值指示添加是否成功: * -如果添加成功,那么将*success的值设为1。 * -因为元素已存在而造成添加失败时,将*success的值设为0。 * * T = O(N) */ intset *intsetAdd(intset *is, int64_t value, uint8_t *success) { // 计算编码value所需的长度 uint8_t valenc = _intsetValueEncoding(value); uint32_t pos; // 默认设置插入为成功 if (success) *success = 1; /* Upgrade encoding if necessary. If we need to upgrade, we know that * this value should be either appended (if > 0) or prepended (if < 0), * because it lies outside the range of existing values. */ // 如果value的编码比整数集合现在的编码要大 // 那么表示value必然可以添加到整数集合中 // 并且整数集合需要对自身进行升级,才能满足value所需的编码 if (valenc > intrev32ifbe(is->encoding)) { /* This always succeeds, so we don't need to curry *success. */ // T = O(N) return intsetUpgradeAndAdd(is,value); } else { // 运行到这里,表示整数集合现有的编码方式适用于value /* Abort if the value is already present in the set. * This call will populate "pos" with the right position to insert * the value when it cannot be found. */ // 在整数集合中查找value,看他是否存在: // -如果存在,那么将*success设置为0,并返回未经改动的整数集合 // -如果不存在,那么可以插入value的位置将被保存到pos指针中 // 等待后续程序使用 if (intsetSearch(is,value,&pos)) { if (success) *success = 0; return is; } // 运行到这里,表示value不存在于集合中 // 程序需要将value添加到整数集合中 // 为value在集合中分配空间 is = intsetResize(is,intrev32ifbe(is->length)+1); // 如果新元素不是被添加到底层数组的末端 // 那么需要对现有元素的数据进行移动,空出pos上的位置,用于设置新值 // 举个例子 // 如果数组为: // | x | y | z | ? | // |<----->| // 而新元素n的pos为1,那么数组将移动y和z两个元素 // | x | y | y | z | // |<----->| // 这样就可以将新元素设置到pos上了: // | x | n | y | z | // T = O(N) if (pos < intrev32ifbe(is->length)) intsetMoveTail(is,pos,pos+1); } // 将新值设置到底层数组的指定位置中 _intsetSet(is,pos,value); // 增一集合元素数量的计数器 is->length = intrev32ifbe(intrev32ifbe(is->length)+1); // 返回添加新元素后的整数集合 return is; /* p.s.上面的代码可以重构成以下更简单的形式: if (valenc > intrev32(is->encoding)) { return intsetUpgradeAndAdd(is, value); } if (intsetSearch(is, value, &pos)) { if (success) *success = 0; return is; } else { is = intsetResize(is, intrev32ifbe(is->length) + 1); if (pos < intrev32ifbe(is->length)) intsetMoveTail(is, pos, pos + 1); _intsetSet(is, pos, value); is->length = intrev32ifbe(intrev32ifbe(is->length) + 1); return is; } */ }