HelenOS sources
This source file includes following definitions.
- count
- erase
- lower_bound
- lower_bound_const
- upper_bound
- upper_bound_const
- equal_range
- equal_range_const
- emplace
- insert
- insert
- insert
- count
- erase
- lower_bound
- lower_bound_const
- upper_bound
- upper_bound_const
- equal_range
- equal_range_const
- emplace
- insert
- insert
- insert
#ifndef LIBCPP_BITS_ADT_RBTREE_POLICIES
#define LIBCPP_BITS_ADT_RBTREE_POLICIES
#include <__bits/adt/rbtree_node.hpp>
#include <utility>
namespace std::aux
{
struct rbtree_single_policy
{
template<class Tree, class Key>
static typename Tree::size_type count(const Tree& tree, const Key& key)
{
return tree.find(key) == tree.end() ? 0 : 1;
}
template<class Tree, class Key>
static typename Tree::size_type erase(Tree& tree, const Key& key)
{
using size_type = typename Tree::size_type;
auto it = tree.find(key);
if (it == tree.end())
return size_type{};
else
tree.delete_node(it.node());
return size_type{1};
}
template<class Tree, class Key>
static typename Tree::iterator lower_bound(const Tree& tree, const Key& key)
{
using iterator = typename Tree::iterator;
using node_type = typename Tree::node_type;
auto it = lower_bound_const(tree, key);
return iterator{const_cast<node_type*>(it.node()), it.end()};
}
template<class Tree, class Key>
static typename Tree::const_iterator lower_bound_const(const Tree& tree, const Key& key)
{
using const_iterator = typename Tree::const_iterator;
auto node = tree.find_parent_for_insertion(key);
const_iterator it{node, false};
auto beg = tree.begin();
auto end = tree.end();
if (tree.key_compare_(tree.get_key(*it), key))
{
if (it != end)
return ++it;
else
return it;
}
else if (tree.key_compare_(key, tree.get_key(*it)))
{
if (it != beg)
return --it;
else
return it;
}
else
return it;
return it;
}
template<class Tree, class Key>
static typename Tree::iterator upper_bound(const Tree& tree, const Key& key)
{
using iterator = typename Tree::iterator;
using node_type = typename Tree::node_type;
auto it = upper_bound_const(tree, key);
return iterator{const_cast<node_type*>(it.node()), it.end()};
}
template<class Tree, class Key>
static typename Tree::const_iterator upper_bound_const(const Tree& tree, const Key& key)
{
auto it = lower_bound_const(tree, key);
if (it == tree.end())
return it;
else
return ++it;
}
template<class Tree, class Key>
static pair<
typename Tree::iterator,
typename Tree::iterator
> equal_range(Tree& tree, const Key& key)
{
return make_pair(
lower_bound(tree, key),
upper_bound(tree, key)
);
}
template<class Tree, class Key>
static pair<
typename Tree::const_iterator,
typename Tree::const_iterator
> equal_range_const(const Tree& tree, const Key& key)
{
return make_pair(
lower_bound_const(tree, key),
upper_bound_const(tree, key)
);
}
template<class Tree, class... Args>
static pair<
typename Tree::iterator, bool
> emplace(Tree& tree, Args&&... args)
{
using value_type = typename Tree::value_type;
using iterator = typename Tree::iterator;
using node_type = typename Tree::node_type;
auto val = value_type{forward<Args>(args)...};
auto parent = tree.find_parent_for_insertion(tree.get_key(val));
if (parent && tree.keys_equal(tree.get_key(parent->value), tree.get_key(val)))
return make_pair(iterator{parent, false}, false);
auto node = new node_type{move(val)};
return insert(tree, node, parent);
}
template<class Tree, class Value>
static pair<
typename Tree::iterator, bool
> insert(Tree& tree, const Value& val)
{
using iterator = typename Tree::iterator;
using node_type = typename Tree::node_type;
auto parent = tree.find_parent_for_insertion(tree.get_key(val));
if (parent && tree.keys_equal(tree.get_key(parent->value), tree.get_key(val)))
return make_pair(iterator{parent, false}, false);
auto node = new node_type{val};
return insert(tree, node, parent);
}
template<class Tree, class Value>
static pair<
typename Tree::iterator, bool
> insert(Tree& tree, Value&& val)
{
using iterator = typename Tree::iterator;
using node_type = typename Tree::node_type;
auto parent = tree.find_parent_for_insertion(tree.get_key(val));
if (parent && tree.keys_equal(tree.get_key(parent->value), tree.get_key(val)))
return make_pair(iterator{parent, false}, false);
auto node = new node_type{forward<Value>(val)};
return insert(tree, node, parent);
}
template<class Tree>
static pair<
typename Tree::iterator, bool
> insert(
Tree& tree, typename Tree::node_type* node,
typename Tree::node_type* parent
)
{
using iterator = typename Tree::iterator;
if (!node)
return make_pair(tree.end(), false);
++tree.size_;
if (!parent)
{
node->color = rbcolor::black;
tree.root_ = node;
}
else
{
if (tree.keys_comp(tree.get_key(node->value), parent->value))
parent->add_left_child(node);
else
parent->add_right_child(node);
tree.repair_after_insert_(node);
tree.update_root_(node);
}
return make_pair(iterator{node, false}, true);
}
};
struct rbtree_multi_policy
{
template<class Tree, class Key>
static typename Tree::size_type count(const Tree& tree, const Key& key)
{
using size_type = typename Tree::size_type;
auto it = tree.find(key);
if (it == tree.end())
return size_type{};
size_type res{};
while (it != tree.end() && tree.keys_equal(tree.get_key(*it), key))
{
++res;
++it;
}
return res;
}
template<class Tree, class Key>
static typename Tree::size_type erase(Tree& tree, const Key& key)
{
using size_type = typename Tree::size_type;
auto it = tree.find(key);
if (it == tree.end())
return size_type{};
size_type res{};
while (it != tree.end() && tree.keys_equal(tree.get_key(*it), key))
{
++res;
it = tree.erase(it);
}
return res;
}
template<class Tree, class Key>
static typename Tree::iterator lower_bound(const Tree& tree, const Key& key)
{
auto it = lower_bound_const(tree, key);
return typename Tree::iterator{
const_cast<typename Tree::node_type*>(it.node()), it.end()
};
}
template<class Tree, class Key>
static typename Tree::const_iterator lower_bound_const(const Tree& tree, const Key& key)
{
using const_iterator = typename Tree::const_iterator;
auto node = tree.find_parent_for_insertion(key);
const_iterator it{node, false};
auto beg = tree.begin();
auto end = tree.end();
if (tree.keys_comp(key, *it))
--it;
while (tree.keys_equal(tree.get_key(*it), key) && it != beg)
--it;
if (it != beg)
++it;
if (tree.key_compare_(tree.get_key(*it), key))
{
if (it != end)
return ++it;
else
return it;
}
return it;
}
template<class Tree, class Key>
static typename Tree::iterator upper_bound(const Tree& tree, const Key& key)
{
auto it = upper_bound_const(tree, key);
return typename Tree::iterator{
const_cast<typename Tree::node_type*>(it.node()), it.end()
};
}
template<class Tree, class Key>
static typename Tree::const_iterator upper_bound_const(const Tree& tree, const Key& key)
{
auto it = lower_bound(tree, key);
if (it == tree.end())
return it;
else if (tree.keys_equal(tree.get_key(*it), key))
{
while (it != tree.end() && tree.keys_equal(tree.get_key(*it), key))
++it;
return it;
}
return it;
}
template<class Tree, class Key>
static pair<
typename Tree::iterator,
typename Tree::iterator
> equal_range(const Tree& tree, const Key& key)
{
return make_pair(
lower_bound(tree, key),
upper_bound(tree, key)
);
}
template<class Tree, class Key>
static pair<
typename Tree::const_iterator,
typename Tree::const_iterator
> equal_range_const(const Tree& tree, const Key& key)
{
return make_pair(
lower_bound_const(tree, key),
upper_bound_const(tree, key)
);
}
template<class Tree, class... Args>
static typename Tree::iterator emplace(Tree& tree, Args&&... args)
{
using node_type = typename Tree::node_type;
auto node = new node_type{forward<Args>(args)...};
return insert(tree, node);
}
template<class Tree, class Value>
static typename Tree::iterator insert(Tree& tree, const Value& val)
{
using node_type = typename Tree::node_type;
auto node = new node_type{val};
return insert(tree, node);
}
template<class Tree, class Value>
static typename Tree::iterator insert(Tree& tree, Value&& val)
{
using node_type = typename Tree::node_type;
auto node = new node_type{forward<Value>(val)};
return insert(tree, node);
}
template<class Tree>
static typename Tree::iterator insert(
Tree& tree, typename Tree::node_type* node,
typename Tree::node_type* = nullptr
)
{
using iterator = typename Tree::iterator;
if (!node)
return tree.end();
auto parent = tree.find_parent_for_insertion(tree.get_key(node->value));
++tree.size_;
if (!parent)
{
node->color = rbcolor::black;
tree.root_ = node;
}
else
{
if (tree.keys_comp(tree.get_key(node->value), parent->value))
parent->add_left_child(node);
else if (tree.keys_comp(tree.get_key(parent->value), node->value))
parent->add_right_child(node);
else
{
parent->add(node);
tree.update_root_(parent);
return iterator{node, false};
}
tree.repair_after_insert_(node);
tree.update_root_(node);
}
return iterator{node, false};
}
};
}
#endif
HelenOS homepage, sources at GitHub