50#ifndef OR_TOOLS_UTIL_STRONG_INTEGERS_H_
51#define OR_TOOLS_UTIL_STRONG_INTEGERS_H_
60#include "absl/base/attributes.h"
61#include "absl/base/port.h"
62#include "absl/strings/str_format.h"
63#include "absl/strings/string_view.h"
71#define DEFINE_STRONG_INDEX_TYPE(index_type_name) \
72 struct index_type_name##_index_tag_ { \
73 static constexpr absl::string_view TypeName() { return #index_type_name; } \
75 typedef ::operations_research::StrongIndex<index_type_name##_index_tag_> \
82#define DEFINE_STRONG_INT64_TYPE(integer_type_name) \
83 struct integer_type_name##_integer_tag_ { \
84 static constexpr absl::string_view TypeName() { \
85 return #integer_type_name; \
88 typedef ::operations_research::StrongInt64<integer_type_name##_integer_tag_> \
98#define STRONG_ASSIGNMENT_OP(StrongClass, IntType, op) \
99 ThisType& operator op(const ThisType & arg_value) { \
100 value_ op arg_value.value(); \
103 ThisType& operator op(IntType arg_value) { \
104 value_ op arg_value; \
108#define INCREMENT_AND_DECREMENT_OPERATORS \
109 ThisType& operator++() { \
113 const ThisType operator++(int) { \
114 ThisType temp(*this); \
118 ThisType& operator--() { \
122 const ThisType operator--(int) { \
123 ThisType temp(*this); \
136template <
typename StrongIndexName>
143 return StrongIndexName::TypeName();
148 return static_cast<size_t>(
x.value());
164 constexpr int value()
const {
return value_; }
166 template <
typename ValType>
168 return static_cast<ValType
>(value_);
191template <
typename StrongIntegerName>
198 return StrongIntegerName::TypeName();
203 return static_cast<size_t>(
x.value());
218 constexpr int64_t
value()
const {
return value_; }
220 template <
typename ValType>
222 return static_cast<ValType
>(value_);
243#undef STRONG_ASSIGNMENT_OP
244#undef INCREMENT_AND_DECREMENT_OPERATORS
249template <
typename StrongIndexName>
252 return os << arg.
value();
255template <
typename Sink,
typename... T>
257 absl::Format(&sink,
"%v", arg.
value());
260template <
typename StrongIntegerName>
263 return os << arg.
value();
266template <
typename Sink,
typename... T>
268 absl::Format(&sink,
"%v", arg.
value());
272#define STRONG_TYPE_ARITHMETIC_OP(StrongType, IntType, op) \
273 template <typename StrongName> \
274 constexpr StrongType<StrongName> operator op(StrongType<StrongName> id_1, \
275 StrongType<StrongName> id_2) { \
276 return StrongType<StrongName>(id_1.value() op id_2.value()); \
278 template <typename StrongName> \
279 constexpr StrongType<StrongName> operator op(StrongType<StrongName> id, \
281 return StrongType<StrongName>(id.value() op arg_val); \
283 template <typename StrongName> \
284 constexpr StrongType<StrongName> operator op(IntType arg_val, \
285 StrongType<StrongName> id) { \
286 return StrongType<StrongName>(arg_val op id.value()); \
301#undef STRONG_TYPE_ARITHMETIC_OP
304#define STRONG_TYPE_COMPARISON_OP(StrongType, IntType, op) \
305 template <typename StrongName> \
306 static inline constexpr bool operator op(StrongType<StrongName> id_1, \
307 StrongType<StrongName> id_2) { \
308 return id_1.value() op id_2.value(); \
310 template <typename StrongName> \
311 static inline constexpr bool operator op(StrongType<StrongName> id, \
313 return id.value() op val; \
315 template <typename StrongName> \
316 static inline constexpr bool operator op(IntType val, \
317 StrongType<StrongName> id) { \
318 return val op id.value(); \
334#undef STRONG_TYPE_COMPARISON_OP
337template <
typename StrongIndexName,
typename H>
339 return H::combine(std::move(h), i.value());
342template <
typename StrongIntegerName,
typename H>
344 return H::combine(std::move(h), i.value());
351template <
typename Tag>
355template <
typename Tag>
363template <
typename Tag>
371 static constexpr bool is_specialized =
true;
372 static constexpr bool is_signed = numeric_limits<NativeTypeT>::is_signed;
373 static constexpr bool is_integer = numeric_limits<NativeTypeT>::is_integer;
374 static constexpr bool is_exact = numeric_limits<NativeTypeT>::is_exact;
375 static constexpr bool has_infinity =
376 numeric_limits<NativeTypeT>::has_infinity;
377 static constexpr bool has_quiet_NaN =
378 numeric_limits<NativeTypeT>::has_quiet_NaN;
379 static constexpr bool has_signaling_NaN =
380 numeric_limits<NativeTypeT>::has_signaling_NaN;
381 static constexpr float_denorm_style has_denorm =
382 numeric_limits<NativeTypeT>::has_denorm;
383 static constexpr bool has_denorm_loss =
384 numeric_limits<NativeTypeT>::has_denorm_loss;
385 static constexpr float_round_style round_style =
386 numeric_limits<NativeTypeT>::round_style;
387 static constexpr bool is_iec559 = numeric_limits<NativeTypeT>::is_iec559;
388 static constexpr bool is_bounded = numeric_limits<NativeTypeT>::is_bounded;
389 static constexpr bool is_modulo = numeric_limits<NativeTypeT>::is_modulo;
390 static constexpr int digits = numeric_limits<NativeTypeT>::digits;
391 static constexpr int digits10 = numeric_limits<NativeTypeT>::digits10;
392 static constexpr int max_digits10 = numeric_limits<NativeTypeT>::max_digits10;
393 static constexpr int radix = numeric_limits<NativeTypeT>::radix;
394 static constexpr int min_exponent = numeric_limits<NativeTypeT>::min_exponent;
395 static constexpr int min_exponent10 =
396 numeric_limits<NativeTypeT>::min_exponent10;
397 static constexpr int max_exponent = numeric_limits<NativeTypeT>::max_exponent;
398 static constexpr int max_exponent10 =
399 numeric_limits<NativeTypeT>::max_exponent10;
400 static constexpr bool traps = numeric_limits<NativeTypeT>::traps;
401 static constexpr bool tinyness_before =
402 numeric_limits<NativeTypeT>::tinyness_before;
406 return StrongIntT(numeric_limits<NativeTypeT>::min());
409 return StrongIntT(numeric_limits<NativeTypeT>::lowest());
412 return StrongIntT(numeric_limits<NativeTypeT>::max());
422template <
typename Tag>
430 static constexpr bool is_specialized =
true;
431 static constexpr bool is_signed = numeric_limits<NativeTypeT>::is_signed;
432 static constexpr bool is_integer = numeric_limits<NativeTypeT>::is_integer;
433 static constexpr bool is_exact = numeric_limits<NativeTypeT>::is_exact;
434 static constexpr bool has_infinity =
435 numeric_limits<NativeTypeT>::has_infinity;
436 static constexpr bool has_quiet_NaN =
437 numeric_limits<NativeTypeT>::has_quiet_NaN;
438 static constexpr bool has_signaling_NaN =
439 numeric_limits<NativeTypeT>::has_signaling_NaN;
440 static constexpr float_denorm_style has_denorm =
441 numeric_limits<NativeTypeT>::has_denorm;
442 static constexpr bool has_denorm_loss =
443 numeric_limits<NativeTypeT>::has_denorm_loss;
444 static constexpr float_round_style round_style =
445 numeric_limits<NativeTypeT>::round_style;
446 static constexpr bool is_iec559 = numeric_limits<NativeTypeT>::is_iec559;
447 static constexpr bool is_bounded = numeric_limits<NativeTypeT>::is_bounded;
448 static constexpr bool is_modulo = numeric_limits<NativeTypeT>::is_modulo;
449 static constexpr int digits = numeric_limits<NativeTypeT>::digits;
450 static constexpr int digits10 = numeric_limits<NativeTypeT>::digits10;
451 static constexpr int max_digits10 = numeric_limits<NativeTypeT>::max_digits10;
452 static constexpr int radix = numeric_limits<NativeTypeT>::radix;
453 static constexpr int min_exponent = numeric_limits<NativeTypeT>::min_exponent;
454 static constexpr int min_exponent10 =
455 numeric_limits<NativeTypeT>::min_exponent10;
456 static constexpr int max_exponent = numeric_limits<NativeTypeT>::max_exponent;
457 static constexpr int max_exponent10 =
458 numeric_limits<NativeTypeT>::max_exponent10;
459 static constexpr bool traps = numeric_limits<NativeTypeT>::traps;
460 static constexpr bool tinyness_before =
461 numeric_limits<NativeTypeT>::tinyness_before;
465 return StrongIntT(numeric_limits<NativeTypeT>::min());
468 return StrongIntT(numeric_limits<NativeTypeT>::lowest());
471 return StrongIntT(numeric_limits<NativeTypeT>::max());
StrongIndex & operator=(int arg_value)
STRONG_ASSIGNMENT_OP(StrongIndex, int,+=)
struct ABSL_DEPRECATED("Use absl::Hash instead") Hasher
constexpr ThisType operator-() const
INCREMENT_AND_DECREMENT_OPERATORS
constexpr ThisType operator+() const
constexpr StrongIndex(int value)
constexpr int value() const
constexpr ValType value() const
StrongIndex< StrongIndexName > ThisType
STRONG_ASSIGNMENT_OP(StrongIndex, int, -=)
static constexpr absl::string_view TypeName()
constexpr int64_t value() const
constexpr ValType value() const
constexpr StrongInt64(int64_t value)
NOLINTBEGIN(google-explicit-constructor)
constexpr ThisType operator-() const
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, *=)
static constexpr absl::string_view TypeName()
constexpr ThisType operator~() const
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t,+=)
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t,<<=)
INCREMENT_AND_DECREMENT_OPERATORS
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, -=)
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, %=)
struct ABSL_DEPRECATED("Use absl::Hash instead") Hasher
constexpr ThisType operator+() const
StrongInt64 & operator=(int64_t arg_value)
NOLINTEND(google-explicit-constructor)
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t, > >=)
STRONG_ASSIGNMENT_OP(StrongInt64, int64_t,/=)
StrongInt64< StrongIntegerName > ThisType
In SWIG mode, we don't want anything besides these top-level includes.
H AbslHashValue(H h, const StrongIndex< StrongIndexName > &i)
– ABSL HASHING SUPPORT --------------------------------------------------—
std::ostream & operator<<(std::ostream &out, const Assignment &assignment)
void AbslStringify(Sink &sink, StrongIndex< T... > arg)
#define STRONG_TYPE_COMPARISON_OP(StrongType, IntType, op)
– NON-MEMBER COMPARISON OPERATORS ---------------------------------------—
#define STRONG_TYPE_ARITHMETIC_OP(StrongType, IntType, op)
– NON-MEMBER ARITHMETIC OPERATORS ---------------------------------------—
static constexpr StrongIntT denorm_min()
static constexpr StrongIntT epsilon()
static constexpr StrongIntT signaling_NaN()
static constexpr StrongIntT round_error()
static constexpr StrongIntT infinity()
static constexpr StrongIntT quiet_NaN()
static constexpr StrongIntT lowest()
static constexpr StrongIntT epsilon()
static constexpr StrongIntT denorm_min()
static constexpr StrongIntT lowest()
static constexpr StrongIntT infinity()
static constexpr StrongIntT signaling_NaN()
static constexpr StrongIntT round_error()
static constexpr StrongIntT quiet_NaN()