From 4abbb39aec00dd178b1119e9cc84c9702f7192bb Mon Sep 17 00:00:00 2001 From: Alex Nguyen Date: Fri, 15 Jan 2021 00:31:14 -0500 Subject: [PATCH] add move assignment operator and fix static code check --- src/mlpack/core/tree/hollow_ball_bound.hpp | 3 ++ .../core/tree/hollow_ball_bound_impl.hpp | 41 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/mlpack/core/tree/hollow_ball_bound.hpp b/src/mlpack/core/tree/hollow_ball_bound.hpp index d8b65dcf87..d699eab693 100644 --- a/src/mlpack/core/tree/hollow_ball_bound.hpp +++ b/src/mlpack/core/tree/hollow_ball_bound.hpp @@ -86,6 +86,9 @@ class HollowBallBound //! Move constructor: take possession of another bound. HollowBallBound(HollowBallBound&& other); + //! Move assignment operator. + HollowBallBound& operator=(HollowBallBound&& other); + //! Destructor to release allocated memory. ~HollowBallBound(); diff --git a/src/mlpack/core/tree/hollow_ball_bound_impl.hpp b/src/mlpack/core/tree/hollow_ball_bound_impl.hpp index b8446ec350..8ccd06225c 100644 --- a/src/mlpack/core/tree/hollow_ball_bound_impl.hpp +++ b/src/mlpack/core/tree/hollow_ball_bound_impl.hpp @@ -80,15 +80,17 @@ template HollowBallBound& HollowBallBound:: operator=(const HollowBallBound& other) { - if (ownsMetric) - delete metric; - - radii = other.radii; - center = other.center; - hollowCenter = other.hollowCenter; - metric = other.metric; - ownsMetric = false; + if (this != &other) + { + if (ownsMetric) + delete metric; + radii = other.radii; + center = other.center; + hollowCenter = other.hollowCenter; + metric = other.metric; + ownsMetric = false; + } return *this; } @@ -111,6 +113,29 @@ HollowBallBound::HollowBallBound( other.ownsMetric = false; } +//! Move assignment operator. +template +HollowBallBound& HollowBallBound:: +operator=(HollowBallBound&& other) +{ + if (this != &other) + { + radii = other.radii; + center = std::move(other.center); + hollowCenter = std::move(other.hollowCenter); + metric = other.metric; + ownsMetric = other.ownsMetric; + + other.radii.Hi() = 0.0; + other.radii.Lo() = 0.0; + other.center = arma::Col(); + other.hollowCenter = arma::Col(); + other.metric = nullptr; + other.ownsMetric = false; + } + return *this; +} + //! Destructor to release allocated memory. template HollowBallBound::~HollowBallBound()