cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
parameter_dict.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2024 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21
22#include <utility>
23
24#include <fmt/format.h> // IWYU pragma: keep
25
27
28namespace stat_bench {
29namespace param {
30
34
35auto ParameterDict::empty() const noexcept -> bool { return data_.empty(); }
36
37auto ParameterDict::has(const ParameterName& param_name) const -> bool {
38 return data_.count(param_name) == 1;
39}
40
41auto ParameterDict::get_as_double(const ParameterName& param_name) const
42 -> double {
43 const auto iter = data_.find(param_name);
44 if (iter == data_.end()) {
46 fmt::format("Parameter {} not found.", param_name));
47 }
48 return iter->second.to_double();
49}
50
51auto ParameterDict::get_as_variant(const ParameterName& param_name) const
53 const auto iter = data_.find(param_name);
54 if (iter == data_.end()) {
56 fmt::format("Parameter {} not found.", param_name));
57 }
58 return iter->second.to_variant();
59}
60
61auto ParameterDict::format_to(fmt::format_context::iterator out) const
62 -> fmt::format_context::iterator {
63 bool is_first = true;
64 for (const auto& [key, value] : data_) {
65 if (is_first) {
66 is_first = false;
67 } else {
68 *out = ',';
69 ++out;
70 *out = ' ';
71 ++out;
72 }
73 out = fmt::format_to(out, "{}={}", key, value.to_string());
74 }
75
76 return out;
77}
78
80 -> std::unordered_map<util::Utf8String, util::Utf8String> {
81 std::unordered_map<util::Utf8String, util::Utf8String> data;
82 data.reserve(data_.size());
83 for (const auto& pair : data_) {
84 data.try_emplace(pair.first.str(), pair.second.to_string());
85 }
86 return data;
87}
88
89auto ParameterDict::clone_without(const ParameterName& param_name) const
90 -> ParameterDict {
91 if (!has(param_name)) {
93 fmt::format("Parameter {} not found.", param_name));
94 }
96 new_data.reserve(data_.size());
97 for (const auto& pair : data_) {
98 if (pair.first != param_name) {
99 new_data.try_emplace(pair.first, pair.second);
100 }
101 }
102 return ParameterDict{std::move(new_data)};
103}
104
105auto ParameterDict::calculate_hash() const -> std::size_t {
106 std::size_t hash = 0;
107 std::hash<ParameterName> name_hash{};
108 for (const auto& pair : data_) {
109 hash ^= name_hash(pair.first);
110 hash ^= pair.second.calculate_hash();
111 }
112 return hash;
113}
114
115auto ParameterDict::operator==(const ParameterDict& rhs) const -> bool {
116 return data_ == rhs.data_;
117}
118
119auto ParameterDict::operator!=(const ParameterDict& rhs) const -> bool {
120 return !(*this == rhs);
121}
122
123} // namespace param
124} // namespace stat_bench
125
126namespace fmt {
127
128auto formatter<stat_bench::param::ParameterDict>::format(
129 const stat_bench::param::ParameterDict& val, format_context& context) const
130 -> typename format_context::iterator {
131 return val.format_to(context.out());
132}
133
134} // namespace fmt
Class of exceptions in this library.
Class of dictionaries of parameters.
auto format_to(fmt::format_context::iterator out) const -> fmt::format_context::iterator
Format to string.
auto get_as_double(const ParameterName &param_name) const -> double
Get a parameter value as double.
auto has(const ParameterName &param_name) const -> bool
Check whether this has a parameter with a name.
auto get_as_variant(const ParameterName &param_name) const -> ParameterValueVariant
Get a parameter value as a variant object.
auto empty() const noexcept -> bool
Check whether this is empty.
auto clone_without(const ParameterName &param_name) const -> ParameterDict
Create a new dictionary without a parameter.
auto operator!=(const ParameterDict &rhs) const -> bool
Check whether this is not equal to another dictionary.
auto as_string_dict() const -> std::unordered_map< util::Utf8String, util::Utf8String >
Get as a dictionary of string values.
ParameterDict(util::OrderedMap< ParameterName, ParameterValue > data)
Constructor.
auto calculate_hash() const -> std::size_t
Calculate hash value.
auto operator==(const ParameterDict &rhs) const -> bool
Check whether this is equal to another dictionary.
Class of names of parameters.
Class of mapping which preserves order of insertion.
Definition ordered_map.h:39
auto try_emplace(const Key &key, Args &&... args) -> std::pair< iterator, bool >
Insert a pair if the key does not exist.
void reserve(std::size_t size)
Reserve memory space.
Namespace of fmt library.
Namespace of parameters of benchmarks.
std::variant< bool, std::intmax_t, std::uintmax_t, long double, std::string > ParameterValueVariant
Type of variant of parameter values.
Namespace of utility functions and classes.
Namespace of stat_bench source codes.
STL namespace.
Definition of ParameterDict class.
Definition of ParameterName class.