rbtree.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <stdlib.h>
  26. struct rbnode {
  27. struct {
  28. struct rbnode *rbe_left;
  29. struct rbnode *rbe_right;
  30. struct rbnode *rbe_parent;
  31. int rbe_color;
  32. } entry;
  33. char content[0];
  34. };
  35. struct rbtree {
  36. struct rbnode *rbh_root;
  37. int (*cmp)(void *k1, void *k2);
  38. void (*augment)(void *elm);
  39. };
  40. typedef struct rbnode RbNode;
  41. typedef struct rbtree RbTree;
  42. void rbtree_remove(struct rbtree *, void* iter);
  43. // return a iterator
  44. void* rbtree_insert(struct rbtree *, void *treenode);
  45. void* rbtree_find(struct rbtree *, void *val);
  46. void* rbtree_next(struct rbtree *, void* iter);
  47. void* rbtree_min(struct rbtree *);
  48. void* rbtree_max(struct rbtree *);
  49. void* rbtree_left(void *node);
  50. void* rbtree_right(void *node);
  51. void* rbtree_parent(void *node);
  52. void rbtree_free(struct rbtree *, void (*free_cb)(void*));