forked from mlubin/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strong_integers.h
353 lines (302 loc) · 14.4 KB
/
strong_integers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Generates strongly typed integer types.
//
// StrongIndex is a simple template class mechanism for defining "logical"
// index-like class types that support some of the same functionalities
// as int, but which prevent assignment, construction, and
// other operations from other similar integer-like types. Essentially, the
// template class StrongIndex<StrongIndexName> has the additional
// property that it cannot be assigned to or constructed from another
// StrongIndex with a different StrongIndexName.
//
// Usage
// DEFINE_STRONG_INDEX_TYPE(name);
// where name is the desired (unique) name for the "logical" index type.
//
// StrongInt64 is a more general strong integer class based on int64_t.
// It has the same general type safeness, and supports more integer operators.
//
// Usage
// DEFINE_STRONG_INT64_TYPE(name);
// where name is the desired (unique) name for the "logical" int64_t type.
//
// SUPPORTED OPERATIONS --------------------------------------------------------
//
// The StrongIndex type is limited and only supports following operators are
// supported: unary: ++ (both prefix and postfix), comparison: ==, !=, <, <=, >,
// >=; assignment: =, +=, -=,; stream: <<. Each operator allows the same
// StrongIndexName and the int to be used on both left- and right-hand sides.
//
// The StrongInt64 type supports all integer operators across StrongInt64
// with the same StrongIntegerName and int64_t.
//
// Both support an accessor value() returning the stored value.
//
// The classes also define hash functors that allows the strong types to be used
// as key to hashable containers.
#ifndef OR_TOOLS_UTIL_STRONG_INTEGERS_H_
#define OR_TOOLS_UTIL_STRONG_INTEGERS_H_
#include <stddef.h>
#include <cstdint>
#include <functional>
#include <iosfwd>
#include <ostream> // NOLINT
#include <type_traits>
#include "absl/base/port.h"
#include "absl/strings/string_view.h"
#include "ortools/base/logging.h"
#include "ortools/base/macros.h"
namespace operations_research {
// Defines the StrongIndex and typedefs it to index_type_name.
//
// Note: The struct index_type_name ## _index_tag_ trickery is needed to ensure
// that a new type is created per index_type_name.
#define DEFINE_STRONG_INDEX_TYPE(index_type_name) \
struct index_type_name##_index_tag_ { \
static constexpr absl::string_view TypeName() { return #index_type_name; } \
}; \
typedef ::operations_research::StrongIndex<index_type_name##_index_tag_> \
index_type_name;
// Defines the StrongInt64 and typedefs it to integer_type_name.
//
// Note: The struct integer_type_name ## _integer_tag_ trickery is needed to
// ensure that a new type is created per integer_type_name.
#define DEFINE_STRONG_INT64_TYPE(integer_type_name) \
struct integer_type_name##_integer_tag_ { \
static constexpr absl::string_view TypeName() { \
return #integer_type_name; \
} \
}; \
typedef ::operations_research::StrongInt64<integer_type_name##_integer_tag_> \
integer_type_name;
// ----------- Implementation ------------
// Note: we need two classes as it is the only way to have different set of
// operators on each class.
// Note: We use to class to easily define a different set of operators for the
// index and int64_t type.
#define STRONG_ASSIGNMENT_OP(StrongClass, IntType, op) \
ThisType& operator op(const ThisType& arg_value) { \
value_ op arg_value.value(); \
return *this; \
} \
ThisType& operator op(IntType arg_value) { \
value_ op arg_value; \
return *this; \
}
#define INCREMENT_AND_DECREMENT_OPERATORS \
ThisType& operator++() { \
++value_; \
return *this; \
} \
const ThisType operator++(int v) { \
ThisType temp(*this); \
++value_; \
return temp; \
} \
ThisType& operator--() { \
--value_; \
return *this; \
} \
const ThisType operator--(int v) { \
ThisType temp(*this); \
--value_; \
return temp; \
}
// Holds an int value and behaves as an int by exposing assignment,
// unary, comparison, and arithmetic operators.
//
// The template parameter StrongIndexName defines the name for the int type and
// must be unique within a binary (the convenient DEFINE_STRONG_INDEX_TYPE macro
// at the start of the file generates a unique StrongIndexName).
//
// This class is NOT thread-safe.
template <typename StrongIndexName>
class StrongIndex {
public:
typedef int ValueType; // Needed for StrongVector.
typedef StrongIndex<StrongIndexName> ThisType; // Syntactic sugar.
static constexpr absl::string_view TypeName() {
return StrongIndexName::TypeName();
}
struct ABSL_DEPRECATED("Use absl::Hash instead") Hasher {
size_t operator()(const StrongIndex& x) const {
return static_cast<size_t>(x.value());
}
};
constexpr StrongIndex() : value_(0) {}
// NOLINTBEGIN(google-explicit-constructor)
constexpr StrongIndex(int value) : value_(value) {}
// NOLINTEND(google-explicit-constructor)
StrongIndex& operator=(int arg_value) {
value_ = arg_value;
return *this;
}
// The class provides a value() accessor returning the stored int value_
// as well as a templatized accessor that is just a syntactic sugar for
// static_cast<T>(var.value());
constexpr int value() const { return value_; }
template <typename ValType> // Needed for StrongVector.
constexpr ValType value() const {
return static_cast<ValType>(value_);
}
constexpr const ThisType operator+() const { return ThisType(value_); }
constexpr const ThisType operator-() const { return ThisType(-value_); }
INCREMENT_AND_DECREMENT_OPERATORS;
STRONG_ASSIGNMENT_OP(StrongIndex, int, +=);
STRONG_ASSIGNMENT_OP(StrongIndex, int, -=);
private:
int value_;
};
// Holds an int64_t value and behaves as an int64_t by exposing assignment,
// unary, comparison, and arithmetic operators.
//
// The template parameter StrongIntegerName defines the name for the int type
// and must be unique within a binary (the convenient DEFINE_STRONG_INTEGER_TYPE
// macro at the start of the file generates a unique StrongIntegerName).
//
// This class is NOT thread-safe.
template <typename StrongIntegerName>
class StrongInt64 {
public:
typedef int64_t ValueType; // Needed for StrongVector.
typedef StrongInt64<StrongIntegerName> ThisType; // Syntactic sugar.
static constexpr absl::string_view TypeName() {
return StrongIntegerName::TypeName();
}
struct ABSL_DEPRECATED("Use absl::Hash instead") Hasher {
size_t operator()(const StrongInt64& x) const {
return static_cast<size_t>(x.value());
}
};
constexpr StrongInt64() : value_(0) {}
// NOLINTBEGIN(google-explicit-constructor)
constexpr StrongInt64(int64_t value) : value_(value) {}
// NOLINTEND(google-explicit-constructor)
StrongInt64& operator=(int64_t arg_value) {
value_ = arg_value;
return *this;
}
constexpr int64_t value() const { return value_; }
template <typename ValType> // Needed for StrongVector.
constexpr ValType value() const {
return static_cast<ValType>(value_);
}
INCREMENT_AND_DECREMENT_OPERATORS;
constexpr const ThisType operator+() const { return ThisType(value_); }
constexpr const ThisType operator-() const { return ThisType(-value_); }
constexpr const ThisType operator~() const { return ThisType(~value_); }
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, +=);
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, -=);
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, *=);
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, /=);
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, <<=);
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, >>=);
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, %=);
private:
int64_t value_;
};
#undef STRONG_ASSIGNMENT_OP
#undef INCREMENT_AND_DECREMENT_OPERATORS
// -- NON-MEMBER STREAM OPERATORS ----------------------------------------------
// We provide the << operator, primarily for logging purposes. Currently, there
// seems to be no need for an >> operator.
template <typename StrongIndexName>
std::ostream& operator<<(std::ostream& os, // NOLINT
StrongIndex<StrongIndexName> arg) {
return os << arg.value();
}
template <typename StrongIntegerName>
std::ostream& operator<<(std::ostream& os, // NOLINT
StrongInt64<StrongIntegerName> arg) {
return os << arg.value();
}
// -- NON-MEMBER ARITHMETIC OPERATORS ------------------------------------------
#define STRONG_TYPE_ARITHMETIC_OP(StrongType, IntType, op) \
template <typename StrongName> \
constexpr StrongType<StrongName> operator op(StrongType<StrongName> id_1, \
StrongType<StrongName> id_2) { \
return StrongType<StrongName>(id_1.value() op id_2.value()); \
} \
template <typename StrongName> \
constexpr StrongType<StrongName> operator op(StrongType<StrongName> id, \
IntType arg_val) { \
return StrongType<StrongName>(id.value() op arg_val); \
} \
template <typename StrongName> \
constexpr StrongType<StrongName> operator op(IntType arg_val, \
StrongType<StrongName> id) { \
return StrongType<StrongName>(arg_val op id.value()); \
}
STRONG_TYPE_ARITHMETIC_OP(StrongIndex, int, +);
STRONG_TYPE_ARITHMETIC_OP(StrongIndex, int, -);
STRONG_TYPE_ARITHMETIC_OP(StrongIndex, int, *);
STRONG_TYPE_ARITHMETIC_OP(StrongIndex, int, %);
STRONG_TYPE_ARITHMETIC_OP(StrongInt64, int64_t, +);
STRONG_TYPE_ARITHMETIC_OP(StrongInt64, int64_t, -);
STRONG_TYPE_ARITHMETIC_OP(StrongInt64, int64_t, *);
STRONG_TYPE_ARITHMETIC_OP(StrongInt64, int64_t, /);
STRONG_TYPE_ARITHMETIC_OP(StrongInt64, int64_t, <<);
STRONG_TYPE_ARITHMETIC_OP(StrongInt64, int64_t, >>);
STRONG_TYPE_ARITHMETIC_OP(StrongInt64, int64_t, %);
#undef STRONG_TYPE_ARITHMETIC_OP
// -- NON-MEMBER COMPARISON OPERATORS ------------------------------------------
#define STRONG_TYPE_COMPARISON_OP(StrongType, IntType, op) \
template <typename StrongName> \
static inline constexpr bool operator op(StrongType<StrongName> id_1, \
StrongType<StrongName> id_2) { \
return id_1.value() op id_2.value(); \
} \
template <typename StrongName> \
static inline constexpr bool operator op(StrongType<StrongName> id, \
IntType val) { \
return id.value() op val; \
} \
template <typename StrongName> \
static inline constexpr bool operator op(IntType val, \
StrongType<StrongName> id) { \
return val op id.value(); \
}
STRONG_TYPE_COMPARISON_OP(StrongIndex, int, ==); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongIndex, int, !=); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongIndex, int, <); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongIndex, int, <=); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongIndex, int, >); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongIndex, int, >=); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongInt64, int64_t, ==); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongInt64, int64_t, !=); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongInt64, int64_t, <); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongInt64, int64_t, <=); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongInt64, int64_t, >); // NOLINT
STRONG_TYPE_COMPARISON_OP(StrongInt64, int64_t, >=); // NOLINT
#undef STRONG_TYPE_COMPARISON_OP
// -- ABSL HASHING SUPPORT -----------------------------------------------------
template <typename StrongIndexName, typename H>
H AbslHashValue(H h, const StrongIndex<StrongIndexName>& i) {
return H::combine(std::move(h), i.value());
}
template <typename StrongIntegerName, typename H>
H AbslHashValue(H h, const StrongInt64<StrongIntegerName>& i) {
return H::combine(std::move(h), i.value());
}
} // namespace operations_research
// -- STD HASHING SUPPORT -----------------------------------------------------
namespace std {
template <typename Tag>
struct hash<operations_research::StrongIndex<Tag> >
: ::operations_research::StrongIndex<Tag>::Hasher {};
template <typename Tag>
struct hash<operations_research::StrongInt64<Tag> >
: ::operations_research::StrongInt64<Tag>::Hasher {};
} // namespace std
#endif // OR_TOOLS_UTIL_STRONG_INTEGERS_H_