Clingo
Loading...
Searching...
No Matches
symbol.hh
1#pragma once
2
3#include <clingo/core/number.hh>
4
5#include <clingo/util/hash.hh>
6#include <clingo/util/print.hh>
7#include <clingo/util/unordered_set.hh>
8
9#include <ostream>
10#include <span>
11
12namespace CppClingo {
13
16
18class String {
19 public:
23 constexpr String() = default;
24
26 void acquire() const noexcept;
28 void release() const noexcept;
29
31 static auto to_rep(String str) noexcept -> uint64_t { return static_cast<uint64_t>(str.rep_); }
33 static auto from_rep(uint64_t rep) noexcept -> String { return String{static_cast<uintptr_t>(rep)}; }
34
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;
53
55 [[nodiscard]] auto hash() const -> size_t;
56
58 friend auto operator==(String a, String b) -> bool { return a.rep_ == b.rep_; }
60 friend auto operator==(String a, std::string_view b) -> bool { return a.view() == b; }
62 friend auto operator==(std::string_view a, String b) -> bool { return a == b.view(); }
63
65 friend auto operator<=>(String a, String b) -> std::strong_ordering { return a.view() <=> b.view(); }
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; }
70
72 friend auto operator<<(std::ostream &out, String const &str) -> std::ostream &;
73
75 friend auto operator<<(Util::OutputBuffer &out, String const &str) -> Util::OutputBuffer &;
76
77 private:
78 constexpr String(uintptr_t rep) noexcept : rep_{rep} {}
79 uintptr_t rep_ = 0;
80};
81
85using StringVec = std::vector<String>;
87using StringSpan = std::span<String const>;
88
94 public:
96 constexpr SharedString() = default;
98 explicit SharedString(String ref, bool acquire = true) noexcept : ref_{ref} {
99 if (acquire) {
100 ref.acquire();
101 }
102 }
104 ~SharedString() { ref_.release(); }
106 SharedString(SharedString const &other) noexcept : ref_{other.ref_} { ref_.acquire(); }
108 SharedString(SharedString &&other) noexcept { std::swap(other.ref_, ref_); }
110 auto operator=(SharedString const &other) noexcept -> SharedString & {
111 if (&other != this) {
112 ref_.release();
113 ref_ = other.ref_;
114 ref_.acquire();
115 }
116 return *this;
117 }
119 auto operator=(SharedString &&other) noexcept -> SharedString & {
120 if (&other != this) {
121 ref_.release();
122 ref_ = other.ref_;
123 other.ref_ = String::from_rep(0);
124 }
125 return *this;
126 }
128 [[nodiscard]] auto get() const -> String const & { return ref_; }
130 [[nodiscard]] auto operator*() const -> String const & { return ref_; }
132 [[nodiscard]] auto operator->() const -> String const * { return &ref_; }
136 [[nodiscard]] static auto to_rep(SharedString sym) -> uint64_t {
137 auto ret = std::exchange(sym.ref_, String{});
138 return String::to_rep(ret);
139 }
141 [[nodiscard]] static auto from_rep(uint64_t repr) -> SharedString {
142 return SharedString{String::from_rep(repr), false};
143 }
144
146 [[nodiscard]] auto hash() const -> size_t { return CppClingo::Util::value_hash(ref_); }
147
149 friend auto operator==(SharedString const &a, SharedString const &b) -> bool { return a.ref_ == b.get(); }
151 friend auto operator==(SharedString const &a, String const &b) -> bool { return a.ref_ == b; }
153 friend auto operator==(String const &a, SharedString const &b) -> bool { return a == b.get(); }
155 friend auto operator==(std::string_view a, SharedString const &b) -> bool { return a == b->view(); }
157 friend auto operator==(SharedString const &a, std::string_view b) -> bool { return a->view() == b; }
158
160 friend auto operator<=>(SharedString const &a, SharedString const &b) -> std::strong_ordering {
161 return a.get() <=> b.get();
162 }
164 friend auto operator<=>(SharedString const &a, String const &b) -> std::strong_ordering { return a.get() <=> b; }
166 friend auto operator<=>(String const &a, SharedString const &b) -> std::strong_ordering { return a <=> b.get(); }
168 friend auto operator<=>(std::string_view a, SharedString const &b) -> std::strong_ordering {
169 return a <=> b->view();
170 }
172 friend auto operator<=>(SharedString const &a, std::string_view b) -> std::strong_ordering {
173 return a->view() <=> b;
174 }
175
176 private:
177 String ref_;
178};
179
183using SharedStringVec = std::vector<SharedString>;
187using SharedStringSpan = std::span<SharedString const>;
188
190inline auto as_string_ptr(SharedString const *ptr) -> String const * {
191 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
192 return reinterpret_cast<String const *>(ptr);
193}
194
196template <class T> inline auto as_string_span(T const &vec) -> StringSpan {
197 return {as_string_ptr(vec.data()), vec.size()};
198}
199
201inline auto as_shared_string_ptr(String const *ptr) -> SharedString const * {
202 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
203 return reinterpret_cast<SharedString const *>(ptr);
204}
205
207template <class T> inline auto as_shared_string_span(T const &vec) -> SharedStringSpan {
208 return {as_shared_string_ptr(vec.data()), vec.size()};
209}
210
214enum class SymbolType : uint8_t { number, sup, inf, string, tuple, function };
215
216class Symbol;
218using SymbolSpan = std::span<Symbol const>;
220using SymbolVec = std::vector<Symbol>;
222using Assignment = std::vector<std::optional<Symbol>>;
223
225class Symbol {
226 public:
230 constexpr Symbol() = default;
232 [[nodiscard]] auto type() const noexcept -> SymbolType;
234 [[nodiscard]] auto num() const noexcept -> Number const &;
236 [[nodiscard]] auto str() const noexcept -> String;
238 [[nodiscard]] auto name() const noexcept -> String;
240 [[nodiscard]] auto args() const noexcept -> SymbolSpan;
242 [[nodiscard]] auto flip_classical_sign() const -> std::optional<Symbol>;
246 [[nodiscard]] auto has_sign() const -> bool;
250 [[nodiscard]] auto has_classical_sign() const -> bool;
251
253 [[nodiscard]] auto hash() const -> size_t { return CppClingo::Util::value_hash(rep_); }
254
258 [[nodiscard]] auto signature() const -> std::optional<std::tuple<String, size_t, bool>>;
259
261 friend auto compare(Symbol const &a, Symbol const &b) -> int;
263 friend auto compare(Number const &a, Symbol const &b) -> int;
265 friend auto compare(Symbol const &a, Number const &b) -> int;
266
268 friend auto operator==(Symbol const &a, Symbol const &b) -> bool { return a.rep_ == b.rep_; }
270 friend auto operator==(Number const &a, Symbol const &b) -> bool { return Number::to_repr(a) == b.rep_; }
272 friend auto operator==(Symbol const &a, Number const &b) -> bool { return a.rep_ == Number::to_repr(b); }
273
275 friend auto operator<=>(Symbol const &a, Symbol const &b) -> std::strong_ordering { return compare(a, b) <=> 0; }
277 friend auto operator<=>(Number const &a, Symbol const &b) -> std::strong_ordering { return compare(a, b) <=> 0; }
279 friend auto operator<=>(Symbol const &a, Number const &b) -> std::strong_ordering { return compare(a, b) <=> 0; }
280
282 static auto to_rep(Symbol sym) noexcept -> uint64_t { return sym.rep_; }
284 static auto from_rep(uint64_t rep) noexcept -> Symbol { return Symbol{rep}; }
285
287 void acquire() const noexcept;
289 void release() const noexcept;
290
292 friend auto operator<<(std::ostream &out, Symbol const &sym) -> std::ostream &;
293
295 friend auto operator<<(Util::OutputBuffer &out, Symbol const &sym) -> Util::OutputBuffer &;
296
297 private:
298 Symbol(uint64_t repr) noexcept : rep_{repr} {}
299 uint64_t rep_ = 0;
300};
301
307 public:
311 constexpr SharedSymbol() noexcept = default;
313 explicit SharedSymbol(Symbol sym, bool acquire = true) noexcept : ref_{sym} {
314 if (acquire) {
315 ref_.acquire();
316 }
317 }
319 ~SharedSymbol() { ref_.release(); }
321 SharedSymbol(SharedSymbol const &sym) noexcept : ref_{sym.ref_} { ref_.acquire(); }
323 SharedSymbol(SharedSymbol &&sym) noexcept { std::swap(sym.ref_, ref_); }
325 auto operator=(SharedSymbol const &sym) noexcept -> SharedSymbol & {
326 if (&sym != this) {
327 ref_.release();
328 ref_ = sym.ref_;
329 ref_.acquire();
330 }
331 return *this;
332 }
334 auto operator=(SharedSymbol &&sym) noexcept -> SharedSymbol & {
335 if (&sym != this) {
336 ref_.release();
337 ref_ = sym.ref_;
338 sym.ref_ = Symbol::from_rep(0);
339 }
340 return *this;
341 }
345 [[nodiscard]] auto get() const -> Symbol const & { return ref_; }
347 [[nodiscard]] auto operator*() const -> Symbol const & { return ref_; }
349 [[nodiscard]] auto operator->() const -> Symbol const * { return &ref_; }
354 [[nodiscard]] static auto to_rep(SharedSymbol const &sym) -> uint64_t {
355 sym.ref_.acquire();
356 return Symbol::to_rep(sym.ref_);
357 }
361 [[nodiscard]] static auto from_rep(uint64_t repr) -> SharedSymbol {
362 auto ret = SharedSymbol();
363 ret.ref_ = Symbol::from_rep(repr);
364 return ret;
365 }
366
368 [[nodiscard]] auto hash() const -> size_t { return CppClingo::Util::value_hash(ref_); }
369
371 friend auto compare(SharedSymbol const &a, SharedSymbol const &b) -> int { return compare(a.get(), b.get()); }
372
374 friend auto operator==(SharedSymbol const &a, SharedSymbol const &b) -> bool { return a.get() == b.get(); }
375
377 friend auto operator==(SharedSymbol const &a, Symbol const &b) -> bool { return a.get() == b; }
378
380 friend auto operator==(Symbol const &a, SharedSymbol const &b) -> bool { return a == b.get(); }
381
383 friend auto operator<=>(SharedSymbol const &a, SharedSymbol const &b) -> std::strong_ordering {
384 return compare(a, b) <=> 0;
385 }
386
388 friend auto operator<=>(SharedSymbol const &a, Symbol const &b) -> std::strong_ordering {
389 return compare(*a, b) <=> 0;
390 }
391
393 friend auto operator<=>(Symbol const &a, SharedSymbol const &b) -> std::strong_ordering {
394 return compare(a, *b) <=> 0;
395 }
396
397 private:
398 Symbol ref_ = Symbol::from_rep(0);
399};
400
402using SharedSymbolSpan = std::span<SharedSymbol const>;
404using SharedSymbolVec = std::vector<SharedSymbol>;
405
407inline auto as_symbol_ptr(SharedSymbol const *ptr) -> Symbol const * {
408 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
409 return reinterpret_cast<Symbol const *>(ptr);
410}
411
413template <class T> inline auto as_symbol_span(T const &vec) -> SymbolSpan {
414 return {as_symbol_ptr(vec.data()), vec.size()};
415}
416
418inline auto as_shared_symbol_ptr(Symbol const *ptr) -> SharedSymbol const * {
419 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
420 return reinterpret_cast<SharedSymbol const *>(ptr);
421}
422
424template <class T> inline auto as_shared_symbol_span(T const &vec) -> SharedSymbolSpan {
425 return {as_shared_symbol_ptr(vec.data()), vec.size()};
426}
427
430 public:
432 void mark(Symbol const &sym);
434 void mark(String const &str);
435
436 private:
437 std::vector<Symbol> stack_;
438};
439
442 public:
444 virtual ~SymbolOwner() = default;
446 virtual void mark(SymbolCollector &gc) const = 0;
447};
448
455 public:
457 virtual ~SymbolStore() noexcept = default;
458
462 [[nodiscard]] auto string(std::string_view str) -> SharedString;
463
465 [[nodiscard]] static auto sup() noexcept -> Symbol;
467 [[nodiscard]] static auto inf() noexcept -> Symbol;
469 //
472 [[nodiscard]] static auto str(SharedString str) noexcept -> SharedSymbol;
474 [[nodiscard]] static auto str(String str) noexcept -> SharedSymbol;
476 [[nodiscard]] auto num(Number num) noexcept -> SharedSymbol;
478 [[nodiscard]] static auto num(int32_t num) noexcept -> SharedSymbol;
482 [[nodiscard]] auto tup(SharedSymbolSpan args) -> SharedSymbol;
484 [[nodiscard]] auto tup(SymbolSpan args) -> SharedSymbol;
488 [[nodiscard]] auto fun(SharedString const &name, SharedSymbolSpan args, bool sign) -> SharedSymbol;
490 [[nodiscard]] auto fun(String name, SymbolSpan args, bool sign) -> SharedSymbol;
491
492 // interface to create floating references
493
497 [[nodiscard]] auto string_ref(std::string_view str) -> String;
498
503 [[nodiscard]] static auto str_ref(String str) noexcept -> Symbol;
505 [[nodiscard]] auto num_ref(Number num) noexcept -> Symbol;
507 [[nodiscard]] static auto num_ref(int32_t num) noexcept -> Symbol;
511 [[nodiscard]] auto tup_ref(SymbolSpan args) -> Symbol;
515 [[nodiscard]] auto fun_ref(String name, SymbolSpan args, bool sign) -> Symbol;
516
521 void gc_block() noexcept { do_gc_block(true); }
526 void gc_unblock() noexcept { do_gc_block(false); }
528 void gc_add_owner(SymbolOwner const &owner) { do_gc_add_owner(owner); }
530 void gc_del_owner(SymbolOwner const &owner) noexcept { do_gc_del_owner(owner); }
535 auto gc() -> std::tuple<size_t, size_t, size_t> { return do_gc(); }
536
537 private:
538 [[nodiscard]] virtual auto do_tup(SymbolSpan args, bool referenced) -> Symbol = 0;
539 [[nodiscard]] virtual auto do_fun(String str, SymbolSpan args, bool sign, bool referenced) -> Symbol = 0;
540 [[nodiscard]] virtual auto do_string(std::string_view str, bool referenced) -> String = 0;
541 [[nodiscard]] virtual auto do_num(Number num) noexcept -> Symbol = 0;
542
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;
547};
548
550using USymbolStore = std::unique_ptr<SymbolStore>;
551
553class GCLock {
554 public:
556 GCLock(SymbolStore &store) : store_{&store} { store_->gc_block(); }
558 ~GCLock() { store_->gc_unblock(); }
559
560 private:
561 SymbolStore *store_;
562};
563
568
574
582auto make_symbol_store(bool slotted, bool shared) -> USymbolStore;
583
585class NameGen {
586 public:
590 NameGen(SymbolStore &store, StringSet names, char const *prefix)
591 : store_{store}, names_{names.begin(), names.end()}, prefix_{prefix} {}
593 NameGen(NameGen &&) noexcept = delete;
595 void init(StringSet names, char const *prefix) {
596 num_ = 0;
597 names_.insert(names.begin(), names.end());
598 prefix_ = prefix;
599 }
601 [[nodiscard]] auto add_name(String name) -> bool { return names_.emplace(name).second; }
603 [[nodiscard]] auto new_name() -> String;
605 [[nodiscard]] auto store() const -> SymbolStore & { return store_; }
606
607 private:
609 SymbolStore &store_;
611 SharedStringSet names_;
613 char const *prefix_;
615 size_t num_ = 0;
616};
617
619
620} // namespace CppClingo
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