redis下的adlist
//adlist.h
#ifndef __ADLIST__H__
#define __ADLIST__H__ typedef struct listNode_ {
struct listNode_ *prev;
struct listNode_ *next;
void *value;
} listNode; typedef struct listIter_ {
listNode *next;
int direction;
}listIter; typedef struct List_ {
listNode *head;
listNode *tail;
void *(*dup)(void *ptr);
void (*free)(void *ptr);
int (*match)(void *ptr, void *key);
unsigned long len;
}List; class ListM {
public:
enum {
AL_START_HEAD = ,
AL_START_TAIL = ,
AL_START_ERR
}; static List *ListCreate(void);
static void ListRelease(List *list); static List *ListAddNodeHead(List *list, void *value);
static List *ListAddNodeTail(List *list, void *value);
static List *ListInsertNode(List *list,
listNode *old_node,
void *value,
int after); static void ListDelNode(List *list, listNode *node); static listIter *ListGetIterator(List *list, int direction);
static listNode *ListNext(listIter *iter);
static void ListReleaseIterator(listIter *iter); static List *ListDup(List *orig);
static listNode *ListSearchKey(List *list, void *key);
static listNode *ListIndex(List *list, long index); static void ListRewind(List *list, listIter *li);
static void ListRewindTail(List *list, listIter *li);
static void ListRotate(List *list); static void Traversal(List *list);
}; #endif //__ADLIST__H__
//adlist.cpp
#include <stdlib.h>
#include <iostream>
#include "adlist.h"
#include "zmalloc.h" List* ListM::ListCreate(void) {
List *list;
if((list = (List *)zmalloc(sizeof(List))) == NULL)
return NULL; list->head = list->tail = NULL;
list->len = ;
list->dup = NULL;
list->free = NULL;
list->match = NULL;
return list;
} void ListM::ListRelease(List *list) {
unsigned long len = ;
listNode *current, *next; current = list->head;
len = list->len;
while(len--) {
next = current->next;
if(list->free) list->free(current->value);
zfree(current);
current = next;
}
zfree(list);
} List *ListM::ListAddNodeHead(List *list, void *value) { listNode *node; if((node = (listNode *)zmalloc(sizeof(*node))) == NULL) {
return NULL;
}
node->value = value; if(list->len == ) {
list->head = list->tail = node;
node->prev = node->next = NULL;
} else {
node->prev = NULL;
node->next = list->head;
list->head->prev = node;
list->head = node;
}
list->len++; return list;
} List *ListM::ListAddNodeTail(List *list, void *value) { listNode *node; if((node = (listNode *)zmalloc(sizeof(*node))) == NULL) {
return NULL;
} node->value = value;
if(list->len == ) {
list->head = list->tail = node;
node->prev = node->next = NULL;
} else {
node->prev = list->tail;
node->next = NULL;
list->tail->next = node;
list->tail = node;
}
list->len++; return list;
} List *ListM::ListInsertNode(List *list, listNode *old_node, void *value,int after) {
listNode *node; if((node = (listNode *)zmalloc(sizeof(*node))) == NULL) {
return NULL;
} node->value = value;
if(after) {
node->prev = old_node;
node->next = old_node->next;
if(list->tail == old_node) {
list->tail = node;
}
} else {
node->next = old_node;
node->prev = old_node->prev;
if(list->head == old_node) {
list->head = node;
}
} if(node->prev != NULL) {
node->prev->next = node;
} if(node->next != NULL) {
node->next->prev = node;
}
list->len++; return list;
} void ListM::ListDelNode(List *list, listNode *node) {
if(node->prev)
node->prev->next = node->next;
else
list->head = node->next; if(node->next)
node->next->prev = node->prev;
else
list->tail = node->prev; if(list->free) list->free(node->value);
zfree(node);
list->len--;
} listIter *ListM::ListGetIterator(List *list, int direction) { listIter *iter; if((iter = (listIter *)zmalloc(sizeof(*iter))) == NULL) return NULL; if(direction == AL_START_HEAD)
iter->next = list->head;
else
iter->next = list->tail;
iter->direction = direction; return iter;
} listNode *ListM::ListNext(listIter *iter) {
listNode *current = iter->next; if(current != NULL) {
if(iter->direction == AL_START_HEAD)
iter->next = current->next;
else
iter->next = current->prev;
}
return current;
} void ListM::ListReleaseIterator(listIter *iter) {
zfree(iter);
} listNode *ListM::ListSearchKey(List *list, void *key) {
listNode *node = NULL;
listIter *iter = NULL; iter = ListGetIterator(list, AL_START_HEAD);
while((node = ListNext(iter)) != NULL) {
if(list->match) {
if(list->match(node->value, key)) {
ListReleaseIterator(iter);
return node;
}
} else {
if(key == node->value) {
ListReleaseIterator(iter);
return node;
}
}
} ListReleaseIterator(iter);
return NULL;
} listNode *ListM::ListIndex(List *list, long index) {
listNode *node = NULL; if(index < ) {
index = (-index) - ;
node = list->tail;
while(index-- && node) node = node->prev;
} else {
node = list->head;
while(index-- && node) node = node->next;
}
return node;
} void ListM::ListRewind(List *list, listIter *li) {
li->next = list->head;
li->direction = AL_START_HEAD;
} void ListM::ListRewindTail(List *list, listIter *li) {
li->next = list->tail;
li->direction = AL_START_TAIL;
} void ListM::ListRotate(List *list) {
listNode *tail = list->tail; if(list->len <= ) return; list->tail = tail->prev;
list->tail->next = NULL; list->head->prev = tail;
tail->prev = NULL;
tail->next = list->head;
list->head = tail;
} void ListM::Traversal(List *list) {
if(list->head == list->tail ) return; listNode *node = NULL;
listIter *iter = ListGetIterator(list, AL_START_HEAD);
std::cout << "list data : ";
while((node = ListNext(iter)) != NULL) {
std::cout << *(int*)node->value << "\t";
}
std::cout << std::endl;
}
//main.cpp
#include <iostream>
#include "adlist.h" int main() { List *l = ListM::ListCreate(); int value = ;
ListM::ListAddNodeHead(l, (void*)&value); int value2 = ;
ListM::ListAddNodeHead(l, (void*)&value2); int value3 = ;
ListM::ListAddNodeTail(l, (void*)&value3); ListM::Traversal(l); return ;
}
redis下的adlist的更多相关文章
- redis下操作hash对象
redis下操作hash对象 hash用于存储对象,对象的格式为键值对 命令 设置 设置单个属性 HSET key field value 设置多个属性 HMSET key field value [ ...
- Redis源代码-数据结构Adlist双端列表
Redis的Adlist实现了数据结构中的双端链表,整个结构例如以下: 链表节点定义: typedef struct listNode { struct listNode *prev; struct ...
- 安装redis出现cc adlist.o /bin/sh:1:cc:not found的解决方法
安装redis时 提示执行make命令时提示 CC adlist.o /bin/sh: cc: 未找到命令 问题原因:这是由于系统没有安装gcc环境,因此在进行编译时才会出现上面提示,当安装好gc ...
- 安装redis出现cc adlist.o /bin/sh:1:cc:not found
安装redis时 提示执行make命令时, 提示 CC adlist.o /bin/sh: cc: 未找到命令 问题原因:这是由于系统没有安装gcc环境,因此在进行编译时才会出现上面提示,当安装好gc ...
- redis下操作Set和Zset
redis操作set 无序集合 元素为string类型 元素具有唯一性,不重复 命令 设置 添加元素 SADD key member [member ...] 获取 返回key集合所有的元素 SME ...
- redis下操作String
redis操作string string是redis最基本的类型 最大能存储512MB数据 string类型是二进制安全的,即可以为任何数据,比如数字.图片.序列化对象等 基本命令 设置 设置键值 s ...
- redis下的字符串处理
redis设计一款sds对象[字符串对象] 优点:可跨平台的内存处理zmalloc:内存消耗的线性增长优势:每次加SDS_MAX_PREALLOC(1MB)的空间: 重写了各种字符串操作的函数: 写跨 ...
- redis 下key的过期时间详解 :expire
memcached 和 redis 的set命令都有expire参数,可以设置key的过期时间.但是redis是一个可以对数据持久化的key-value database,它的key过期策略还是和me ...
- redis下操作列表list
list 列表的元素类型为string 按照插入顺序排序 在列表的头部或者尾部添加元素 命令 设置 在头部插入数据 LPUSH key value [value ...] 在尾部插入数据 RPUSH ...
随机推荐
- JAVA练手--String
package tet; public class kk { public static void main(String[] args) { //1. { String Stra = "1 ...
- zabbix与nagios八项重要对比 结论根据业务环境需求决定
1.web功能: Nagios简单直观,报警与数据都在同一页面,***.红色即为问题项.Nagios web端不要做任何配置. Zabbix监控数据与报警是分开的,查看问题项需要看触发器,查看数据在最 ...
- [转]bootstrap-datetimepicker 火狐浏览器报错
本文转自:https://segmentfault.com/a/1190000008457568 使用bootstrap-datetimepicker日期选择插件时发现在火狐浏览器下报错: 未压缩版报 ...
- spring security认证
1 开发基于表单的认证 Spring security核心的功能 认证(你是谁?) 授权(你能干什么?) 攻击防护(防止伪造身份) spring security实现了默认的用户名+密码认证,默认用户 ...
- 第8天:javascriptDOM小 案例、onmouseover 、onmouseout
案例 为元素注册点击事件,弹出对话框 <input type="button" id="btn" value="开发分离"> & ...
- [PHP] PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1)
PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1): 1.bench.php 可在PHP源代码的 php-src/Zend 目录 2.micro_bench.php 也可以在 PHP ...
- PowerDesigner16导出SQL时如何添加注释
添加注释方法 https://jingyan.baidu.com/article/47a29f24652e44c0142399c3.html 重点是修改value的值 alter table [%QU ...
- LOJ572: Misaka Network 与求和
传送门 假设 \(f^k(i)\) 就是 \(f(i)\) 莫比乌斯反演得到 \[ans=\sum_{i=1}^{N}\lfloor\frac{N}{i}\rfloor^2\sum_{d|i}f(d) ...
- Html5 锚 <a>的页内跳转, name=abc herf=#abc
锚点是网页制作中超级链接的一种,又叫命名锚记.命名锚记像一个迅速定位器一样是一种页面内的超级链接,运用相当普遍. 英文名:anchor 使用命名锚记可以在文档中设置标记,这些标记通常放在文档的特定主题 ...
- SQL-字符串运算符和函数
COALESCE(columnname,string) 函数 将 NULL 值作为字符串(用空字符串或其他字符串替换 NULL)- 接受一列输入值(字段)如果该字段为 NULL,则返回后面替换的字符串 ...