Skip to content

Commit

Permalink
radix_tree: constify _iterate
Browse files Browse the repository at this point in the history
Use nodes as const.
Swap order of arguments.
  • Loading branch information
Zdenek Kabelac committed Jun 3, 2024
1 parent 1e2a344 commit b77edc3
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions base/data-struct/radix-tree-adaptive.c
Original file line number Diff line number Diff line change
Expand Up @@ -978,57 +978,57 @@ bool radix_tree_lookup(struct radix_tree *rt, const void *key, size_t keylen,
}

// FIXME: build up the keys too
static bool _iterate(struct value *v, struct radix_tree_iterator *it)
static bool _iterate(struct radix_tree_iterator *it, const struct value *v)
{
unsigned i;
struct value_chain *vc;
struct prefix_chain *pc;
struct node4 *n4;
struct node16 *n16;
struct node48 *n48;
struct node256 *n256;
const struct value_chain *vc;
const struct prefix_chain *pc;
const struct node4 *n4;
const struct node16 *n16;
const struct node48 *n48;
const struct node256 *n256;

switch (v->type) {
case UNSET:
// can't happen
break;

case VALUE:
return it->visit(it, NULL, 0, v->value);
return it->visit(it, NULL, 0, v->value);

case VALUE_CHAIN:
vc = v->value.ptr;
return it->visit(it, NULL, 0, vc->value) && _iterate(&vc->child, it);
return it->visit(it, NULL, 0, vc->value) && _iterate(it, &vc->child);

case PREFIX_CHAIN:
pc = v->value.ptr;
return _iterate(&pc->child, it);
return _iterate(it, &pc->child);

case NODE4:
n4 = (struct node4 *) v->value.ptr;
n4 = (const struct node4 *) v->value.ptr;
for (i = 0; i < n4->nr_entries; i++)
if (!_iterate(n4->values + i, it))
return false;
if (!_iterate(it, n4->values + i))
return false;
return true;

case NODE16:
n16 = (struct node16 *) v->value.ptr;
n16 = (const struct node16 *) v->value.ptr;
for (i = 0; i < n16->nr_entries; i++)
if (!_iterate(n16->values + i, it))
if (!_iterate(it, n16->values + i))
return false;
return true;

case NODE48:
n48 = (struct node48 *) v->value.ptr;
n48 = (const struct node48 *) v->value.ptr;
for (i = 0; i < n48->nr_entries; i++)
if (!_iterate(n48->values + i, it))
if (!_iterate(it, n48->values + i))
return false;
return true;

case NODE256:
n256 = (struct node256 *) v->value.ptr;
n256 = (const struct node256 *) v->value.ptr;
for (i = 0; i < 256; i++)
if (n256->values[i].type != UNSET && !_iterate(n256->values + i, it))
if (n256->values[i].type != UNSET && !_iterate(it, n256->values + i))
return false;
return true;
}
Expand All @@ -1044,7 +1044,7 @@ void radix_tree_iterate(struct radix_tree *rt, const void *key, size_t keylen,
const uint8_t *ke = kb + keylen;
struct lookup_result lr = _lookup_prefix(&rt->root, kb, ke);
if (lr.kb == ke || _prefix_chain_matches(&lr, ke))
(void) _iterate(lr.v, it);
(void) _iterate(it, lr.v);
}

//----------------------------------------------------------------
Expand Down

0 comments on commit b77edc3

Please sign in to comment.