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

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 整数集合(intset)用于集合键。当一个集合只包含整数值元素,并且数量不多的时候,会使用整数集合作为集合键的底层实现。相对于直接保存字符串,整数集合能够很好地节约内存,但是由于是数组保存,需要特别关注数组长度。 定义:(intset.h) [cce lang=”c”] typedef str

整数集合(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

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
9天前
|
XML JSON NoSQL
Redis的常用数据结构之字符串类型
Redis的常用数据结构之字符串类型
19 0
|
16天前
|
存储 消息中间件 NoSQL
Redis数据类型详解:选择合适的数据结构优化你的应用
Redis数据类型详解:选择合适的数据结构优化你的应用
|
1月前
|
存储 NoSQL Java
使用 Redis 的 List 数据结构实现分页查询的思路
使用 Redis 的 List 数据结构实现分页查询的思路
|
21天前
|
存储 NoSQL 算法
【Redis技术进阶之路】「底层源码解析」揭秘高效存储模型与数据结构底层实现(字典)(二)
【Redis技术进阶之路】「底层源码解析」揭秘高效存储模型与数据结构底层实现(字典)
34 0
|
1月前
|
缓存 算法 安全
Java集合框架:深入探究数据结构与算法的精华
Java集合框架:深入探究数据结构与算法的精华
|
21天前
|
存储 NoSQL Redis
作者推荐 |【Redis技术进阶之路】「原理系列开篇」揭秘高效存储模型与数据结构底层实现(SDS)(三)
作者推荐 |【Redis技术进阶之路】「原理系列开篇」揭秘高效存储模型与数据结构底层实现(SDS)
19 0
|
23天前
|
存储 NoSQL Java
Redis 数据结构操作入门
Redis 数据结构操作入门
15 0
|
1月前
|
索引 Python
Python数据结构讲解集合
Python数据结构讲解集合
17 0
|
存储 NoSQL 关系型数据库
Redis命令——集合(Set)
Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。 Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
862 0
|
存储 NoSQL Redis
redis必杀命令:集合(Set)
题记: Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。 Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
1003 0

热门文章

最新文章