redis6.0.5之adlist阅读笔记--整数集合(intset)
***********************************************************************
整数集合的结构体定义如下
typedef struct intset {
uint32_t encoding; 编码格式
uint32_t length; 元素个数
int8_t contents[]; 元素保存数组
} intset;
***********************************************************************
/* Note that these encodings are ordered, so:
* INTSET_ENC_INT16 < INTSET_ENC_INT32 < INTSET_ENC_INT64. */
#define INTSET_ENC_INT16 (sizeof(int16_t))
#define INTSET_ENC_INT32 (sizeof(int32_t))
#define INTSET_ENC_INT64 (sizeof(int64_t))
sizeof(int16_t) 2
sizeof(int32_t) 4
sizeof(int64_t) 8
***********************************************************************
/* Return the required encoding for the provided value. */
对于提供的值,返回需要编码的类型
static uint8_t _intsetValueEncoding(int64_t v) {
if (v < INT32_MIN || v > INT32_MAX) 超过32位,那么需要64位
return INTSET_ENC_INT64;
else if (v < INT16_MIN || v > INT16_MAX) 超过16位,那么需要32位
return INTSET_ENC_INT32;
else
return INTSET_ENC_INT16;
}
INT32_MIN -2147483648
INT32_MAX 2147483647
INT16_MIN -32768
INT16_MAX 32767
***********************************************************************
/* Return the value at pos, given an encoding. */
给定一个编码类型,返回对应位置上的值
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位的类型
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;
}
}
小端情况下,什么也不做
#define memrev16ifbe(p) ((void)(0))
#define memrev32ifbe(p) ((void)(0))
#define memrev64ifbe(p) ((void)(0))
***********************************************************************
/* Return the value at pos, using the configured encoding. */
用配置编码类型,返回对应位置上的值
static int64_t _intsetGet(intset *is, int pos) {
return _intsetGetEncoded(is,pos,intrev32ifbe(is->encoding));
}
#define intrev32ifbe(v) (v)
***********************************************************************
/* Set the value at pos, using the configured encoding. */
用配置的编码存放值到给定地址
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;
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);
}
}
***********************************************************************
/* Create an empty intset. */ 新建
intset *intsetNew(void) {
intset *is = zmalloc(sizeof(intset));
is->encoding = intrev32ifbe(INTSET_ENC_INT16);
is->length = 0;
return is;
}
***********************************************************************
/* Resize the intset */ 扩容
static intset *intsetResize(intset *is, uint32_t len) {
uint32_t size = len*intrev32ifbe(is->encoding);
is = zrealloc(is,sizeof(intset)+size);
return is;
}
***********************************************************************
/* Search for the position of "value". Return 1 when the value was found and
* sets "pos" to the position of the value within the intset. Return 0 when
* the value is not present in the intset and sets "pos" to the position
* where "value" can be inserted. */
查找值应该存放的位置,返回1当该值在整数集合中找到,并且保存该值在整数集合中的位置。
返回0,当该值在整数集合不存在时,保存该值可以在整数集合中存放的位置
static uint8_t intsetSearch(intset *is, int64_t value, uint32_t *pos) {
int min = 0, max = intrev32ifbe(is->length)-1, mid = -1;
int64_t cur = -1;
/* The value can never be found when the set is empty */
不可能在一个空集合中找到该值
if (intrev32ifbe(is->length) == 0) { //如果长度为0,就没有数字,那么可以存放在第一个位置
if (pos) *pos = 0;
return 0;
} else {
/* Check for the case where we know we cannot find the value,
* but do know the insert position. */
把极端找不到该值的情况排除,但是保存可以存放的位置
if (value > _intsetGet(is,max)) {
if (pos) *pos = intrev32ifbe(is->length); //比最大值还大的情况,放在最后
return 0;
} else if (value < _intsetGet(is,0)) { //比最小值还小,放在最前面,即第一个位置
if (pos) *pos = 0;
return 0;
}
}
while(max >= min) { 在区间内开始查找,采用对半查找法
mid = ((unsigned int)min + (unsigned int)max) >> 1; //获取中位数
cur = _intsetGet(is,mid); //获取中位数的值
if (value > cur) { //在后半段
min = mid+1;
} else if (value < cur) //在前半段
max = mid-1;
} else {
break; //找到
}
}
if (value == cur) {
if (pos) *pos = mid; 找到情况下,保存位置,返回1
return 1;
} else {
if (pos) *pos = min; 找不到的情况下,返回可以按顺序保存该值的位置
return 0;
}
}
***********************************************************************
/* 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); 原长度
int prepend = value < 0 ? 1 : 0;
传入的值放在前面还是后面的标志, 小于0放在前面,意味着比所有值都要小(即小于原编码的最小值),大于同理
/* 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));
/* 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); 因为多了一个元素,原长度加1
return is;
}
***********************************************************************
static void intsetMoveTail(intset *is, uint32_t from, uint32_t to) {
void *src, *dst;
uint32_t bytes = intrev32ifbe(is->length)-from; 相差位置数,因为从0开始计数,所以不需要加1
uint32_t encoding = intrev32ifbe(is->encoding);
if (encoding == INTSET_ENC_INT64) {
src = (int64_t*)is->contents+from; 原位置
dst = (int64_t*)is->contents+to; 目标位置
bytes *= sizeof(int64_t); 需要拷贝的具体比特数目
} else if (encoding == INTSET_ENC_INT32) {
src = (int32_t*)is->contents+from;
dst = (int32_t*)is->contents+to;
bytes *= sizeof(int32_t);
} else {
src = (int16_t*)is->contents+from;
dst = (int16_t*)is->contents+to;
bytes *= sizeof(int16_t);
}
memmove(dst,src,bytes);
}
这里就不怕内存超出分配的空间 ?
比如from 为5,to为10,
***********************************************************************
/* 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. */
如果新值超出了原来的编码,那么需要升级编码。我们知道新值如果大于0就放在最后,小于0就放在最前面,
因为新值超出了原编码的范围
if (valenc > intrev32ifbe(is->encoding)) { //新值超出了原编码范围
/* This always succeeds, so we don't need to curry *success. */
这个总是成功的,所以我们不需要操作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,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;
}
***********************************************************************
/* Delete integer from intset */ 从集合中删除一个元素
intset *intsetRemove(intset *is, int64_t value, int *success) {
uint8_t valenc = _intsetValueEncoding(value);
uint32_t pos;
if (success) *success = 0;
if (valenc <= intrev32ifbe(is->encoding) && intsetSearch(is,value,&pos)) {
新值编码比原编码小并且在集合中(如果新值的编码大于原编码,那么肯定不在集合中)
uint32_t len = intrev32ifbe(is->length); 长度
/* We know we can delete */
满足了上述情况,可以删除
if (success) *success = 1;
/* Overwrite value with tail and update length */
if (pos < (len-1)) intsetMoveTail(is,pos+1,pos); 不是最后一个,就需要移动
is = intsetResize(is,len-1); 最后一个这里直接就缩减了
is->length = intrev32ifbe(len-1); 长度减少1
}
return is;
}
***********************************************************************
/* Determine whether a value belongs to this set */
确认一个值是否属于这个集合
uint8_t intsetFind(intset *is, int64_t value) {
uint8_t valenc = _intsetValueEncoding(value);
return valenc <= intrev32ifbe(is->encoding) && intsetSearch(is,value,NULL);
新值编码比原编码小并且在集合中(如果新值的编码大于原编码,那么肯定不在集合中)
}
***********************************************************************
/* Return random member */ 返回一个随机元素
int64_t intsetRandom(intset *is) {
return _intsetGet(is,rand()%intrev32ifbe(is->length));
}
***********************************************************************
/* Get the value at the given position. When this position is
* out of range the function returns 0, when in range it returns 1. */
返回给定位置的值,当给定位置超出了范围就返回0,在范围之内就返回1
uint8_t intsetGet(intset *is, uint32_t pos, int64_t *value) {
if (pos < intrev32ifbe(is->length)) { 在范围之内
*value = _intsetGet(is,pos); 获取值
return 1;
}
return 0;
}
***********************************************************************
/* Return intset length */ 返回集合元素个数
uint32_t intsetLen(const intset *is) {
return intrev32ifbe(is->length);
}
***********************************************************************
/* Return intset blob size in bytes. */ 返回集合整体字节数
size_t intsetBlobLen(intset *is) {
return sizeof(intset)+intrev32ifbe(is->length)*intrev32ifbe(is->encoding);
}
***********************************************************************