smats  0.0.1
Satisfability Modulo Arithmetic Theories Symbols
Loading...
Searching...
No Matches
variable.h
1
11#pragma once
12// IWYU pragma: no_include <string_view>
13
14#include <cstddef>
15#include <cstdint>
16#include <functional>
17#include <memory>
18#include <ostream>
19#include <string>
20#include <utility>
21
22#include "smats/util/concepts.h"
23#include "smats/util/hash.hpp"
24
25namespace smats {
26
34class Variable {
35 public:
36 using Id = std::size_t;
37
39 enum class Type : std::uint8_t {
41 INTEGER,
42 BINARY,
43 BOOLEAN,
44 };
45
52 Variable() : id_{0}, name_{nullptr} {};
58 explicit Variable(const std::string& name, Type type = Type::CONTINUOUS);
59 Variable(const Variable&) = default;
60 Variable(Variable&& other) noexcept : id_(other.id_), name_(std::move(other.name_)) { other.id_ = 0; }
61 Variable& operator=(const Variable&) = default;
62 Variable& operator=(Variable&& other) noexcept {
63 id_ = other.id_;
64 name_ = std::move(other.name_);
65 other.id_ = 0;
66 return *this;
67 }
68
74 [[nodiscard]] bool is_dummy() const { return id_ == 0; }
76 [[nodiscard]] Id id() const { return id_; }
78 [[nodiscard]] Type type() const { return static_cast<Type>(Id{id_} >> (7 * 8)); }
80 [[nodiscard]] const std::string& name() const;
81
83 [[nodiscard]] inline bool equal_to(const Variable& o) const noexcept { return id_ == o.id_; }
85 [[nodiscard]] inline bool less(const Variable& o) const noexcept { return id_ < o.id_; }
87 inline void hash(InvocableHashAlgorithm auto& hasher) const noexcept { hash_append(hasher, id_); }
88
89 private:
103
104 Id id_;
105 std::shared_ptr<const std::string> name_;
106};
107
108std::ostream& operator<<(std::ostream& os, const Variable& var);
109
110std::ostream& operator<<(std::ostream& os, Variable::Type type);
111
112} // namespace smats
113
114namespace std {
115template <>
116struct less<smats::Variable> {
117 bool operator()(const smats::Variable& lhs, const smats::Variable& rhs) const { return lhs.less(rhs); }
118};
119
120template <>
121struct equal_to<smats::Variable> {
122 bool operator()(const smats::Variable& lhs, const smats::Variable& rhs) const { return lhs.equal_to(rhs); }
123};
124
125template <>
126struct hash<smats::Variable> : public smats::DefaultHash {};
127
128} // namespace std
Definition variable.h:34
std::shared_ptr< const std::string > name_
Name of the variable.
Definition variable.h:105
Variable()
Definition variable.h:52
Id id_
Unique identifier for the variable. The high-order byte stores the Type.
Definition variable.h:104
static Id get_next_id(Variable::Type type)
Definition variable.cpp:19
bool less(const Variable &o) const noexcept
Definition variable.h:85
bool is_dummy() const
Definition variable.h:74
void hash(InvocableHashAlgorithm auto &hasher) const noexcept
Definition variable.h:87
Id id() const
Definition variable.h:76
Type type() const
Definition variable.h:78
Type
Definition variable.h:39
@ INTEGER
An INTEGER variable takes an int value.
@ BINARY
A BINARY variable takes an integer value from {0, 1}.
@ CONTINUOUS
A CONTINUOUS variable takes a double value.
@ BOOLEAN
A BOOLEAN variable takes a bool value.
const std::string & name() const
Definition variable.cpp:33
bool equal_to(const Variable &o) const noexcept
Definition variable.h:83
Definition concepts.h:88
ExpressionKind enum.
void hash_append(HashAlgorithm &hasher, const T &hashable) noexcept
Definition hash.hpp:79
Definition gmp.cpp:14
Definition hash.hpp:265