3 #include <nlohmann/json.hpp> 30 template <std::
integral TOut, std::
floating_po
int TIn>
inline TOut
numeric_cast(
const TIn number)
32 if (std::isfinite(number)) {
34 if (number < std::nextafter(static_cast<TIn>(std::numeric_limits<TOut>::max()), 0) &&
35 number > std::nextafter(static_cast<TIn>(std::numeric_limits<TOut>::min()), 0)) {
38 return static_cast<TOut
>(number >= 0 ? number + TIn(0.5)
43 throw std::overflow_error(
"numeric cast overflow");
54 template <std::forward_iterator Titer,
typename Tfunction,
typename T = double,
55 typename Taggregate_function>
57 Taggregate_function aggregator = std::plus<T>())
60 for (
auto i = begin; i != end; ++i) {
61 for (
auto j = i; ++j != end;) {
62 x = aggregator(x, f(*i, *j));
69 template <std::ranges::range T> T
erase_range(T target,
const T& values)
71 target.erase(std::remove_if(target.begin(), target.end(),
73 return std::find(values.begin(), values.end(), i) !=
90 using base = std::pair<T, T>;
94 : base(std::minmax(a, b))
98 bool contains(
const T& value)
const {
return value == base::first || value == base::second; }
118 template <std::
floating_po
int T, std::
integral T
int =
int> Tint
to_bin(T x, T dx = 1)
120 return (x < 0) ? Tint(x / dx - 0.5) : Tint(x / dx + 0.5);
145 template <std::
floating_po
int T
float =
double>
class Quantize 167 x = int(val / dx + 0.5) * dx;
170 x = int(val / dx - 0.5) * dx;
190 template <
typename T>
operator T()
192 if (std::is_integral<T>::value) {
193 return T((x - xmin) / dx + 0.5);
200 concept StringStreamable = requires(T a) {
201 { std::istringstream() >> a };
202 { std::ostringstream() << a };
214 template <StringStreamable T>
auto splitConvert(
const std::string& words)
216 auto stream = std::istringstream(words);
217 return std::vector<T>(std::istream_iterator<T>(stream), std::istream_iterator<T>());
225 template <std::ranges::range Range>
227 requires StringStreamable<std::ranges::range_value_t<Range>>
229 std::ostringstream o;
230 if (!values.empty()) {
231 o << *values.begin();
232 std::for_each(std::next(values.begin()), values.end(), [&](
auto& val) { o <<
" " << val; });
239 using value_type = std::shared_ptr<T>;
240 std::vector<value_type>
vec;
242 auto begin() noexcept {
return vec.begin(); }
244 auto begin()
const noexcept {
return vec.begin(); }
246 auto end() noexcept {
return vec.end(); }
248 auto end()
const noexcept {
return vec.end(); }
250 auto empty()
const noexcept {
return vec.empty(); }
252 auto size()
const noexcept {
return vec.size(); }
254 auto& back() noexcept {
return vec.back(); }
256 auto& back()
const noexcept {
return vec.back(); }
258 auto& front() noexcept {
return vec.front(); }
260 auto& front()
const noexcept {
return vec.front(); }
262 auto& at(
size_t n) {
return vec.at(n); }
264 auto& at(
size_t n)
const {
return vec.at(n); }
266 template <
typename Tderived,
class... Args,
267 class = std::enable_if_t<std::is_base_of<T, Tderived>::value>>
270 vec.push_back(std::make_shared<Tderived>(args...));
273 template <
typename Tderived,
class... Args,
274 class = std::enable_if_t<std::is_base_of<T, Tderived>::value>>
277 vec.push_back(std::make_shared<Tderived>(args...));
292 template <typename Tderived, class = std::enable_if_t<std::is_base_of<T, Tderived>::value>>
295 vec.assign(d.
vec.begin(), d.
vec.end());
299 template <typename Tderived, class = std::enable_if_t<std::is_base_of<T, Tderived>::value>>
303 for (
auto& base : vec) {
304 if (
auto derived = std::dynamic_pointer_cast<Tderived>(base); derived) {
311 template <typename Tderived, class = std::enable_if_t<std::is_base_of<T, Tderived>::value>>
312 std::shared_ptr<Tderived> findFirstOf()
const 314 for (
auto& base : vec) {
315 if (
auto derived = std::dynamic_pointer_cast<Tderived>(base); derived) {
322 template <
typename U>
323 friend void to_json(nlohmann::json&,
331 for (
auto& shared_ptr : b.
vec) {
332 j.push_back(*shared_ptr);
335 catch (
const std::exception& e) {
336 throw std::runtime_error(
"error converting to json: "s + e.what());
344 for (
const auto& it : j) {
345 std::shared_ptr<T> ptr = it;
349 catch (
const std::exception& e) {
350 throw std::runtime_error(
"error converting from json: "s + e.what());
auto splitConvert(const std::string &words)
Convert space separated words into vector of type T.
Definition: auxiliary.h:214
void emplace_back(const Args &... args)
Create an (derived) instance and append a pointer to it to the vector.
Definition: auxiliary.h:275
Tint to_bin(T x, T dx=1)
Round a floating point to integer for use with histogram binning.
Definition: auxiliary.h:118
double T
floating point size
Definition: units.h:73
Quantize(Tfloat dx, Tfloat xmin=0)
Constructor.
Definition: auxiliary.h:156
T for_each_unique_pair(Titer begin, Titer end, Tfunction f, Taggregate_function aggregator=std::plus< T >())
Iterate over pairs in container, call a function on the elements, and sum results.
Definition: auxiliary.h:56
T erase_range(T target, const T &values)
Erase from target range all values found in values range.
Definition: auxiliary.h:69
std::string joinToString(const Range &values) requires StringStreamable< std
Convert range of values into space separated string.
Definition: auxiliary.h:226
auto & operator=(const BasePointerVector< Tderived > &d)
Allow assignment to a vector of ancestors.
Definition: auxiliary.h:293
Ordered pair where first<=second
Definition: auxiliary.h:88
Cell list class templates.
Definition: actions.cpp:11
void emplace_back(Args &... args)
Create an (derived) instance and append a pointer to it to the vector.
Definition: auxiliary.h:268
TOut numeric_cast(const TIn number)
Convert floating point number to integral number.
Definition: auxiliary.h:30
std::vector< value_type > vec
Vector of shared pointers to base class.
Definition: auxiliary.h:240
auto & push_back(value_type arg)
Append a pointer to a (derived) instance to the vector.
Definition: auxiliary.h:286
Helper class for storing vectors of base pointers.
Definition: auxiliary.h:237
Quantize & operator=(Tfloat val)
Assigment operator.
Definition: auxiliary.h:163
Round and bin numbers for use with tables and histograms.
Definition: auxiliary.h:145
Quantize & operator()(Tfloat val)
Assignment with function operator.
Definition: auxiliary.h:183
auto find() const
Pointer list to all matching type.
Definition: auxiliary.h:300