diff --git a/src/ckdb.c b/src/ckdb.c index 4b8d31fa..586b729a 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -1325,11 +1325,10 @@ static void dsp_transfer(K_ITEM *item, FILE *stream) } // order by name asc -static double cmp_transfer(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_transfer(K_ITEM *a, K_ITEM *b) { - double c = (double)strcmp(DATA_TRANSFER(a)->name, - DATA_TRANSFER(b)->name); - return c; + return CMP_STR(DATA_TRANSFER(a)->name, + DATA_TRANSFER(b)->name); } static K_ITEM *find_transfer(char *name) @@ -1816,13 +1815,13 @@ cleanup: } // order by userid asc,workername asc -static double cmp_workerstatus(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_workerstatus(K_ITEM *a, K_ITEM *b) { - double c = (double)(DATA_WORKERSTATUS(a)->userid - - DATA_WORKERSTATUS(b)->userid); - if (c == 0.0) { - c = strcmp(DATA_WORKERSTATUS(a)->workername, - DATA_WORKERSTATUS(b)->workername); + cmp_t c = CMP_BIGINT(DATA_WORKERSTATUS(a)->userid, + DATA_WORKERSTATUS(b)->userid); + if (c == 0) { + c = CMP_STR(DATA_WORKERSTATUS(a)->workername, + DATA_WORKERSTATUS(b)->workername); } return c; } @@ -1866,8 +1865,8 @@ static K_ITEM *_find_create_workerstatus(int64_t userid, char *workername, bool #define find_create_workerstatus(_u, _w) _find_create_workerstatus(_u, _w, true) #define find_workerstatus(_u, _w) _find_create_workerstatus(_u, _w, false) -static double cmp_userstats_workerstatus(K_ITEM *a, K_ITEM *b); -static double cmp_sharesummary(K_ITEM *a, K_ITEM *b); +static cmp_t cmp_userstats_workerstatus(K_ITEM *a, K_ITEM *b); +static cmp_t cmp_sharesummary(K_ITEM *a, K_ITEM *b); /* All data is loaded, now update workerstatus last_share, last_idle, last_stats * Since shares are all part of a sharesummary, there's no need to search shares @@ -1962,25 +1961,25 @@ static void workerstatus_update(AUTHS *auths, SHARES *shares, USERSTATS *usersta } // default tree order by username asc,expirydate desc -static double cmp_users(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_users(K_ITEM *a, K_ITEM *b) { - double c = strcmp(DATA_USERS(a)->username, + cmp_t c = CMP_STR(DATA_USERS(a)->username, DATA_USERS(b)->username); - if (c == 0.0) { - c = tvdiff(&(DATA_USERS(b)->expirydate), - &(DATA_USERS(a)->expirydate)); + if (c == 0) { + c = CMP_TV(DATA_USERS(b)->expirydate, + DATA_USERS(a)->expirydate); } return c; } // order by userid asc,expirydate desc -static double cmp_userid(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_userid(K_ITEM *a, K_ITEM *b) { - double c = (double)(DATA_USERS(a)->userid - - DATA_USERS(b)->userid); - if (c == 0.0) { - c = tvdiff(&(DATA_USERS(b)->expirydate), - &(DATA_USERS(a)->expirydate)); + cmp_t c = CMP_BIGINT(DATA_USERS(a)->userid, + DATA_USERS(b)->userid); + if (c == 0) { + c = CMP_TV(DATA_USERS(b)->expirydate, + DATA_USERS(a)->expirydate); } return c; } @@ -2210,16 +2209,16 @@ void users_reload() } // order by userid asc,workername asc,expirydate desc -static double cmp_workers(K_ITEM *a, K_ITEM *b) -{ - double c = (double)(DATA_WORKERS(a)->userid - - DATA_WORKERS(b)->userid); - if (c == 0.0) { - c = strcmp(DATA_WORKERS(a)->workername, - DATA_WORKERS(b)->workername); - if (c == 0.0) { - c = tvdiff(&(DATA_WORKERS(b)->expirydate), - &(DATA_WORKERS(a)->expirydate)); +static cmp_t cmp_workers(K_ITEM *a, K_ITEM *b) +{ + cmp_t c = CMP_BIGINT(DATA_WORKERS(a)->userid, + DATA_WORKERS(b)->userid); + if (c == 0) { + c = CMP_STR(DATA_WORKERS(a)->workername, + DATA_WORKERS(b)->workername); + if (c == 0) { + c = CMP_TV(DATA_WORKERS(b)->expirydate, + DATA_WORKERS(a)->expirydate); } } return c; @@ -2635,19 +2634,19 @@ void workers_reload() } // order by userid asc,paydate asc,payaddress asc,expirydate desc -static double cmp_payments(K_ITEM *a, K_ITEM *b) -{ - double c = (double)(DATA_PAYMENTS(a)->userid - - DATA_PAYMENTS(b)->userid); - if (c == 0.0) { - c = tvdiff(&(DATA_PAYMENTS(a)->paydate), - &(DATA_PAYMENTS(b)->paydate)); - if (c == 0.0) { - c = strcmp(DATA_PAYMENTS(a)->payaddress, - DATA_PAYMENTS(b)->payaddress); - if (c == 0.0) { - c = tvdiff(&(DATA_PAYMENTS(b)->expirydate), - &(DATA_PAYMENTS(a)->expirydate)); +static cmp_t cmp_payments(K_ITEM *a, K_ITEM *b) +{ + cmp_t c = CMP_BIGINT(DATA_PAYMENTS(a)->userid, + DATA_PAYMENTS(b)->userid); + if (c == 0) { + c = CMP_TV(DATA_PAYMENTS(a)->paydate, + DATA_PAYMENTS(b)->paydate); + if (c == 0) { + c = CMP_STR(DATA_PAYMENTS(a)->payaddress, + DATA_PAYMENTS(b)->payaddress); + if (c == 0) { + c = CMP_TV(DATA_PAYMENTS(b)->expirydate, + DATA_PAYMENTS(a)->expirydate); } } } @@ -2778,13 +2777,13 @@ void payments_reload() } // order by workinfoid asc,expirydate asc -static double cmp_workinfo(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_workinfo(K_ITEM *a, K_ITEM *b) { - double c = (double)(DATA_WORKINFO(a)->workinfoid - - DATA_WORKINFO(b)->workinfoid); + cmp_t c = CMP_BIGINT(DATA_WORKINFO(a)->workinfoid, + DATA_WORKINFO(b)->workinfoid); if (c == 0) { - c = tvdiff(&(DATA_WORKINFO(a)->expirydate), - &(DATA_WORKINFO(b)->expirydate)); + c = CMP_TV(DATA_WORKINFO(a)->expirydate, + DATA_WORKINFO(b)->expirydate); } return c; } @@ -2815,21 +2814,20 @@ inline int32_t _coinbase1height(char *coinbase1, WHERE_FFL_ARGS) return height; } -static double _cmp_height(char *coinbase1a, char *coinbase1b, WHERE_FFL_ARGS) +static cmp_t _cmp_height(char *coinbase1a, char *coinbase1b, WHERE_FFL_ARGS) { - double c = (double)(_coinbase1height(coinbase1a, WHERE_FFL_PASS) - - _coinbase1height(coinbase1b, WHERE_FFL_PASS)); - return c; + return CMP_INT(_coinbase1height(coinbase1a, WHERE_FFL_PASS), + _coinbase1height(coinbase1b, WHERE_FFL_PASS)); } // order by height asc,createdate asc -static double cmp_workinfo_height(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_workinfo_height(K_ITEM *a, K_ITEM *b) { - double c = cmp_height(DATA_WORKINFO(a)->coinbase1, - DATA_WORKINFO(b)->coinbase1); + cmp_t c = cmp_height(DATA_WORKINFO(a)->coinbase1, + DATA_WORKINFO(b)->coinbase1); if (c == 0) { - c = tvdiff(&(DATA_WORKINFO(a)->createdate), - &(DATA_WORKINFO(b)->createdate)); + c = CMP_TV(DATA_WORKINFO(a)->createdate, + DATA_WORKINFO(b)->createdate); } return c; } @@ -2963,8 +2961,8 @@ unparam: static bool _sharesummary_update(PGconn *conn, SHARES *s_row, SHAREERRORS *e_row, K_ITEM *ss_item, char *by, char *code, char *inet, tv_t *cd, WHERE_FFL_ARGS); -static double cmp_sharesummary_workinfoid(K_ITEM *a, K_ITEM *b); -static double cmp_shares(K_ITEM *a, K_ITEM *b); +static cmp_t cmp_sharesummary_workinfoid(K_ITEM *a, K_ITEM *b); +static cmp_t cmp_shares(K_ITEM *a, K_ITEM *b); /* N.B. a DB check can be done to find sharesummaries that have missed being * aged (and a possible problem with the aging process): @@ -3228,25 +3226,25 @@ void workinfo_reload() } // order by workinfoid asc,userid asc,workername asc,createdate asc,nonce asc,expirydate desc -static double cmp_shares(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_shares(K_ITEM *a, K_ITEM *b) { - double c = (double)(DATA_SHARES(a)->workinfoid - - DATA_SHARES(b)->workinfoid); + cmp_t c = CMP_BIGINT(DATA_SHARES(a)->workinfoid, + DATA_SHARES(b)->workinfoid); if (c == 0) { - c = (double)(DATA_SHARES(a)->userid - - DATA_SHARES(b)->userid); + c = CMP_BIGINT(DATA_SHARES(a)->userid, + DATA_SHARES(b)->userid); if (c == 0) { - c = strcmp(DATA_SHARES(a)->workername, - DATA_SHARES(b)->workername); + c = CMP_STR(DATA_SHARES(a)->workername, + DATA_SHARES(b)->workername); if (c == 0) { - c = tvdiff(&(DATA_SHARES(a)->createdate), - &(DATA_SHARES(b)->createdate)); + c = CMP_TV(DATA_SHARES(a)->createdate, + DATA_SHARES(b)->createdate); if (c == 0) { - c = strcmp(DATA_SHARES(a)->nonce, - DATA_SHARES(b)->nonce); + c = CMP_STR(DATA_SHARES(a)->nonce, + DATA_SHARES(b)->nonce); if (c == 0) { - c = tvdiff(&(DATA_SHARES(b)->expirydate), - &(DATA_SHARES(a)->expirydate)); + c = CMP_TV(DATA_SHARES(b)->expirydate, + DATA_SHARES(a)->expirydate); } } } @@ -3362,19 +3360,19 @@ static bool shares_fill() } // order by workinfoid asc,userid asc,createdate asc,nonce asc,expirydate desc -static double cmp_shareerrors(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_shareerrors(K_ITEM *a, K_ITEM *b) { - double c = (double)(DATA_SHAREERRORS(a)->workinfoid - - DATA_SHAREERRORS(b)->workinfoid); + cmp_t c = CMP_BIGINT(DATA_SHAREERRORS(a)->workinfoid, + DATA_SHAREERRORS(b)->workinfoid); if (c == 0) { - c = (double)(DATA_SHAREERRORS(a)->userid - - DATA_SHAREERRORS(b)->userid); + c = CMP_BIGINT(DATA_SHAREERRORS(a)->userid, + DATA_SHAREERRORS(b)->userid); if (c == 0) { - c = tvdiff(&(DATA_SHAREERRORS(a)->createdate), - &(DATA_SHAREERRORS(b)->createdate)); + c = CMP_TV(DATA_SHAREERRORS(a)->createdate, + DATA_SHAREERRORS(b)->createdate); if (c == 0) { - c = tvdiff(&(DATA_SHAREERRORS(b)->expirydate), - &(DATA_SHAREERRORS(a)->expirydate)); + c = CMP_TV(DATA_SHAREERRORS(b)->expirydate, + DATA_SHAREERRORS(a)->expirydate); } } } @@ -3486,32 +3484,32 @@ static void dsp_sharesummary(K_ITEM *item, FILE *stream) } // default tree order by userid asc,workername asc,workinfoid asc for reporting -static double cmp_sharesummary(K_ITEM *a, K_ITEM *b) -{ - double c = (double)(DATA_SHARESUMMARY(a)->userid - - DATA_SHARESUMMARY(b)->userid); - if (c == 0.0) { - c = strcmp(DATA_SHARESUMMARY(a)->workername, - DATA_SHARESUMMARY(b)->workername); - if (c == 0.0) { - c = (double)(DATA_SHARESUMMARY(a)->workinfoid - - DATA_SHARESUMMARY(b)->workinfoid); +static cmp_t cmp_sharesummary(K_ITEM *a, K_ITEM *b) +{ + cmp_t c = CMP_BIGINT(DATA_SHARESUMMARY(a)->userid, + DATA_SHARESUMMARY(b)->userid); + if (c == 0) { + c = CMP_STR(DATA_SHARESUMMARY(a)->workername, + DATA_SHARESUMMARY(b)->workername); + if (c == 0) { + c = CMP_BIGINT(DATA_SHARESUMMARY(a)->workinfoid, + DATA_SHARESUMMARY(b)->workinfoid); } } return c; } // order by workinfoid asc,userid asc,workername asc for flagging complete -static double cmp_sharesummary_workinfoid(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_sharesummary_workinfoid(K_ITEM *a, K_ITEM *b) { - double c = (double)(DATA_SHARESUMMARY(a)->workinfoid - - DATA_SHARESUMMARY(b)->workinfoid); - if (c == 0.0) { - c = (double)(DATA_SHARESUMMARY(a)->userid - - DATA_SHARESUMMARY(b)->userid); - if (c == 0.0) { - c = strcmp(DATA_SHARESUMMARY(a)->workername, - DATA_SHARESUMMARY(b)->workername); + cmp_t c = CMP_BIGINT(DATA_SHARESUMMARY(a)->workinfoid, + DATA_SHARESUMMARY(b)->workinfoid); + if (c == 0) { + c = CMP_BIGINT(DATA_SHARESUMMARY(a)->userid, + DATA_SHARESUMMARY(b)->userid); + if (c == 0) { + c = CMP_STR(DATA_SHARESUMMARY(a)->workername, + DATA_SHARESUMMARY(b)->workername); } } return c; @@ -4004,15 +4002,16 @@ void sharesummary_reload() } // order by height asc,blockhash asc,expirydate asc -static double cmp_blocks(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_blocks(K_ITEM *a, K_ITEM *b) { - double c = DATA_BLOCKS(a)->height - DATA_BLOCKS(b)->height; + cmp_t c = CMP_INT(DATA_BLOCKS(a)->height, + DATA_BLOCKS(b)->height); if (c == 0) { - c = strcmp(DATA_BLOCKS(a)->blockhash, - DATA_BLOCKS(b)->blockhash); + c = CMP_STR(DATA_BLOCKS(a)->blockhash, + DATA_BLOCKS(b)->blockhash); if (c == 0) { - c = tvdiff(&(DATA_BLOCKS(a)->expirydate), - &(DATA_BLOCKS(b)->expirydate)); + c = CMP_TV(DATA_BLOCKS(a)->expirydate, + DATA_BLOCKS(b)->expirydate); } } return c; @@ -4341,19 +4340,19 @@ void blocks_reload() } // order by userid asc,createdate asc,authid asc,expirydate desc -static double cmp_auths(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_auths(K_ITEM *a, K_ITEM *b) { - double c = (double)(DATA_AUTHS(a)->userid - - DATA_AUTHS(b)->userid); + cmp_t c = CMP_BIGINT(DATA_AUTHS(a)->userid, + DATA_AUTHS(b)->userid); if (c == 0) { - c = tvdiff(&(DATA_AUTHS(a)->createdate), - &(DATA_AUTHS(b)->createdate)); + c = CMP_TV(DATA_AUTHS(a)->createdate, + DATA_AUTHS(b)->createdate); if (c == 0) { - c = (double)(DATA_AUTHS(a)->authid - - DATA_AUTHS(b)->authid); + c = CMP_BIGINT(DATA_AUTHS(a)->authid, + DATA_AUTHS(b)->authid); if (c == 0) { - c = tvdiff(&(DATA_AUTHS(b)->expirydate), - &(DATA_AUTHS(a)->expirydate)); + c = CMP_TV(DATA_AUTHS(b)->expirydate, + DATA_AUTHS(a)->expirydate); } } } @@ -4577,13 +4576,13 @@ void auths_reload() } // order by poolinstance asc,createdate asc -static double cmp_poolstats(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_poolstats(K_ITEM *a, K_ITEM *b) { - double c = (double)strcmp(DATA_POOLSTATS(a)->poolinstance, - DATA_POOLSTATS(b)->poolinstance); + cmp_t c = CMP_STR(DATA_POOLSTATS(a)->poolinstance, + DATA_POOLSTATS(b)->poolinstance); if (c == 0) { - c = tvdiff(&(DATA_POOLSTATS(a)->createdate), - &(DATA_POOLSTATS(b)->createdate)); + c = CMP_TV(DATA_POOLSTATS(a)->createdate, + DATA_POOLSTATS(b)->createdate); } return c; } @@ -4832,19 +4831,19 @@ static void dsp_userstats(K_ITEM *item, FILE *stream) /* order by userid asc,statsdate asc,poolinstance asc,workername asc as per required for userstats homepage summarisation */ -static double cmp_userstats(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_userstats(K_ITEM *a, K_ITEM *b) { - double c = (double)(DATA_USERSTATS(a)->userid - - DATA_USERSTATS(b)->userid); + cmp_t c = CMP_BIGINT(DATA_USERSTATS(a)->userid, + DATA_USERSTATS(b)->userid); if (c == 0) { - c = tvdiff(&(DATA_USERSTATS(a)->statsdate), - &(DATA_USERSTATS(b)->statsdate)); + c = CMP_TV(DATA_USERSTATS(a)->statsdate, + DATA_USERSTATS(b)->statsdate); if (c == 0) { - c = (double)strcmp(DATA_USERSTATS(a)->poolinstance, - DATA_USERSTATS(b)->poolinstance); + c = CMP_STR(DATA_USERSTATS(a)->poolinstance, + DATA_USERSTATS(b)->poolinstance); if (c == 0) { - c = (double)strcmp(DATA_USERSTATS(a)->workername, - DATA_USERSTATS(b)->workername); + c = CMP_STR(DATA_USERSTATS(a)->workername, + DATA_USERSTATS(b)->workername); } } } @@ -4853,32 +4852,32 @@ static double cmp_userstats(K_ITEM *a, K_ITEM *b) /* order by userid asc,workername asc temporary tree for summing userstats when sending user homepage info */ -static double cmp_userstats_workername(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_userstats_workername(K_ITEM *a, K_ITEM *b) { - double c = (double)(DATA_USERSTATS(a)->userid - - DATA_USERSTATS(b)->userid); + cmp_t c = CMP_BIGINT(DATA_USERSTATS(a)->userid, + DATA_USERSTATS(b)->userid); if (c == 0) { - c = (double)strcmp(DATA_USERSTATS(a)->workername, - DATA_USERSTATS(b)->workername); + c = CMP_STR(DATA_USERSTATS(a)->workername, + DATA_USERSTATS(b)->workername); } return c; } /* order by statsdate,userid asc,statsdate asc,workername asc,poolinstance asc as per required for DB summarisation */ -static double cmp_userstats_statsdate(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_userstats_statsdate(K_ITEM *a, K_ITEM *b) { - double c = tvdiff(&(DATA_USERSTATS(a)->statsdate), - &(DATA_USERSTATS(b)->statsdate)); + cmp_t c = CMP_TV(DATA_USERSTATS(a)->statsdate, + DATA_USERSTATS(b)->statsdate); if (c == 0) { - c = (double)(DATA_USERSTATS(a)->userid - - DATA_USERSTATS(b)->userid); + c = CMP_BIGINT(DATA_USERSTATS(a)->userid, + DATA_USERSTATS(b)->userid); if (c == 0) { - c = (double)strcmp(DATA_USERSTATS(a)->workername, - DATA_USERSTATS(b)->workername); + c = CMP_STR(DATA_USERSTATS(a)->workername, + DATA_USERSTATS(b)->workername); if (c == 0) { - c = (double)strcmp(DATA_USERSTATS(a)->poolinstance, - DATA_USERSTATS(b)->poolinstance); + c = CMP_STR(DATA_USERSTATS(a)->poolinstance, + DATA_USERSTATS(b)->poolinstance); } } } @@ -4888,19 +4887,19 @@ static double cmp_userstats_statsdate(K_ITEM *a, K_ITEM *b) /* order by userid asc,workername asc,statsdate asc,poolinstance asc built during data load to update workerstatus at the end of the load and used during reload to discard stats already in the DB */ -static double cmp_userstats_workerstatus(K_ITEM *a, K_ITEM *b) +static cmp_t cmp_userstats_workerstatus(K_ITEM *a, K_ITEM *b) { - double c = (double)(DATA_USERSTATS(a)->userid - - DATA_USERSTATS(b)->userid); + cmp_t c = CMP_BIGINT(DATA_USERSTATS(a)->userid, + DATA_USERSTATS(b)->userid); if (c == 0) { - c = (double)strcmp(DATA_USERSTATS(a)->workername, - DATA_USERSTATS(b)->workername); + c = CMP_STR(DATA_USERSTATS(a)->workername, + DATA_USERSTATS(b)->workername); if (c == 0) { - c = tvdiff(&(DATA_USERSTATS(a)->statsdate), - &(DATA_USERSTATS(b)->statsdate)); + c = CMP_TV(DATA_USERSTATS(a)->statsdate, + DATA_USERSTATS(b)->statsdate); if (c == 0) { - c = (double)strcmp(DATA_USERSTATS(a)->poolinstance, - DATA_USERSTATS(b)->poolinstance); + c = CMP_STR(DATA_USERSTATS(a)->poolinstance, + DATA_USERSTATS(b)->poolinstance); } } } diff --git a/src/ktree.c b/src/ktree.c index e823ee6d..00c96456 100644 --- a/src/ktree.c +++ b/src/ktree.c @@ -459,12 +459,12 @@ static K_TREE *right_rotate(K_TREE *root, K_TREE *about) return(root); } -K_TREE *_add_to_ktree(K_TREE *root, K_ITEM *data, double (*cmp_funct)(K_ITEM *, K_ITEM *), KTREE_FFL_ARGS) +K_TREE *_add_to_ktree(K_TREE *root, K_ITEM *data, cmp_t (*cmp_funct)(K_ITEM *, K_ITEM *), KTREE_FFL_ARGS) { K_TREE *ktree; K_TREE *x, *y; K_TREE *pp; - double cmp; + cmp_t cmp; if (root == NULL) FAIL("%s", "ADDNULL add ktree is NULL"); @@ -490,13 +490,13 @@ K_TREE *_add_to_ktree(K_TREE *root, K_ITEM *data, double (*cmp_funct)(K_ITEM *, while (x->isNil == No) { y = x; - if ((cmp = (*cmp_funct)(ktree->data, x->data)) < 0.0) + if ((cmp = (*cmp_funct)(ktree->data, x->data)) < 0) x = x->left; else x = x->right; } ktree->parent = y; - if (cmp < 0.0) + if (cmp < 0) y->left = ktree; else y->right = ktree; @@ -560,18 +560,18 @@ K_TREE *_add_to_ktree(K_TREE *root, K_ITEM *data, double (*cmp_funct)(K_ITEM *, return(root); } -K_ITEM *_find_in_ktree(K_TREE *ktree, K_ITEM *data, double (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS) +K_ITEM *_find_in_ktree(K_TREE *ktree, K_ITEM *data, cmp_t (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS) { - double cmp = -1.0; + cmp_t cmp = -1; if (ktree == NULL) FAIL("%s", "FINDNULL find ktree is NULL"); - while (ktree->isNil == No && cmp != 0.0) + while (ktree->isNil == No && cmp != 0) { if ((cmp = (*cmp_funct)(ktree->data, data))) { - if (cmp > 0.0) + if (cmp > 0) ktree = ktree->left; else ktree = ktree->right; @@ -590,21 +590,21 @@ K_ITEM *_find_in_ktree(K_TREE *ktree, K_ITEM *data, double (*cmp_funct)(K_ITEM * } } -K_ITEM *_find_after_in_ktree(K_TREE *ktree, K_ITEM *data, double (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS) +K_ITEM *_find_after_in_ktree(K_TREE *ktree, K_ITEM *data, cmp_t (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS) { K_TREE *old = NULL; - double cmp = -1.0, oldcmp = -1; + cmp_t cmp = -1, oldcmp = -1; if (ktree == NULL) FAIL("%s", "FINDNULL find_after ktree is NULL"); - while (ktree->isNil == No && cmp != 0.0) + while (ktree->isNil == No && cmp != 0) { if ((cmp = (*cmp_funct)(ktree->data, data))) { old = ktree; oldcmp = cmp; - if (cmp > 0.0) + if (cmp > 0) ktree = ktree->left; else ktree = ktree->right; @@ -620,7 +620,7 @@ K_ITEM *_find_after_in_ktree(K_TREE *ktree, K_ITEM *data, double (*cmp_funct)(K_ { if (old) { - if (oldcmp > 0.0) + if (oldcmp > 0) { *ctx = old; return(old->data); @@ -635,21 +635,21 @@ K_ITEM *_find_after_in_ktree(K_TREE *ktree, K_ITEM *data, double (*cmp_funct)(K_ } } -K_ITEM *_find_before_in_ktree(K_TREE *ktree, K_ITEM *data, double (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS) +K_ITEM *_find_before_in_ktree(K_TREE *ktree, K_ITEM *data, cmp_t (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS) { K_TREE *old = NULL; - double cmp = 1.0, oldcmp = 1; + cmp_t cmp = 1, oldcmp = 1; if (ktree == NULL) FAIL("%s", "FINDNULL find_before ktree is NULL"); - while (ktree->isNil == No && cmp != 0.0) + while (ktree->isNil == No && cmp != 0) { if ((cmp = (*cmp_funct)(ktree->data, data))) { old = ktree; oldcmp = cmp; - if (cmp > 0.0) + if (cmp > 0) ktree = ktree->left; else ktree = ktree->right; @@ -665,7 +665,7 @@ K_ITEM *_find_before_in_ktree(K_TREE *ktree, K_ITEM *data, double (*cmp_funct)(K { if (old) { - if (oldcmp < 0.0) + if (oldcmp < 0) { *ctx = old; return(old->data); @@ -761,13 +761,13 @@ static K_TREE *removeFixup(K_TREE *root, K_TREE *fix) // Does this work OK when you remove the last element in the ktree? // It should return the root as 'nil' -K_TREE *_remove_from_ktree(K_TREE *root, K_ITEM *data, double (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS) +K_TREE *_remove_from_ktree(K_TREE *root, K_ITEM *data, cmp_t (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS) { K_TREE_CTX tmpctx[1]; K_TREE *found; K_ITEM *fdata; K_TREE *x, *y, *nil2; - // double cmp; + // cmp_t cmp; int yred; //check_ktree(root, ">remove", NULL, 1, 1, 1, KTREE_FFL_PASS); @@ -789,7 +789,7 @@ K_TREE *_remove_from_ktree(K_TREE *root, K_ITEM *data, double (*cmp_funct)(K_ITE if (fdata == NULL) return(root); - if (cmp_funct(fdata, data) != 0.0) + if (cmp_funct(fdata, data) != 0) FAIL("%s", "BADFIND cmp(found, remove) != 0"); found = *ctx; @@ -889,7 +889,7 @@ DBG("@remove nil2->left wasn't nil!!!\n"); { DBG("@remove nil2->right wasn't nil!!!\n"); } - cmp = 0.0; + cmp = 0; fdata = first_in_ktree(root, tmpctx);; while (fdata != NULL) { @@ -920,7 +920,7 @@ DBG("@remove found nil2 in ktree(right) %f!!!\n", cmp); /* if (dbg != 0) { - cmp = 0.0 + cmp = 0; fdata = first_in_ktree(root, tmpctx);; while (fdata != NULL) { @@ -931,7 +931,7 @@ if (dbg != 0) fdata = next_in_ktree(tmpctx);; } - if (cmp < -10.0 || cmp > 10.0) + if (cmp < -10 || cmp > 10) { DBG("@remove after balance=%f :(\n", cmp); } diff --git a/src/ktree.h b/src/ktree.h index 7a416838..dfacb4c1 100644 --- a/src/ktree.h +++ b/src/ktree.h @@ -23,6 +23,17 @@ __maybe_unused const char *func, \ __maybe_unused const int line +#define cmp_t int32_t + +#define CMP_STR(a,b) strcmp((a),(b)) +#define CMP_INT(a,b) ((a)-(b)) +#define CMP_BIG_Z(a,b) (((a) < (b)) ? -1 : 1) +#define CMP_BIG(a,b) (((a) == (b)) ? 0 : CMP_BIG_Z(a,b)) +#define CMP_TV(a,b) (((a).tv_sec == (b).tv_sec) ? CMP_BIG((a).tv_usec,(b).tv_usec) : \ + CMP_BIG_Z((a).tv_sec,(b).tv_sec)) +#define CMP_BIGINT CMP_BIG +#define CMP_DOUBLE CMP_BIG + typedef struct ktree { bool isNil; @@ -50,15 +61,15 @@ extern K_ITEM *_next_in_ktree(K_TREE_CTX *ctx, KTREE_FFL_ARGS); #define next_in_ktree(_ctx) _next_in_ktree(_ctx, KLIST_FFL_HERE) extern K_ITEM *_prev_in_ktree(K_TREE_CTX *ctx, KTREE_FFL_ARGS); #define prev_in_ktree(_ctx) _prev_in_ktree(_ctx, KLIST_FFL_HERE) -extern K_TREE *_add_to_ktree(K_TREE *root, K_ITEM *data, double (*cmp_func)(K_ITEM *, K_ITEM *), KTREE_FFL_ARGS); +extern K_TREE *_add_to_ktree(K_TREE *root, K_ITEM *data, cmp_t (*cmp_func)(K_ITEM *, K_ITEM *), KTREE_FFL_ARGS); #define add_to_ktree(_root, _data, _cmp_func) _add_to_ktree(_root, _data, _cmp_func, KLIST_FFL_HERE) -extern K_ITEM *_find_in_ktree(K_TREE *root, K_ITEM *data, double (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS); +extern K_ITEM *_find_in_ktree(K_TREE *root, K_ITEM *data, cmp_t (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS); #define find_in_ktree(_root, _data, _cmp_funct, _ctx) _find_in_ktree(_root, _data, _cmp_funct, _ctx, KLIST_FFL_HERE) -extern K_ITEM *_find_after_in_ktree(K_TREE *ktree, K_ITEM *data, double (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS); +extern K_ITEM *_find_after_in_ktree(K_TREE *ktree, K_ITEM *data, cmp_t (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS); #define find_after_in_ktree(_ktree, _data, _cmp_funct, _ctx) _find_after_in_ktree(_ktree, _data, _cmp_funct, _ctx, KLIST_FFL_HERE) -extern K_ITEM *_find_before_in_ktree(K_TREE *ktree, K_ITEM *data, double (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS); +extern K_ITEM *_find_before_in_ktree(K_TREE *ktree, K_ITEM *data, cmp_t (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS); #define find_before_in_ktree(_ktree, _data, _cmp_funct, _ctx) _find_before_in_ktree(_ktree, _data, _cmp_funct, _ctx, KLIST_FFL_HERE) -extern K_TREE *_remove_from_ktree(K_TREE *root, K_ITEM *data, double (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS); +extern K_TREE *_remove_from_ktree(K_TREE *root, K_ITEM *data, cmp_t (*cmp_funct)(K_ITEM *, K_ITEM *), K_TREE_CTX *ctx, KTREE_FFL_ARGS); #define remove_from_ktree(_root, _data, _cmp_funct, _ctx) _remove_from_ktree(_root, _data, _cmp_funct, _ctx, KLIST_FFL_HERE) extern K_TREE *_free_ktree(K_TREE *root, void (*free_funct)(void *), KTREE_FFL_ARGS); #define free_ktree(_root, _free_funct) _free_ktree(_root, _free_funct, KLIST_FFL_HERE)