3#include <clingo/core/number.hh>
5#include <clingo/util/hash.hh>
6#include <clingo/util/print.hh>
7#include <clingo/util/unordered_set.hh>
31 static auto
to_rep(
String str) noexcept -> uint64_t {
return static_cast<uint64_t
>(str.rep_); }
38 [[nodiscard]]
auto c_str() const -> const
char *;
42 [[nodiscard]] auto
view() const -> std::string_view;
46 [[nodiscard]] auto
data() const -> const
char *;
48 [[nodiscard]] auto
empty() const ->
bool;
50 [[nodiscard]] auto
size() const ->
size_t;
52 [[nodiscard]] auto
starts_with(std::string_view prefix) const ->
bool;
55 [[nodiscard]] auto
hash() const ->
size_t;
58 friend auto operator==(
String a,
String b) ->
bool {
return a.rep_ == b.rep_; }
67 friend auto operator<=>(std::string_view a,
String b) -> std::strong_ordering {
return a <=> b.view(); }
69 friend auto operator<=>(
String a, std::string_view b) -> std::strong_ordering {
return a.view() <=> b; }
78 constexpr String(uintptr_t rep) noexcept : rep_{rep} {}
111 if (&other !=
this) {
120 if (&other !=
this) {
128 [[nodiscard]]
auto get() const ->
String const & {
return ref_; }
137 auto ret = std::exchange(sym.ref_,
String{});
161 return a.get() <=> b.get();
169 return a <=> b->view();
173 return a->view() <=> b;
192 return reinterpret_cast<String const *
>(ptr);
214enum class SymbolType : uint8_t { number, sup, inf, string, tuple, function };
234 [[nodiscard]] auto
num() const noexcept ->
Number const &;
258 [[nodiscard]]
auto signature() const -> std::optional<std::tuple<
String,
size_t,
bool>>;
268 friend auto operator==(
Symbol const &a,
Symbol const &b) ->
bool {
return a.rep_ == b.rep_; }
282 static auto to_rep(
Symbol sym)
noexcept -> uint64_t {
return sym.rep_; }
292 friend auto operator<<(std::ostream &out,
Symbol const &sym) -> std::ostream &;
295 friend auto operator<<(Util::OutputBuffer &out,
Symbol const &sym) -> Util::OutputBuffer &;
298 Symbol(uint64_t repr) noexcept : rep_{repr} {}
345 [[nodiscard]]
auto get() const ->
Symbol const & {
return ref_; }
409 return reinterpret_cast<Symbol const *
>(ptr);
437 std::vector<Symbol> stack_;
535 auto gc() -> std::tuple<size_t, size_t, size_t> {
return do_gc(); }
538 [[nodiscard]]
virtual auto do_tup(
SymbolSpan args,
bool referenced) ->
Symbol = 0;
540 [[nodiscard]]
virtual auto do_string(std::string_view
str,
bool referenced) ->
String = 0;
543 virtual void do_gc_block(
bool block)
noexcept = 0;
544 virtual void do_gc_add_owner(
SymbolOwner const &owner) = 0;
545 virtual void do_gc_del_owner(
SymbolOwner const &owner)
noexcept = 0;
546 virtual auto do_gc() -> std::tuple<size_t, size_t, size_t> = 0;
591 : store_{
store}, names_{names.begin(), names.end()}, prefix_{prefix} {}
597 names_.insert(names.begin(), names.end());
601 [[nodiscard]]
auto add_name(
String name) ->
bool {
return names_.emplace(name).second; }
Helper to block garbage collection.
Definition symbol.hh:553
GCLock(SymbolStore &store)
Block garbage collection.
Definition symbol.hh:556
~GCLock()
Unblock garbage collection.
Definition symbol.hh:558
Generator for auxiliary names.
Definition symbol.hh:585
NameGen(NameGen &&) noexcept=delete
Delete move/copy constructor.
auto new_name() -> String
Generate a unique variable name.
NameGen(SymbolStore &store, StringSet names, char const *prefix)
Constructor taking a set of variables names.
Definition symbol.hh:590
auto store() const -> SymbolStore &
Return the associated symbol store.
Definition symbol.hh:605
auto add_name(String name) -> bool
Add a name returning true if it is not yet used.
Definition symbol.hh:601
An arbitrary precision integer.
Definition number.hh:27
static auto to_repr(Number const &num) -> uint64_t
Get the internal representation of the number.
Definition number.hh:263
Class managing the lifetime of a String.
Definition symbol.hh:93
auto operator=(SharedString &&other) noexcept -> SharedString &
Move assignment.
Definition symbol.hh:119
friend auto operator==(SharedString const &a, std::string_view b) -> bool
Equality compare two strings.
Definition symbol.hh:157
auto operator=(SharedString const &other) noexcept -> SharedString &
Copy assignment.
Definition symbol.hh:110
SharedString(String ref, bool acquire=true) noexcept
Take ownership of the string reference.
Definition symbol.hh:98
friend auto operator<=>(SharedString const &a, std::string_view b) -> std::strong_ordering
Equality compare two strings.
Definition symbol.hh:172
~SharedString()
Release ownership of the held string reference.
Definition symbol.hh:104
auto operator->() const -> String const *
Get the contained string reference.
Definition symbol.hh:132
friend auto operator<=>(SharedString const &a, SharedString const &b) -> std::strong_ordering
Compare two strings.
Definition symbol.hh:160
friend auto operator==(SharedString const &a, SharedString const &b) -> bool
Equality compare two strings.
Definition symbol.hh:149
friend auto operator<=>(SharedString const &a, String const &b) -> std::strong_ordering
Compare two strings.
Definition symbol.hh:164
static auto to_rep(SharedString sym) -> uint64_t
Get an integer representation of the string.
Definition symbol.hh:136
auto get() const -> String const &
Get the contained string reference.
Definition symbol.hh:128
SharedString(SharedString &&other) noexcept
Move constructor.
Definition symbol.hh:108
friend auto operator==(SharedString const &a, String const &b) -> bool
Equality compare two strings.
Definition symbol.hh:151
friend auto operator==(std::string_view a, SharedString const &b) -> bool
Equality compare two strings.
Definition symbol.hh:155
SharedString(SharedString const &other) noexcept
Copy constructor.
Definition symbol.hh:106
auto hash() const -> size_t
Compute the hash of the string.
Definition symbol.hh:146
friend auto operator==(String const &a, SharedString const &b) -> bool
Equality compare two strings.
Definition symbol.hh:153
constexpr SharedString()=default
Construct an empty string.
auto operator*() const -> String const &
Get the contained string reference.
Definition symbol.hh:130
static auto from_rep(uint64_t repr) -> SharedString
Construct a string from its representation.
Definition symbol.hh:141
friend auto operator<=>(std::string_view a, SharedString const &b) -> std::strong_ordering
Equality compare two strings.
Definition symbol.hh:168
friend auto operator<=>(String const &a, SharedString const &b) -> std::strong_ordering
Compare two strings.
Definition symbol.hh:166
Class managing the lifetime of a Symbol.
Definition symbol.hh:306
auto operator*() const -> Symbol const &
Get the contained string reference.
Definition symbol.hh:347
auto hash() const -> size_t
Compute the hash of the symbol.
Definition symbol.hh:368
friend auto operator<=>(SharedSymbol const &a, SharedSymbol const &b) -> std::strong_ordering
Less than compare two symbols.
Definition symbol.hh:383
friend auto operator==(SharedSymbol const &a, Symbol const &b) -> bool
Equality compare two symbols.
Definition symbol.hh:377
SharedSymbol(SharedSymbol const &sym) noexcept
Copy constructor.
Definition symbol.hh:321
auto operator=(SharedSymbol &&sym) noexcept -> SharedSymbol &
Move assignment.
Definition symbol.hh:334
auto get() const -> Symbol const &
Get a reference to the contained symbol.
Definition symbol.hh:345
static auto from_rep(uint64_t repr) -> SharedSymbol
Create a shared symbol from its representation.
Definition symbol.hh:361
auto operator->() const -> Symbol const *
Get the contained string reference.
Definition symbol.hh:349
static auto to_rep(SharedSymbol const &sym) -> uint64_t
Get an integer representation of the symbol.
Definition symbol.hh:354
SharedSymbol(SharedSymbol &&sym) noexcept
Move constructor.
Definition symbol.hh:323
friend auto operator<=>(Symbol const &a, SharedSymbol const &b) -> std::strong_ordering
Less than compare two symbols.
Definition symbol.hh:393
friend auto compare(SharedSymbol const &a, SharedSymbol const &b) -> int
Compare two symbols.
Definition symbol.hh:371
friend auto operator==(SharedSymbol const &a, SharedSymbol const &b) -> bool
Equality compare two symbols.
Definition symbol.hh:374
auto operator=(SharedSymbol const &sym) noexcept -> SharedSymbol &
Copy assignment.
Definition symbol.hh:325
constexpr SharedSymbol() noexcept=default
Create a symbol for number zero.
friend auto operator==(Symbol const &a, SharedSymbol const &b) -> bool
Equality compare two symbols.
Definition symbol.hh:380
friend auto operator<=>(SharedSymbol const &a, Symbol const &b) -> std::strong_ordering
Less than compare two symbols.
Definition symbol.hh:388
~SharedSymbol()
Release ownership of the held symbol.
Definition symbol.hh:319
Reference to a string stored in a symbol store.
Definition symbol.hh:18
auto c_str() const -> const char *
Get the underlying C string.
friend auto operator<=>(String a, String b) -> std::strong_ordering
Less than compare two strings.
Definition symbol.hh:65
void acquire() const noexcept
Manually increment the reference count of the string.
static auto to_rep(String str) noexcept -> uint64_t
Convert a string to its integer representation.
Definition symbol.hh:31
auto view() const -> std::string_view
Get a string view.
void release() const noexcept
Manually decrement the reference count of the string.
friend auto operator<<(Util::OutputBuffer &out, String const &str) -> Util::OutputBuffer &
Append the given string to the buffer.
auto hash() const -> size_t
Compute the hash of the string.
friend auto operator<=>(std::string_view a, String b) -> std::strong_ordering
Less than compare two strings.
Definition symbol.hh:67
auto data() const -> const char *
Get the underlying character array.
friend auto operator<=>(String a, std::string_view b) -> std::strong_ordering
Less than compare two strings.
Definition symbol.hh:69
static auto from_rep(uint64_t rep) noexcept -> String
Construct a string from its integer representation.
Definition symbol.hh:33
constexpr String()=default
Construct an empty string.
auto empty() const -> bool
Test if the string is empty.
auto starts_with(std::string_view prefix) const -> bool
Check if the string starts with the given string.
friend auto operator<<(std::ostream &out, String const &str) -> std::ostream &
Output the given string.
auto size() const -> size_t
Get the length of the string.
friend auto operator==(String a, std::string_view b) -> bool
Equality compare a string and a string view.
Definition symbol.hh:60
friend auto operator==(std::string_view a, String b) -> bool
Equality compare a string view and a string.
Definition symbol.hh:62
Helper class to mark owned symbols.
Definition symbol.hh:429
void mark(String const &str)
Mark a string.
void mark(Symbol const &sym)
Mark a symbol and its descendants.
Interface for classes owning references to symbols.
Definition symbol.hh:441
virtual ~SymbolOwner()=default
Destroy the symbol owner.
virtual void mark(SymbolCollector &gc) const =0
Function called to mark all owned symbols.
A store for symbols.
Definition symbol.hh:454
virtual ~SymbolStore() noexcept=default
Destroy the store and all symbols in it.
void gc_del_owner(SymbolOwner const &owner) noexcept
Delete a symbol owner.
Definition symbol.hh:530
auto tup(SharedSymbolSpan args) -> SharedSymbol
Construct a tuple.
void gc_unblock() noexcept
Unblock garbage collection.
Definition symbol.hh:526
auto fun_ref(String name, SymbolSpan args, bool sign) -> Symbol
Construct a function symbol.
auto tup_ref(SymbolSpan args) -> Symbol
Construct a tuple.
auto num(Number num) noexcept -> SharedSymbol
Construct a number.
static auto inf() noexcept -> Symbol
Construct the supremum constant (#sup).
static auto str(SharedString str) noexcept -> SharedSymbol
Construct a quoted string.
auto fun(SharedString const &name, SharedSymbolSpan args, bool sign) -> SharedSymbol
Construct a function symbol.
auto string_ref(std::string_view str) -> String
Construct a string.
auto num_ref(Number num) noexcept -> Symbol
Construct a number (e.g., 42).
static auto str_ref(String str) noexcept -> Symbol
Construct a quoted string.
static auto sup() noexcept -> Symbol
Construct the infimum constant (#inf).
auto gc() -> std::tuple< size_t, size_t, size_t >
Cleanup symbols.
Definition symbol.hh:535
void gc_block() noexcept
Block garbage collection.
Definition symbol.hh:521
void gc_add_owner(SymbolOwner const &owner)
Add a symbol owner.
Definition symbol.hh:528
Variant-like class to store symbols stored in a symbol store.
Definition symbol.hh:225
friend auto operator<=>(Symbol const &a, Symbol const &b) -> std::strong_ordering
Compare two symbols.
Definition symbol.hh:275
auto flip_classical_sign() const -> std::optional< Symbol >
Flip the classical sign of the symbol.
auto num() const noexcept -> Number const &
Get the numeric value of the symbol.
auto has_classical_sign() const -> bool
Check whether the symbol has a sign.
auto name() const noexcept -> String
Get the name of the symbol.
static auto from_rep(uint64_t rep) noexcept -> Symbol
Create a symbol from its representation.
Definition symbol.hh:284
auto hash() const -> size_t
Compute the hash of the symbol.
Definition symbol.hh:253
void acquire() const noexcept
Manually increment the reference count of the symbol.
auto has_sign() const -> bool
Check whether the symbol has a sign.
friend auto operator<=>(Number const &a, Symbol const &b) -> std::strong_ordering
Compare symbols and numbers.
Definition symbol.hh:277
auto args() const noexcept -> SymbolSpan
Get the arguments of the symbol.
auto str() const noexcept -> String
Get the (raw) string value of the symbol.
friend auto operator<=>(Symbol const &a, Number const &b) -> std::strong_ordering
Compare symbols and numbers.
Definition symbol.hh:279
constexpr Symbol()=default
Create a reference to number zero.
static auto to_rep(Symbol sym) noexcept -> uint64_t
Get the representation of the symbol.
Definition symbol.hh:282
friend auto operator==(Number const &a, Symbol const &b) -> bool
Equality compare numbers and symbols.
Definition symbol.hh:270
auto signature() const -> std::optional< std::tuple< String, size_t, bool > >
Get the signature of the symbol.
friend auto compare(Symbol const &a, Symbol const &b) -> int
Compare two symbols.
void release() const noexcept
Manually decrement the reference count of the symbol.
auto type() const noexcept -> SymbolType
Get the type of the symbol.
friend auto operator==(Symbol const &a, Number const &b) -> bool
Equality compare numbers and symbols.
Definition symbol.hh:272
Create an output buffer that bears some similarities with C++'s iostreams.
Definition print.hh:24
auto as_string_ptr(SharedString const *ptr) -> String const *
Convert a shared string pointer into a string pointer.
Definition symbol.hh:190
Util::unordered_set< String > StringSet
A set of strings.
Definition symbol.hh:83
std::span< Symbol const > SymbolSpan
A span of symbols.
Definition symbol.hh:218
std::vector< String > StringVec
A vector of strings.
Definition symbol.hh:85
auto as_string_span(T const &vec) -> StringSpan
Convert a collection of shared strings into a string span.
Definition symbol.hh:196
auto as_shared_symbol_ptr(Symbol const *ptr) -> SharedSymbol const *
Convert a symbol pointer into a shared symbol pointer.
Definition symbol.hh:418
std::vector< SharedSymbol > SharedSymbolVec
A vector of symbols.
Definition symbol.hh:404
void init_default_symbol_store(USymbolStore store)
Initialize the default symbol store.
std::vector< std::optional< Symbol > > Assignment
Assignment mapping variables to symbols.
Definition symbol.hh:222
std::span< SharedSymbol const > SharedSymbolSpan
A span of symbols.
Definition symbol.hh:402
auto as_symbol_span(T const &vec) -> SymbolSpan
Convert a shared symbol collection into a symbol span.
Definition symbol.hh:413
auto default_symbol_store() -> SymbolStore &
Get the default symbol store.
auto as_shared_string_ptr(String const *ptr) -> SharedString const *
Convert a string pointer into a shared string pointer.
Definition symbol.hh:201
std::unique_ptr< SymbolStore > USymbolStore
A pointer to a symbol store.
Definition symbol.hh:550
auto make_symbol_store(bool slotted, bool shared) -> USymbolStore
Construct a new symbol store.
std::span< String const > StringSpan
A span of strings.
Definition symbol.hh:87
std::span< SharedString const > SharedStringSpan
A vector of strings.
Definition symbol.hh:187
SymbolType
Enumeration of available symbols types.
Definition symbol.hh:214
auto as_symbol_ptr(SharedSymbol const *ptr) -> Symbol const *
Convert a shared symbol pointer into a symbol pointer.
Definition symbol.hh:407
std::vector< Symbol > SymbolVec
A vector of symbols.
Definition symbol.hh:220
auto as_shared_string_span(T const &vec) -> SharedStringSpan
Convert a collection of strings into a shared string span.
Definition symbol.hh:207
Util::unordered_set< SharedString > SharedStringSet
A set of strings.
Definition symbol.hh:181
std::vector< SharedString > SharedStringVec
A vector of strings.
Definition symbol.hh:183
auto as_shared_symbol_span(T const &vec) -> SharedSymbolSpan
Convert a symbol collection into a shared symbol span.
Definition symbol.hh:424
tsl::hopscotch_set< Key, Hash, KeyEqual, Allocator, NeighborhoodSize, StoreHash, GrowthPolicy > unordered_set
Alias for unordered sets.
Definition unordered_set.hh:16
auto value_hash(std::type_info const &x) -> size_t
Compute a hash for type_info.
Definition hash.hh:277