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