patricia.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef PATRICIA_H_
  2. #define PATRICIA_H_
  3. #include <stddef.h> /* size_t */
  4. #include <stdint.h> /* uint8_t */
  5. /**
  6. * Structure used to store Patricia tree.
  7. */
  8. typedef struct patricia_internal PATRICIA;
  9. /**
  10. * patricia_init(void):
  11. * Create a Patricia tree to be used for mapping arbitrary-length keys to
  12. * records. Return NULL on failure.
  13. */
  14. PATRICIA * patricia_init(void);
  15. /**
  16. * patricia_insert(tree, key, keylen, rec):
  17. * Associate the provided ${key} of length ${keylen} bytes with the pointer
  18. * ${rec}, which must be non-NULL. Return (-1) on error, 0 on success, and 1
  19. * if the key already exists.
  20. */
  21. int patricia_insert(PATRICIA *, const uint8_t *, size_t, void *);
  22. /**
  23. * patricia_lookup(tree, key, keylen):
  24. * Look up the provided ${key} of length ${keylen} bytes. Return a pointer to
  25. * the associated _record pointer_ if the key is present in the tree (this can
  26. * be used to change the record pointer associated with the key); or NULL
  27. * otherwise.
  28. *
  29. * Note that a record can be deleted from a Patricia tree as follows:
  30. * void ** recp = patricia_lookup(tree, key, keylen);
  31. * if (recp != NULL)
  32. * *recp = NULL;
  33. * but this does not reduce the memory used by the tree as one might expect
  34. * from reducing its size.
  35. */
  36. void ** patricia_lookup(PATRICIA *, const uint8_t *, size_t);
  37. /**
  38. * patricia_foreach(tree, func, cookie):
  39. * Traverse the ${tree} in lexicographical order of stored keys, and call
  40. * ${func(cookie, key, keylen, rec)} for each (key, record) pair. Stop the
  41. * traversal early if ${func} returns a non-zero value; return zero, the
  42. * non-zero value returned by ${func}, or (-1) if an error occurs in the
  43. * tree traversal.
  44. */
  45. int patricia_foreach(PATRICIA *, int(void *, uint8_t *, size_t, void *),
  46. void *);
  47. /**
  48. * patricia_free(tree):
  49. * Free the ${tree}.
  50. */
  51. void patricia_free(PATRICIA *);
  52. #endif /* !PATRICIA_H_ */