dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
symbolic_variable.h
1#pragma once
2
3#include <cstddef>
4#include <functional>
5#include <memory>
6#include <ostream>
7#include <string>
8
9#include "dlinear/symbolic/hash.h"
10
11namespace dlinear::drake {
12namespace symbolic {
13
15class Variable {
16 public:
17 typedef size_t Id;
18
20 enum class Type {
22 INTEGER,
23 BINARY,
24 BOOLEAN,
25 };
26
27 Variable(const Variable &) = default;
28 Variable &operator=(const Variable &) = default;
29 Variable(Variable &&) = default;
30 Variable &operator=(Variable &&) = default;
31
42
44 ~Variable() = default;
45
48 explicit Variable(std::string name, Type type = Type::CONTINUOUS);
49
52 bool is_dummy() const { return get_id() == 0; }
53 Id get_id() const;
54 Type get_type() const;
55 size_t get_hash() const { return std::hash<Id>{}(id_); }
56 std::string get_name() const;
57 std::string to_string() const;
58
60 bool equal_to(const Variable &v) const { return id_ == v.id_; }
61
63 bool less(const Variable &v) const { return id_ < v.id_; }
64
65 friend std::ostream &operator<<(std::ostream &os, const Variable &var);
66
67 private:
68 static std::vector<std::string> names_;
69 // Produces a unique ID for a variable.
70 static Id get_next_id();
71 Id id_{};
73};
74
75std::ostream &operator<<(std::ostream &os, Variable::Type type);
76
77} // namespace symbolic
78
79} // namespace dlinear::drake
80
81namespace std {
82/* Provides std::less<dlinear::drake::symbolic::Variable>. */
83template <>
84struct less<dlinear::drake::symbolic::Variable> {
85 bool operator()(const dlinear::drake::symbolic::Variable &lhs, const dlinear::drake::symbolic::Variable &rhs) const {
86 return lhs.less(rhs);
87 }
88};
89
90/* Provides std::equal_to<dlinear::drake::symbolic::Variable>. */
91template <>
92struct equal_to<dlinear::drake::symbolic::Variable> {
93 bool operator()(const dlinear::drake::symbolic::Variable &lhs, const dlinear::drake::symbolic::Variable &rhs) const {
94 return lhs.equal_to(rhs);
95 }
96};
97
98template <>
99struct hash<dlinear::drake::symbolic::Variable> {
100 size_t operator()(const dlinear::drake::symbolic::Variable &v) const { return v.get_hash(); }
101};
102
103} // namespace std
Represents a symbolic variable.
Type
Supported types of symbolic variables.
@ 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 mpq_class value.
@ BOOLEAN
A BOOLEAN variable takes a bool value.
static std::vector< std::string > names_
Names of variables.
~Variable()=default
Default destructor.
bool is_dummy() const
Checks if this is a dummy variable (ID = 0) which is created by the default constructor.
Variable(std::string name, Type type=Type::CONTINUOUS)
Constructs a variable with a string.
bool equal_to(const Variable &v) const
Checks the equality of two variables based on their ID values.
bool less(const Variable &v) const
Compares two variables based on their ID values.
Global namespace for the dlinear library.
STL namespace.