14#ifndef __cpp_lib_containers_ranges
15 void append_range(
const FileList& rhs)
noexcept { insert(end(), rhs.cbegin(), rhs.cend()); }
25[[nodiscard]] T no_convert(
int value,
int)
noexcept
27 return static_cast<T
>(value);
39template <
class Elem,
class Traits,
class Alloc>
40std::basic_istream<Elem, Traits>& getline(
41 std::basic_istream<Elem, Traits>* stream,
42 std::basic_string<Elem, Traits, Alloc>* str,
47 return getline(*stream, *str, delimiter);
54[[nodiscard]]
bool directory_exists(
const char* dir)
noexcept;
60[[nodiscard]]
bool file_exists(
const char* path)
noexcept;
68[[nodiscard]] FileList read_directory(
69 const string_view name,
70 const string_view match =
"*",
71 const bool for_files =
true
79[[nodiscard]] FileList find_rasters(
const string_view dir, YearSize year);
84void make_directory(
const char* dir)
noexcept;
89void make_directory_recursive(
string dir)
noexcept;
96[[nodiscard]]
inline constexpr MathSize sq(
const T& x)
98 return static_cast<MathSize
>(x) *
static_cast<MathSize
>(x);
107template <
unsigned int N,
class T>
108[[nodiscard]]
constexpr T pow_int(
const T& base)
112 : N % 2 == 0 ? pow_int<N / 2, T>(base) * pow_int<N / 2, T>(base)
113 : base * pow_int<(N - 1) / 2, T>(base) * pow_int<(N - 1) / 2, T>(base);
121template <
unsigned int N,
class T>
122[[nodiscard]]
constexpr T bit_mask()
124 return static_cast<T
>(pow_int<N, int64_t>(2) - 1);
132template <
unsigned int N>
133[[nodiscard]] MathSize round_to_precision(
const MathSize value)
noexcept
138 static const auto b = pow_int<N, int64_t>(10);
139 return round(value * b) / b;
161 explicit UsageCount(
string for_what)
noexcept;
181[[nodiscard]] typename std::vector<T>::iterator insert_sorted(std::vector<T>* vec, T const& item)
183 return vec->insert(std::upper_bound(vec->begin(), vec->end(), item), item);
192void insert_unique(std::vector<T>* vec, T
const& item)
194 const auto i = std::lower_bound(vec->begin(), vec->end(), item);
195 if (i == vec->end() || *i != item)
197 vec->insert(i, item);
211template <
typename T,
typename V>
212T binary_find(
const T lower,
const T upper,
const V value,
const std::function<V(T)>& fct)
214 const auto mid = lower + (upper - lower) / 2;
219 if (fct(mid) < value)
221 return binary_find(lower, mid, value, fct);
223 return binary_find(mid + 1, upper, value, fct);
236template <
typename T,
typename V>
237T binary_find_checked(
const T lower,
const T upper,
const V value,
const std::function<V(T)>& fct)
239 if (fct(lower) < value)
243 if (fct(upper) >= value)
247 return binary_find(lower, upper, value, fct);
251 using is_transparent = void;
252 [[nodiscard]]
size_t operator()(
const char* txt)
const
254 return std::hash<std::string_view>{}(txt);
256 [[nodiscard]]
size_t operator()(std::string_view txt)
const
258 return std::hash<std::string_view>{}(txt);
260 [[nodiscard]]
size_t operator()(
const std::string& txt)
const
262 return std::hash<std::string>{}(txt);
266using string_map = unordered_map<string, V, string_hash, std::equal_to<>>;
273[[nodiscard]]
inline Radians ellipse_angle(
const MathSize length_to_breadth,
const Radians& theta)
275 return Radians{atan2(sin(theta) / length_to_breadth, cos(theta))}.fix();
281 bool operator()(
const std::shared_ptr<T>& lhs,
const std::shared_ptr<T>& rhs)
const
291class sp_set :
public std::set<shared_ptr<T>, sp_less<T>>
293 using base = std::set<shared_ptr<T>,
sp_less<T>>;
296 template <
class... Args>
297 auto sp_emplace(Args&&... args)
300 return base::emplace(
new T(std::forward<Args>(args)...));
303inline string find_value(
304 const string_view key,
305 const string_view within,
306 const string fallback =
""
309 const auto c = within.find(key);
310 if (c != string::npos)
312 const string str = &within.at(c +
string(key).length());
313 return str.substr(0, str.find(
' '));
317inline int str_to_int(
const string str) {
return stoi(str); }
318inline MathSize str_to_value(
const string str) {
return static_cast<MathSize
>(stod(str)); }
320inline bool find_value(
321 const string_view key,
322 const string_view within,
324 T (*convert)(
const string_view str)
327 const auto str = find_value(key, within);
330 *result = convert(str);
336string get_canonical_path(
const char*
const dir_root,
string path);
341 LazyPath(
const string dir_root,
const string path)
344 return std::filesystem::absolute(dir_root).generic_string();
348 LazyPath(
const string_view dir_root,
const string_view path)
349 :
LazyPath(
string(dir_root),
string(path))
351 bool empty()
const {
return path_.empty(); }
352 string intended_path()
const noexcept {
return std::format(
"{}/{}", dir_root_, path_); }
353 const char* canonical()
const
355 if (
nullptr == canonical_path_)
357 canonical_path_ = make_unique<string>(get_canonical_path(dir_root_.c_str(), path_));
359 return canonical_path_->c_str();
363 dir_root_ = rhs.dir_root_;
365 canonical_path_ =
nullptr;
370 dir_root_ = std::move(rhs.dir_root_);
371 path_ = std::move(rhs.path_);
372 canonical_path_ = std::move(rhs.canonical_path_);
373 rhs.canonical_path_ =
nullptr;
376 LazyPath& operator=(
const string& path)
noexcept
379 canonical_path_ =
nullptr;
386 mutable unique_ptr<string> canonical_path_{
nullptr};
389string tolower(
string value);
391string toupper(
string value);
393std::ostream& operator<<(std::ostream& os,
const std::optional<V>& v)
405 const string previous_directory;
406 const string current_directory;
407 pushd(
string path) noexcept
408 : previous_directory{std::filesystem::current_path().generic_string()},
409 current_directory{[&]() {
415 auto p_rel = std::filesystem::relative(path);
416 auto p_abs = std::filesystem::absolute(p_rel);
417 auto p = p_abs.lexically_normal();
418 std::filesystem::current_path(p);
419 return p.generic_string();
422 ~pushd()
noexcept { std::filesystem::current_path(previous_directory); }
423 pushd& operator=(
const pushd& rhs)
noexcept =
delete;
424 pushd& operator=(
pushd&& rhs)
noexcept =
delete;
Provides the ability to determine how many times something is used during a simulation.
Definition Util.h:145
string for_what_
What this is tracking usage of.
Definition Util.h:153
atomic< size_t > count_
How many times this has been used.
Definition Util.h:149
UsageCount & operator++() noexcept
Increment operator.
Definition Util.cpp:109
UsageCount(string for_what) noexcept
Constructor.
Definition Util.cpp:108