[redis设计与实现][5]基本数据结构——整数集合

整数集合(intset)用于集合键。当一个集合只包含整数值元素,并且数量不多的时候,会使用整数集合作为集合键的底层实现。相对于直接保存字符串,整数集合能够很好地节约内存,但是由于是数组保存,需要特别关注数组长度。

定义:(intset.h)

[cce lang=”c”]
typedef struct intset {
//编码方式
uint32_t encoding;
//集合包含的元素数量
uint32_t length;
//保存元素的数组
int8_t contents[];
} intset;
[/cce]
encoding:

  • INTSETENCINT16:content数组每一项都是一个int16_t类型的数值(有符号)
  • INTSETENCINT32:content数组每一项都是一个int32_t类型的数值
  • INTSETENCINT64:content数组每一项都是一个int64_t类型的数值

整数集合支持长度升级,但是不支持长度降级。当插入的最大长度超过当前编码方式容纳的最大值的时候,会对编码类型进行升级。但是如果删除了一个大数字,整体整数集合不会再进行降级。

intset *intsetNew(void);(创建一个新的整数集合)
[cce lang=”c”]
intset *intsetNew(void) {
intset *is = zmalloc(sizeof(intset));
//对于x86架构采用小端序,#define intrev32ifbe(v) (v)
// 初始化的时候编码为INTSET_ENC_INT16
is->encoding = intrev32ifbe(INTSET_ENC_INT16);
is->length = 0;
return is;
}
[/cce]
intset *intsetAdd(intset *is, int64t value, uint8t *success);(添加到整数集合)
[cce lang=”c”]
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. */
//查找当前数值是否已经存在
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;
}
static uint8_t _intsetValueEncoding(int64_t v) {
if (v < INT32_MIN || v > INT32_MAX)
return INTSET_ENC_INT64;
else if (v < INT16_MIN || v > INT16_MAX)
return INTSET_ENC_INT32;
else
return INTSET_ENC_INT16;
}
//升级整数集合
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);
//重新分配整数集合大小
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)
//插入的值小于0,放到最前面
_intsetSet(is,0,value);
else
//插入的值大于0,放到最后面
_intsetSet(is,intrev32ifbe(is->length),value);
//修改整数集合长度
is->length = intrev32ifbe(intrev32ifbe(is->length)+1);
return is;
}
//重新分配整数集合大小
static intset *intsetResize(intset *is, uint32_t len) {
uint32_t size = len*intrev32ifbe(is->encoding);
is = zrealloc(is,sizeof(intset)+size);
return 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) {
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;
}
}
//插入到指定位置
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);
}
}
//查找数值是否存在(二分查找)
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) {
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. */
//如果插入数值比最大的大或者比最小的小,直接退出,设置pos
if (value > _intsetGet(is,intrev32ifbe(is->length)-1)) {
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;
return 1;
} else {
if (pos) *pos = min;
return 0;
}
}
//从指定位置开始移动到最尾
static void intsetMoveTail(intset *is, uint32_t from, uint32_t to) {
void *src, *dst;
uint32_t bytes = intrev32ifbe(is->length)-from;
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);
}
intset *intsetRemove(intset *is, int64_t value, int *success);(移除元素)

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);
}
return is;
}
[/cce]


转载自:https://coolex.info/blog/452.html

上一篇:自动化运维工具之puppet简单实用


下一篇:linux进入单用户模式修改管理员密码