pulsatrix
Loading...
Searching...
No Matches
datalog_weighted_fact_database.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstddef>
9#include <unordered_map>
10
13
14namespace pulsatrix::datalog {
15
17template <typename T>
18using WeightedFactSet = std::unordered_map<Atom, T, AtomHash>;
19
48template <typename T>
50public:
53
58 void set(Atom fact, T weight);
59
60 [[nodiscard]] bool contains(const Atom& fact) const { return facts_.find(fact) != facts_.end(); }
61
65 [[nodiscard]] T weight_of(const Atom& fact, T default_weight) const {
66 auto it = facts_.find(fact);
67 return it == facts_.end() ? default_weight : it->second;
68 }
69
70 [[nodiscard]] std::size_t size() const { return facts_.size(); }
71 [[nodiscard]] bool empty() const { return facts_.empty(); }
72 [[nodiscard]] const WeightedFactSet<T>& facts() const { return facts_; }
73
74 [[nodiscard]] bool operator==(const WeightedFactDatabase& other) const { return facts_ == other.facts_; }
75 [[nodiscard]] bool operator!=(const WeightedFactDatabase& other) const { return !(*this == other); }
76
77private:
78 WeightedFactSet<T> facts_;
79};
80
81// Explicit instantiation declarations -- definitions live in datalog_weighted_fact_database.cpp,
82// instantiated there for the two Value types this mission ships (bool, double) plus float for
83// the real-valued semiring's stated "double/float" scope. Mirrors Mission 0's own
84// declaration/definition split (datalog_rule.hpp/.cpp, datalog_fact_database.hpp/.cpp) rather
85// than making this a header-only template -- deliberate, since only a closed, known set of
86// Value types is ever needed (no caller in this mission instantiates an arbitrary T).
87extern template class WeightedFactDatabase<bool>;
88extern template class WeightedFactDatabase<double>;
89extern template class WeightedFactDatabase<float>;
90extern template class WeightedFactDatabase<DualNumber<double>>;
91
92} // namespace pulsatrix::datalog
An atom is a predicate name applied to a tuple of Terms, e.g. edge(a, b) or ancestor(X,...
Definition datalog_atom.hpp:24
A weighted fact database: a map from ground atom to a semiring value type T (bool for the trivial/boo...
Definition datalog_weighted_fact_database.hpp:49
const WeightedFactSet< T > & facts() const
Definition datalog_weighted_fact_database.hpp:72
bool operator!=(const WeightedFactDatabase &other) const
Definition datalog_weighted_fact_database.hpp:75
bool contains(const Atom &fact) const
Definition datalog_weighted_fact_database.hpp:60
WeightedFactDatabase(WeightedFactSet< T > facts)
bool operator==(const WeightedFactDatabase &other) const
Definition datalog_weighted_fact_database.hpp:74
T weight_of(const Atom &fact, T default_weight) const
The fact's current weight, or default_weight (conventionally Semiring::zero()) if the fact is not pre...
Definition datalog_weighted_fact_database.hpp:65
bool empty() const
Definition datalog_weighted_fact_database.hpp:71
void set(Atom fact, T weight)
Sets (overwrites) a fact's weight.
std::size_t size() const
Definition datalog_weighted_fact_database.hpp:70
A Datalog atom: a predicate name plus a tuple of terms, no function symbols. Phase 3 Mission 0 of cam...
Forward-mode-automatic-differentiation semiring (DualNumber<T>/DualSemiring<T>) – a second,...
Definition datalog_atom.hpp:14
std::unordered_map< Atom, T, AtomHash > WeightedFactSet
The concrete map type a WeightedFactDatabase<T> stores facts in.
Definition datalog_weighted_fact_database.hpp:18