alpaqa 1.0.0a19
Nonconvex constrained optimization
Loading...
Searching...
No Matches
params.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <alpaqa/export.hpp>
5
6#include <optional>
7#include <span>
8#include <stdexcept>
9#include <string_view>
10#include <tuple>
11
12namespace alpaqa::params {
13
14/// Represents a parameter value encoded as a string in the format
15/// `abc.def.key=value`.
17 /// Full key string, used for diagnostics.
18 std::string_view full_key;
19 /// The subkey to resolve next.
20 std::string_view key;
21 /// The value of the parameter to store.
22 std::string_view value;
23};
24
25/// Custom parameter parsing exception.
26struct ALPAQA_EXPORT invalid_param : std::invalid_argument {
27 using std::invalid_argument::invalid_argument;
28};
29
30/// Split the string @p full on the first occurrence of @p tok.
31/// Returns (s, "") if tok was not found.
32inline auto split_key(std::string_view full, char tok = '.') {
33 auto tok_pos = full.find(tok);
34 if (tok_pos == full.npos)
35 return std::make_tuple(full, std::string_view{});
36 std::string_view key{full.begin(), full.begin() + tok_pos};
37 std::string_view rem{full.begin() + tok_pos + 1, full.end()};
38 return std::make_tuple(key, rem);
39}
40
41/// Update/overwrite the first argument based on the option in @p s.
42template <class T>
43void ALPAQA_EXPORT set_param(T &, ParamString); /* deliberately undefined */
44
45/// Overwrites @p t based on the @p options that start with @p prefix.
46/// If @p used is not `nullopt`, sets corresponding flag of the options that
47/// were used.
48template <class T>
50 T &t, std::string_view prefix, std::span<const std::string_view> options,
51 std::optional<std::span<unsigned>> used = std::nullopt) {
52
53 size_t index = 0;
54 for (const auto &kv : options) {
55 auto [key, value] = split_key(kv, '=');
56 auto [pfx, remainder] = split_key(key);
57 auto curr_index = index++;
58 if (pfx != prefix)
59 continue;
60 if (used)
61 ++(*used)[curr_index];
62 set_param(t, {.full_key = kv, .key = remainder, .value = value});
63 }
64}
65
66} // namespace alpaqa::params
std::string_view key
The subkey to resolve next.
Definition params.hpp:20
std::string_view full_key
Full key string, used for diagnostics.
Definition params.hpp:18
auto split_key(std::string_view full, char tok='.')
Split the string full on the first occurrence of tok.
Definition params.hpp:32
void set_param(T &, const json &j)
Update/overwrite the first argument based on the JSON object j.
Definition json.cpp:248
void set_params(T &t, std::string_view prefix, std::span< const std::string_view > options, std::optional< std::span< unsigned > > used=std::nullopt)
Overwrites t based on the options that start with prefix.
Definition params.hpp:49
std::string_view value
The value of the parameter to store.
Definition params.hpp:22
Represents a parameter value encoded as a string in the format abc.def.key=value.
Definition params.hpp:16
constexpr const auto inf
Definition config.hpp:112
Custom parameter parsing exception.
Definition params.hpp:26