/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/util/include/RAT/TimeSort.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/util/include/RAT/TimeSort.hh Source File
Ratpac-two
TimeSort.hh
1 #ifndef __RAT_TimeSort__
2 #define __RAT_TimeSort__
3 
4 #include <algorithm>
5 #include <utility>
6 #include <vector>
7 
8 namespace RAT {
10 template <typename T>
11 inline bool TimeSort(const std::pair<T, T> &firstPair, const std::pair<T, T> &secondPair) {
12  return (firstPair.first < secondPair.first);
13 }
14 
15 /* Functions to sort and merge overlapping time intervals. */
16 inline void TimeSortMerge(std::vector<std::pair<int, int>> &pulseTimes) {
17  sort(pulseTimes.begin(), pulseTimes.end(), TimeSort<int>);
18 
19  if (pulseTimes.size() == 0) return;
20 
21  std::vector<std::pair<int, int>> merged;
22  std::pair<int, int> current = pulseTimes[0];
23 
24  for (unsigned int i = 1; i < pulseTimes.size(); i++) {
25  // offset -1 to fuse adjacent integers
26  if (pulseTimes[i].first - 1 <= current.second) {
27  if (current.second < pulseTimes[i].second) current.second = pulseTimes[i].second;
28  } else {
29  merged.push_back(current);
30  current = pulseTimes[i];
31  }
32  }
33  merged.push_back(current);
34 
35  // Exchange storage
36  pulseTimes.swap(merged);
37 }
38 
39 inline void TimeSortMerge(std::vector<std::pair<double, double>> &pulseTimes) {
40  sort(pulseTimes.begin(), pulseTimes.end(), TimeSort<double>);
41 
42  if (pulseTimes.size() == 0) return;
43 
44  std::vector<std::pair<double, double>> merged;
45  std::pair<double, double> current = pulseTimes[0];
46 
47  for (unsigned int i = 1; i < pulseTimes.size(); i++) {
48  if (pulseTimes[i].first <= current.second) {
49  if (current.second < pulseTimes[i].second) current.second = pulseTimes[i].second;
50  } else {
51  merged.push_back(current);
52  current = pulseTimes[i];
53  }
54  }
55  merged.push_back(current);
56 
57  // Exchange storage
58  pulseTimes.swap(merged);
59 }
60 
61 } // namespace RAT
62 
63 #endif
Definition: CCCrossSecMessenger.hh:29
bool TimeSort(const std::pair< T, T > &firstPair, const std::pair< T, T > &secondPair)
Definition: TimeSort.hh:11