Clingo
Loading...
Searching...
No Matches
stats.hh
1#pragma once
2
3#include <clingo/core.hh>
4
5#include <clingo/stats.h>
6
7namespace Clingo {
8
12
22
23class ConstStatsArray;
24class StatsArray;
25class ConstStatsMap;
26class StatsMap;
27
30 public:
32 explicit ConstStats(clingo_stats_t const *stats, uint64_t key) : stats_{stats}, key_{key} {}
33
38 friend auto c_cast(ConstStats const &stats) -> clingo_stats_t const * { return stats.stats_; }
39
43 [[nodiscard]] auto type() const -> StatsType {
44 return static_cast<StatsType>(Detail::call<clingo_stats_type>(stats_, key_));
45 }
46
51
56 [[nodiscard]] auto at(size_t index) const -> ConstStats;
57
59 [[nodiscard]] auto operator[](size_t index) const -> ConstStats { return at(index); }
60
64 [[nodiscard]] auto map() const -> ConstStatsMap;
65
69 [[nodiscard]] auto get(std::string_view name) const -> ConstStats;
70
72 [[nodiscard]] auto operator[](std::string_view name) const -> ConstStats { return get(name); }
73
77 [[nodiscard]] auto value() const -> double {
78 if (type() == StatsType::value) {
79 return Detail::call<clingo_stats_value_get>(stats_, key_);
80 }
81 throw std::logic_error{"not a value"};
82 }
83
85 [[nodiscard]] auto operator*() const -> double { return value(); }
86
92 [[nodiscard]] auto to_string() const -> std::string {
93 auto bld = StringBuilder{};
94 Detail::handle_error(clingo_stats_to_string(stats_, key_, c_cast(bld)));
95 return std::string{bld.str()};
96 }
97
98 private:
99 friend class Stats;
100
101 clingo_stats_t const *stats_;
102 uint64_t key_;
103};
104
106class Stats : public ConstStats {
107 public:
109 explicit Stats(clingo_stats_t *stats, uint64_t key) : ConstStats{stats, key} {}
110
112 friend auto c_cast(Stats const &stats) -> clingo_stats_t * { return stats.stats_(); }
113
115 [[nodiscard]] auto array() const -> StatsArray;
116
118 [[nodiscard]] auto at(size_t index) const -> Stats;
119
121 [[nodiscard]] auto operator[](size_t index) const -> Stats { return at(index); }
122
124 [[nodiscard]] auto map() const -> StatsMap;
125
127 [[nodiscard]] auto get(std::string_view name) const -> Stats;
128
130 [[nodiscard]] auto operator[](std::string_view name) const -> Stats { return get(name); }
131
132 using ConstStats::value;
133
137 void value(double value) const {
138 if (type() == StatsType::value) {
139 Detail::handle_error(clingo_stats_value_set(stats_(), key_, value));
140 } else {
141 throw std::logic_error{"not a value"};
142 }
143 }
144
147 auto operator=(double value) const -> Stats { // NOLINT
148 this->value(value);
149 return *this;
150 }
151
152 private:
153 [[nodiscard]] auto stats_() const -> clingo_stats_t * {
154 // NOLINTNEXTLINE
155 return const_cast<clingo_stats_t *>(ConstStats::stats_);
156 }
157};
158
161 public:
165 using size_type = std::size_t;
167 using difference_type = std::ptrdiff_t;
171 using pointer = Detail::ArrowProxy<value_type>;
173 using iterator = Detail::RandomAccessIterator<ConstStatsArray>;
174
179 explicit ConstStatsArray(clingo_stats_t const *stats, uint64_t key) : stats_{stats}, key_{key} {}
180
182 [[nodiscard]] auto at(size_t index) const -> ConstStats { return ConstStats{stats_, at_(index)}; }
183
185 [[nodiscard]] auto operator[](size_t index) const -> ConstStats { return at(index); }
186
190 [[nodiscard]] auto size() const -> size_t { return Detail::call<clingo_stats_array_size>(stats_, key_); }
191
195 [[nodiscard]] auto begin() const -> iterator { return iterator{*this, 0}; }
196
200 [[nodiscard]] auto end() const -> iterator { return iterator{*this, size()}; }
201
202 private:
203 friend class StatsArray;
204 [[nodiscard]] auto at_(size_t index) const -> uint64_t {
205 return Detail::call<clingo_stats_array_at>(stats_, key_, index);
206 }
207
208 clingo_stats_t const *stats_;
209 uint64_t key_;
210};
211
213 if (type() == StatsType::array) {
214 return ConstStatsArray{stats_, key_};
215 }
216 throw std::logic_error{"not an array"};
217}
218
219inline auto ConstStats::at(size_t index) const -> ConstStats {
220 return array().at(index);
221}
222
225 public:
229 using size_type = std::size_t;
231 using difference_type = std::ptrdiff_t;
235 using pointer = Detail::ArrowProxy<value_type>;
237 using iterator = Detail::RandomAccessIterator<StatsArray>;
238
241
243 [[nodiscard]] auto at(size_t index) const -> Stats { return Stats{stats_(), at_(index)}; }
244
246 [[nodiscard]] auto operator[](size_t index) const -> Stats { return at(index); }
247
255 [[nodiscard]] auto push(StatsType type) const -> Stats {
256 return Stats{stats_(),
257 Detail::call<clingo_stats_array_push>(stats_(), key_, static_cast<clingo_stats_type_t>(type))};
258 }
259
265 [[nodiscard]] auto ensure(size_t index, StatsType type) const -> Stats {
266 size_t n = size();
267 if (index < n) {
268 return at(index);
269 }
270 for (; n < index; ++n) {
271 std::ignore = push(type);
272 }
273 return push(type);
274 }
275
277 [[nodiscard]] auto begin() const -> iterator { return iterator{*this, 0}; }
278
280 [[nodiscard]] auto end() const -> iterator { return iterator{*this, size()}; }
281
282 private:
283 [[nodiscard]] auto stats_() const -> clingo_stats_t * {
284 // NOLINTNEXTLINE
285 return const_cast<clingo_stats_t *>(ConstStatsArray::stats_);
286 }
287};
288
289inline auto Stats::array() const -> StatsArray {
290 if (type() == StatsType::array) {
291 return StatsArray{stats_(), key_};
292 }
293 throw std::logic_error{"not an array"};
294}
295inline auto Stats::at(size_t index) const -> Stats {
296 return array().at(index);
297}
298
301 public:
303 using key_type = std::string_view;
307 using value_type = std::pair<key_type, mapped_type>;
309 using size_type = std::size_t;
311 using difference_type = std::ptrdiff_t;
315 using pointer = Detail::ArrowProxy<value_type>;
317 using iterator = Detail::RandomAccessIterator<ConstStatsMap>;
318
323 explicit ConstStatsMap(clingo_stats_t const *stats, uint64_t key) : stats_{stats}, key_{key} {}
324
328 [[nodiscard]] auto size() const -> size_t { return Detail::call<clingo_stats_map_size>(stats_, key_); }
329
334 [[nodiscard]] auto at(size_t index) const -> value_type {
335 auto [name, subkey] = at_(index);
336 return {name, ConstStats{stats_, subkey}};
337 }
338
340 [[nodiscard]] auto operator[](std::string_view name) const -> ConstStats { return get(name); }
341
343 [[nodiscard]] auto get(std::string_view name) const -> ConstStats { return ConstStats{stats_, at_(name)}; }
344
349 [[nodiscard]] auto contains(std::string_view name) const -> bool {
350 return Detail::call<clingo_stats_map_has_subkey>(stats_, key_, name.data(), name.size());
351 }
352
356 [[nodiscard]] auto begin() const -> iterator { return iterator{*this, 0}; }
357
361 [[nodiscard]] auto end() const -> iterator { return iterator{*this, size()}; }
362
363 private:
364 friend class StatsMap;
365
366 [[nodiscard]] auto at_(std::string_view name) const -> uint64_t {
367 return Detail::call<clingo_stats_map_at>(stats_, key_, name.data(), name.size());
368 }
369
370 [[nodiscard]] auto at_(size_t index) const -> std::pair<std::string_view, uint64_t> {
371 auto [data, size] = Detail::call<clingo_stats_map_subkey_name>(stats_, key_, index);
372 auto str = std::string_view{data, size};
373 return {str, at_(str)};
374 }
375
376 clingo_stats_t const *stats_;
377 uint64_t key_;
378};
379
381 if (type() == StatsType::map) {
382 return ConstStatsMap{stats_, key_};
383 }
384 throw std::logic_error{"not a map"};
385}
386
387inline auto ConstStats::get(std::string_view name) const -> ConstStats {
388 return map().get(name);
389}
390
392class StatsMap : public ConstStatsMap {
393 public:
395 using key_type = std::string_view;
399 using value_type = std::pair<key_type, mapped_type>;
401 using size_type = std::size_t;
403 using difference_type = std::ptrdiff_t;
407 using pointer = Detail::ArrowProxy<value_type>;
409 using iterator = Detail::RandomAccessIterator<StatsMap>;
410
415 explicit StatsMap(clingo_stats_t *stats, uint64_t key) : ConstStatsMap{stats, key} {}
416
418 [[nodiscard]] auto at(size_t index) const -> value_type {
419 auto [name, subkey] = at_(index);
420 return {name, Stats{stats_(), subkey}};
421 }
422
424 [[nodiscard]] auto operator[](std::string_view name) const -> Stats { return get(name); }
425
427 [[nodiscard]] auto get(std::string_view name) const -> Stats { return Stats{stats_(), at_(name)}; }
428
440 [[nodiscard]] auto insert(std::string_view name, StatsType type) const -> Stats {
441 return Stats{stats_(), Detail::call<clingo_stats_map_add_subkey>(stats_(), key_, name.data(), name.size(),
442 static_cast<clingo_stats_type_t>(type))};
443 }
444
448 [[nodiscard]] auto begin() const -> iterator { return iterator{*this, 0}; }
449
453 [[nodiscard]] auto end() const -> iterator { return iterator{*this, size()}; }
454
455 private:
456 [[nodiscard]] auto stats_() const -> clingo_stats_t * {
457 // NOLINTNEXTLINE
458 return const_cast<clingo_stats_t *>(ConstStatsMap::stats_);
459 }
460};
461
462inline auto Stats::map() const -> StatsMap {
463 if (type() == StatsType::map) {
464 return StatsMap{stats_(), key_};
465 }
466 throw std::logic_error{"not a map"};
467}
468
469inline auto Stats::get(std::string_view name) const -> Stats {
470 return map().get(name);
471}
472
474
475} // namespace Clingo
Class modeling an immutable view on an array of statistics entries.
Definition stats.hh:160
auto begin() const -> iterator
Get an iterator to the beginning of the array.
Definition stats.hh:195
auto end() const -> iterator
Get an iterator to the end of the array.
Definition stats.hh:200
auto size() const -> size_t
Get the size of the array.
Definition stats.hh:190
ConstStats value_type
The value type of the array, which are stats entries.
Definition stats.hh:163
std::ptrdiff_t difference_type
The difference type of the array.
Definition stats.hh:167
ConstStatsArray(clingo_stats_t const *stats, uint64_t key)
Construct a statistics array from a pointer to the C API and a key.
Definition stats.hh:179
auto operator[](size_t index) const -> ConstStats
Get a statistics entry at the given index if the entry is an array.
Definition stats.hh:185
Detail::ArrowProxy< value_type > pointer
The pointer type of the array, which is a proxy to stats entries.
Definition stats.hh:171
std::size_t size_type
The size type of the array.
Definition stats.hh:165
Detail::RandomAccessIterator< ConstStatsArray > iterator
The iterator type, which is a random access iterator over stats entries.
Definition stats.hh:173
auto at(size_t index) const -> ConstStats
Get a statistics entry at the given index if the entry is an array.
Definition stats.hh:182
Class modeling an immutable view on a map of statistics entries.
Definition stats.hh:300
auto operator[](std::string_view name) const -> ConstStats
Get a statistics entry with the given name if the entry is a map.
Definition stats.hh:340
auto begin() const -> iterator
Get an iterator to the beginning of the map.
Definition stats.hh:356
auto get(std::string_view name) const -> ConstStats
Get a statistics entry with the given name if the entry is a map.
Definition stats.hh:343
std::string_view key_type
The key type of the map, which is a string view.
Definition stats.hh:303
std::ptrdiff_t difference_type
The difference type of the map.
Definition stats.hh:311
auto at(size_t index) const -> value_type
Get the name entry pair at the given index.
Definition stats.hh:334
auto size() const -> size_t
Get the size of the map.
Definition stats.hh:328
auto contains(std::string_view name) const -> bool
Check if the map contains a subkey with the given name.
Definition stats.hh:349
auto end() const -> iterator
Get an iterator to the end of the map.
Definition stats.hh:361
Detail::RandomAccessIterator< ConstStatsMap > iterator
The iterator type, which is a random access iterator over value types.
Definition stats.hh:317
ConstStatsMap(clingo_stats_t const *stats, uint64_t key)
Construct a statistics map from a pointer to the C API and a key.
Definition stats.hh:323
std::size_t size_type
The size type of the map.
Definition stats.hh:309
std::pair< key_type, mapped_type > value_type
The value type of the map, which is a pair of key and mapped type.
Definition stats.hh:307
Detail::ArrowProxy< value_type > pointer
The pointer type of the map, which is a proxy to the value type.
Definition stats.hh:315
value_type reference
The reference type of the map, which is a pair of key and mapped type.
Definition stats.hh:313
Class modeling an immutable view on a statistics entry.
Definition stats.hh:29
auto to_string() const -> std::string
Get a string representation of the statistics entry.
Definition stats.hh:92
auto value() const -> double
Get the value of the statistics entry if it is a value.
Definition stats.hh:77
friend auto c_cast(ConstStats const &stats) -> clingo_stats_t const *
Cast to the underlying C API type.
Definition stats.hh:38
ConstStats(clingo_stats_t const *stats, uint64_t key)
Construct a statistics entry from a pointer to the C API and a key.
Definition stats.hh:32
auto type() const -> StatsType
Get the type of the statistics entry.
Definition stats.hh:43
auto operator*() const -> double
Get the value of the statistics entry if it is a value.
Definition stats.hh:85
Class modeling a mutable view on an array of statistics entries.
Definition stats.hh:224
std::ptrdiff_t difference_type
The difference type of the array.
Definition stats.hh:231
auto ensure(size_t index, StatsType type) const -> Stats
Ensure that the array has an entry at the given index.
Definition stats.hh:265
Detail::RandomAccessIterator< StatsArray > iterator
The iterator type, which is a random access iterator over stats entries.
Definition stats.hh:237
auto at(size_t index) const -> Stats
Get a statistics entry at the given index if the entry is an array.
Definition stats.hh:243
StatsArray(clingo_stats_t *stats, uint64_t key)
Construct a statistics array from a pointer to the C API and a key.
Definition stats.hh:240
Stats value_type
The value type of the array, which are stats entries.
Definition stats.hh:227
Detail::ArrowProxy< value_type > pointer
The pointer type of the array, which is a proxy to stats entries.
Definition stats.hh:235
auto begin() const -> iterator
Get an iterator to the beginning of the array.
Definition stats.hh:277
auto operator[](size_t index) const -> Stats
Get a statistics entry at the given index if the entry is an array.
Definition stats.hh:246
auto push(StatsType type) const -> Stats
Append a new statistics entry of the given type to the array.
Definition stats.hh:255
auto end() const -> iterator
Get an iterator to the end of the array.
Definition stats.hh:280
std::size_t size_type
The size type of the array.
Definition stats.hh:229
Class modeling a mutable view on a map of statistics entries.
Definition stats.hh:392
auto at(size_t index) const -> value_type
Get the name entry pair at the given index.
Definition stats.hh:418
value_type reference
The reference type of the map, which corresponds to the value type.
Definition stats.hh:405
StatsMap(clingo_stats_t *stats, uint64_t key)
Construct a statistics map from a pointer to the C API and a key.
Definition stats.hh:415
std::ptrdiff_t difference_type
The difference type of the map.
Definition stats.hh:403
Detail::RandomAccessIterator< StatsMap > iterator
The iterator type, which is a random access iterator over value types.
Definition stats.hh:409
std::size_t size_type
The size type of the map.
Definition stats.hh:401
auto begin() const -> iterator
Get an iterator to the beginning of the map.
Definition stats.hh:448
std::string_view key_type
The key type of the map, which is a string view.
Definition stats.hh:395
auto insert(std::string_view name, StatsType type) const -> Stats
Insert a statistics entry with the given name and type into the map.
Definition stats.hh:440
auto operator[](std::string_view name) const -> Stats
Get a statistics entry with the given name if the entry is a map.
Definition stats.hh:424
auto end() const -> iterator
Get an iterator to the end of the map.
Definition stats.hh:453
std::pair< key_type, mapped_type > value_type
The value type of the map, which is a pair of key and mapped type.
Definition stats.hh:399
auto get(std::string_view name) const -> Stats
Get a statistics entry with the given name if the entry is a map.
Definition stats.hh:427
Detail::ArrowProxy< value_type > pointer
The pointer type of the map, which is a proxy to the value type.
Definition stats.hh:407
Class modeling a mutable view on a statistics entry.
Definition stats.hh:106
Stats(clingo_stats_t *stats, uint64_t key)
Construct a statistics entry from a pointer to the C API and a key.
Definition stats.hh:109
auto operator=(double value) const -> Stats
Set the value of the statistics entry if it is a value.
Definition stats.hh:147
friend auto c_cast(Stats const &stats) -> clingo_stats_t *
Cast to the underlying C API type.
Definition stats.hh:112
auto value() const -> double
Get the value of the statistics entry if it is a value.
Definition stats.hh:77
void value(double value) const
Set the value of the statistics entry if it is a value.
Definition stats.hh:137
A string builder for constructing strings.
Definition core.hh:524
CLINGO_VISIBILITY_DEFAULT bool clingo_stats_value_set(clingo_stats_t *stats, uint64_t key, double value)
Set the value of the given entry.
CLINGO_VISIBILITY_DEFAULT bool clingo_stats_to_string(clingo_stats_t const *stats, uint64_t key, clingo_string_builder_t *builder)
Get a string representation of the statistics.
int clingo_stats_type_t
Corresponding type to clingo_stats_type.
Definition stats.h:56
struct clingo_statistic clingo_stats_t
Handle for the solver stats.
Definition stats.h:59
@ clingo_stats_type_map
the entry is a map
Definition stats.h:53
@ clingo_stats_type_value
the entry is a (double) value
Definition stats.h:51
@ clingo_stats_type_array
the entry is an array
Definition stats.h:52
@ tuple
Theory tuples "(t1,...,tn)".
@ map
The configuration entry is a map of configurations.
@ value
The configuration entry is a double value.
@ array
The configuration entry is a array of configurations.
auto get(std::string_view name) const -> Stats
Get a statistics entry with the given name if the entry is a map.
Definition stats.hh:469
auto at(size_t index) const -> Stats
Get a statistics entry at the given index if the entry is an array.
Definition stats.hh:295
auto get(std::string_view name) const -> ConstStats
Get a statistics entry with the given name if the entry is a map.
Definition stats.hh:387
auto map() const -> StatsMap
Get a view on the map of statistics entries if the entry is a map.
Definition stats.hh:462
auto map() const -> ConstStatsMap
Get a view on the map of statistics entries if the entry is a map.
Definition stats.hh:380
auto array() const -> StatsArray
Get a view on the array of statistics entries if the entry is an array.
Definition stats.hh:289
auto array() const -> ConstStatsArray
Get a view on the array of statistics entries if the entry is an array.
Definition stats.hh:212
StatsType
Enumeration of statistics types.
Definition stats.hh:14
auto at(size_t index) const -> ConstStats
Get a statistics entry at the given index if the entry is an array.
Definition stats.hh:219
@ map
The statistics entry holds map.
@ value
The statistics entry holds a value.
@ array
The statistics entry holds an array.