Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kbtree: add kb_itr_prev and more precise kb_itr_getp #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion kbtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,37 @@ typedef struct {
if (itr->p < itr->stack) return 0; \
if (itr->p->x && itr->p->i < itr->p->x->n) return 1; \
} \
}
} \
static inline int kb_itr_prev_##name(kbtree_##name##_t *b, kbitr_t *itr) \
{ \
if (itr->p < itr->stack) return 0; \
for (;;) { \
while (itr->p->x && itr->p->i >= 0) { \
itr->p[1].x = itr->p->x->is_internal? __KB_PTR(b, itr->p->x)[itr->p->i] : 0; \
itr->p[1].i = itr->p[1].x ? itr->p[1].x->n : -1; \
++itr->p; \
} \
--itr->p; \
if (itr->p < itr->stack) return 0; \
--itr->p->i; \
if (itr->p->x && itr->p->i >= 0) return 1; \
} \
} \
static int kb_itr_getp_##name(kbtree_##name##_t *b, const key_t * __restrict k, kbitr_t *itr) \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding inline here? The compiler will trigger defined but not used warnings when there is no inline.
The above definition has the same problem:

static int kb_itr_get_##name(

{ \
int i, r = 0; \
itr->p = itr->stack; \
itr->p->x = b->root; \
while (itr->p->x) { \
i = __kb_getp_aux_##name(itr->p->x, k, &r); \
itr->p->i = i; \
if (i >= 0 && r == 0) return 1; \
++itr->p->i; \
itr->p[1].x = itr->p->x->is_internal? __KB_PTR(b, itr->p->x)[i + 1] : 0; \
++itr->p; \
} \
return 0; \
} \

#define KBTREE_INIT(name, key_t, __cmp) \
__KB_TREE_T(name) \
Expand Down Expand Up @@ -387,7 +417,9 @@ typedef struct {

#define kb_itr_first(name, b, i) kb_itr_first_##name(b, i)
#define kb_itr_get(name, b, k, i) kb_itr_get_##name(b, k, i)
#define kb_itr_getp(name, b, k, i) kb_itr_getp_##name(b, k, i)
#define kb_itr_next(name, b, i) kb_itr_next_##name(b, i)
#define kb_itr_prev(name, b, i) kb_itr_prev_##name(b, i)
#define kb_itr_key(type, itr) __KB_KEY(type, (itr)->p->x)[(itr)->p->i]
#define kb_itr_valid(itr) ((itr)->p >= (itr)->stack)

Expand Down