Commit 186097b0 authored by Michal Vaner's avatar Michal Vaner
Browse files

RBTree::swap()

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac451@4034 e5f2f494-b856-4b98-b285-d166d9295462
parent cd266f92
......@@ -294,6 +294,16 @@ public:
Result insert(const isc::dns::Name& name, RBNode<T>** inserted_node);
//@}
/// \brief Swaps two tree's contents.
///
/// This acts the same as many std::*.swap functions, exchanges the
/// contents. This doesn't throw anything.
void swap(RBTree<T>& other) {
std::swap(root_, other.root_);
std::swap(NULLNODE, other.NULLNODE);
std::swap(node_count_, other.node_count_);
}
private:
/// \name RBTree balance functions
//@{
......
......@@ -176,4 +176,34 @@ TEST_F(RBTreeTest, dumpTree) {
str2 << "tree has 13 node(s)\nb. (black)\n a. (black)\n NULL\n NULL\n d.e.f. (black)[invisible] \n begin down from d.e.f.\n w.y. (black)[invisible] \n begin down from w.y.\n p. (black)\n o. (red)\n NULL\n NULL\n q. (red)\n NULL\n NULL\n end down from w.y.\n x. (red)\n NULL\n NULL\n z. (red)\n begin down from z.\n j. (black)\n NULL\n NULL\n end down from z.\n NULL\n NULL\n end down from d.e.f.\n c. (red)\n NULL\n NULL\n g.h. (red)\n begin down from g.h.\n i. (black)\n NULL\n NULL\n end down from g.h.\n NULL\n NULL\n";
EXPECT_EQ(str.str(), str2.str());
}
TEST_F(RBTreeTest, swap) {
// Store info about the first tree
std::ostringstream str1;
rbtree.dumpTree(str1);
size_t count1(rbtree.getNodeCount());
// Create second one and store state
RBTree<int> tree2;
RBNode<int>* node;
tree2.insert(Name("second"), &node);
std::ostringstream str2;
tree2.dumpTree(str2);
// Swap them
ASSERT_NO_THROW(tree2.swap(rbtree));
// Check their sizes
ASSERT_EQ(1, rbtree.getNodeCount());
ASSERT_EQ(count1, tree2.getNodeCount());
// And content
std::ostringstream out;
rbtree.dumpTree(out);
ASSERT_EQ(str2.str(), out.str());
out.str("");
tree2.dumpTree(out);
ASSERT_EQ(str1.str(), out.str());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment