Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
BinMap.cpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "cheetah/utils/BinMap.h"
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wall"
27 #pragma GCC diagnostic ignored "-Wpragmas"
28 #pragma GCC diagnostic ignored "-Wunused-parameter"
29 #pragma GCC diagnostic ignored "-Wunused-variable"
30 #include <boost/units/quantity.hpp>
31 #pragma GCC diagnostic pop
32 #include <functional>
33 
34 namespace ska {
35 namespace cheetah {
36 namespace utils {
37 
38 namespace detail {
39 template<typename DataType>
41  template<typename T>
42  inline
43  DataType operator()(DataType const& data, T const& t) const {
44  return data * t;
45  }
46  template<typename T>
47  inline
48  DataType divide(DataType const& data, T const& t) const {
49  return data/DataType(t);
50  }
51 };
52 
53 template<typename Unit, typename DataType>
54 struct DataTypeOperationsHelper<boost::units::quantity<Unit, DataType>> {
55  template<typename T>
56  inline
57  boost::units::quantity<Unit,DataType> operator()(boost::units::quantity<Unit,DataType> const& data, T const& t) const {
58  return data * static_cast<DataType>(t);
59  }
60  template<typename T>
61  inline
62  boost::units::quantity<Unit,DataType> divide(boost::units::quantity<Unit,DataType> const& data, T const& t) const {
63  return data/static_cast<DataType>(t);
64  }
65 };
66 
67 } // namespace detail
68 
69 template<typename DataType>
71  : _n_bins(0)
72  , _lower(0)
73  , _width(0)
74 {
75 }
76 
77 template<typename DataType>
79 {
80 }
81 
82 template<typename DataType>
83 BinMap<DataType>::BinMap(unsigned int number_of_bins)
84  : _n_bins(number_of_bins)
85  , _lower(0)
86  , _width(0)
87 {
88 }
89 
90 template<typename DataType>
91 BinMap<DataType>::BinMap(unsigned int number_of_bins, DataType start, DataType const& end)
92  : _n_bins(number_of_bins)
93  , _lower(std::move(start))
94 {
95  set_upper_bound(end);
96 }
97 
98 template<typename DataType>
99 void BinMap<DataType>::reset(unsigned int number_of_bins)
100 {
101  _n_bins = number_of_bins;
102 }
103 
104 template<typename DataType>
106 {
107  _width = width;
108  _halfwidth = detail::DataTypeOperationsHelper<DataType>().divide(width, 2);
109 }
110 
111 template<typename DataType>
112 void BinMap<DataType>::set_bounds(DataType lower, DataType upper)
113 {
114  _lower = lower;
115  set_upper_bound(upper);
116 }
117 
118 template<typename DataType>
119 void BinMap<DataType>::set_lower_bound(DataType const start)
120 {
121  _lower = start;
122 }
123 
124 template<typename DataType>
125 void BinMap<DataType>::set_upper_bound(DataType const& end)
126 {
127  set_bin_width( detail::DataTypeOperationsHelper<DataType>().divide((end - _lower),_n_bins));
128 }
129 
130 template<typename DataType>
131 inline DataType BinMap<DataType>::lower_bound() const
132 {
133  return _lower;
134 }
135 
136 template<typename DataType>
137 inline DataType BinMap<DataType>::upper_bound() const
138 {
139  return last_bin_assignment_value() + _halfwidth;
140 }
141 
142 template<typename DataType>
143 inline DataType BinMap<DataType>::bin_assignment_number(int index) const
144 {
145  return _lower + _halfwidth + detail::DataTypeOperationsHelper<DataType>()(_width, index);
146 }
147 
148 template<typename DataType>
149 DataType BinMap<DataType>::bin_start(unsigned int index) const
150 {
151  return bin_assignment_number(index) - _halfwidth;
152 }
153 
154 template<typename DataType>
155 DataType BinMap<DataType>::bin_end(unsigned int index) const
156 {
157  return bin_assignment_number(index) + _halfwidth;
158 }
159 
160 template<typename DataType>
161 unsigned int BinMap<DataType>::bin_index(DataType const& value) const
162 {
163  return (int)(0.5 + ((value - _lower)/ _width ));
164 }
165 
166 template<typename DataType>
167 inline unsigned BinMap<DataType>::last_bin_index() const
168 {
169  return _n_bins-1;
170 }
171 
172 template<typename DataType>
174 {
175  return _lower + detail::DataTypeOperationsHelper<DataType>()(_width, last_bin_index()) + _halfwidth;
176 }
177 
178 template<typename DataType>
180 {
181  return _lower + _halfwidth;
182 }
183 
184 template<typename DataType>
186 {
187  return _width;
188 }
189 
190 template<typename DataType>
192 {
193  return _n_bins;
194 }
195 
196 template<typename DataType>
197 bool BinMap<DataType>::equal(const BinMap<DataType>& map) const
198 {
199  return map._lower == _lower && map._n_bins == _n_bins && map._width == _width;
200 }
201 
202 template<typename DataType>
203 bool operator==(const BinMap<DataType>& map_1, const BinMap<DataType>& map_2)
204 {
205  return map_1.equal(map_2);
206 }
207 
208 } // namespace utils
209 } // namespace cheetah
210 } // namespace ska
211 
212 // hashing function to allow storage of type in STL containers
213 namespace std {
214  template<typename DataType>
215  struct hash<ska::cheetah::utils::BinMap<DataType>> {
216  std::size_t operator()(const ska::cheetah::utils::BinMap<DataType>& map) const {
217  return std::hash<unsigned>()(map.number_of_bins()) ^ std::hash<DataType>()(map.lower_bound()) ^ (std::hash<DataType>()(map.bin_width()) << 4);
218  }
219  };
220 } // namespace std
unsigned int bin_index(DataType const &) const
give the bin Index of the value. n.b no range checks are made so passing endValue() will return an i...
Definition: BinMap.cpp:161
Definition: Units.h:112
Explicitly map a range on to a set of constant width indexed data bins.
Definition: BinMap.h:48
Some limits and constants for FLDO.
Definition: Brdz.h:35
void reset(unsigned int number_of_bins)
reset the number of bins
Definition: BinMap.cpp:99
void set_bin_width(DataType)
set the width of each bin
Definition: BinMap.cpp:105
void set_lower_bound(DataType)
set the assignemnt value for the first bin (n.b. not the same as the lower limit of the first bin) ...
Definition: BinMap.cpp:119
unsigned int number_of_bins() const
the total number of bins
Definition: BinMap.cpp:191
DataType bin_width() const
the width of a the bin (all are the same width)
Definition: BinMap.cpp:185
void set_bounds(DataType lower, DataType upper)
set the the lower bound for first bin
Definition: BinMap.cpp:112