LSST Applications 29.1.1,g0fba68d861+94d977d4f8,g1fd858c14a+0a42b1a450,g21d47ad084+bae5d1592d,g35bb328faa+fcb1d3bbc8,g36ff55ed5b+4036fd6440,g4e0f332c67+abab7ee1ee,g53246c7159+fcb1d3bbc8,g60b5630c4e+4036fd6440,g67b6fd64d1+31de10a2f7,g72a202582f+7a25662ef1,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g86c591e316+1a75853d69,g8852436030+8220ab3cb6,g88f4e072da+7005418d1d,g89139ef638+31de10a2f7,g8b8da53e10+8f7b08dc1c,g9125e01d80+fcb1d3bbc8,g989de1cb63+31de10a2f7,g9f1445be69+4036fd6440,g9f33ca652e+fcef3ba435,ga9baa6287d+4036fd6440,ga9e4eb89a6+a41a34c2ba,gabe3b4be73+1e0a283bba,gb0b61e0e8e+d456af7c26,gb1101e3267+f17a9d70ea,gb58c049af0+f03b321e39,gb89ab40317+31de10a2f7,gce29eb0867+05ed69485a,gcf25f946ba+8220ab3cb6,gd6cbbdb0b4+11317e7a17,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+b4f50ea554,ge278dab8ac+50e2446c94,ge410e46f29+31de10a2f7,ge80e9994a3+32bb9bc1c9,gf5e32f922b+fcb1d3bbc8,gf67bdafdda+31de10a2f7
LSST Data Management Base Package
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1#ifndef LSST_GAUSS2D_FIT_UTIL_H
2#define LSST_GAUSS2D_FIT_UTIL_H
3
4#include <algorithm>
5#include <memory>
6#include <set>
7#include <sstream>
8#include <stdexcept>
9#include <string>
10#include <vector>
11
12namespace lsst::gauss2d::fit {
13
19template <typename T>
21 const bool isclose;
22 const double diff_abs;
23 const T margin;
24
25 std::string str() const {
27 ss.precision(5);
28 ss << "isclose=" << isclose << " from diff=" << diff_abs << " <= margin=" << margin;
29 return ss.str();
30 }
31};
32
47template <typename T>
48IsCloseResult<T> isclose(T a, T b, T rtol = 1e-5, T atol = 1e-8) {
49 const T diff_abs = std::abs(a - b);
50 const T margin = atol + rtol * std::abs(b);
51 return IsCloseResult<T>{diff_abs <= margin, diff_abs, margin};
52}
53
54// The rest of these functions are mainly intended to make printing container members easier
55template <template <typename...> class Map, class Key, class Value>
56std::set<Key> map_keys(const Map<Key, Value>& map) {
57 std::set<Key> result;
58 for (const auto& it : map) {
59 result.insert(it.first);
60 }
61 return result;
62}
63
64template <template <typename...> class Map, class Key, class Value>
68 for (const auto& it : map) result.insert(it.first.get());
69 return result;
70}
71
72template <template <typename...> class Map, class Key, class Value>
74 const Map<Key, std::shared_ptr<const Value>>& map) {
76 for (const auto& it : map) result.insert(it.second);
77 return result;
78}
79
80template <template <typename...> class Map, class Key, class Value>
82 const Map<Key, std::reference_wrapper<const Value>>& map) {
84 for (const auto& it : map) result.insert(it.second);
85 return result;
86}
87
88// Builds a new vector with only unique elements, preserving order
89// Note: std::unique only removes consecutive identical elements
90// Also, this returns a new vector, potentially copying elements
91template <typename T>
93 std::set<T> set{};
94 std::vector<T> rval{};
95 rval.reserve(vec.size());
96 for (const auto& elem : vec) {
97 auto result = set.insert(elem);
98 if (result.second) rval.push_back(elem);
99 }
100 return rval;
101}
102
103/*
104template <typename T>
105std::string str_ptr(const T* obj) {
106 return (obj == nullptr) ? "None" : obj->str();
107}
108
109template <typename T>
110std::string str_iter_ptr(const T& container) {
111 std::string str = "[";
112 for (const auto& obj : container) str += str_ptr(obj) + ",";
113 return str.substr(0, str.size() - 1) + "]";
114}
115
116*/
117template <template <typename...> class Container, class Value>
118Container<Value> head_iter(const Container<Value>& container, size_t n) {
119 return Container<Value>(container.begin(), container.begin() + n);
120}
121
122template <template <typename...> class Container, class Value>
123Container<Value> tail_iter(const Container<Value>& container, size_t n) {
124 return Container<Value>(container.end() - n, container.end());
125}
126
127} // namespace lsst::gauss2d::fit
128
129#endif
std::set< std::reference_wrapper< const Key > > map_keys_ref_const(const Map< std::reference_wrapper< const Key >, Value, std::less< const Key > > &map)
Definition util.h:65
std::set< std::shared_ptr< const Value > > map_values_shared_ptr_const(const Map< Key, std::shared_ptr< const Value > > &map)
Definition util.h:73
IsCloseResult< T > isclose(T a, T b, T rtol=1e-5, T atol=1e-8)
Check if two values are close, within some tolerances.
Definition util.h:48
Container< Value > head_iter(const Container< Value > &container, size_t n)
Definition util.h:118
std::set< Key > map_keys(const Map< Key, Value > &map)
Definition util.h:56
Container< Value > tail_iter(const Container< Value > &container, size_t n)
Definition util.h:123
std::vector< T > nonconsecutive_unique(const std::vector< T > &vec)
Definition util.h:92
std::set< std::reference_wrapper< const Value > > map_values_ref_const(const Map< Key, std::reference_wrapper< const Value > > &map)
Definition util.h:81
T precision(T... args)
T reserve(T... args)
T size(T... args)
T str(T... args)
The result of an isclose() check.
Definition util.h:20
std::string str() const
Definition util.h:25