3#include <clingo/ground/literal.hh>
4#include <clingo/ground/matcher.hh>
5#include <clingo/ground/statement.hh>
7#include <clingo/util/small_vector.hh>
10#include <memory_resource>
12namespace CppClingo::Ground {
26 using Values = std::map<Symbol, ValueState>;
31 using Pairs = std::vector<Symbol>;
49 [[nodiscard]]
auto elems() const -> std::span<
size_t const>;
55 [[nodiscard]] auto
todo_elems() -> std::span<
size_t const>;
58 [[nodiscard]] auto num_values_() const ->
size_t;
63 ptrdiff_t propagated_pairs_ = 0;
64 ptrdiff_t propagated_elems_ = 0;
67 bool enqueued_ = false;
84 BaseSortAggr(
size_t size,
bool domain_elems,
bool single_pass_elems)
85 : atoms_{0, size, size}, domain_elems_{domain_elems}, single_pass_elems_{single_pass_elems} {}
98 [[nodiscard]]
auto size() const ->
size_t;
103 [[nodiscard]] auto index(
Key sym) const ->
size_t;
105 [[nodiscard]] auto nth(
size_t i) const ->
AtomSet::const_iterator;
115 [[nodiscard]] auto domain_elems() const ->
bool;
117 [[nodiscard]] auto single_pass_elems() const ->
bool;
123 bool single_pass_elems_;
146 bool domain_elems,
bool single_pass_elems)
147 : base_{global.size(), domain_elems, single_pass_elems}, global_{std::move(global)}, term_{std::move(term)},
148 mbr_{&mbr}, index_{index} {}
157 [[nodiscard]] auto term() const ->
Term const &;
161 [[nodiscard]] auto domain_elems() const ->
bool;
166 [[nodiscard]] auto single_pass_elems() const ->
bool;
168 [[nodiscard]] auto index() const ->
size_t;
183 auto atom_index(
AtomMap::iterator it) ->
size_t;
186 void print(std::ostream &out,
bool print_index);
196 void enqueue_(
AtomMap::iterator it);
203 std::vector<
size_t> queue_;
204 std::pmr::monotonic_buffer_resource *mbr_;
205 AtomKey *atom_key_ =
nullptr;
232 friend auto operator<<(std::ostream &out,
MatchSortAggr const &m) -> std::ostream &;
238 std::vector<
Symbol> mutable eval_;
251 [[nodiscard]]
auto do_domain() const ->
bool override;
254 [[nodiscard]] auto do_single_pass() const ->
bool override;
256 [[nodiscard]] auto do_matcher(std::pmr::monotonic_buffer_resource &mbr,
MatcherType type,
257 std::vector<
bool> const &bound)
258 -> std::pair<
UMatcher, std::optional<
size_t>> override;
260 [[nodiscard]] auto do_score([[maybe_unused]] std::vector<
bool> const &bound, [[maybe_unused]]
double estimate) const
262 [[nodiscard]] auto do_domain_size(
EstimateSelector sel) const -> std::optional<
double> override;
264 void do_print(std::ostream &out) const override;
268 [[nodiscard]] auto do_copy() const ->
ULit override;
270 [[nodiscard]] auto do_hash() const ->
size_t override;
272 [[nodiscard]] auto do_equal_to(
Lit const &other) const ->
bool override;
274 [[nodiscard]] auto do_compare_to(
Lit const &other) const -> std::weak_ordering override;
276 size_t offset_ = invalid_offset;
289 : state_{&state}, loc_weight_{std::move(loc_weight)}, tuple_{std::move(tuple)}, body_{std::move(body)},
290 node_{node}, num_cond_{num_cond}, priority_{priority} {}
294 : state_{other.state_}, loc_weight_{other.loc_weight_}, tuple_{
copy_uvec(other.tuple_)},
295 body_{
copy_uvec(other.body_)}, node_{other.node_}, num_cond_{other.num_cond_}, priority_{other.priority_} {};
306 [[nodiscard]]
auto do_body() const ->
ULitVec const & override;
307 [[nodiscard]] auto do_important() const ->
VariableSet override;
308 [[nodiscard]] auto do_is_important(
size_t index) const ->
bool override;
309 void do_init([[maybe_unused]]
size_t gen) override;
310 [[nodiscard]] auto do_report(
EvalContext const &ctx) ->
bool override;
312 [[nodiscard]] auto do_priority() const ->
size_t override;
313 void do_print_head(std::ostream &out) const override;
314 void do_print(std::ostream &out) const override;
315 [[nodiscard]] auto do_profile_node() const ->
ProfileNodeInternal *
override {
return node_; }
317 auto get_cond_(
EvalContext const &ctx) -> std::pair<size_t, bool>;
328static_assert(std::is_nothrow_move_constructible_v<StmSortAggrElem>);
329static_assert(std::is_nothrow_move_assignable_v<StmSortAggrElem>);
330static_assert(std::is_copy_assignable_v<StmSortAggrElem>);
342 [[nodiscard]]
auto do_domain() const ->
bool override;
344 [[nodiscard]] auto do_single_pass() const ->
bool override;
346 [[nodiscard]] auto do_matcher(std::pmr::monotonic_buffer_resource &mbr,
MatcherType type,
347 std::vector<
bool> const &bound)
348 -> std::pair<
UMatcher, std::optional<
size_t>> override;
350 [[nodiscard]] auto do_score([[maybe_unused]] std::vector<
bool> const &bound, [[maybe_unused]]
double estimate) const
353 [[nodiscard]] auto do_domain_size(
EstimateSelector sel) const -> std::optional<
double> override;
355 void do_print(std::ostream &out) const override;
359 [[nodiscard]] auto do_copy() const ->
ULit override;
361 [[nodiscard]] auto do_hash() const ->
size_t override;
363 [[nodiscard]] auto do_equal_to(
Lit const &other) const ->
bool override;
365 [[nodiscard]] auto do_compare_to(
Lit const &other) const -> std::weak_ordering override;
368 size_t offset_ = invalid_offset;
Extensible ground representation of sort aggregates.
Definition sort.hh:18
auto todo_elems() -> std::span< size_t const >
Get the elements that have to be propagated.
auto elems() const -> std::span< size_t const >
Get the aggregate elements.
AtomSortAggr()=default
Initialize an empty aggregate.
auto todo_values(SymbolStore &store) -> std::span< Symbol const >
Get the values to propagate.
void accumulate(Symbol sym, bool fact)
Accumulate a symbol.
auto is_fact(Symbol sym) const -> bool
Check if the given symbol tuple is a fact.
auto enqueue() -> bool
Enqueue aggregate to propagate its elements.
std::map< Symbol, ValueState > Values
The set of symbols accumulated by the sort aggregate atom.
Definition sort.hh:26
std::vector< Symbol > Pairs
The set of symbol pairs derived from the values.
Definition sort.hh:31
void dequeue()
Dequeue an aggregate whose elements have been propagated.
void add_elem(size_t idx)
Add a new element.
The base implementation of an atom base.
Definition base.hh:148
KeyType Key
The key identifies an atom and is usually associated with further state.
Definition base.hh:151
auto contains(Key const &sym, MatcherType type) const -> std::optional< size_t >
Check if the base contains the given atom with in the given generation.
Definition base.hh:160
The base capturing derived sort aggregate atoms.
Definition sort.hh:74
auto size() const -> size_t
Get the number of derived atoms.
auto add(size_t idx, Symbol val) -> bool
Add an atom to the base.
auto is_fact(Key sym) const -> bool
Check if the given atom is a fact.
KeyType Key
The key identifies an atom and is usually associated with further state.
Definition base.hh:151
Util::ordered_map< Symbol const *, AtomSortAggr, Util::array_hash, Util::array_equal_to > AtomMap
Map containing the atoms.
Definition sort.hh:79
BaseSortAggr(size_t size, bool domain_elems, bool single_pass_elems)
Construct an empty base.
Definition sort.hh:84
Util::ordered_map< Key, size_t > AtomSet
Map containing the derived atoms and their values.
Definition sort.hh:81
Context object to capture state used during instantiation.
Definition instantiator.hh:41
Literal representing a stratified sort aggregate.
Definition sort.hh:333
LitSortAggrStrat(StateSortAggr &state, std::vector< StmSortAggrElem > elems)
Construct a sort aggregate literal.
Definition sort.hh:336
Literal representing a sort aggregate.
Definition sort.hh:243
LitSortAggr(StateSortAggr &state)
Construct a sort aggregate literal.
Definition sort.hh:246
The base class for groundable literals.
Definition literal.hh:34
A term like object to match sort aggregates.
Definition sort.hh:210
auto vars() const -> VariableSet
Get the variables of the matcher.
BaseSortAggr::Key Key
The key to match against.
Definition sort.hh:213
MatchSortAggr(StateSortAggr &state)
Construct the matcher.
Definition sort.hh:216
Profile node that can hold children.
Definition profile.hh:156
A queue to process instantiators.
Definition instantiator.hh:225
State storing all necessary information to ground sort aggregates.
Definition sort.hh:129
StateSortAggr(std::pmr::monotonic_buffer_resource &mbr, VariableVec global, UTerm term, size_t index, bool domain_elems, bool single_pass_elems)
Initialize an aggregate state.
Definition sort.hh:145
std::pair< size_t, Symbol > ElementKey
Keys for aggregate elements storing their aggregate atom index and symbol pair.
Definition sort.hh:135
Util::ordered_map< ElementKey, Util::small_vector< size_t > > ElementMap
A map from tuples to their conditions.
Definition sort.hh:142
BaseSortAggr::AtomMap AtomMap
A map from global variables to the aggregate representation.
Definition sort.hh:138
auto global() const -> VariableVec const &
Get the global variables in the aggregate.
Class to store state for grounding.
Definition base.hh:451
Gather sort aggregate elements.
Definition sort.hh:280
StmSortAggrElem(StmSortAggrElem const &other)
Copy constructor.
Definition sort.hh:293
auto operator=(StmSortAggrElem &&other) noexcept -> StmSortAggrElem &=default
Move assignment.
auto operator=(StmSortAggrElem const &other) -> StmSortAggrElem &=default
Copy assignment.
StmSortAggrElem(StmSortAggrElem &&other) noexcept=default
Move constructor.
StmSortAggrElem(StateSortAggr &state, Location loc_weight, UTermVec tuple, ULitVec body, size_t num_cond, size_t priority, ProfileNodeInternal *node)
Construct the statement.
Definition sort.hh:287
Base class for groundable statements.
Definition statement.hh:17
Term interface.
Definition term.hh:68
The Location of an expression in an input source.
Definition location.hh:44
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
Interface to output literals.
Definition output.hh:61
Interface to output statements.
Definition output.hh:82
A store for symbols.
Definition symbol.hh:454
Variant-like class to store symbols stored in a symbol store.
Definition symbol.hh:225
std::vector< Symbol > SymbolVec
A vector of symbols.
Definition symbol.hh:220
EstimateSelector
Selector for which estimates to consider.
Definition core.hh:204
VariableSet::values_container_type VariableVec
A vector of variables.
Definition base.hh:21
Util::ordered_set< size_t > VariableSet
A set of variables.
Definition base.hh:19
@ derived
Indicates that the atom is derived by some rule but not a fact.
@ fact
Indicates that the atom is derived by a fact.
MatcherType
Enumeration of matcher types.
Definition instantiator.hh:54
std::unique_ptr< Matcher > UMatcher
A unique pointer to a matcher.
Definition instantiator.hh:106
std::unique_ptr< Lit > ULit
A unique pointer holding a literal.
Definition literal.hh:29
VarSelectMode
Available variable selection modes.
Definition literal.hh:21
std::vector< ULit > ULitVec
A vector of literals.
Definition literal.hh:31
auto copy_uvec(std::vector< std::unique_ptr< T > > const &vec)
Helper to copy vectors with copyable elements.
Definition term.hh:45
std::unique_ptr< Term > UTerm
A unique pointer holding a term.
Definition term.hh:34
std::vector< UTerm > UTermVec
A vector of terms.
Definition term.hh:36
tsl::ordered_map< Key, T, Hash, KeyEqual, Allocator, ValueTypeContainer, IndexType > ordered_map
Alias for ordered maps.
Definition ordered_map.hh:16
The state of a value provided by some element.
Definition sort.hh:21
bool propagated
whether the value has been propagated
Definition sort.hh:23
bool fact
value is provided by a fact
Definition sort.hh:22