50 #ifndef OR_TOOLS_UTIL_STRONG_INTEGERS_H_
51 #define OR_TOOLS_UTIL_STRONG_INTEGERS_H_
59 #include <type_traits>
61 #include "absl/base/port.h"
62 #include "absl/strings/string_view.h"
72 #define DEFINE_STRONG_INDEX_TYPE(index_type_name) \
73 struct index_type_name##_index_tag_ { \
74 static constexpr absl::string_view TypeName() { return #index_type_name; } \
76 typedef ::operations_research::StrongIndex<index_type_name##_index_tag_> \
83 #define DEFINE_STRONG_INT64_TYPE(integer_type_name) \
84 struct integer_type_name##_integer_tag_ { \
85 static constexpr absl::string_view TypeName() { \
86 return #integer_type_name; \
89 typedef ::operations_research::StrongInt64<integer_type_name##_integer_tag_> \
99 #define STRONG_ASSIGNMENT_OP(StrongClass, IntType, op) \
100 ThisType& operator op(const ThisType& arg_value) { \
101 value_ op arg_value.value(); \
104 ThisType& operator op(IntType arg_value) { \
105 value_ op arg_value; \
109 #define INCREMENT_AND_DECREMENT_OPERATORS \
110 ThisType& operator++() { \
114 const ThisType operator++(int) { \
115 ThisType temp(*this); \
119 ThisType& operator--() { \
123 const ThisType operator--(int) { \
124 ThisType temp(*this); \
137 template <
typename StrongIndexName>
144 return StrongIndexName::TypeName();
149 return static_cast<size_t>(x.
value());
165 constexpr
int value()
const {
return value_; }
167 template <
typename ValType>
169 return static_cast<ValType
>(value_);
192 template <
typename StrongIntegerName>
199 return StrongIntegerName::TypeName();
204 return static_cast<size_t>(x.
value());
219 constexpr int64_t
value()
const {
return value_; }
221 template <
typename ValType>
223 return static_cast<ValType
>(value_);
244 #undef STRONG_ASSIGNMENT_OP
245 #undef INCREMENT_AND_DECREMENT_OPERATORS
250 template <
typename StrongIndexName>
253 return os << arg.
value();
256 template <
typename StrongIntegerName>
259 return os << arg.
value();
263 #define STRONG_TYPE_ARITHMETIC_OP(StrongType, IntType, op) \
264 template <typename StrongName> \
265 constexpr StrongType<StrongName> operator op(StrongType<StrongName> id_1, \
266 StrongType<StrongName> id_2) { \
267 return StrongType<StrongName>(id_1.value() op id_2.value()); \
269 template <typename StrongName> \
270 constexpr StrongType<StrongName> operator op(StrongType<StrongName> id, \
272 return StrongType<StrongName>(id.value() op arg_val); \
274 template <typename StrongName> \
275 constexpr StrongType<StrongName> operator op(IntType arg_val, \
276 StrongType<StrongName> id) { \
277 return StrongType<StrongName>(arg_val op id.value()); \
292 #undef STRONG_TYPE_ARITHMETIC_OP
295 #define STRONG_TYPE_COMPARISON_OP(StrongType, IntType, op) \
296 template <typename StrongName> \
297 static inline constexpr bool operator op(StrongType<StrongName> id_1, \
298 StrongType<StrongName> id_2) { \
299 return id_1.value() op id_2.value(); \
301 template <typename StrongName> \
302 static inline constexpr bool operator op(StrongType<StrongName> id, \
304 return id.value() op val; \
306 template <typename StrongName> \
307 static inline constexpr bool operator op(IntType val, \
308 StrongType<StrongName> id) { \
309 return val op id.value(); \
325 #undef STRONG_TYPE_COMPARISON_OP
328 template <
typename StrongIndexName,
typename H>
330 return H::combine(std::move(h), i.
value());
333 template <
typename StrongIntegerName,
typename H>
335 return H::combine(std::move(h), i.
value());
342 template <
typename Tag>
346 template <
typename Tag>
STRONG_ASSIGNMENT_OP(StrongIndex, int, -=)
struct ABSL_DEPRECATED("Use absl::Hash instead") Hasher
StrongIndex< StrongIndexName > ThisType
constexpr StrongIndex(int value)
INCREMENT_AND_DECREMENT_OPERATORS
constexpr const ThisType operator+() const
constexpr int value() const
STRONG_ASSIGNMENT_OP(StrongIndex, int,+=)
constexpr const ThisType operator-() const
constexpr ValType value() const
StrongIndex & operator=(int arg_value)
static constexpr absl::string_view TypeName()
StrongInt64 & operator=(int64_t arg_value)
struct ABSL_DEPRECATED("Use absl::Hash instead") Hasher
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, *=)
StrongInt64< StrongIntegerName > ThisType
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t,+=)
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, >>=)
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t,<<=)
INCREMENT_AND_DECREMENT_OPERATORS
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t,/=)
constexpr const ThisType operator+() const
constexpr const ThisType operator~() const
constexpr const ThisType operator-() const
constexpr ValType value() const
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, -=)
constexpr int64_t value() const
constexpr StrongInt64(int64_t value)
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, %=)
static constexpr absl::string_view TypeName()
Collection of objects used to extend the Constraint Solver library.
H AbslHashValue(H h, const StrongIndex< StrongIndexName > &i)
std::ostream & operator<<(std::ostream &out, const Assignment &assignment)
STRONG_TYPE_ARITHMETIC_OP(StrongIndex, int,+)
STRONG_TYPE_COMPARISON_OP(StrongIndex, int,==)
uint64_t Hash(uint64_t num, uint64_t c)