diff --git a/Framework/Kernel/inc/MantidKernel/TimeSplitter.h b/Framework/Kernel/inc/MantidKernel/TimeSplitter.h index 43637bbdcd95..2ac09cbdc8fe 100644 --- a/Framework/Kernel/inc/MantidKernel/TimeSplitter.h +++ b/Framework/Kernel/inc/MantidKernel/TimeSplitter.h @@ -13,7 +13,11 @@ namespace Mantid { namespace Kernel { -/** TimeSplitter : TODO: DESCRIPTION +/** + * TimeSplitter is an object that contains a mapping of time regions [inclusive, exclusive) that map to output workspace + * indices. No time can be mapped to two output workspace indices and all time from the beginning to the end is + * accounted for. A negative workspace index indicates that the data in that region should be ignored. This object + * converts all negative indices to -1. */ class MANTID_KERNEL_DLL TimeSplitter { public: @@ -29,7 +33,7 @@ class MANTID_KERNEL_DLL TimeSplitter { private: void clearAndReplace(const Types::Core::DateAndTime &start, const Types::Core::DateAndTime &stop, const int value); - void debugPrint() const; + std::string debugPrint() const; std::map m_roi_map; }; diff --git a/Framework/Kernel/src/TimeSplitter.cpp b/Framework/Kernel/src/TimeSplitter.cpp index c7a2a280a0ab..3b6be931b700 100644 --- a/Framework/Kernel/src/TimeSplitter.cpp +++ b/Framework/Kernel/src/TimeSplitter.cpp @@ -35,9 +35,11 @@ TimeSplitter::TimeSplitter(const Types::Core::DateAndTime &start, const Types::C clearAndReplace(start, stop, DEFAULT_VALUE); } -void TimeSplitter::debugPrint() const { +std::string TimeSplitter::debugPrint() const { + std::stringstream msg; for (const auto iter : m_roi_map) - std::cout << iter.second << "|" << iter.first << "\n"; + msg << iter.second << "|" << iter.first << "\n"; + return msg.str(); } void TimeSplitter::addROI(const Types::Core::DateAndTime &start, const Types::Core::DateAndTime &stop, @@ -156,28 +158,37 @@ std::vector TimeSplitter::outputWorkspaceIndices() const { /** * Returns a Mantid::Kernel::TimeROI for the requested workspace index. - * This will return an empty TimeROI if the workspace index is <0 (ignore value) - * or does not exist in the TimeSplitter. + * This will raise an exception if the workspace index does not exist in the TimeSplitter. */ TimeROI TimeSplitter::getTimeROI(const int workspaceIndex) { + // convert things less than -1 to -1 + const int effectiveIndex = std::max(workspaceIndex, -1); + TimeROI output; - // only build a result if this is not the IGNORE_VALUE - if (workspaceIndex > IGNORE_VALUE) { - using map_value_type = std::map::value_type; - auto indexFinder = [workspaceIndex](const map_value_type &value) { return value.second == workspaceIndex; }; - // find the first place this workspace index exists - auto iter = std::find_if(m_roi_map.begin(), m_roi_map.end(), indexFinder); - // add the ROI found then loop until we reach the end - while (iter != m_roi_map.end()) { - // add the ROI - const auto startTime = iter->first; - iter++; + using map_value_type = std::map::value_type; + auto indexFinder = [effectiveIndex](const map_value_type &value) { return value.second == effectiveIndex; }; + // find the first place this workspace index exists + auto iter = std::find_if(m_roi_map.begin(), m_roi_map.end(), indexFinder); + // add the ROI found then loop until we reach the end + while (iter != m_roi_map.end()) { + // add the ROI + const auto startTime = iter->first; + iter++; + // if the next iterator is the end there is nothing to add + if (iter != m_roi_map.end()) { const auto stopTime = iter->first; output.addROI(startTime, stopTime); - - // look for the next place the workspace index occurs - iter = std::find_if(iter, m_roi_map.end(), indexFinder); } + + // look for the next place the workspace index occurs + iter = std::find_if(iter, m_roi_map.end(), indexFinder); + } + + // error check that something is there + // ignore index being empty is ok + if ((workspaceIndex >= 0) && (output.empty())) { + std::stringstream msg; + msg << "No regions exist for workspace index " << workspaceIndex; } return output; diff --git a/Framework/Kernel/test/TimeSplitterTest.h b/Framework/Kernel/test/TimeSplitterTest.h index f0e37430b5f5..a572cfd8cb2d 100644 --- a/Framework/Kernel/test/TimeSplitterTest.h +++ b/Framework/Kernel/test/TimeSplitterTest.h @@ -170,7 +170,9 @@ class TimeSplitterTest : public CxxTest::TestSuite { // add the same output index, but with a gap from the previous splitter.addROI(FOUR, FIVE, 1); - TS_ASSERT(splitter.getTimeROI(-1).empty()); + roi = splitter.getTimeROI(-2); // intentionally trying a "big" negative for ignore filter + TS_ASSERT(!roi.empty()); + TS_ASSERT_EQUALS(roi.numBoundaries(), 2); TS_ASSERT(splitter.getTimeROI(0).empty()); TS_ASSERT(splitter.getTimeROI(0).empty()); roi = splitter.getTimeROI(1);