ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
irep_idt.h
Go to the documentation of this file.
1#ifndef IREP_IDT_H
2#define IREP_IDT_H
3
4#include <algorithm>
5#include <functional>
7#include <util/base/prefix.h>
8
9// A 4-byte handle into a global string interning pool. Equality and hashing
10// are O(1) (integer compare); the underlying string is fetched on demand.
11class irep_idt final
12{
13public:
14 // this is safe for static objects
15
16 constexpr irep_idt() : no(0)
17 {
18 }
19
20 // this is safe for static objects
21 constexpr static irep_idt make_from_table_index(unsigned no)
22 {
23 return irep_idt(no);
24 }
25
26 // this one is not safe for static objects
27 irep_idt(const char *s) : no(get_string_pool()[s])
28 {
29 }
30
31 // this one is not safe for static objects
32 irep_idt(const std::string &s) : no(get_string_pool()[s])
33 {
34 }
35
36 irep_idt(const irep_idt &) = default;
37
38 // access
40 constexpr irep_idt(irep_idt &&other) : no(other.no)
41 {
42 }
43
44 friend bool has_prefix(const irep_idt &s, std::string_view prefix)
45 {
46 return has_prefix(std::string_view(s.as_string()), prefix);
47 }
48
49 friend bool has_suffix(const irep_idt &s, std::string_view suffix)
50 {
51 return has_suffix(std::string_view(s.as_string()), suffix);
52 }
53
54 inline bool empty() const
55 {
56 return no == 0; // string 0 is exactly the empty string
57 }
58
59 inline char operator[](size_t i) const
60 {
61 return as_string()[i];
62 }
63
64 // the address returned is guaranteed to be stable
65 inline const char *c_str() const
66 {
67 return as_string().c_str();
68 }
69
70 inline size_t size() const
71 {
72 return as_string().size();
73 }
74
75 // ordering -- not the same as lexicographical ordering
76
77 inline bool operator<(const irep_idt &b) const
78 {
79 return no < b.no;
80 }
81
82 // comparison with same type
83
84 inline bool operator==(const irep_idt &b) const
85 {
86 return no == b.no;
87 } // really fast equality testing
88
89 inline bool operator!=(const irep_idt &b) const
90 {
91 return no != b.no;
92 } // really fast equality testing
93
94 // comparison with other types
95
96 bool operator==(const char *b) const
97 {
98 return as_string() == b;
99 }
100 bool operator!=(const char *b) const
101 {
102 return as_string() != b;
103 }
104
105 bool operator==(const std::string &b) const
106 {
107 return as_string() == b;
108 }
109 bool operator!=(const std::string &b) const
110 {
111 return as_string() != b;
112 }
113 bool operator<(const std::string &b) const
114 {
115 return as_string() < b;
116 }
117 bool operator>(const std::string &b) const
118 {
119 return as_string() > b;
120 }
121 bool operator<=(const std::string &b) const
122 {
123 return as_string() <= b;
124 }
125 bool operator>=(const std::string &b) const
126 {
127 return as_string() >= b;
128 }
129
130 int compare(const irep_idt &b) const
131 {
132 if (no == b.no)
133 return 0; // equal
134 return as_string().compare(b.as_string());
135 }
136
137 // the reference returned is guaranteed to be stable
138 const std::string &as_string() const
139 {
140 return get_string_pool().get_string(no);
141 }
142
143 // modifying
144
145 inline void clear()
146 {
147 no = 0;
148 }
149
150 inline void swap(irep_idt &b)
151 {
152 unsigned t = no;
153 no = b.no;
154 b.no = t;
155 }
156
157 inline irep_idt &operator=(const irep_idt &b) = default;
158 inline irep_idt &operator=(irep_idt &&other) = default;
159
160 // output
161
162 std::ostream &operator<<(std::ostream &out) const
163 {
164 return out << as_string();
165 }
166
167 inline unsigned get_no() const
168 {
169 return no;
170 }
171
172 inline size_t hash() const
173 {
174 return no;
175 }
176
177private:
178 constexpr explicit irep_idt(unsigned _no) : no(_no)
179 {
180 }
181
182 unsigned no;
183};
184
185inline std::ostream &operator<<(std::ostream &out, const irep_idt &a)
186{
187 return a.operator<<(out);
188}
189
190namespace std
191{
192template <>
193struct hash<irep_idt>
194{
195 size_t operator()(const irep_idt &s) const
196 {
197 return s.hash();
198 }
199};
200} // namespace std
201
202#endif
Definition irep_idt.h:12
bool operator==(const char *b) const
Definition irep_idt.h:96
const char * c_str() const
Definition irep_idt.h:65
const std::string & as_string() const
Definition irep_idt.h:138
size_t size() const
Definition irep_idt.h:70
size_t hash() const
Definition irep_idt.h:172
void swap(irep_idt &b)
Definition irep_idt.h:150
bool operator<=(const std::string &b) const
Definition irep_idt.h:121
bool operator==(const std::string &b) const
Definition irep_idt.h:105
unsigned get_no() const
Definition irep_idt.h:167
bool operator!=(const irep_idt &b) const
Definition irep_idt.h:89
irep_idt(const std::string &s)
Definition irep_idt.h:32
constexpr irep_idt()
Definition irep_idt.h:16
std::ostream & operator<<(std::ostream &out) const
Definition irep_idt.h:162
bool operator==(const irep_idt &b) const
Definition irep_idt.h:84
irep_idt(const irep_idt &)=default
bool operator<(const irep_idt &b) const
Definition irep_idt.h:77
constexpr irep_idt(irep_idt &&other)
source object other, this is effectively just a copy constructor.
Definition irep_idt.h:40
bool operator!=(const char *b) const
Definition irep_idt.h:100
static constexpr irep_idt make_from_table_index(unsigned no)
Definition irep_idt.h:21
void clear()
Definition irep_idt.h:145
int compare(const irep_idt &b) const
Definition irep_idt.h:130
char operator[](size_t i) const
Definition irep_idt.h:59
bool operator<(const std::string &b) const
Definition irep_idt.h:113
bool empty() const
Definition irep_idt.h:54
bool operator!=(const std::string &b) const
Definition irep_idt.h:109
friend bool has_prefix(const irep_idt &s, std::string_view prefix)
Definition irep_idt.h:44
friend bool has_suffix(const irep_idt &s, std::string_view suffix)
Definition irep_idt.h:49
irep_idt & operator=(irep_idt &&other)=default
irep_idt(const char *s)
Definition irep_idt.h:27
bool operator>=(const std::string &b) const
Definition irep_idt.h:125
bool operator>(const std::string &b) const
Definition irep_idt.h:117
irep_idt & operator=(const irep_idt &b)=default
const std::string & get_string(size_t no) const
Definition string_pool.h:67
@ other
Definition converter_compare.cpp:887
std::ostream & operator<<(std::ostream &out, const irep_idt &a)
Definition irep_idt.h:185
Definition exception.cpp:51
string_pool & get_string_pool()
Definition string_pool.h:107
size_t operator()(const irep_idt &s) const
Definition irep_idt.h:195