Applied pass by value to save copy constructors for pointers with non-trivial copy constructor and const node_ptr & to avoid creating temporaries.

[SVN r80575]
This commit is contained in:
Ion Gaztañaga
2012-09-18 16:38:42 +00:00
parent 22e6899bdc
commit ed4b8ae830
9 changed files with 84 additions and 38 deletions

View File

@@ -172,18 +172,17 @@ class circular_slist_algorithms
static node_ptr get_previous_previous_node(const node_ptr & this_node) static node_ptr get_previous_previous_node(const node_ptr & this_node)
{ return get_previous_previous_node(this_node, this_node); } { return get_previous_previous_node(this_node, this_node); }
//! <b>Requires</b>: this_node and prev_prev_init_node must be in the same circular list. //! <b>Requires</b>: this_node and p must be in the same circular list.
//! //!
//! <b>Effects</b>: Returns the previous node of the previous node of this_node in the //! <b>Effects</b>: Returns the previous node of the previous node of this_node in the
//! circular list starting. the search from prev_init_node. The first node checked //! circular list starting. the search from p. The first node checked
//! for equality is NodeTraits::get_next((NodeTraits::get_next(prev_prev_init_node)). //! for equality is NodeTraits::get_next((NodeTraits::get_next(p)).
//! //!
//! <b>Complexity</b>: Linear to the number of elements in the circular list. //! <b>Complexity</b>: Linear to the number of elements in the circular list.
//! //!
//! <b>Throws</b>: Nothing. //! <b>Throws</b>: Nothing.
static node_ptr get_previous_previous_node(const node_ptr & prev_prev_init_node, const node_ptr & this_node) static node_ptr get_previous_previous_node(node_ptr p, const node_ptr & this_node)
{ {
node_ptr p = prev_prev_init_node;
node_ptr p_next = NodeTraits::get_next(p); node_ptr p_next = NodeTraits::get_next(p);
node_ptr p_next_next = NodeTraits::get_next(p_next); node_ptr p_next_next = NodeTraits::get_next(p_next);
while (this_node != p_next_next){ while (this_node != p_next_next){

View File

@@ -71,24 +71,36 @@ struct default_avltree_node_traits_impl
static node_ptr get_parent(const const_node_ptr & n) static node_ptr get_parent(const const_node_ptr & n)
{ return n->parent_; } { return n->parent_; }
static node_ptr get_parent(const node_ptr & n)
{ return n->parent_; }
static void set_parent(const node_ptr & n, const node_ptr & p) static void set_parent(const node_ptr & n, const node_ptr & p)
{ n->parent_ = p; } { n->parent_ = p; }
static node_ptr get_left(const const_node_ptr & n) static node_ptr get_left(const const_node_ptr & n)
{ return n->left_; } { return n->left_; }
static node_ptr get_left(const node_ptr & n)
{ return n->left_; }
static void set_left(const node_ptr & n, const node_ptr & l) static void set_left(const node_ptr & n, const node_ptr & l)
{ n->left_ = l; } { n->left_ = l; }
static node_ptr get_right(const const_node_ptr & n) static node_ptr get_right(const const_node_ptr & n)
{ return n->right_; } { return n->right_; }
static node_ptr get_right(const node_ptr & n)
{ return n->right_; }
static void set_right(const node_ptr & n, const node_ptr & r) static void set_right(const node_ptr & n, const node_ptr & r)
{ n->right_ = r; } { n->right_ = r; }
static balance get_balance(const const_node_ptr & n) static balance get_balance(const const_node_ptr & n)
{ return n->balance_; } { return n->balance_; }
static balance get_balance(const node_ptr & n)
{ return n->balance_; }
static void set_balance(const node_ptr & n, balance b) static void set_balance(const node_ptr & n, balance b)
{ n->balance_ = b; } { n->balance_ = b; }

View File

@@ -31,9 +31,8 @@ class common_slist_algorithms
typedef typename NodeTraits::const_node_ptr const_node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr;
typedef NodeTraits node_traits; typedef NodeTraits node_traits;
static node_ptr get_previous_node(const node_ptr & prev_init_node, const node_ptr & this_node) static node_ptr get_previous_node(node_ptr p, const node_ptr & this_node)
{ {
node_ptr p = prev_init_node;
for( node_ptr p_next for( node_ptr p_next
; this_node != (p_next = NodeTraits::get_next(p)) ; this_node != (p_next = NodeTraits::get_next(p))
; p = p_next){ ; p = p_next){

View File

@@ -47,12 +47,18 @@ struct list_node_traits
static node_ptr get_previous(const const_node_ptr & n) static node_ptr get_previous(const const_node_ptr & n)
{ return n->prev_; } { return n->prev_; }
static node_ptr get_previous(const node_ptr & n)
{ return n->prev_; }
static void set_previous(const node_ptr & n, const node_ptr & prev) static void set_previous(const node_ptr & n, const node_ptr & prev)
{ n->prev_ = prev; } { n->prev_ = prev; }
static node_ptr get_next(const const_node_ptr & n) static node_ptr get_next(const const_node_ptr & n)
{ return n->next_; } { return n->next_; }
static node_ptr get_next(const node_ptr & n)
{ return n->next_; }
static void set_next(const node_ptr & n, const node_ptr & next) static void set_next(const node_ptr & n, const node_ptr & next)
{ n->next_ = next; } { n->next_ = next; }
}; };

View File

@@ -71,24 +71,36 @@ struct default_rbtree_node_traits_impl
static node_ptr get_parent(const const_node_ptr & n) static node_ptr get_parent(const const_node_ptr & n)
{ return n->parent_; } { return n->parent_; }
static node_ptr get_parent(const node_ptr & n)
{ return n->parent_; }
static void set_parent(const node_ptr & n, const node_ptr & p) static void set_parent(const node_ptr & n, const node_ptr & p)
{ n->parent_ = p; } { n->parent_ = p; }
static node_ptr get_left(const const_node_ptr & n) static node_ptr get_left(const const_node_ptr & n)
{ return n->left_; } { return n->left_; }
static node_ptr get_left(const node_ptr & n)
{ return n->left_; }
static void set_left(const node_ptr & n, const node_ptr & l) static void set_left(const node_ptr & n, const node_ptr & l)
{ n->left_ = l; } { n->left_ = l; }
static node_ptr get_right(const const_node_ptr & n) static node_ptr get_right(const const_node_ptr & n)
{ return n->right_; } { return n->right_; }
static node_ptr get_right(const node_ptr & n)
{ return n->right_; }
static void set_right(const node_ptr & n, const node_ptr & r) static void set_right(const node_ptr & n, const node_ptr & r)
{ n->right_ = r; } { n->right_ = r; }
static color get_color(const const_node_ptr & n) static color get_color(const const_node_ptr & n)
{ return n->color_; } { return n->color_; }
static color get_color(const node_ptr & n)
{ return n->color_; }
static void set_color(const node_ptr & n, color c) static void set_color(const node_ptr & n, color c)
{ n->color_ = c; } { n->color_ = c; }
@@ -117,24 +129,36 @@ struct compact_rbtree_node_traits_impl
static node_ptr get_parent(const const_node_ptr & n) static node_ptr get_parent(const const_node_ptr & n)
{ return ptr_bit::get_pointer(n->parent_); } { return ptr_bit::get_pointer(n->parent_); }
static node_ptr get_parent(const node_ptr & n)
{ return ptr_bit::get_pointer(n->parent_); }
static void set_parent(const node_ptr & n, const node_ptr & p) static void set_parent(const node_ptr & n, const node_ptr & p)
{ ptr_bit::set_pointer(n->parent_, p); } { ptr_bit::set_pointer(n->parent_, p); }
static node_ptr get_left(const const_node_ptr & n) static node_ptr get_left(const const_node_ptr & n)
{ return n->left_; } { return n->left_; }
static node_ptr get_left(const node_ptr & n)
{ return n->left_; }
static void set_left(const node_ptr & n, const node_ptr & l) static void set_left(const node_ptr & n, const node_ptr & l)
{ n->left_ = l; } { n->left_ = l; }
static node_ptr get_right(const const_node_ptr & n) static node_ptr get_right(const const_node_ptr & n)
{ return n->right_; } { return n->right_; }
static node_ptr get_right(const node_ptr & n)
{ return n->right_; }
static void set_right(const node_ptr & n, const node_ptr & r) static void set_right(const node_ptr & n, const node_ptr & r)
{ n->right_ = r; } { n->right_ = r; }
static color get_color(const const_node_ptr & n) static color get_color(const const_node_ptr & n)
{ return (color)ptr_bit::get_bits(n->parent_); } { return (color)ptr_bit::get_bits(n->parent_); }
static color get_color(const node_ptr & n)
{ return (color)ptr_bit::get_bits(n->parent_); }
static void set_color(const node_ptr & n, color c) static void set_color(const node_ptr & n, color c)
{ ptr_bit::set_bits(n->parent_, c != 0); } { ptr_bit::set_bits(n->parent_, c != 0); }

View File

@@ -45,6 +45,9 @@ struct slist_node_traits
static node_ptr get_next(const const_node_ptr & n) static node_ptr get_next(const const_node_ptr & n)
{ return n->next_; } { return n->next_; }
static node_ptr get_next(const node_ptr & n)
{ return n->next_; }
static void set_next(const node_ptr & n, const node_ptr & next) static void set_next(const node_ptr & n, const node_ptr & next)
{ n->next_ = next; } { n->next_ = next; }
}; };

View File

@@ -485,15 +485,14 @@ class tree_algorithms
//! <b>Complexity</b>: Logarithmic to the size of the subtree. //! <b>Complexity</b>: Logarithmic to the size of the subtree.
//! //!
//! <b>Throws</b>: Nothing. //! <b>Throws</b>: Nothing.
static node_ptr minimum (const node_ptr & node) static node_ptr minimum (node_ptr node)
{ {
node_ptr p(node); for(node_ptr p_left = NodeTraits::get_left(node)
for(node_ptr p_left = NodeTraits::get_left(p)
;p_left ;p_left
;p_left = NodeTraits::get_left(p)){ ;p_left = NodeTraits::get_left(node)){
p = p_left; node = p_left;
} }
return p; return node;
} }
//! <b>Requires</b>: 'node' is a node of a tree but not the header. //! <b>Requires</b>: 'node' is a node of a tree but not the header.
@@ -503,15 +502,14 @@ class tree_algorithms
//! <b>Complexity</b>: Logarithmic to the size of the subtree. //! <b>Complexity</b>: Logarithmic to the size of the subtree.
//! //!
//! <b>Throws</b>: Nothing. //! <b>Throws</b>: Nothing.
static node_ptr maximum(const node_ptr & node) static node_ptr maximum(node_ptr node)
{ {
node_ptr p(node); for(node_ptr p_right = NodeTraits::get_right(node)
for(node_ptr p_right = NodeTraits::get_right(p)
;p_right ;p_right
;p_right = NodeTraits::get_right(p)){ ;p_right = NodeTraits::get_right(node)){
p = p_right; node = p_right;
} }
return p; return node;
} }
//! <b>Requires</b>: 'node' must not be part of any tree. //! <b>Requires</b>: 'node' must not be part of any tree.
@@ -1171,14 +1169,13 @@ class tree_algorithms
//! <b>Complexity</b>: Logarithmic to the number of nodes in the tree. //! <b>Complexity</b>: Logarithmic to the number of nodes in the tree.
//! //!
//! <b>Throws</b>: Nothing. //! <b>Throws</b>: Nothing.
static std::size_t depth(const const_node_ptr & node) static std::size_t depth(const_node_ptr node)
{ {
const_node_ptr p(node);
std::size_t depth = 0; std::size_t depth = 0;
node_ptr p_parent; node_ptr p_parent;
while(p != NodeTraits::get_parent(p_parent = NodeTraits::get_parent(p))){ while(node != NodeTraits::get_parent(p_parent = NodeTraits::get_parent(node))){
++depth; ++depth;
p = p_parent; node = p_parent;
} }
return depth; return depth;
} }
@@ -1295,12 +1292,10 @@ class tree_algorithms
} }
template<class Disposer> template<class Disposer>
static void dispose_subtree(const node_ptr & node, Disposer disposer) static void dispose_subtree(node_ptr x, Disposer disposer)
{ {
node_ptr save;
node_ptr x(node);
while (x){ while (x){
save = NodeTraits::get_left(x); node_ptr save(NodeTraits::get_left(x));
if (save) { if (save) {
// Right rotation // Right rotation
NodeTraits::set_left(x, NodeTraits::get_right(save)); NodeTraits::set_left(x, NodeTraits::get_right(save));

View File

@@ -44,18 +44,27 @@ struct tree_node_traits
static node_ptr get_parent(const const_node_ptr & n) static node_ptr get_parent(const const_node_ptr & n)
{ return n->parent_; } { return n->parent_; }
static node_ptr get_parent(const node_ptr & n)
{ return n->parent_; }
static void set_parent(const node_ptr & n, const node_ptr & p) static void set_parent(const node_ptr & n, const node_ptr & p)
{ n->parent_ = p; } { n->parent_ = p; }
static node_ptr get_left(const const_node_ptr & n) static node_ptr get_left(const const_node_ptr & n)
{ return n->left_; } { return n->left_; }
static node_ptr get_left(const node_ptr & n)
{ return n->left_; }
static void set_left(const node_ptr & n, const node_ptr & l) static void set_left(const node_ptr & n, const node_ptr & l)
{ n->left_ = l; } { n->left_ = l; }
static node_ptr get_right(const const_node_ptr & n) static node_ptr get_right(const const_node_ptr & n)
{ return n->right_; } { return n->right_; }
static node_ptr get_right(const node_ptr & n)
{ return n->right_; }
static void set_right(const node_ptr & n, const node_ptr & r) static void set_right(const node_ptr & n, const node_ptr & r)
{ n->right_ = r; } { n->right_ = r; }
}; };

View File

@@ -805,26 +805,26 @@ class rbtree_algorithms
// NodeTraits::get_parent(NodeTraits::get_parent(p)) == p; // NodeTraits::get_parent(NodeTraits::get_parent(p)) == p;
} }
static void rebalance_after_erasure(const node_ptr & header, const node_ptr &xnode, const node_ptr &xnode_parent) static void rebalance_after_erasure(const node_ptr & header, node_ptr x, node_ptr x_parent)
{ {
node_ptr x(xnode), x_parent(xnode_parent); while(x != NodeTraits::get_parent(header) && (!x || NodeTraits::get_color(x) == NodeTraits::black())){
while(x != NodeTraits::get_parent(header) && (x == node_ptr() || NodeTraits::get_color(x) == NodeTraits::black())){
if(x == NodeTraits::get_left(x_parent)){ if(x == NodeTraits::get_left(x_parent)){
node_ptr w = NodeTraits::get_right(x_parent); node_ptr w = NodeTraits::get_right(x_parent);
BOOST_ASSERT(w);
if(NodeTraits::get_color(w) == NodeTraits::red()){ if(NodeTraits::get_color(w) == NodeTraits::red()){
NodeTraits::set_color(w, NodeTraits::black()); NodeTraits::set_color(w, NodeTraits::black());
NodeTraits::set_color(x_parent, NodeTraits::red()); NodeTraits::set_color(x_parent, NodeTraits::red());
tree_algorithms::rotate_left(x_parent, header); tree_algorithms::rotate_left(x_parent, header);
w = NodeTraits::get_right(x_parent); w = NodeTraits::get_right(x_parent);
} }
if((NodeTraits::get_left(w) == node_ptr() || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black()) && if((!NodeTraits::get_left(w) || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black()) &&
(NodeTraits::get_right(w) == node_ptr() || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black())){ (!NodeTraits::get_right(w) || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black())){
NodeTraits::set_color(w, NodeTraits::red()); NodeTraits::set_color(w, NodeTraits::red());
x = x_parent; x = x_parent;
x_parent = NodeTraits::get_parent(x_parent); x_parent = NodeTraits::get_parent(x_parent);
} }
else { else {
if(NodeTraits::get_right(w) == node_ptr() || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black()){ if(!NodeTraits::get_right(w) || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black()){
NodeTraits::set_color(NodeTraits::get_left(w), NodeTraits::black()); NodeTraits::set_color(NodeTraits::get_left(w), NodeTraits::black());
NodeTraits::set_color(w, NodeTraits::red()); NodeTraits::set_color(w, NodeTraits::red());
tree_algorithms::rotate_right(w, header); tree_algorithms::rotate_right(w, header);
@@ -847,14 +847,14 @@ class rbtree_algorithms
tree_algorithms::rotate_right(x_parent, header); tree_algorithms::rotate_right(x_parent, header);
w = NodeTraits::get_left(x_parent); w = NodeTraits::get_left(x_parent);
} }
if((NodeTraits::get_right(w) == node_ptr() || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black()) && if((!NodeTraits::get_right(w) || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black()) &&
(NodeTraits::get_left(w) == node_ptr() || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black())){ (!NodeTraits::get_left(w) || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black())){
NodeTraits::set_color(w, NodeTraits::red()); NodeTraits::set_color(w, NodeTraits::red());
x = x_parent; x = x_parent;
x_parent = NodeTraits::get_parent(x_parent); x_parent = NodeTraits::get_parent(x_parent);
} }
else { else {
if(NodeTraits::get_left(w) == node_ptr() || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black()){ if(!NodeTraits::get_left(w) || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black()){
NodeTraits::set_color(NodeTraits::get_right(w), NodeTraits::black()); NodeTraits::set_color(NodeTraits::get_right(w), NodeTraits::black());
NodeTraits::set_color(w, NodeTraits::red()); NodeTraits::set_color(w, NodeTraits::red());
tree_algorithms::rotate_left(w, header); tree_algorithms::rotate_left(w, header);
@@ -874,9 +874,8 @@ class rbtree_algorithms
} }
static void rebalance_after_insertion(const node_ptr & header, const node_ptr &pnode) static void rebalance_after_insertion(const node_ptr & header, node_ptr p)
{ {
node_ptr p(pnode);
NodeTraits::set_color(p, NodeTraits::red()); NodeTraits::set_color(p, NodeTraits::red());
while(p != NodeTraits::get_parent(header) && NodeTraits::get_color(NodeTraits::get_parent(p)) == NodeTraits::red()){ while(p != NodeTraits::get_parent(header) && NodeTraits::get_color(NodeTraits::get_parent(p)) == NodeTraits::red()){
node_ptr p_parent(NodeTraits::get_parent(p)); node_ptr p_parent(NodeTraits::get_parent(p));