tree.hpp 3.5 KB

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