HOPS
HOPS class reference
MHO_SkyFreqGrid.hh
Go to the documentation of this file.
1 #ifndef MHO_SkyFreqGrid_HH__
2 #define MHO_SkyFreqGrid_HH__
3 
4 #include <algorithm>
5 #include <cmath>
6 #include <cstddef>
7 #include <vector>
8 
9 namespace hops
10 {
11 
23 {
24  public:
25  static constexpr double DEFAULT_TOL_MHZ = 0.001;
26 
28 
29  explicit MHO_SkyFreqGrid(std::vector< double > grid_MHz, double tol = DEFAULT_TOL_MHZ)
30  : fGrid(std::move(grid_MHz)), fTol(tol)
31  {
32  std::sort(fGrid.begin(), fGrid.end());
33  }
34 
35  //queue a sky_freq for inclusion; takes effect after Finalize()
36  void Add(double sky_freq_MHz) { fPending.push_back(sky_freq_MHz); }
37 
38  //sort pending entries and merge into the canonical grid, deduplicating within fTol
39  void Finalize()
40  {
41  std::sort(fPending.begin(), fPending.end());
42  fGrid.clear();
43  for(double f : fPending)
44  {
45  bool dup = false;
46  for(double g : fGrid)
47  {
48  if(std::fabs(f - g) < fTol)
49  {
50  dup = true;
51  break;
52  }
53  }
54  if(!dup)
55  {
56  fGrid.push_back(f);
57  }
58  }
59  std::sort(fGrid.begin(), fGrid.end());
60  fPending.clear();
61  }
62 
63  void SetTolerance(double tol_MHz) { fTol = tol_MHz; }
64 
65  double GetTolerance() const { return fTol; }
66 
67  bool Empty() const { return fGrid.empty(); }
68 
69  std::size_t Size() const { return fGrid.size(); }
70 
71  const std::vector< double >& Frequencies() const { return fGrid; }
72 
73  //returns true if some grid entry is within tolerance of sky_freq_MHz;
74  //out_index holds that entry's position when found.
75  bool FindIndex(double sky_freq_MHz, std::size_t& out_index) const
76  {
77  for(std::size_t i = 0; i < fGrid.size(); i++)
78  {
79  if(std::fabs(sky_freq_MHz - fGrid[i]) < fTol)
80  {
81  out_index = i;
82  return true;
83  }
84  }
85  return false;
86  }
87 
88  bool Contains(double sky_freq_MHz) const
89  {
90  std::size_t idx;
91  return FindIndex(sky_freq_MHz, idx);
92  }
93 
94  private:
95  std::vector< double > fGrid;
96  std::vector< double > fPending;
97  double fTol;
98 };
99 
100 } // namespace hops
101 
102 #endif
Creates a sorted, tolerance-deduplicated list of sky frequencies (MHz) with indexed lookup....
Definition: MHO_SkyFreqGrid.hh:23
void SetTolerance(double tol_MHz)
Definition: MHO_SkyFreqGrid.hh:63
std::size_t Size() const
Definition: MHO_SkyFreqGrid.hh:69
bool FindIndex(double sky_freq_MHz, std::size_t &out_index) const
Definition: MHO_SkyFreqGrid.hh:75
const std::vector< double > & Frequencies() const
Definition: MHO_SkyFreqGrid.hh:71
void Finalize()
Definition: MHO_SkyFreqGrid.hh:39
MHO_SkyFreqGrid(std::vector< double > grid_MHz, double tol=DEFAULT_TOL_MHZ)
Definition: MHO_SkyFreqGrid.hh:29
void Add(double sky_freq_MHz)
Definition: MHO_SkyFreqGrid.hh:36
double GetTolerance() const
Definition: MHO_SkyFreqGrid.hh:65
MHO_SkyFreqGrid()
Definition: MHO_SkyFreqGrid.hh:27
static constexpr double DEFAULT_TOL_MHZ
Definition: MHO_SkyFreqGrid.hh:25
bool Empty() const
Definition: MHO_SkyFreqGrid.hh:67
bool Contains(double sky_freq_MHz) const
Definition: MHO_SkyFreqGrid.hh:88
Definition: MHO_AdhocFlagging.hh:18