From: Berk Hess Date: Tue, 23 Mar 2021 14:47:26 +0000 (+0000) Subject: Add comparison operator to BasicVector X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=6fff49f17a7a716451919691d689b0d621b981fe;p=alexxy%2Fgromacs.git Add comparison operator to BasicVector The default comparison seemed to compare the memory addresses and not the elements. --- diff --git a/api/legacy/include/gromacs/math/vectypes.h b/api/legacy/include/gromacs/math/vectypes.h index d4d5211d11..55f28e2a4d 100644 --- a/api/legacy/include/gromacs/math/vectypes.h +++ b/api/legacy/include/gromacs/math/vectypes.h @@ -4,7 +4,7 @@ * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. * Copyright (c) 2013,2014,2016,2017,2018 by the GROMACS development team. - * Copyright (c) 2019,2020, by the GROMACS development team, led by + * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -123,6 +123,16 @@ public: ValueType& operator[](int i) { return x_[i]; } //! Indexing operator to make the class work as the raw array. ValueType operator[](int i) const { return x_[i]; } + //! Return whether all elements compare equal + bool operator==(const BasicVector& right) + { + return x_[0] == right[0] && x_[1] == right[1] && x_[2] == right[2]; + } + //! Return whether any elements compare unequal + bool operator!=(const BasicVector& right) + { + return x_[0] != right[0] || x_[1] != right[1] || x_[2] != right[2]; + } //! Allow inplace addition for BasicVector BasicVector& operator+=(const BasicVector& right) { diff --git a/src/gromacs/math/tests/vectypes.cpp b/src/gromacs/math/tests/vectypes.cpp index 3cbaab7b72..7049e97433 100644 --- a/src/gromacs/math/tests/vectypes.cpp +++ b/src/gromacs/math/tests/vectypes.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2014,2015,2016,2018,2019, by the GROMACS development team, led by + * Copyright (c) 2014,2015,2016,2018,2019,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -118,6 +118,24 @@ TEST(RVecTest, WorksAs_rvec_Array) EXPECT_EQ(4, r[1][ZZ]); } +TEST(RVecTest, ComparesEqual) +{ + RVec a(1, 2, 3); + RVec b(1, 2, 3); + RVec c(1, 2, 2); + EXPECT_EQ(a == b, true); + EXPECT_EQ(a == c, false); +} + +TEST(RVecTest, ComparesUnequal) +{ + RVec a(1, 2, 3); + RVec b(1, 2, 3); + RVec c(1, 2, 2); + EXPECT_EQ(a != b, false); + EXPECT_EQ(a != c, true); +} + /*! \brief * Test overloaded math operations */