From ac97ae0d947f2d5cf2ff83e4fb8512087b1b8d9c Mon Sep 17 00:00:00 2001 From: Vasileios Karakasis <karakasis@cscs.ch> Date: Fri, 29 Jan 2016 15:34:17 +0100 Subject: [PATCH] Added comparison operators for cell records. The will be useful for sorting cell records. Equality and ordering is defined solely on records IDs. --- src/swcio.hpp | 25 +++++++++++++++++++++++++ tests/test_swcio.cpp | 16 ++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/swcio.hpp b/src/swcio.hpp index ac39de4e..ff605500 100644 --- a/src/swcio.hpp +++ b/src/swcio.hpp @@ -79,18 +79,43 @@ public: cell_record(const cell_record &other) = default; cell_record &operator=(const cell_record &other) = default; + // Equality and comparison operators friend bool operator==(const cell_record &lhs, const cell_record &rhs) { return lhs.id_ == rhs.id_; } + friend bool operator<(const cell_record &lhs, + const cell_record &rhs) + { + return lhs.id_ < rhs.id_; + } + + friend bool operator<=(const cell_record &lhs, + const cell_record &rhs) + { + return (lhs < rhs) || (lhs == rhs); + } + friend bool operator!=(const cell_record &lhs, const cell_record &rhs) { return !(lhs == rhs); } + friend bool operator>(const cell_record &lhs, + const cell_record &rhs) + { + return !(lhs < rhs) && (lhs != rhs); + } + + friend bool operator>=(const cell_record &lhs, + const cell_record &rhs) + { + return !(lhs < rhs); + } + friend std::ostream &operator<<(std::ostream &os, const cell_record &cell); kind type() const diff --git a/tests/test_swcio.cpp b/tests/test_swcio.cpp index 24ff682b..00310bbf 100644 --- a/tests/test_swcio.cpp +++ b/tests/test_swcio.cpp @@ -87,6 +87,22 @@ TEST(cell_record, construction) } } +TEST(cell_record, comparison) +{ + using namespace nestmc::io; + + { + // check comparison operators + cell_record cell0(cell_record::custom, 0, 1., 1., 1., 1., -1); + cell_record cell1(cell_record::custom, 0, 2., 3., 4., 5., -1); + cell_record cell2(cell_record::custom, 1, 2., 3., 4., 5., -1); + EXPECT_EQ(cell0, cell1); + EXPECT_LT(cell0, cell2); + EXPECT_GT(cell2, cell1); + } + +} + TEST(swc_parser, invalid_input) { using namespace nestmc::io; -- GitLab