Belle II Software  release-08-01-10
NumberSequence.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <framework/utilities/NumberSequence.h>
10 
11 #include <utility>
12 #include <vector>
13 
14 namespace Belle2 {
20  std::set<int64_t> generate_number_sequence(const std::string& str)
21  {
22  // Tokenize input
23  std::vector<std::string> tokens;
24  unsigned int lastdelimiter = 0;
25  for (unsigned int i = 0; i < str.size(); ++i) {
26  if (str[i] == ',') {
27  tokens.push_back(str.substr(lastdelimiter, i - lastdelimiter));
28  lastdelimiter = i + 1;
29  }
30  }
31  std::string last = str.substr(lastdelimiter);
32  if (last.size() != 0) {
33  tokens.push_back(last);
34  }
35 
36  // Convert tokens into intervals
37  std::vector<std::pair<int64_t, int64_t>> intervals;
38  intervals.reserve(tokens.size());
39  for (const auto& token : tokens) {
40  size_t interval_marker_position = token.find(':');
41  // Single number
42  if (interval_marker_position == std::string::npos) {
43  int64_t number = std::stol(token);
44  intervals.emplace_back(number, number);
45  } else {
46  int64_t interval_begin = std::stol(token.substr(0, interval_marker_position));
47  int64_t interval_end = std::stol(token.substr(interval_marker_position + 1));
48  intervals.emplace_back(interval_begin, interval_end);
49  }
50  }
51 
52  // Convert tokens into number sequence
53  std::set<int64_t> sequence;
54  for (const auto& interval : intervals) {
55  for (int64_t number = interval.first; number <= interval.second; ++number)
56  sequence.insert(number);
57  }
58 
59  return sequence;
60  }
61 
63 }
std::set< int64_t > generate_number_sequence(const std::string &str)
Generate a sequence of numbers defined by a string.
Abstract base class for different kinds of events.