From 6451b4712a04eb2c551cc137bbdd44bc1eee7f05 Mon Sep 17 00:00:00 2001 From: Antti Soininen <soininen@ill.fr> Date: Tue, 18 Dec 2018 15:44:23 +0100 Subject: [PATCH] Add isContiguous to IndexSet. Re #24355 --- .../Indexing/inc/MantidIndexing/IndexSet.h | 17 +++++++++++++++++ Framework/Indexing/test/IndexSetTest.h | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/Framework/Indexing/inc/MantidIndexing/IndexSet.h b/Framework/Indexing/inc/MantidIndexing/IndexSet.h index dfb3c25774a..91e88ca7a1d 100644 --- a/Framework/Indexing/inc/MantidIndexing/IndexSet.h +++ b/Framework/Indexing/inc/MantidIndexing/IndexSet.h @@ -91,6 +91,8 @@ public: return m_indices[index]; } + bool isContiguous() const noexcept; + protected: ~IndexSet() = default; @@ -139,6 +141,21 @@ IndexSet<T>::IndexSet(const std::vector<size_t> &indices, size_t fullRange) m_size = m_indices.size(); } +/** + * Check if the index range is contiguous and in ascending order. + */ +template <class T> +bool IndexSet<T>::isContiguous() const noexcept { + if (!m_isRange || m_indices.size() > 1) { + for (size_t i = 0; i < m_indices.size() - 1; ++i) { + if (m_indices[i] + 1 != m_indices[i + 1]) { + return false; + } + } + } + return true; +} + } // namespace detail } // namespace Indexing } // namespace Mantid diff --git a/Framework/Indexing/test/IndexSetTest.h b/Framework/Indexing/test/IndexSetTest.h index 916f4429dd1..a8b337d480d 100644 --- a/Framework/Indexing/test/IndexSetTest.h +++ b/Framework/Indexing/test/IndexSetTest.h @@ -137,6 +137,17 @@ public: TS_ASSERT_THROWS_NOTHING(--it2); TS_ASSERT_EQUALS(*it2, 0); } + + void test_isContiguous() { + const IndexSetTester empty; + TS_ASSERT(empty.isContiguous()) + const IndexSetTester range(3); + TS_ASSERT(range.isContiguous()) + const IndexSetTester manualRange({3, 4, 5}, 6); + TS_ASSERT(manualRange.isContiguous()) + IndexSetTester nonContiguous({2, 1, 3}, 4); + TS_ASSERT(!nonContiguous.isContiguous()) + } }; #endif /* MANTID_INDEXING_INDEXSETTEST_H_ */ -- GitLab