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
Changes from 1 commit
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
27 changes: 10 additions & 17 deletions src/mlpack/methods/det/dtree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,16 @@ DTree<MatType, TagType>& DTree<MatType, TagType>::operator=(const DTree<MatType,
logVolume = obj.logVolume;
bucketTag = obj.bucketTag;
alphaUpper = obj.alphaUpper;

//Copying the children
left = new DTree(*obj.left);
right = new DTree(*obj.right);

return *this;
}

template <typename MatType, typename TagType>
DTree<MatType, TagType>::DTree(DTree&& obj)
DTree<MatType, TagType>::DTree(DTree&& obj)
{
//Moving the values from object
start = std::move(obj.start);
Expand All @@ -229,14 +229,8 @@ DTree<MatType, TagType>::DTree(DTree&& obj)
logVolume = std::move(obj.logVolume);
bucketTag = std::move(obj.bucketTag);
alphaUpper = std::move(obj.alphaUpper);

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

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

//Set obj to default values
obj.start = 0;
Expand All @@ -256,11 +250,11 @@ DTree<MatType, TagType>::DTree(DTree&& obj)

}


template <typename MatType, typename TagType>
DTree<MatType, TagType>& DTree<MatType, TagType>::operator=(DTree<MatType, TagType>&& obj)
{
//Moving the values from obj
if (this != &obj){
Copy link
Member

Choose a reason for hiding this comment

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

I think the test to see if this is the same as the object being copied is unnecessary.

//Moving the values from object
start = std::move(obj.start);
end = std::move(obj.end);
maxVals = std::move(obj.maxVals);
Expand All @@ -275,12 +269,12 @@ DTree<MatType, TagType>& DTree<MatType, TagType>::operator=(DTree<MatType, TagTy
logVolume = std::move(obj.logVolume);
bucketTag = std::move(obj.bucketTag);
alphaUpper = std::move(obj.alphaUpper);
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't bother with std::move() for primitive types like int, double, etc., since move will degrade to copies for primitive types. As a result, using move() there can actually make the code more confusing, because we can't assume that the rvalues will be set to default values or something. (That is my current understanding of the spec.)


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

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

Choose a reason for hiding this comment

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

Since you're just moving pointers here, you don't actually need to use std::move(), you can just do left = obj.left and right = obj.right.


Expand All @@ -299,10 +293,9 @@ DTree<MatType, TagType>& DTree<MatType, TagType>::operator=(DTree<MatType, TagTy
obj.alphaUpper = 0.0;
obj.left = NULL;
obj.right = NULL;
}
}


// Root node initializers
template <typename MatType, typename TagType>
DTree<MatType, TagType>::DTree(const StatType& maxVals,
Expand Down