tree.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* $Id$ */
  2. /*
  3. * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
  4. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef __PJPP_TREE_HPP__
  21. #define __PJPP_TREE_HPP__
  22. #include <pj/rbtree.h>
  23. //
  24. // Tree.
  25. //
  26. class PJ_Tree
  27. {
  28. public:
  29. typedef pj_rbtree_comp Comp;
  30. class iterator;
  31. class reverse_iterator;
  32. class Node : private pj_rbtree_node
  33. {
  34. friend class PJ_Tree;
  35. friend class iterator;
  36. friend class reverse_iterator;
  37. public:
  38. Node() {}
  39. explicit Node(void *data) { user_data = data; }
  40. void set_user_data(void *data) { user_data = data; }
  41. void *get_user_data() const { return user_data; }
  42. };
  43. class iterator
  44. {
  45. public:
  46. iterator() {}
  47. iterator(const iterator &rhs) : tr_(rhs.tr_), nd_(rhs.nd_) {}
  48. iterator(pj_rbtree *tr, pj_rbtree_node *nd) : tr_(tr), nd_(nd) {}
  49. Node *operator*() { return (Node*)nd_; }
  50. bool operator==(const iterator &rhs) const { return tr_==rhs.tr_ && nd_==rhs.nd_; }
  51. iterator &operator=(const iterator &rhs) { tr_=rhs.tr_; nd_=rhs.nd_; return *this; }
  52. void operator++() { nd_=pj_rbtree_next(tr_, nd_); }
  53. void operator--() { nd_=pj_rbtree_prev(tr_, nd_); }
  54. protected:
  55. pj_rbtree *tr_;
  56. pj_rbtree_node *nd_;
  57. };
  58. class reverse_iterator : public iterator
  59. {
  60. public:
  61. reverse_iterator() {}
  62. reverse_iterator(const reverse_iterator &it) : iterator(it) {}
  63. reverse_iterator(pj_rbtree *t, pj_rbtree_node *n) : iterator(t, n) {}
  64. reverse_iterator &operator=(const reverse_iterator &rhs) { iterator::operator=(rhs); return *this; }
  65. Node *operator*() { return (Node*)nd_; }
  66. bool operator==(const reverse_iterator &rhs) const { return iterator::operator==(rhs); }
  67. void operator++() { nd_=pj_rbtree_prev(tr_, nd_); }
  68. void operator--() { nd_=pj_rbtree_next(tr_, nd_); }
  69. };
  70. explicit PJ_Tree(Comp *comp) { pj_rbtree_init(&t_, comp); }
  71. iterator begin()
  72. {
  73. return iterator(&t_, pj_rbtree_first(&t_));
  74. }
  75. iterator end()
  76. {
  77. return iterator(&t_, NULL);
  78. }
  79. reverse_iterator rbegin()
  80. {
  81. return reverse_iterator(&t_, pj_rbtree_last(&t_));
  82. }
  83. reverse_iterator rend()
  84. {
  85. return reverse_iterator(&t_, NULL);
  86. }
  87. bool insert(Node *node)
  88. {
  89. return pj_rbtree_insert(&t_, node)==0 ? true : false;
  90. }
  91. Node *find(const void *key)
  92. {
  93. return (Node*)pj_rbtree_find(&t_, key);
  94. }
  95. Node *erase(Node *node)
  96. {
  97. return (Node*)pj_rbtree_erase(&t_, node);
  98. }
  99. unsigned max_height(Node *node=NULL)
  100. {
  101. return pj_rbtree_max_height(&t_, node);
  102. }
  103. unsigned min_height(Node *node=NULL)
  104. {
  105. return pj_rbtree_min_height(&t_, node);
  106. }
  107. private:
  108. pj_rbtree t_;
  109. };
  110. #endif /* __PJPP_TREE_HPP__ */