22 #include <boost/shared_ptr.hpp> 23 #include <boost/function.hpp> 31 template<
class KEY,
class VALUE>
58 height_(1), keyValue_(keyValue) {
66 keyValue_(keyValue), left(l), right(r) {
69 inline const KEY&
key()
const {
return keyValue_.first;}
70 inline const VALUE&
value()
const {
return keyValue_.second;}
79 inline const value_type&
keyValue()
const {
return root_->keyValue_;}
80 inline const KEY&
key()
const {
return root_->key(); }
81 inline const VALUE&
value()
const {
return root_->value(); }
90 if (ll.
height() >= lr.height())
94 BTree _right(lr.right(), xd, r);
95 return BTree(_left, lr.keyValue(), _right);
97 }
else if (hr > hl + 2) {
99 if (rr.height() >= rl.
height())
107 return BTree(l, xd, r);
123 root_(new
Node(keyValue)) {
128 root_(new
Node(l, keyValue, r)) {
145 const KEY&
x = xd.first;
156 return add(std::make_pair(x, d));
161 if (!root_)
return false;
162 if (x ==
key())
return true;
171 return (other.
root_ == root_);
179 if (other.
root_ == root_)
return true;
192 const value_type&
min()
const {
193 if (!root_)
throw std::invalid_argument(
"BTree::min: empty tree");
200 if (!root_)
throw std::invalid_argument(
"BTree::remove_min: empty tree");
207 if (t1.
empty())
return t2;
208 if (t2.
empty())
return t1;
209 const value_type& xd = t2.
min();
215 if (!root_)
return BTree();
226 return (root_ !=
nullptr) ? root_->height_ : 0;
231 if (!root_)
return 0;
240 const Node* node = root_.get();
242 const KEY&
key = node->key();
243 if (k < key) node = node->left.root_.get();
244 else if (key < k) node = node->right.root_.get();
245 else return node->value();
248 throw std::invalid_argument(
"BTree::find: key not found");
252 void print(
const std::string&
s =
"")
const {
255 std::stringstream
ss;
257 k.print(
s + ss.str() +
" ");
263 void iter(boost::function<
void(
const KEY&,
const VALUE&)>
f)
const {
285 ACC
fold(boost::function<ACC(
const KEY&,
const VALUE&,
const ACC&)>
f,
286 const ACC&
a)
const {
287 if (!root_)
return a;
308 return path_.top().first;
312 return path_.top().second;
320 if (path_.empty())
return;
321 sharedNode
t = current()->right.root_;
325 while (!path_.empty() && done())
328 path_.top().second =
true;
331 path_.push(std::make_pair(t,
false));
354 path_.push(std::make_pair(t,
false));
361 return path_ == __x.
path_;
366 return path_ != __x.
path_;
371 if (path_.empty())
throw std::invalid_argument(
372 "operator*: tried to dereference end");
373 return current()->keyValue_;
378 if (path_.empty())
throw std::invalid_argument(
379 "operator->: tried to dereference end");
380 return &(current()->keyValue_);
404 return const_iterator(root_);
408 const_iterator
end()
const {
409 return const_iterator();
bool operator!=(const Self &__x) const
BTree add(const value_type &xd) const
static BTree balance(const BTree &l, const value_type &xd, const BTree &r)
const VALUE & value() const
std::pair< KEY, VALUE > value_type
const BTree & right() const
reference operator*() const
const value_type & reference
static BTree merge(const BTree &t1, const BTree &t2)
Node(const value_type &keyValue)
void print(const std::string &s="") const
bool operator==(const Self &__x) const
std::pair< KEY, VALUE > value_type
std::forward_iterator_tag iterator_category
const mpreal root(const mpreal &x, unsigned long int k, mp_rnd_t r=mpreal::get_default_rnd())
const value_type keyValue_
bool mem(const KEY &x) const
BTree add(const KEY &x, const VALUE &d) const
Const iterator Not trivial: iterator keeps a stack to indicate current path from root_.
void iter(boost::function< void(const KEY &, const VALUE &)> f) const
static const Line3 l(Rot3(), 1, 1)
ACC fold(boost::function< ACC(const KEY &, const VALUE &, const ACC &)> f, const ACC &a) const
const VALUE & value() const
const_iterator(const sharedNode &root)
const value_type & min() const
bool operator==(const BTree &other) const
BTree(const value_type &keyValue)
const_iterator begin() const
Node(const BTree &l, const value_type &keyValue, const BTree &r)
bool same(const BTree &other) const
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
const sharedNode & current() const
BTree(const BTree &l, const value_type &keyValue, const BTree &r)
std::stack< flagged > path_
bool operator!=(const BTree &other) const
std::pair< sharedNode, bool > flagged
ptrdiff_t difference_type
const VALUE & find(const KEY &k) const
static std::stringstream ss
const value_type * pointer
BTree< KEY, TO > map(boost::function< TO(const KEY &, const VALUE &)> f) const
pointer operator->() const
const_iterator end() const
BTree & operator=(const BTree &other)
BTree(const BTree &other)
boost::shared_ptr< const Node > sharedNode
const BTree & left() const
const value_type & keyValue() const
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x