22#define checkVecLen(v,l) isce3::core::checkVecLenDebug(v,l,#v,__PRETTY_FUNCTION__)
23#define check2dVecLen(v,l,w) isce3::core::check2dVecLenDebug(v,l,w,#v,__PRETTY_FUNCTION__)
25namespace isce3 {
namespace core {
29 inline uint64_t getTimeStampMillisec() {
30 using namespace std::chrono;
31 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
36 inline std::string getTempString(
const std::string& suffix) {
37 if (suffix.length() == 0) {
38 return "tmp_" + std::to_string(getTimeStampMillisec());
40 return "tmp_" + suffix +
"_" + std::to_string(getTimeStampMillisec());
49 inline void checkVecLenDebug(
const std::vector<T> &vec,
size_t len,
const char *vec_name,
50 const char *parent_func) {
51 if (vec.size() != len) {
52 std::string errstr =
"In '" + std::string(parent_func) +
"': Vector '" +
53 std::string(vec_name) +
"' has size " +
54 std::to_string(vec.size()) +
", expected size " +
55 std::to_string(len) +
".";
56 throw std::invalid_argument(errstr);
62 inline void check2dVecLenDebug(
const std::vector<std::vector<T>> &vec,
63 size_t len,
size_t width,
const char *vec_name,
64 const char *parent_func) {
65 if ((vec.size() != len) && (vec[0].size() != width)) {
66 std::string errstr =
"In '" + std::string(parent_func) +
"': Vector '" +
67 std::string(vec_name) +
"' has size (" +
68 std::to_string(vec.size()) +
" x " +
69 std::to_string(vec[0].size()) +
"), expected size (" +
70 std::to_string(len) +
" x " + std::to_string(width) +
").";
71 throw std::invalid_argument(errstr);
80 inline std::vector<T> arange(T low, T high, T increment) {
86 while (current < high) {
87 data.push_back(current);
99 inline std::vector<T> linspace(T low, T high, std::size_t number) {
103 T increment = (high - low) / (number - 1);
113 for (std::size_t i = 0; i < number - 1; ++i) {
114 data.push_back(low + i * increment);
117 data.push_back(high);
126 template <
typename T>
127 inline void linspace(T low, T high, std::vector<T> & data) {
129 int number = data.size();
130 T increment = (high - low) / (number - 1);
140 for (std::size_t i = 0; i < number; ++i) {
141 data[i] = low + i * increment;
150 template <
typename T>
151 inline void linspace(T low, T high, std::valarray<T> & data) {
153 int number = data.size();
154 T increment = (high - low) / (number - 1);
164 for (std::size_t i = 0; i < number; ++i) {
165 data[i] = low + i * increment;
175 template <
typename T>
176 inline std::vector<T> logspace(T first, T last, std::size_t number, T base=10) {
180 T increment = (last - first) / (number - 1);
186 data.push_back(pow(base, first));
190 for (std::size_t i = 0; i < number - 1; ++i) {
191 T value = pow(base, first + i * increment);
192 data.push_back(value);
195 data.push_back(pow(base, last));
203 template <
typename T>
204 inline std::vector<T> stringToVector(
const std::string & str) {
206 std::stringstream stream(str);
210 while (stream >> number) {
211 vec.push_back(number);
220 template <
typename T>
221 inline std::string vectorToString(
const std::vector<T> & vec) {
223 std::stringstream stream;
225 std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(stream,
" "));
235 template <
typename T>
237 compareFloatingPoint(T first, T second) {
238 const T scale = std::max({
static_cast<T
>(1.0), std::abs(first), std::abs(second)});
239 return std::abs(first - second) <= std::numeric_limits<T>::epsilon() * scale;
246 template <
typename T>
248 compareComplex(std::complex<T> first, std::complex<T> second) {
249 return compareFloatingPoint(first.real(), second.real())
250 && compareFloatingPoint(first.imag(), second.imag());
257 template <
typename T>
258 inline std::valarray<bool>
259 makeMask(
const std::valarray<std::complex<T>> & v, std::complex<T> noDataValue) {
260 std::valarray<bool> mask(v.size());
261 for (
size_t i = 0; i < v.size(); ++i) {
262 if (compareComplex(v[i], noDataValue)) {
275 template <
typename T>
276 inline std::valarray<bool>
277 makeMask(
const std::valarray<T> & v, T noDataValue) {
278 std::valarray<bool> mask(v.size());
279 for (
size_t i = 0; i < v.size(); ++i) {
280 if (compareFloatingPoint(v[i], noDataValue)) {
292 inline void insertionSort(std::valarray<double> & a,
293 std::valarray<double> & b,
294 std::valarray<double> & c) {
295 for (
int i = 1; i < a.size(); ++i) {
297 const double tempa = a[i];
298 const double tempb = b[i];
299 const double tempc = c[i];
300 while (j >= 0 && a[j] > tempa) {
315 inline void insertionSort(std::valarray<double> & a,
316 std::valarray<double> & b,
317 std::valarray<short> & c) {
318 for (
int i = 1; i < a.size(); ++i) {
320 const double tempa = a[i];
321 const double tempb = b[i];
322 const short tempc = c[i];
323 while (j >= 0 && a[j] > tempa) {
336 inline int binarySearch(
const std::valarray<double> & array,
double value) {
340 int right = array.size() - 1;
342 while (left <= right) {
343 const int middle =
static_cast<int>(std::round(0.5 * (left + right)));
344 if (left == (right - 1)) {
348 if (array[middle] <= value) {
350 }
else if (array[middle] > value) {
360 inline const T & clamp(
const T & x,
const T & lower,
const T & upper) {
361 return std::min(upper, std::max(x, lower));
base interpolator is an abstract base class
Definition BinarySearchFunc.cpp:5