Skip to content
Snippets Groups Projects
Commit b110033b authored by Daniel Salzman's avatar Daniel Salzman
Browse files

qp-trie: replace value_t with trie_val_t

parent 75dcaecf
Branches
No related merge requests found
......@@ -63,7 +63,7 @@ typedef struct leaf {
byte flags;
#endif
tkey_t *key; /*!< The pointer must be aligned to 4-byte multiples! */
value_t val;
trie_val_t val;
} leaf_t;
struct tkey {
......@@ -286,7 +286,7 @@ size_t qp_trie_weight(const struct qp_trie *tbl)
return tbl->weight;
}
value_t* qp_trie_get_try(trie_t *tbl, const char *key, uint32_t len)
trie_val_t* qp_trie_get_try(trie_t *tbl, const char *key, uint32_t len)
{
assert(tbl);
if (!tbl->weight)
......@@ -304,7 +304,7 @@ value_t* qp_trie_get_try(trie_t *tbl, const char *key, uint32_t len)
return &t->leaf.val;
}
int qp_trie_del(struct qp_trie *tbl, const char *key, uint32_t len, value_t *val)
int qp_trie_del(struct qp_trie *tbl, const char *key, uint32_t len, trie_val_t *val)
{
assert(tbl);
if (!tbl->weight)
......@@ -324,7 +324,7 @@ int qp_trie_del(struct qp_trie *tbl, const char *key, uint32_t len, value_t *val
return KNOT_ENOENT;
mm_free(&tbl->mm, t->leaf.key);
if (val != NULL)
*val = t->leaf.val; // we return value_t directly when deleting
*val = t->leaf.val; // we return trie_val_t directly when deleting
--tbl->weight;
if (unlikely(!p)) { // whole trie was a single leaf
assert(tbl->weight == 0);
......@@ -608,7 +608,7 @@ static int ns_next_leaf(nstack_t *ns)
} while (true);
}
int qp_trie_get_leq(struct qp_trie *tbl, const char *key, uint32_t len, value_t **val)
int qp_trie_get_leq(struct qp_trie *tbl, const char *key, uint32_t len, trie_val_t **val)
{
assert(tbl && val);
*val = NULL; // so on failure we can just return;
......@@ -683,7 +683,7 @@ static int mk_leaf(node_t *leaf, const char *key, uint32_t len, knot_mm_t *mm)
return KNOT_EOK;
}
value_t* qp_trie_get_ins(struct qp_trie *tbl, const char *key, uint32_t len)
trie_val_t* qp_trie_get_ins(struct qp_trie *tbl, const char *key, uint32_t len)
{
assert(tbl);
// First leaf in an empty tbl?
......@@ -752,8 +752,8 @@ err_leaf:
return NULL;
}
/*! \brief Apply a function to every value_t*, in order; a recursive solution. */
static int apply_trie(node_t *t, int (*f)(value_t *, void *), void *d)
/*! \brief Apply a function to every trie_val_t*, in order; a recursive solution. */
static int apply_trie(node_t *t, int (*f)(trie_val_t *, void *), void *d)
{
assert(t);
if (!isbranch(t))
......@@ -764,7 +764,7 @@ static int apply_trie(node_t *t, int (*f)(value_t *, void *), void *d)
return KNOT_EOK;
}
int qp_trie_apply(struct qp_trie *tbl, int (*f)(value_t *, void *), void *d)
int qp_trie_apply(struct qp_trie *tbl, int (*f)(trie_val_t *, void *), void *d)
{
assert(tbl && f);
if (!tbl->weight)
......@@ -822,7 +822,7 @@ const char* qp_trie_it_key(qp_trie_it_t *it, size_t *len)
return key->chars;
}
value_t* qp_trie_it_val(qp_trie_it_t *it)
trie_val_t* qp_trie_it_val(qp_trie_it_t *it)
{
assert(it && it->len);
node_t *t = it->stack[it->len - 1];
......
......@@ -20,19 +20,18 @@
#include <stdint.h>
#include "libknot/mm_ctx.h"
#include "contrib/hhash.h" /* only for value_t */
/*!
* \file \brief Native API of QP-tries:
*
* - keys are char strings, not necessarily zero-terminated,
* the structure copies the contents of the passed keys
* - values are typedef void* value_t, typically you get an ephemeral pointer to it
* - values are void* pointers, typically you get an ephemeral pointer to it
* - key lengths are limited by 2^32-1 ATM
*/
/*! Opaque structure holding a QP-trie. */
struct qp_trie;
/*! \brief Element value. */
typedef void* trie_val_t;
/*! Opaque type for holding a QP-trie iterator. */
typedef struct qp_trie_it qp_trie_it_t;
......@@ -50,10 +49,10 @@ void qp_trie_clear(struct qp_trie *tbl);
size_t qp_trie_weight(const struct qp_trie *tbl);
/*! \brief Search the trie, returning NULL on failure. */
value_t* qp_trie_get_try(struct qp_trie *tbl, const char *key, uint32_t len);
trie_val_t* qp_trie_get_try(struct qp_trie *tbl, const char *key, uint32_t len);
/*! \brief Search the trie, inserting NULL value_t on failure. */
value_t* qp_trie_get_ins(struct qp_trie *tbl, const char *key, uint32_t len);
/*! \brief Search the trie, inserting NULL trie_val_t on failure. */
trie_val_t* qp_trie_get_ins(struct qp_trie *tbl, const char *key, uint32_t len);
/*!
* \brief Search for less-or-equal element.
......@@ -62,21 +61,21 @@ value_t* qp_trie_get_ins(struct qp_trie *tbl, const char *key, uint32_t len);
* \return KNOT_EOK for exact match, 1 for previous, KNOT_ENOENT for not-found,
* or KNOT_E*.
*/
int qp_trie_get_leq(struct qp_trie *tbl, const char *key, uint32_t len, value_t **val);
int qp_trie_get_leq(struct qp_trie *tbl, const char *key, uint32_t len, trie_val_t **val);
/*!
* \brief Apply a function to every value_t, in order.
* \brief Apply a function to every trie_val_t, in order.
*
* \return KNOT_EOK if success or KNOT_E* if error.
*/
int qp_trie_apply(struct qp_trie *tbl, int (*f)(value_t *, void *), void *d);
int qp_trie_apply(struct qp_trie *tbl, int (*f)(trie_val_t *, void *), void *d);
/*!
* \brief Remove an item, returning KNOT_EOK if succeeded or KNOT_ENOENT if not found.
*
* If val!=NULL and deletion succeeded, the deleted value is set.
*/
int qp_trie_del(struct qp_trie *tbl, const char *key, uint32_t len, value_t *val);
int qp_trie_del(struct qp_trie *tbl, const char *key, uint32_t len, trie_val_t *val);
/*! \brief Create a new iterator pointing to the first element (if any). */
qp_trie_it_t* qp_trie_it_begin(struct qp_trie *tbl);
......@@ -99,4 +98,4 @@ void qp_trie_it_free(qp_trie_it_t *it);
const char* qp_trie_it_key(qp_trie_it_t *it, size_t *len);
/*! \brief Return pointer to the value of the current element (writable). */
value_t* qp_trie_it_val(qp_trie_it_t *it);
trie_val_t* qp_trie_it_val(qp_trie_it_t *it);
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment