3#include <clingo/core.hh>
5#include <clingo/symbol.h>
53 return reinterpret_cast<Symbol const *
>(sym);
99 if (rep_ !=
other.rep_) {
101 rep_ = std::exchange(
other.rep_, 0);
141 auto [data, size] = Detail::call<clingo_symbol_name>(rep_);
149 auto [data, size] = Detail::call<clingo_symbol_string>(rep_);
187 return std::string{
bld.str()};
252 out << sym.to_string();
274 return Symbol{Detail::call<clingo_symbol_create_number_str>(c_cast(
lib), str.data(), str.size()),
false};
301 return Symbol{Detail::call<clingo_symbol_create_string>(c_cast(
lib), str.data(), str.size()),
false};
315 return Symbol{Detail::call<clingo_symbol_create_function>(c_cast(
lib), str.data(), str.size(),
316 c_cast(arguments.data()), arguments.size(), is_positive),
331 return Function(
lib, str, std::span{arguments.begin(), arguments.end()}, is_positive);
340 return Symbol{Detail::call<clingo_symbol_create_tuple>(c_cast(
lib), c_cast(arguments.data()), arguments.size()),
350 return Tuple(
lib, std::span{arguments.begin(), arguments.end()});
358 return Symbol{Detail::call<clingo_parse_term>(c_cast(
lib), str.data(), str.size()),
false};
367template <>
struct hash<Clingo::Symbol> {
368 auto operator()(
Clingo::Symbol const &sym)
const noexcept ->
size_t {
return sym.
hash(); }
The main library class for managing global information and logging.
Definition core.hh:471
A string builder for constructing strings.
Definition core.hh:524
Class modeling a symbol in Clingo.
Definition symbol.hh:68
auto is_negative() const -> bool
Check whether a function or number symbol is negative.
Definition symbol.hh:161
~Symbol()
The destructor releases the symbol.
Definition symbol.hh:73
auto hash() const noexcept -> size_t
Compute a hash value for the symbol.
Definition symbol.hh:226
auto type() const -> SymbolType
Get the type of the symbol.
Definition symbol.hh:176
friend auto operator<<(std::ostream &out, Symbol const &sym) -> std::ostream &
Output the symbol to a stream.
Definition symbol.hh:251
auto to_string() const -> std::string
Get a string representation of the symbol.
Definition symbol.hh:184
Symbol()=default
Default constructor, creates a symbol holding number zero.
auto match(std::string_view name, size_t arity, bool positive=true) const -> bool
Match the symbol against a function signature.
Definition symbol.hh:208
auto operator=(Symbol const &other) noexcept -> Symbol &
The copy assignment acquires the symbol and releases the old one.
Definition symbol.hh:83
auto name() const -> std::string_view
Get the name of the symbol if it is a function.
Definition symbol.hh:140
friend auto operator<=>(Symbol const &a, Symbol const &b) noexcept -> std::strong_ordering
Compare two symbols.
Definition symbol.hh:242
friend auto c_cast(Symbol const &sym) -> clingo_symbol_t const &
Cast the symbol to its C API representation.
Definition symbol.hh:122
Symbol(clingo_symbol_t rep, bool acquire)
Construct a symbol from its C API representation.
Definition symbol.hh:112
Symbol(Symbol &&other) noexcept
The move constructor transfers ownership of the symbol.
Definition symbol.hh:93
auto string() const -> std::string_view
Get the string value of the symbol if it is a string.
Definition symbol.hh:148
auto arguments() const -> SymbolSpan
Get the arguments of a function or tuple symbol.
Definition symbol.hh:166
auto signature() const -> std::optional< std::tuple< std::string_view, size_t, bool > >
Get the signature of the symbol if it is a function.
Definition symbol.hh:195
auto operator=(Symbol &&other) noexcept -> Symbol &
The move assignment transfers ownership and releases the old one.
Definition symbol.hh:98
friend auto operator==(Symbol const &a, Symbol const &b) noexcept -> bool
Compare two symbols for equality.
Definition symbol.hh:233
auto is_positive() const -> bool
Check whether a function or number symbol is positive.
Definition symbol.hh:156
Symbol(Symbol const &other) noexcept
The copy constructor acquires the symbol.
Definition symbol.hh:78
auto match(size_t arity) const -> bool
Match the symbol against a tuple signature.
Definition symbol.hh:217
friend auto c_cast(Symbol const *sym) -> clingo_symbol_t const *
Cast the symbol to its C API representation.
Definition symbol.hh:127
auto number() const -> int
Get the numeric value of the symbol if it is a number.
Definition symbol.hh:135
CLINGO_VISIBILITY_DEFAULT int clingo_symbol_compare(clingo_symbol_t a, clingo_symbol_t b)
Check if a symbol is less than another symbol.
CLINGO_VISIBILITY_DEFAULT clingo_symbol_t clingo_symbol_create_supremum(void)
Construct a symbol representing #sup.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string(clingo_symbol_t symbol, clingo_string_builder_t *builder)
Get the string representation of a symbol.
CLINGO_VISIBILITY_DEFAULT size_t clingo_symbol_hash(clingo_symbol_t symbol)
Calculate a hash code of a symbol.
uint64_t clingo_symbol_t
Type to represent a symbol.
Definition symbol.h:51
CLINGO_VISIBILITY_DEFAULT void clingo_symbol_acquire(clingo_symbol_t symbol)
Acquire ownership of the given symbol.
CLINGO_VISIBILITY_DEFAULT clingo_symbol_type_t clingo_symbol_type(clingo_symbol_t symbol)
Get the type of a symbol.
int clingo_symbol_type_t
Corresponding type to clingo_symbol_type.
Definition symbol.h:45
CLINGO_VISIBILITY_DEFAULT void clingo_symbol_release(clingo_symbol_t symbol)
Release ownership of the given symbol.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_arguments(clingo_symbol_t symbol, clingo_symbol_t const **arguments, size_t *arguments_size)
Get the arguments of a symbol.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_equal(clingo_symbol_t a, clingo_symbol_t b)
Check if two symbols are equal.
CLINGO_VISIBILITY_DEFAULT clingo_symbol_t clingo_symbol_create_number(int32_t number)
Construct a symbol representing a number.
CLINGO_VISIBILITY_DEFAULT clingo_symbol_t clingo_symbol_create_infimum(void)
Construct a symbol representing #inf.
@ clingo_symbol_type_number
a numeric symbol, e.g., 1
Definition symbol.h:37
@ clingo_symbol_type_string
a string symbol, e.g., "a"
Definition symbol.h:40
@ clingo_symbol_type_function
a function symbol, e.g., c, -c, or f(1,"a")
Definition symbol.h:42
@ clingo_symbol_type_supremum
the #sup symbol
Definition symbol.h:38
@ clingo_symbol_type_tuple
a tuple symbol, e.g., (1, "a")`
Definition symbol.h:41
@ clingo_symbol_type_infimum
the #inf symbol
Definition symbol.h:39
@ tuple
Theory tuples "(t1,...,tn)".
@ number
a number term, e.g., 42
@ function
a function term, e.g., f(1,2,3)
auto String(Library const &lib, std::string_view str) -> Symbol
Construct a string symbol from a string.
Definition symbol.hh:300
std::vector< Symbol > SymbolVector
A vector of symbols.
Definition symbol.hh:42
auto parse_term(Library const &lib, std::string_view str) -> Symbol
Parse the given string and evaluate it to a symbol.
Definition symbol.hh:357
auto Number(int num) -> Symbol
Construct a numeric symbol from an integer.
Definition symbol.hh:264
std::span< Symbol const > SymbolSpan
A span of symbols, which is a view on a contiguous sequence of symbols.
Definition symbol.hh:38
auto Infimum() -> Symbol
Construct an infimum symbol.
Definition symbol.hh:291
auto Function(Library const &lib, std::string_view str, SymbolSpan arguments={}, bool is_positive=true) -> Symbol
Construct a function symbol from a name and arguments.
Definition symbol.hh:313
auto cpp_cast(clingo_symbol_t const *sym) -> Symbol const *
Cast a C symbol to its C++ representation.
Definition symbol.hh:52
std::initializer_list< Symbol const > SymbolList
A list of symbols.
Definition symbol.hh:40
auto Tuple(Library const &lib, SymbolSpan arguments={}) -> Symbol
Construct a tuple symbol from a list of arguments.
Definition symbol.hh:339
SymbolType
Enumeration of symbol types.
Definition symbol.hh:21
auto Supremum() -> Symbol
Construct a supremum symbol.
Definition symbol.hh:282
@ tuple
The symbol is a tuple.
@ supremum
The symbol is a sentinel for the maximum value.
@ function
The symbol is a function.
@ infimum
The symbol is a sentinel for the minimum value.