/* Set the value at pos, using the configured encoding. */ static void _intsetSet(intset *is, int pos, int64_t value) { if (is->encoding == INTSET_ENC_INT64) { ((int64_t*)is->contents)[pos] = value; memrev64ifbe(((int64_t*)is->contents)+pos); } else if (is->encoding == INTSET_ENC_INT32) { ((int32_t*)is->contents)[pos] = (int32_t)value; memrev32ifbe(((int32_t*)is->contents)+pos); } else { ((int16_t*)is->contents)[pos] = (int16_t)value; memrev16ifbe(((int16_t*)is->contents)+pos); } }
/* * * 设置以 pos 为起点的特定的 enc (编码) 的值 * */ static void _intsetSet(intset *is, int pos, int64_t value) { uint32_t encoding = intrev32ifbe(is->encoding); if (encoding == INTSET_ENC_INT64) { ((int64_t*)is->contents)[pos] = value; // value 顺序存放在数组 contents 中 memrev64ifbe(((int64_t*)is->contents)+pos); // 统一大小端 } else if (encoding == INTSET_ENC_INT32) { ((int32_t*)is->contents)[pos] = value; memrev32ifbe(((int32_t*)is->contents)+pos); } else { ((int16_t*)is->contents)[pos] = value; memrev16ifbe(((int16_t*)is->contents)+pos); } }
//将is中pos位置上的值设为value static void _intsetSet(intset *is, int pos, int64_t value) { uint32_t encoding = intrev32ifbe(is->encoding); //根据编码,将contents转换为对应得数组类型,然后对该位置的元素赋值 if (encoding == INTSET_ENC_INT64) { ((int64_t*)is->contents)[pos] = value; memrev64ifbe(((int64_t*)is->contents)+pos); } else if (encoding == INTSET_ENC_INT32) { ((int32_t*)is->contents)[pos] = value; memrev32ifbe(((int32_t*)is->contents)+pos); } else { ((int16_t*)is->contents)[pos] = value; memrev16ifbe(((int16_t*)is->contents)+pos); } }
/* * 根据给定的编码方式,返回给定位置上的值 * * T = theta(1) */ static int64_t _intsetGetEncoded(intset *is, int pos, uint8_t enc) { int64_t v64; int32_t v32; int16_t v16; if (enc == INTSET_ENC_INT64) { memcpy(&v64,((int64_t*)is->contents)+pos,sizeof(v64)); memrev64ifbe(&v64); return v64; } else if (enc == INTSET_ENC_INT32) { memcpy(&v32,((int32_t*)is->contents)+pos,sizeof(v32)); memrev32ifbe(&v32); return v32; } else { memcpy(&v16,((int16_t*)is->contents)+pos,sizeof(v16)); memrev16ifbe(&v16); return v16; } }
/* Set the value at pos, using the configured encoding. * * 根据集合的编码方式,将底层数组在pos位置上的值设为value。 * * T = O(1) */ static void _intsetSet(intset *is, int pos, int64_t value) { // 取出集合的编码方式 uint32_t encoding = intrev32ifbe(is->encoding); // 根据编码((Enc_t*)is->contents)将数组转换回正确的类型 // 然后((Enc_t*)is->contents)[pos]定位到数组索引上 // 接着((Enc_t*)is->contents)[pos] = value将值赋给数组 // 最后,((Enc_t*)is->contents)+pos定位到刚刚设置的新值上 // 如果有需要的话,memrevEncifbe将对值进行大小端转换 if (encoding == INTSET_ENC_INT64) { ((int64_t*)is->contents)[pos] = value; memrev64ifbe(((int64_t*)is->contents)+pos); } else if (encoding == INTSET_ENC_INT32) { ((int32_t*)is->contents)[pos] = value; memrev32ifbe(((int32_t*)is->contents)+pos); } else { ((int16_t*)is->contents)[pos] = value; memrev16ifbe(((int16_t*)is->contents)+pos); } }
//对于给定的编码enc以及位置pos,返回is中的值 static int64_t _intsetGetEncoded(intset *is, int pos, uint8_t enc) { int64_t v64; int32_t v32; int16_t v16; if (enc == INTSET_ENC_INT64) { //如果是64的编码,将contents转换为指向int64_t的指针,移动到第pos个元素的位置, //将这里起得64位内存复制到v64中。 memcpy(&v64,((int64_t*)is->contents)+pos,sizeof(v64)); memrev64ifbe(&v64); return v64; } else if (enc == INTSET_ENC_INT32) { //同上,只是这时contents相当与是存放int32_t的数组 memcpy(&v32,((int32_t*)is->contents)+pos,sizeof(v32)); memrev32ifbe(&v32); return v32; } else { //同上,只是这时contents相当与是存放int16_t的数组 memcpy(&v16,((int16_t*)is->contents)+pos,sizeof(v16)); memrev16ifbe(&v16); return v16; } }
/* Return the value at pos, given an encoding. * * 根据给定的编码方式enc,返回集合的底层数组在pos索引上的元素。 * * T = O(1) */ static int64_t _intsetGetEncoded(intset *is, int pos, uint8_t enc) { int64_t v64; int32_t v32; int16_t v16; // ((ENCODING*)is->contents) 首先将数组转换回被编码的类型 // 然后((ENCODING*)is->contents)+pos计算出元素在数组中的正确位置 // 之后member(&vEnc, ..., sizeof(vEnc))再从数组中拷贝出正确数量的字节 // 如果有需要的话,memrevEncifbe(&vEnc)会对拷贝出的字节进行大小端转换 // 最后将值返回 if (enc == INTSET_ENC_INT64) { memcpy(&v64,((int64_t*)is->contents)+pos,sizeof(v64)); memrev64ifbe(&v64); return v64; } else if (enc == INTSET_ENC_INT32) { memcpy(&v32,((int32_t*)is->contents)+pos,sizeof(v32)); memrev32ifbe(&v32); return v32; } else { memcpy(&v16,((int16_t*)is->contents)+pos,sizeof(v16)); memrev16ifbe(&v16); return v16; } }