fixed bugs in acl_dlink.c for adding continuous integers;

acl_dlink.c supports for adding negative integers.
This commit is contained in:
zhengshuxin 2017-09-13 11:17:00 +08:00
parent 3d864ea148
commit 8ada09778c
5 changed files with 107 additions and 55 deletions

View File

@ -1,6 +1,10 @@
修改历史列表: 修改历史列表:
------------------------------------------------------------------------ ------------------------------------------------------------------------
608) 2017.9.13
608.1) bugfix: acl_dlink.c 二分块算法当添加两个连续的数据块时,应该进行合并
608.2) feature: acl_dlink.c 二分块算法支持添加负整数
607) 2017.9.6 607) 2017.9.6
607.1) bugfix: acl_mylog.c 内部的线程锁采用递归锁,否则,当记日志时收到信号再 607.1) bugfix: acl_mylog.c 内部的线程锁采用递归锁,否则,当记日志时收到信号再
记日志时会造成死锁 记日志时会造成死锁

View File

@ -13,8 +13,8 @@ extern "C" {
* *
*/ */
typedef struct { typedef struct {
acl_uint64 begin; acl_int64 begin;
acl_uint64 end; acl_int64 end;
void *pnode; void *pnode;
} ACL_DITEM; } ACL_DITEM;
@ -72,69 +72,69 @@ ACL_API ACL_DITEM *acl_dlink_lookup2_by_item(const ACL_DLINK *plink,
/** /**
* *
* @param plink {const ACL_DLINK*} * @param plink {const ACL_DLINK*}
* @param n {acl_uint64} * @param n {acl_int64}
* @return {ACL_DITEM*} * @return {ACL_DITEM*}
*/ */
ACL_API ACL_DITEM *acl_dlink_lookup(const ACL_DLINK *plink, acl_uint64 n); ACL_API ACL_DITEM *acl_dlink_lookup(const ACL_DLINK *plink, acl_int64 n);
/** /**
* *
* @param plink {const ACL_DLINK*} * @param plink {const ACL_DLINK*}
* @param n {acl_uint64} * @param n {acl_int64}
* @param pidx {int*} * @param pidx {int*}
* @return {ACL_DITEM*} * @return {ACL_DITEM*}
*/ */
ACL_API ACL_DITEM *acl_dlink_lookup2(const ACL_DLINK *plink, ACL_API ACL_DITEM *acl_dlink_lookup2(const ACL_DLINK *plink,
acl_uint64 n, int *pidx); acl_int64 n, int *pidx);
/** /**
* *
* @param plink {const ACL_DLINK*} * @param plink {const ACL_DLINK*}
* @param begin {acl_uint64} * @param begin {acl_int64}
* @param end {acl_uint64} * @param end {acl_int64}
* @param pidx {int*} * @param pidx {int*}
* @return {ACL_DITEM*} * @return {ACL_DITEM*}
*/ */
ACL_API ACL_DITEM *acl_dlink_lookup_range(const ACL_DLINK *plink, ACL_API ACL_DITEM *acl_dlink_lookup_range(const ACL_DLINK *plink,
acl_uint64 begin, acl_uint64 end, int *pidx); acl_int64 begin, acl_int64 end, int *pidx);
/** /**
* *
* @param plink {const ACL_DLINK*} * @param plink {const ACL_DLINK*}
* @param off {acl_uint64} * @param off {acl_int64}
* @param pidx {int*} * @param pidx {int*}
* @return {ACL_DITEM*} * @return {ACL_DITEM*}
*/ */
ACL_API ACL_DITEM *acl_dlink_lookup_larger(const ACL_DLINK *plink, ACL_API ACL_DITEM *acl_dlink_lookup_larger(const ACL_DLINK *plink,
acl_uint64 off, int *pidx); acl_int64 off, int *pidx);
/** /**
* *
* @param plink {const ACL_DLINK*} * @param plink {const ACL_DLINK*}
* @param off {acl_uint64} * @param off {acl_int64}
* @param pidx {int*} * @param pidx {int*}
* @return {ACL_DITEM*} * @return {ACL_DITEM*}
*/ */
ACL_API ACL_DITEM *acl_dlink_lookup_lower(const ACL_DLINK *plink, ACL_API ACL_DITEM *acl_dlink_lookup_lower(const ACL_DLINK *plink,
acl_uint64 off, int *pidx); acl_int64 off, int *pidx);
/** /**
* *
* @param plink {ACL_DLINK*} * @param plink {ACL_DLINK*}
* @param begin {acl_uint64} * @param begin {acl_int64}
* @param end {acl_uint64} * @param end {acl_int64}
* @return {ACL_DITEM*} * @return {ACL_DITEM*}
*/ */
ACL_API ACL_DITEM *acl_dlink_insert(ACL_DLINK *plink, ACL_API ACL_DITEM *acl_dlink_insert(ACL_DLINK *plink,
acl_uint64 begin, acl_uint64 end); acl_int64 begin, acl_int64 end);
/** /**
* *
* @param plink {ACL_DLINK*} * @param plink {ACL_DLINK*}
* @param n {acl_uint64} * @param n {acl_int64}
* @return {int} 0OK-1: * @return {int} 0OK-1:
*/ */
ACL_API int acl_dlink_delete(ACL_DLINK *plink, acl_uint64 n); ACL_API int acl_dlink_delete(ACL_DLINK *plink, acl_int64 n);
/** /**
* *
@ -149,18 +149,18 @@ ACL_API int acl_dlink_delete_by_item(ACL_DLINK *plink, ACL_DITEM *pitem);
* @deprecated * @deprecated
*/ */
ACL_API ACL_DITEM *acl_dlink_modify(ACL_DLINK *plink, ACL_API ACL_DITEM *acl_dlink_modify(ACL_DLINK *plink,
acl_uint64 begin, acl_uint64 end); acl_int64 begin, acl_int64 end);
/** /**
* , * ,
* *
* @param plink {ACL_DLINK*} * @param plink {ACL_DLINK*}
* @param begin {acl_uint64} * @param begin {acl_int64}
* @param end {acl_uint64} * @param end {acl_int64}
* @return {int} 0OK-1: * @return {int} 0OK-1:
*/ */
ACL_API int acl_dlink_delete_range(ACL_DLINK *plink, ACL_API int acl_dlink_delete_range(ACL_DLINK *plink,
acl_uint64 begin, acl_uint64 end); acl_int64 begin, acl_int64 end);
/** /**
* *

View File

@ -5,12 +5,55 @@ static ACL_DLINK *build(void)
const char *myname = "build"; const char *myname = "build";
ACL_DLINK *dlink = acl_dlink_create(100); ACL_DLINK *dlink = acl_dlink_create(100);
acl_dlink_insert(dlink, 0, 10); printf("add: -10 -- -10\r\n");
acl_dlink_insert(dlink, -10, -10);
printf("add: -9 -- -9\r\n");
acl_dlink_insert(dlink, -9, -9);
printf("add: -8 -- -7\r\n");
acl_dlink_insert(dlink, -8, -7);
printf("\r\n");
printf("add: -4 -- -4\r\n");
acl_dlink_insert(dlink, -4, -4);
printf("\r\n");
printf("add: -2 -- -1\r\n");
acl_dlink_insert(dlink, -2, -1);
printf("add: 0 -- 7\r\n");
acl_dlink_insert(dlink, 0, 7);
printf("add: 8 -- 10\r\n");
acl_dlink_insert(dlink, 8, 10); acl_dlink_insert(dlink, 8, 10);
printf("add: 11 -- 10\r\n");
acl_dlink_insert(dlink, 11, 10);
printf("add: 12 -- 14\r\n");
acl_dlink_insert(dlink, 12, 12);
printf("add: 13 -- 14\r\n");
acl_dlink_insert(dlink, 13, 14);
printf("add: 15 -- 16\r\n");
acl_dlink_insert(dlink, 15, 16);
printf("add: 17 -- 18\r\n");
acl_dlink_insert(dlink, 17, 18);
printf("add: 15 -- 18\r\n");
acl_dlink_insert(dlink, 15, 18);
printf("\r\n");
printf("add: 20 -- 21\r\n");
acl_dlink_insert(dlink, 20, 21); acl_dlink_insert(dlink, 20, 21);
printf("add: 21 -- 28\r\n");
acl_dlink_insert(dlink, 21, 28); acl_dlink_insert(dlink, 21, 28);
acl_dlink_insert(dlink, 31, 38); printf("\r\n");
printf("add: 31 -- 40\r\n");
acl_dlink_insert(dlink, 31, 40);
printf("add: 32 -- 40\r\n");
acl_dlink_insert(dlink, 32, 40);
printf("add: 33 -- 35\r\n");
acl_dlink_insert(dlink, 33, 35);
printf("add: 41 -- 48\r\n");
acl_dlink_insert(dlink, 41, 48); acl_dlink_insert(dlink, 41, 48);
printf("\r\n");
printf("add: 51 -- 58\r\n");
acl_dlink_insert(dlink, 51, 58); acl_dlink_insert(dlink, 51, 58);
printf("\r\n%s:\r\n", myname); printf("\r\n%s:\r\n", myname);
@ -99,6 +142,10 @@ static void test(void)
} }
int main(int argc acl_unused, char *argv[] acl_unused) int main(int argc acl_unused, char *argv[] acl_unused)
{ {
acl_dlink_free(build());
printf("Enter any key to continue ...\r\n");
getchar();
test(); test();
return (0); return (0);
} }

0
lib_acl/samples/dlink/valgrind.sh Normal file → Executable file
View File

View File

@ -49,7 +49,7 @@ static void dlink_free_callback(void *arg)
* if idx > 0 && idx <= a->count - 1 -----> * if idx > 0 && idx <= a->count - 1 ----->
* if idx > a->count - 1 -----> * if idx > a->count - 1 ----->
*/ */
static int scope_pos(const ACL_ARRAY *a, acl_uint64 n) static int scope_pos(const ACL_ARRAY *a, acl_int64 n)
{ {
ACL_DITEM *pitem_left, *pitem_right; ACL_DITEM *pitem_left, *pitem_right;
int lidx, hidx, midx, ridx, idx; int lidx, hidx, midx, ridx, idx;
@ -108,19 +108,19 @@ static int scope_pos(const ACL_ARRAY *a, acl_uint64 n)
return idx; return idx;
} }
static int begin_pos(const ACL_ARRAY *a, acl_uint64 n) static int begin_pos(const ACL_ARRAY *a, acl_int64 n)
{ {
return scope_pos(a, n); return scope_pos(a, n);
} }
static int end_pos(const ACL_ARRAY *a, acl_uint64 n) static int end_pos(const ACL_ARRAY *a, acl_int64 n)
{ {
return scope_pos(a, n); return scope_pos(a, n);
} }
#ifdef _USE_PRED_INSERT_ #ifdef _USE_PRED_INSERT_
static ACL_DITEM *dlink_pred_insert(ACL_ARRAY *a, int idx_position, static ACL_DITEM *dlink_pred_insert(ACL_ARRAY *a, int idx_position,
acl_uint64 begin, acl_uint64 end) acl_int64 begin, acl_int64 end)
{ {
ACL_DITEM *pitem; ACL_DITEM *pitem;
int ret; int ret;
@ -142,7 +142,7 @@ static ACL_DITEM *dlink_pred_insert(ACL_ARRAY *a, int idx_position,
#endif #endif
static ACL_DITEM *dlink_succ_insert(ACL_ARRAY *a, int idx_position, static ACL_DITEM *dlink_succ_insert(ACL_ARRAY *a, int idx_position,
acl_uint64 begin, acl_uint64 end) acl_int64 begin, acl_int64 end)
{ {
ACL_DITEM *pitem; ACL_DITEM *pitem;
int ret; int ret;
@ -162,7 +162,7 @@ static ACL_DITEM *dlink_succ_insert(ACL_ARRAY *a, int idx_position,
return pitem; return pitem;
} }
static ACL_DITEM *dlink_append(ACL_ARRAY *a, acl_uint64 begin, acl_uint64 end) static ACL_DITEM *dlink_append(ACL_ARRAY *a, acl_int64 begin, acl_int64 end)
{ {
ACL_DITEM *pitem; ACL_DITEM *pitem;
int ret; int ret;
@ -182,7 +182,7 @@ static ACL_DITEM *dlink_append(ACL_ARRAY *a, acl_uint64 begin, acl_uint64 end)
return pitem; return pitem;
} }
static ACL_DITEM *dlink_prepend(ACL_ARRAY *a, acl_uint64 begin, acl_uint64 end) static ACL_DITEM *dlink_prepend(ACL_ARRAY *a, acl_int64 begin, acl_int64 end)
{ {
ACL_DITEM *pitem; ACL_DITEM *pitem;
int ret; int ret;
@ -216,7 +216,7 @@ static int dlink_node_merge(ACL_ARRAY *a, int idx_obj_begin, int idx_src_begin)
return 0; return 0;
} }
static ACL_DITEM *dlink_add(ACL_ARRAY *a, acl_uint64 begin, acl_uint64 end) static ACL_DITEM *dlink_add(ACL_ARRAY *a, acl_int64 begin, acl_int64 end)
{ {
ACL_DITEM *pitem_right, *pitem_left, *pitem; ACL_DITEM *pitem_right, *pitem_left, *pitem;
int idx_begin, idx_end; int idx_begin, idx_end;
@ -255,7 +255,7 @@ static ACL_DITEM *dlink_add(ACL_ARRAY *a, acl_uint64 begin, acl_uint64 end)
* this is to say the begin and end is on the same d-link * this is to say the begin and end is on the same d-link
*/ */
if (end < pitem_left->begin) { if (end + 1 < pitem_left->begin) {
/* /*
* here idx_begin == idx_end must be equal to 0 * here idx_begin == idx_end must be equal to 0
* the begin and the end must be less * the begin and the end must be less
@ -266,7 +266,7 @@ static ACL_DITEM *dlink_add(ACL_ARRAY *a, acl_uint64 begin, acl_uint64 end)
return pitem; return pitem;
} }
if (begin > pitem_left->end) { if (begin > pitem_left->end + 1) {
/* /*
* this is to say begin and end * this is to say begin and end
* between the current node's end * between the current node's end
@ -321,7 +321,7 @@ static ACL_DITEM *dlink_add(ACL_ARRAY *a, acl_uint64 begin, acl_uint64 end)
* begin < pitem_right->begin; * begin < pitem_right->begin;
*/ */
if(begin < pitem_left->begin) { if (begin < pitem_left->begin) {
/* /*
* in the first position of the array * in the first position of the array
* idx_begin == 0 and idx_end >= 1 * idx_begin == 0 and idx_end >= 1
@ -339,7 +339,7 @@ static ACL_DITEM *dlink_add(ACL_ARRAY *a, acl_uint64 begin, acl_uint64 end)
* ===> <= end * ===> <= end
*/ */
if(begin <= pitem_left->end) { if (begin <= pitem_left->end + 1) {
/* /*
* ===> pitem_left->begin * ===> pitem_left->begin
* ===> <= begin * ===> <= begin
@ -347,17 +347,18 @@ static ACL_DITEM *dlink_add(ACL_ARRAY *a, acl_uint64 begin, acl_uint64 end)
* ===> < pitem_right->begin * ===> < pitem_right->begin
* ===> <= end * ===> <= end
* ===> so, just merge the nodes between the * ===> so, just merge the nodes between the
* ===> pitem_left node and the pitem_right node, and include both of them * ===> pitem_left node and the pitem_right node,
* ===> and include both of them
*/ */
/* /*
* merge the pitem_left node, begin ---> end into pitem_right node, * merge the pitem_left node, begin ---> end into pitem_right
* and merge all nodes into one node which are between * node, and merge all nodes into one node which are between
* the pitem_left node and the pitem_right node, * the pitem_left node and the pitem_right node,
* include both of pitem_left node and pitem_right node * include both of pitem_left node and pitem_right node
*/ */
if(end > pitem_right->end) { if (end > pitem_right->end) {
/* /*
* ===> pitem_left->begin * ===> pitem_left->begin
* ===> <= begin * ===> <= begin
@ -401,7 +402,7 @@ static ACL_DITEM *dlink_add(ACL_ARRAY *a, acl_uint64 begin, acl_uint64 end)
* include idx_begin + 1 node and pitem_righ node * include idx_begin + 1 node and pitem_righ node
*/ */
if(end > pitem_right->end) { if (end > pitem_right->end) {
/* /*
* ===> pitem_left->begin * ===> pitem_left->begin
* ===> <= pitem_left->end * ===> <= pitem_left->end
@ -503,12 +504,12 @@ ACL_DITEM *acl_dlink_lookup2_by_item(const ACL_DLINK *plink, ACL_DITEM *pitem, i
return NULL; return NULL;
} }
ACL_DITEM *acl_dlink_lookup(const ACL_DLINK *plink, acl_uint64 n) ACL_DITEM *acl_dlink_lookup(const ACL_DLINK *plink, acl_int64 n)
{ {
return acl_dlink_lookup2(plink, n, NULL); return acl_dlink_lookup2(plink, n, NULL);
} }
ACL_DITEM *acl_dlink_lookup2(const ACL_DLINK *plink, acl_uint64 n, int *pidx) ACL_DITEM *acl_dlink_lookup2(const ACL_DLINK *plink, acl_int64 n, int *pidx)
{ {
int lidx, midx, hidx; int lidx, midx, hidx;
@ -535,8 +536,8 @@ ACL_DITEM *acl_dlink_lookup2(const ACL_DLINK *plink, acl_uint64 n, int *pidx)
return NULL; /*not in the d_link scope */ return NULL; /*not in the d_link scope */
} }
ACL_DITEM *acl_dlink_lookup_range(const ACL_DLINK *plink, acl_uint64 begin, ACL_DITEM *acl_dlink_lookup_range(const ACL_DLINK *plink, acl_int64 begin,
acl_uint64 end, int *pidx) acl_int64 end, int *pidx)
{ {
ACL_DITEM *ditem; ACL_DITEM *ditem;
@ -551,7 +552,7 @@ ACL_DITEM *acl_dlink_lookup_range(const ACL_DLINK *plink, acl_uint64 begin,
} }
ACL_DITEM *acl_dlink_lookup_larger(const ACL_DLINK *plink, ACL_DITEM *acl_dlink_lookup_larger(const ACL_DLINK *plink,
acl_uint64 off, int *pidx) acl_int64 off, int *pidx)
{ {
int i, size; int i, size;
@ -571,7 +572,7 @@ ACL_DITEM *acl_dlink_lookup_larger(const ACL_DLINK *plink,
} }
ACL_DITEM *acl_dlink_lookup_lower(const ACL_DLINK *plink, ACL_DITEM *acl_dlink_lookup_lower(const ACL_DLINK *plink,
acl_uint64 off, int *pidx) acl_int64 off, int *pidx)
{ {
int i, size; int i, size;
@ -590,10 +591,10 @@ ACL_DITEM *acl_dlink_lookup_lower(const ACL_DLINK *plink,
return NULL; /*not in the d_link scope */ return NULL; /*not in the d_link scope */
} }
ACL_DITEM *acl_dlink_insert(ACL_DLINK *plink, acl_uint64 begin, acl_uint64 end) ACL_DITEM *acl_dlink_insert(ACL_DLINK *plink, acl_int64 begin, acl_int64 end)
{ {
if (begin > end) { if (begin > end) {
acl_uint64 tmp; acl_int64 tmp;
/* swap the begin and end if end < begin */ /* swap the begin and end if end < begin */
tmp = begin; tmp = begin;
begin = end; begin = end;
@ -609,7 +610,7 @@ ACL_DITEM *acl_dlink_insert(ACL_DLINK *plink, acl_uint64 begin, acl_uint64 end)
return dlink_add(plink->parray, begin, end); return dlink_add(plink->parray, begin, end);
} }
int acl_dlink_delete(ACL_DLINK *plink, acl_uint64 n) int acl_dlink_delete(ACL_DLINK *plink, acl_int64 n)
{ {
const ACL_DITEM *ditem; const ACL_DITEM *ditem;
int idx; int idx;
@ -631,7 +632,7 @@ int acl_dlink_delete_by_item(ACL_DLINK *plink, ACL_DITEM *pitem)
return 0; return 0;
} }
int acl_dlink_delete_range(ACL_DLINK *plink, acl_uint64 begin, acl_uint64 end) int acl_dlink_delete_range(ACL_DLINK *plink, acl_int64 begin, acl_int64 end)
{ {
ACL_ARRAY *parray = plink->parray; ACL_ARRAY *parray = plink->parray;
ACL_DITEM *pitem, *pitem_low; ACL_DITEM *pitem, *pitem_low;
@ -671,7 +672,7 @@ int acl_dlink_delete_range(ACL_DLINK *plink, acl_uint64 begin, acl_uint64 end)
return 0; return 0;
} else if (end < pitem->end) { } else if (end < pitem->end) {
/* pitem->begin < begin <= end < pitem->end */ /* pitem->begin < begin <= end < pitem->end */
acl_uint64 tmp_begin, tmp_end; acl_int64 tmp_begin, tmp_end;
tmp_begin = end + 1; tmp_begin = end + 1;
tmp_end = pitem->end; tmp_end = pitem->end;
@ -746,10 +747,10 @@ int acl_dlink_delete_range(ACL_DLINK *plink, acl_uint64 begin, acl_uint64 end)
return acl_array_delete_range(parray, low, high, dlink_free_callback); return acl_array_delete_range(parray, low, high, dlink_free_callback);
} }
ACL_DITEM *acl_dlink_modify(ACL_DLINK *plink, acl_uint64 begin, acl_uint64 end) ACL_DITEM *acl_dlink_modify(ACL_DLINK *plink, acl_int64 begin, acl_int64 end)
{ {
if (begin > end) { if (begin > end) {
acl_uint64 tmp; acl_int64 tmp;
/* swap the begin andend if end < begin */ /* swap the begin andend if end < begin */
tmp = begin; tmp = begin;
begin = end; begin = end;