blob: b28e2990dff724f61d992413c838680ce8fef05f [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $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
23#include <pj/rbtree.h>
24
25//
26// Tree.
27//
28class PJ_Tree
29{
30public:
31 typedef pj_rbtree_comp Comp;
32 class iterator;
33 class reverse_iterator;
34
35 class Node : private pj_rbtree_node
36 {
37 friend class PJ_Tree;
38 friend class iterator;
39 friend class reverse_iterator;
40
41 public:
42 Node() {}
43 explicit Node(void *data) { user_data = data; }
44 void set_user_data(void *data) { user_data = data; }
45 void *get_user_data() const { return user_data; }
46 };
47
48 class iterator
49 {
50 public:
51 iterator() {}
52 iterator(const iterator &rhs) : tr_(rhs.tr_), nd_(rhs.nd_) {}
53 iterator(pj_rbtree *tr, pj_rbtree_node *nd) : tr_(tr), nd_(nd) {}
54 Node *operator*() { return (Node*)nd_; }
55 bool operator==(const iterator &rhs) const { return tr_==rhs.tr_ && nd_==rhs.nd_; }
56 iterator &operator=(const iterator &rhs) { tr_=rhs.tr_; nd_=rhs.nd_; return *this; }
57 void operator++() { nd_=pj_rbtree_next(tr_, nd_); }
58 void operator--() { nd_=pj_rbtree_prev(tr_, nd_); }
59 protected:
60 pj_rbtree *tr_;
61 pj_rbtree_node *nd_;
62 };
63
64 class reverse_iterator : public iterator
65 {
66 public:
67 reverse_iterator() {}
68 reverse_iterator(const reverse_iterator &it) : iterator(it) {}
69 reverse_iterator(pj_rbtree *t, pj_rbtree_node *n) : iterator(t, n) {}
70 reverse_iterator &operator=(const reverse_iterator &rhs) { iterator::operator=(rhs); return *this; }
71 Node *operator*() { return (Node*)nd_; }
72 bool operator==(const reverse_iterator &rhs) const { return iterator::operator==(rhs); }
73 void operator++() { nd_=pj_rbtree_prev(tr_, nd_); }
74 void operator--() { nd_=pj_rbtree_next(tr_, nd_); }
75 };
76
77 explicit PJ_Tree(Comp *comp) { pj_rbtree_init(&t_, comp); }
78
79 iterator begin()
80 {
81 return iterator(&t_, pj_rbtree_first(&t_));
82 }
83
84 iterator end()
85 {
86 return iterator(&t_, NULL);
87 }
88
89 reverse_iterator rbegin()
90 {
91 return reverse_iterator(&t_, pj_rbtree_last(&t_));
92 }
93
94 reverse_iterator rend()
95 {
96 return reverse_iterator(&t_, NULL);
97 }
98
99 bool insert(Node *node)
100 {
101 return pj_rbtree_insert(&t_, node)==0 ? true : false;
102 }
103
104 Node *find(const void *key)
105 {
106 return (Node*)pj_rbtree_find(&t_, key);
107 }
108
109 Node *erase(Node *node)
110 {
111 return (Node*)pj_rbtree_erase(&t_, node);
112 }
113
114 unsigned max_height(Node *node=NULL)
115 {
116 return pj_rbtree_max_height(&t_, node);
117 }
118
119 unsigned min_height(Node *node=NULL)
120 {
121 return pj_rbtree_min_height(&t_, node);
122 }
123
124private:
125 pj_rbtree t_;
126};
127
128#endif /* __PJPP_TREE_HPP__ */
129