DataBlockGenerator.cpp 2.78 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
// SPDX - License - Identifier: GPL - 3.0 +
7
#include "MantidDataHandling/DataBlockGenerator.h"
LamarMoore's avatar
LamarMoore committed
8
#include "MantidDataHandling/DataBlock.h"
9
10
11
12
13
14
15

namespace Mantid {
namespace DataHandling {

// -------------------------------------------------------------
// DataBlock Generator
// -------------------------------------------------------------
16
DataBlockGenerator::DataBlockGenerator(
17
    const std::vector<spectrumPair> &intervals)
18
19
    : m_intervals(intervals) {
  // We need to sort the data items.
Stephen's avatar
Stephen committed
20
  auto comparison = [](const spectrumPair &el1, const spectrumPair &el2) {
21
22
23
    return el1.first < el2.first;
  };
  std::sort(m_intervals.begin(), m_intervals.end(), comparison);
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  // If there is an interval then set the current index to the first interval in
  // the sorted container
  if (!m_intervals.empty()) {
    m_currentIntervalIndex = 0;
    m_currentSpectrum = m_intervals[m_currentIntervalIndex.get()].first;

  } else {
    m_currentIntervalIndex = boost::none;
    m_currentSpectrum = -1;
  }
}

/**
 * We need to increment through a series of intervals and need to
 * make sure that we skip the gaps. This does assume that the intervals
 * are ordered.
 */
DataBlockGenerator &DataBlockGenerator::operator++() {
  ++m_currentSpectrum;

  // If there is no valid interval index then do nothing
  if (m_currentIntervalIndex) {
    // We need to check if this index is still in the current interval
    // If not we need to increment the interval or set the interval index
    // to a final state
    auto isinCurrentInterval =
        m_intervals[m_currentIntervalIndex.get()].first <= m_currentSpectrum &&
        m_currentSpectrum <= m_intervals[m_currentIntervalIndex.get()].second;

    if (!isinCurrentInterval) {
      ++(*m_currentIntervalIndex);

      // Check if we are past the last interval or else set it to the
      // first element of the new interval
      if (m_currentIntervalIndex.get() > (m_intervals.size() - 1)) {
        m_currentIntervalIndex = boost::none;
      } else {
        m_currentSpectrum = m_intervals[m_currentIntervalIndex.get()].first;
      }
    }
  }

  return *this;
68
69
}

70
71
72
73
74
75
76
77
78
79
80
81
82
// Postincrement version
DataBlockGenerator DataBlockGenerator::operator++(int) {
  DataBlockGenerator temp(this->m_intervals);
  ++(*this);
  return temp;
}

/**
 * Convenience method for incrementing
 */
void DataBlockGenerator::next() { ++(*this); }

bool DataBlockGenerator::isDone() { return !m_currentIntervalIndex; }
83

Stephen's avatar
Stephen committed
84
specnum_t DataBlockGenerator::getValue() { return m_currentSpectrum; }
LamarMoore's avatar
LamarMoore committed
85
86
} // namespace DataHandling
} // namespace Mantid