Skip to content

Commit

Permalink
Adjust naming.
Browse files Browse the repository at this point in the history
* Use _t only for type aliases.
* Minor cleanup here and there.
  • Loading branch information
BenKaufmann committed Dec 18, 2024
1 parent a6d2795 commit 0915487
Show file tree
Hide file tree
Showing 30 changed files with 887 additions and 919 deletions.
24 changes: 11 additions & 13 deletions potassco/aspif.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ namespace Potassco {
*/
int readAspif(std::istream& prg, AbstractProgram& out);

enum class Theory_t;
//! Supported aspif theory statements.
enum class TheoryType : uint32_t { number = 0, symbol = 1, compound = 2, element = 4, atom = 5, atom_with_guard = 6 };
POTASSCO_SET_DEFAULT_ENUM_MAX(TheoryType::atom_with_guard);

//! Class for parsing logic programs in asp intermediate format.
class AspifInput : public ProgramReader {
public:
//! Creates a new parser object that calls @c out on each parsed element.
AspifInput(AbstractProgram& out);
explicit AspifInput(AbstractProgram& out);

protected:
//! Checks whether stream starts with aspif header.
Expand All @@ -51,11 +53,6 @@ class AspifInput : public ProgramReader {
* output object before/after parsing the current step.
*/
bool doParse() override;
//! Attempts to parse a theory directive of type @c t.
/*!
* \see Potassco::Theory_t
*/
virtual void matchTheory(Theory_t t);

private:
struct Extra;
Expand All @@ -64,6 +61,7 @@ class AspifInput : public ProgramReader {
void matchWLits(bool positive);
void matchString();
void matchIds();
void matchTheory(TheoryType t);

AbstractProgram& out_;
Extra* data_;
Expand All @@ -77,7 +75,7 @@ class AspifInput : public ProgramReader {
class AspifOutput : public AbstractProgram {
public:
//! Creates a new object and associates it with the given output stream.
AspifOutput(std::ostream& os);
explicit AspifOutput(std::ostream& os);
AspifOutput(const AspifOutput&) = delete;
AspifOutput& operator=(const AspifOutput&) = delete;

Expand All @@ -86,23 +84,23 @@ class AspifOutput : public AbstractProgram {
//! Prepares the object for a new program step.
void beginStep() override;
//! Writes an aspif rule directive.
void rule(Head_t ht, const AtomSpan& head, const LitSpan& body) override;
void rule(HeadType ht, const AtomSpan& head, const LitSpan& body) override;
//! Writes an aspif rule directive.
void rule(Head_t ht, const AtomSpan& head, Weight_t bound, const WeightLitSpan& lits) override;
void rule(HeadType ht, const AtomSpan& head, Weight_t bound, const WeightLitSpan& lits) override;
//! Writes an aspif minimize directive.
void minimize(Weight_t prio, const WeightLitSpan& lits) override;
//! Writes an aspif output directive.
void output(const std::string_view& str, const LitSpan& cond) override;
//! Writes an aspif external directive.
void external(Atom_t a, Value_t v) override;
void external(Atom_t a, TruthValue v) override;
//! Writes an aspif assumption directive.
void assume(const LitSpan& lits) override;
//! Writes an aspif projection directive.
void project(const AtomSpan& atoms) override;
//! Writes an aspif edge directive.
void acycEdge(int s, int t, const LitSpan& condition) override;
//! Writes an aspif heuristic directive.
void heuristic(Atom_t a, Heuristic_t t, int bias, unsigned prio, const LitSpan& condition) override;
void heuristic(Atom_t a, DomModifier t, int bias, unsigned prio, const LitSpan& condition) override;

//! Writes an aspif theory number term.
void theoryTerm(Id_t termId, int number) override;
Expand All @@ -121,7 +119,7 @@ class AspifOutput : public AbstractProgram {

protected:
//! Starts writing an aspif directive.
AspifOutput& startDir(Directive_t r);
AspifOutput& startDir(AspifType r);
//! Writes @c x.
template <typename T>
AspifOutput& add(T x);
Expand Down
46 changes: 23 additions & 23 deletions potassco/aspif_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Potassco {
class AspifTextInput : public ProgramReader {
public:
//! Creates a new object and associates it with the given output if any.
AspifTextInput(AbstractProgram* out);
explicit AspifTextInput(AbstractProgram* out);
//! Sets the program to which parsed elements should be output.
void setOutput(AbstractProgram& out);

Expand All @@ -52,22 +52,22 @@ class AspifTextInput : public ProgramReader {
void parseStatements();

private:
bool matchDirective();
void matchRule(char peek);
void matchAtoms(std::string_view sv);
void matchLits();
void matchCondition();
void matchAgg();
void matchDelim(char);
bool matchOpt(std::string_view ts);
Atom_t matchId();
Lit_t matchLit();
int matchInt();
Heuristic_t matchHeuMod();
void matchTerm();
void matchAtomArg();
void matchStr();
void push(char c);
bool matchDirective();
void matchRule(char peek);
void matchAtoms(std::string_view sv);
void matchLits();
void matchCondition();
void matchAgg();
void matchDelim(char);
bool matchOpt(std::string_view ts);
Atom_t matchId();
Lit_t matchLit();
int matchInt();
auto matchHeuMod() -> DomModifier;
void matchTerm();
void matchAtomArg();
void matchStr();
void push(char c);
struct Data;
AbstractProgram* out_;
Data* data_;
Expand All @@ -78,23 +78,23 @@ class AspifTextInput : public ProgramReader {
* Writes a logic program in human-readable text format.
* \ingroup WriteType
*/
class AspifTextOutput : public Potassco::AbstractProgram {
class AspifTextOutput : public AbstractProgram {
public:
AspifTextOutput(std::ostream& os);
explicit AspifTextOutput(std::ostream& os);
~AspifTextOutput() override;
AspifTextOutput(AspifTextOutput&&) = delete;

void initProgram(bool incremental) override;
void beginStep() override;
void rule(Head_t ht, const AtomSpan& head, const LitSpan& body) override;
void rule(Head_t ht, const AtomSpan& head, Weight_t bound, const WeightLitSpan& lits) override;
void rule(HeadType ht, const AtomSpan& head, const LitSpan& body) override;
void rule(HeadType ht, const AtomSpan& head, Weight_t bound, const WeightLitSpan& lits) override;
void minimize(Weight_t prio, const WeightLitSpan& lits) override;
void output(const std::string_view& str, const LitSpan& cond) override;
void external(Atom_t a, Value_t v) override;
void external(Atom_t a, TruthValue v) override;
void assume(const LitSpan& lits) override;
void project(const AtomSpan& atoms) override;
void acycEdge(int s, int t, const LitSpan& condition) override;
void heuristic(Atom_t a, Heuristic_t t, int bias, unsigned prio, const LitSpan& condition) override;
void heuristic(Atom_t a, DomModifier t, int bias, unsigned prio, const LitSpan& condition) override;

void theoryTerm(Id_t termId, int number) override;
void theoryTerm(Id_t termId, const std::string_view& name) override;
Expand Down
71 changes: 35 additions & 36 deletions potassco/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,32 @@ constexpr auto id_max = static_cast<Id_t>(-1);
//! Atom ids are positive integers in the range [atom_min;atom_max].
using Atom_t = uint32_t;
//! Minimum value for atom ids (must not be 0).
constexpr auto atom_min = static_cast<Atom_t>(1);
constexpr auto atom_min = static_cast<Atom_t>(1u);
//! Maximum value for atom ids.
constexpr auto atom_max = static_cast<Atom_t>(((1u) << 31) - 1);
//! Literals are signed atoms.
using Lit_t = int32_t;
//! (Literal) weights are integers.
using Weight_t = int32_t;
//! A literal with an associated weight.
struct WeightLit_t {
struct WeightLit {
Lit_t lit; //!< Literal.
Weight_t weight; //!< Associated weight.

friend constexpr bool operator==(const WeightLit_t& lhs, const WeightLit_t& rhs) noexcept = default;
friend constexpr auto operator<=>(const WeightLit_t& lhs, const WeightLit_t& rhs) noexcept = default;
friend constexpr auto operator==(const WeightLit_t& lhs, Lit_t rhs) noexcept {
friend constexpr bool operator==(const WeightLit& lhs, const WeightLit& rhs) noexcept = default;
friend constexpr auto operator<=>(const WeightLit& lhs, const WeightLit& rhs) noexcept = default;
friend constexpr auto operator==(const WeightLit& lhs, Lit_t rhs) noexcept {
return lhs.lit == rhs && lhs.weight == 1;
}
friend constexpr auto operator<=>(const WeightLit_t& lhs, Lit_t rhs) noexcept {
return lhs <=> WeightLit_t{.lit = rhs, .weight = 1};
friend constexpr auto operator<=>(const WeightLit& lhs, Lit_t rhs) noexcept {
return lhs <=> WeightLit{.lit = rhs, .weight = 1};
}
};

using IdSpan = std::span<const Id_t>;
using AtomSpan = std::span<const Atom_t>;
using LitSpan = std::span<const Lit_t>;
using WeightLitSpan = std::span<const WeightLit_t>;
using WeightLitSpan = std::span<const WeightLit>;
//! Convert a single lvalue into a span with one element.
template <typename T>
requires(std::is_lvalue_reference_v<T>)
Expand All @@ -110,33 +110,33 @@ constexpr auto toSpan(T&& x) -> std::span<std::remove_reference_t<T>> {
}

//! Supported rule head types.
enum class Head_t : unsigned { disjunctive = 0, choice = 1 };
POTASSCO_SET_DEFAULT_ENUM_MAX(Head_t::choice);
enum class HeadType : unsigned { disjunctive = 0, choice = 1 };
POTASSCO_SET_DEFAULT_ENUM_MAX(HeadType::choice);

//! Supported rule body types.
enum class Body_t : unsigned { normal = 0, sum = 1, count = 2 };
POTASSCO_SET_DEFAULT_ENUM_MAX(Body_t::count);
enum class BodyType : unsigned { normal = 0, sum = 1, count = 2 };
POTASSCO_SET_DEFAULT_ENUM_MAX(BodyType::count);

//! Type representing an external value.
enum class Value_t : unsigned { free = 0, true_ = 1, false_ = 2, release = 3 };
[[maybe_unused]] consteval auto enable_meta(std::type_identity<Value_t>) {
using enum Value_t;
//! Type representing a truth or external value.
enum class TruthValue : unsigned { free = 0, true_ = 1, false_ = 2, release = 3 };
[[maybe_unused]] consteval auto enable_meta(std::type_identity<TruthValue>) {
using enum TruthValue;
using namespace std::literals;
return EnumEntries(free, "free"sv, true_, "true"sv, false_, "false"sv, release, "release"sv);
}

//! Supported modifications for domain heuristic.
enum class Heuristic_t : unsigned { level = 0, sign = 1, factor = 2, init = 3, true_ = 4, false_ = 5 };
[[maybe_unused]] consteval auto enable_ops(std::type_identity<Heuristic_t>) -> CmpOps;
[[maybe_unused]] consteval auto enable_meta(std::type_identity<Heuristic_t>) {
using enum Heuristic_t;
enum class DomModifier : unsigned { level = 0, sign = 1, factor = 2, init = 3, true_ = 4, false_ = 5 };
[[maybe_unused]] consteval auto enable_ops(std::type_identity<DomModifier>) -> CmpOps;
[[maybe_unused]] consteval auto enable_meta(std::type_identity<DomModifier>) {
using enum DomModifier;
using namespace std::literals;
return EnumEntries(level, "level"sv, sign, "sign"sv, factor, "factor"sv, init, "init"sv, true_, "true"sv, false_,
"false"sv);
}

//! Supported aspif directives.
enum class Directive_t : unsigned {
//! Supported aspif statements.
enum class AspifType : unsigned {
end = 0,
rule = 1,
minimize = 2,
Expand All @@ -149,7 +149,7 @@ enum class Directive_t : unsigned {
theory = 9,
comment = 10
};
POTASSCO_SET_DEFAULT_ENUM_MAX(Directive_t::comment);
POTASSCO_SET_DEFAULT_ENUM_MAX(AspifType::comment);

//! Basic callback interface for constructing a logic program.
class AbstractProgram {
Expand All @@ -161,9 +161,9 @@ class AbstractProgram {
virtual void beginStep();

//! Add the given rule to the program.
virtual void rule(Head_t ht, const AtomSpan& head, const LitSpan& body) = 0;
virtual void rule(HeadType ht, const AtomSpan& head, const LitSpan& body) = 0;
//! Add the given sum rule to the program.
virtual void rule(Head_t ht, const AtomSpan& head, Weight_t bound, const WeightLitSpan& body) = 0;
virtual void rule(HeadType ht, const AtomSpan& head, Weight_t bound, const WeightLitSpan& body) = 0;
//! Add the given minimize statement to the program.
virtual void minimize(Weight_t prio, const WeightLitSpan& lits) = 0;

Expand All @@ -178,13 +178,13 @@ class AbstractProgram {
virtual void project(const AtomSpan& atoms);
//! Output @c str whenever condition is true in a stable model.
virtual void output(const std::string_view& str, const LitSpan& condition);
//! If `v` is not equal to `Value_t::release`, mark `a` as external and assume value `v`. Otherwise, treat `a` as
//! If `v` is not equal to `TruthValue::release`, mark `a` as external and assume value `v`. Otherwise, treat `a` as
//! regular atom.
virtual void external(Atom_t a, Value_t v);
virtual void external(Atom_t a, TruthValue v);
//! Assume the given literals to true during solving.
virtual void assume(const LitSpan& lits);
//! Apply the given heuristic modification to atom @c a whenever condition is true.
virtual void heuristic(Atom_t a, Heuristic_t t, int bias, unsigned prio, const LitSpan& condition);
virtual void heuristic(Atom_t a, DomModifier t, int bias, unsigned prio, const LitSpan& condition);
//! Assume an edge between @c s and @c t whenever condition is true.
virtual void acycEdge(int s, int t, const LitSpan& condition);
//@}
Expand Down Expand Up @@ -230,13 +230,13 @@ constexpr Atom_t atom(Atom_t atom) { return atom; }
//! Returns the atom of the given literal.
constexpr Atom_t atom(Lit_t lit) { return static_cast<Atom_t>(lit >= 0 ? lit : -lit); }
//! Returns the atom of the given weight literal.
constexpr Atom_t atom(const WeightLit_t& w) { return atom(w.lit); }
constexpr Atom_t atom(const WeightLit& w) { return atom(w.lit); }
//! Returns the positive literal of the given atom.
constexpr Lit_t lit(Atom_t atom) { return static_cast<Lit_t>(atom); }
//! Identity function for literals.
constexpr Lit_t lit(Lit_t lit) { return lit; }
//! Returns the literal of the given weight literal.
constexpr Lit_t lit(const WeightLit_t& w) { return w.lit; }
constexpr Lit_t lit(const WeightLit& w) { return w.lit; }
//! Returns the negative literal of the given atom.
constexpr Lit_t neg(Atom_t a) { return -lit(a); }
//! Returns the complement of the given literal.
Expand All @@ -246,7 +246,7 @@ constexpr Weight_t weight(Atom_t) { return 1; }
//! Returns the weight of the given literal, which is always 1.
constexpr Weight_t weight(Lit_t) { return 1; }
//! Returns the weight of the given weight literal.
constexpr Weight_t weight(const WeightLit_t& w) { return w.weight; }
constexpr Weight_t weight(const WeightLit& w) { return w.weight; }

///@}

Expand Down Expand Up @@ -326,9 +326,9 @@ class Bitset {
static constexpr auto max_count = sizeof(T) * CHAR_BIT;

//! Creates an empty set, i.e. all bits are zero.
constexpr Bitset() noexcept : set_{} {}
constexpr Bitset() noexcept = default;
//! Creates a set with the given elements, i.e. bits at the given positions are set.
constexpr Bitset(std::initializer_list<ElemType> elems) : set_{} {
constexpr Bitset(std::initializer_list<ElemType> elems) {
for (StorageType zero{}; auto e : elems) { set_ |= Potassco::set_bit(zero, +e); }
}
//! Constructs a bitset with all bits in `r` set.
Expand All @@ -353,13 +353,12 @@ class Bitset {

private:
constexpr explicit Bitset(StorageType r) : set_(r) {}
StorageType set_;
StorageType set_{0};
};
static_assert(Bitset<uint32_t>::max_count == 32);
static_assert(Bitset<uint32_t>{}.rep() == 0u);
static_assert(Bitset<uint32_t>::fromRep(8u).contains(3));
static_assert(Bitset<uint32_t>::fromRep(15u).count() == 4u);
static_assert(Bitset<uint32_t>{1, 2, 3}.rep() == 14u);

class DynamicBitset {
public:
Expand Down Expand Up @@ -413,7 +412,7 @@ class RuleBuilder;
* a pointer referencing a buffer internal to the string, making relocation non-trivial.
* In contrast, this class uses an SSO implementation that is more similar to the one from libc++.
*/
class ConstString {
class ConstString final {
public:
using trivially_relocatable = std::true_type; // NOLINT
//! Supported creation modes.
Expand Down
Loading

0 comments on commit 0915487

Please sign in to comment.