Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added copy constructor, move constructor and copy assignment operator. #826

Merged
merged 16 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/mlpack/methods/det/dtree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ DTree<MatType, TagType>& DTree<MatType, TagType>::operator=(const DTree<MatType,
template <typename MatType, typename TagType>
DTree<MatType, TagType>::DTree(DTree&& obj)
{
//Moving the values from object
start = std::move(obj.start);
end = std::move(obj.end);
maxVals = std::move(obj.maxVals);
Expand All @@ -228,7 +229,13 @@ DTree<MatType, TagType>::DTree(DTree&& obj)
logVolume = std::move(obj.logVolume);
bucketTag = std::move(obj.bucketTag);
alphaUpper = std::move(obj.alphaUpper);
left = std::move(obj.left);

//Free the space allocated
delete left;
delete right;

//Moving the children
left = std::move(obj.left);
right = std::move(obj.right);

//Set obj to default values
Expand Down
34 changes: 34 additions & 0 deletions src/mlpack/tests/det_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ BOOST_AUTO_TEST_CASE(CopyConstructorAndOperatorTest)
DTree<arma::mat> tree2(*tree);
DTree<arma::mat> tree3 = *tree;

// Pointers to children of original tree
DTree<arma::mat>* leftChild = tree->Left();
DTree<arma::mat>* rightChild = tree->Right();

//Deleting original DTree
delete tree;

Expand All @@ -528,6 +532,14 @@ BOOST_AUTO_TEST_CASE(CopyConstructorAndOperatorTest)
BOOST_REQUIRE_EQUAL(tree3.minVals[1], 0);
BOOST_REQUIRE_EQUAL(tree3.maxVals[2], 8);
BOOST_REQUIRE_EQUAL(tree3.minVals[2], 1);

//Checking children were safely copied
BOOST_REQUIRE(tree2.Left()==leftChild);
BOOST_REQUIRE(tree2.Right()==rightChild);
BOOST_REQUIRE(tree3.Left()==leftChild);
BOOST_REQUIRE(tree3.Right()==rightChild);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point leftChild and rightChild should be invalid pointers; I bet valgrind will call this an invalid memory access and in some cases this test will fail.



}

// Test the move constructor.
Expand All @@ -542,6 +554,13 @@ BOOST_AUTO_TEST_CASE(MoveConstructorTest)
// Constructing a DTree
DTree<arma::mat>* tree = new DTree<arma::mat>(testData);

<<<<<<< HEAD
=======
// Pointers to children of original tree
DTree<arma::mat>* leftChild = tree->Left();
DTree<arma::mat>* rightChild = tree->Right();

>>>>>>> 952211b83dcfe89a39f9c66df0971c18c451734f
// Moving DTree using move constructor
DTree<arma::mat> tree2(std::move(*tree));

Expand Down Expand Up @@ -569,6 +588,13 @@ BOOST_AUTO_TEST_CASE(MoveOperatorTest)
// Constructing a DTree
DTree<arma::mat>* tree = new DTree<arma::mat>(testData);

<<<<<<< HEAD
=======
// Pointers to children of original tree
DTree<arma::mat>* leftChild = tree->Left();
DTree<arma::mat>* rightChild = tree->Right();

>>>>>>> 952211b83dcfe89a39f9c66df0971c18c451734f
// Constructing copy of DTree using move operator
DTree<arma::mat> tree2 = std::move(*tree);

Expand All @@ -582,6 +608,14 @@ BOOST_AUTO_TEST_CASE(MoveOperatorTest)
BOOST_REQUIRE_EQUAL(tree2.minVals[1], 0);
BOOST_REQUIRE_EQUAL(tree2.maxVals[2], 8);
BOOST_REQUIRE_EQUAL(tree2.minVals[2], 1);
<<<<<<< HEAD
=======

//Checking children were safely moved
BOOST_REQUIRE(tree2.Left()==leftChild);
BOOST_REQUIRE(tree2.Right()==rightChild);

>>>>>>> 952211b83dcfe89a39f9c66df0971c18c451734f
}

BOOST_AUTO_TEST_SUITE_END();