33 template<
class KEY,
class VALUE>
60 height_(1), keyValue_(keyValue) {
68 keyValue_(keyValue), left(l), right(r) {
71 inline const KEY&
key()
const {
return keyValue_.first;}
72 inline const VALUE&
value()
const {
return keyValue_.second;}
81 inline const value_type&
keyValue()
const {
return root_->keyValue_;}
82 inline const KEY&
key()
const {
return root_->key(); }
83 inline const VALUE&
value()
const {
return root_->value(); }
92 if (ll.
height() >= lr.height())
96 BTree _right(lr.right(), xd, r);
97 return BTree(_left, lr.keyValue(), _right);
99 }
else if (hr > hl + 2) {
101 if (rr.height() >= rl.
height())
109 return BTree(l, xd, r);
125 root_(new
Node(keyValue)) {
130 root_(new
Node(l, keyValue, r)) {
147 const KEY&
x = xd.first;
158 return add(std::make_pair(x, d));
163 if (!root_)
return false;
164 if (x ==
key())
return true;
173 return (other.
root_ == root_);
181 if (other.
root_ == root_)
return true;
194 const value_type&
min()
const {
195 if (!root_)
throw std::invalid_argument(
"BTree::min: empty tree");
202 if (!root_)
throw std::invalid_argument(
"BTree::remove_min: empty tree");
209 if (t1.
empty())
return t2;
210 if (t2.
empty())
return t1;
211 const value_type& xd = t2.
min();
217 if (!root_)
return BTree();
228 return (root_ !=
nullptr) ? root_->height_ : 0;
233 if (!root_)
return 0;
242 const Node* node = root_.get();
244 const KEY&
key = node->key();
245 if (k < key) node = node->left.root_.get();
246 else if (key < k) node = node->right.root_.get();
247 else return node->value();
250 throw std::invalid_argument(
"BTree::find: key not found");
254 void print(
const std::string&
s =
"")
const {
257 std::stringstream
ss;
259 k.print(
s + ss.str() +
" ");
288 const ACC&
a)
const {
289 if (!root_)
return a;
310 return path_.top().first;
314 return path_.top().second;
322 if (path_.empty())
return;
323 sharedNode
t = current()->right.root_;
327 while (!path_.empty() && done())
330 path_.top().second =
true;
333 path_.push(std::make_pair(t,
false));
356 path_.push(std::make_pair(t,
false));
363 return path_ == __x.
path_;
368 return path_ != __x.
path_;
373 if (path_.empty())
throw std::invalid_argument(
374 "operator*: tried to dereference end");
375 return current()->keyValue_;
380 if (path_.empty())
throw std::invalid_argument(
381 "operator->: tried to dereference end");
382 return &(current()->keyValue_);
406 return const_iterator(root_);
410 const_iterator
end()
const {
411 return const_iterator();
const BTree & left() const
bool operator!=(const BTree &other) const
static BTree balance(const BTree &l, const value_type &xd, const BTree &r)
bool operator==(const BTree &other) const
bool mem(const KEY &x) const
std::pair< KEY, VALUE > value_type
const sharedNode & current() const
const value_type & reference
static BTree merge(const BTree &t1, const BTree &t2)
std::shared_ptr< const Node > sharedNode
Node(const value_type &keyValue)
void print(const std::string &s="") const
std::pair< KEY, VALUE > value_type
std::forward_iterator_tag iterator_category
const value_type keyValue_
const BTree & right() const
Const iterator Not trivial: iterator keeps a stack to indicate current path from root_.
static const Line3 l(Rot3(), 1, 1)
ACC fold(std::function< ACC(const KEY &, const VALUE &, const ACC &)> f, const ACC &a) const
const_iterator begin() const
const_iterator(const sharedNode &root)
BTree(const value_type &keyValue)
Node(const BTree &l, const value_type &keyValue, const BTree &r)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
BTree(const BTree &l, const value_type &keyValue, const BTree &r)
std::stack< flagged > path_
std::pair< sharedNode, bool > flagged
ptrdiff_t difference_type
static std::stringstream ss
const value_type * pointer
reference operator*() const
bool operator!=(const Self &__x) const
const value_type & keyValue() const
const VALUE & value() const
BTree add(const KEY &x, const VALUE &d) const
BTree add(const value_type &xd) const
BTree< KEY, TO > map(std::function< TO(const KEY &, const VALUE &)> f) const
void iter(std::function< void(const KEY &, const VALUE &)> f) const
BTree & operator=(const BTree &other)
const_iterator end() const
BTree(const BTree &other)
const VALUE & find(const KEY &k) const
pointer operator->() 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
const VALUE & value() const
bool operator==(const Self &__x) const
const value_type & min() const
bool same(const BTree &other) const