diff --git a/Framework/Indexing/inc/MantidIndexing/IndexSet.h b/Framework/Indexing/inc/MantidIndexing/IndexSet.h index dfb3c25774aa69eeff63db6be74df21ebbca5295..91e88ca7a1dcd91e4bd05ebe10237cca5701dfba 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 916f4429dd1990f9e9c9f9eccc7666c94cb2126b..a8b337d480d3d3d8ec2a2485ac0436fc5a276de3 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_ */