1
/* $Id: tree.hpp 2394 2008-12-23 17:27:53Z bennylp $ */
3
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#ifndef __PJPP_TREE_HPP__
21
#define __PJPP_TREE_HPP__
23
#include <pj/rbtree.h>
31
typedef pj_rbtree_comp Comp;
33
class reverse_iterator;
35
class Node : private pj_rbtree_node
38
friend class iterator;
39
friend class reverse_iterator;
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; }
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_); }
64
class reverse_iterator : public 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_); }
77
explicit PJ_Tree(Comp *comp) { pj_rbtree_init(&t_, comp); }
81
return iterator(&t_, pj_rbtree_first(&t_));
86
return iterator(&t_, NULL);
89
reverse_iterator rbegin()
91
return reverse_iterator(&t_, pj_rbtree_last(&t_));
94
reverse_iterator rend()
96
return reverse_iterator(&t_, NULL);
99
bool insert(Node *node)
101
return pj_rbtree_insert(&t_, node)==0 ? true : false;
104
Node *find(const void *key)
106
return (Node*)pj_rbtree_find(&t_, key);
109
Node *erase(Node *node)
111
return (Node*)pj_rbtree_erase(&t_, node);
114
unsigned max_height(Node *node=NULL)
116
return pj_rbtree_max_height(&t_, node);
119
unsigned min_height(Node *node=NULL)
121
return pj_rbtree_min_height(&t_, node);
128
#endif /* __PJPP_TREE_HPP__ */