145#ifndef OR_TOOLS_BASE_INT_TYPE_H_
146#define OR_TOOLS_BASE_INT_TYPE_H_
152#include <type_traits>
154#include "absl/base/attributes.h"
155#include "absl/strings/string_view.h"
159template <
typename IntTypeName,
typename _ValueType>
165#define DEFINE_INT_TYPE(int_type_name, value_type) \
166 struct int_type_name##_tag_ { \
167 static constexpr absl::string_view TypeName() { return #int_type_name; } \
169 typedef ::gtl::IntType<int_type_name##_tag_, value_type> int_type_name;
181template <
typename IntTypeName,
typename _ValueType>
188 return IntTypeName::TypeName();
195 return static_cast<size_t>(arg.
value());
199 template <
typename H>
201 return H::combine(std::move(h), i.value());
220 template <
typename ValType>
222 return static_cast<ValType
>(value_);
245 constexpr bool operator!()
const {
return value_ == 0; }
253#define INT_TYPE_ASSIGNMENT_OP(op) \
254 ThisType& operator op(const ThisType& arg_value) { \
255 value_ op arg_value.value(); \
258 ThisType& operator op(ValueType arg_value) { \
259 value_ op arg_value; \
269#undef INT_TYPE_ASSIGNMENT_OP
284template <
typename IntTypeName,
typename ValueType>
287 return os << arg.
value();
297#define INT_TYPE_ARITHMETIC_OP(op) \
298 template <typename IntTypeName, typename ValueType> \
299 constexpr IntType<IntTypeName, ValueType> operator op( \
300 IntType<IntTypeName, ValueType> id_1, \
301 IntType<IntTypeName, ValueType> id_2) { \
302 return IntType<IntTypeName, ValueType>(id_1.value() op id_2.value()); \
304 template <typename IntTypeName, typename ValueType> \
305 constexpr IntType<IntTypeName, ValueType> operator op( \
306 IntType<IntTypeName, ValueType> id, \
307 typename IntType<IntTypeName, ValueType>::ValueType arg_val) { \
308 return IntType<IntTypeName, ValueType>(id.value() op arg_val); \
310 template <typename IntTypeName, typename ValueType> \
311 constexpr IntType<IntTypeName, ValueType> operator op( \
312 typename IntType<IntTypeName, ValueType>::ValueType arg_val, \
313 IntType<IntTypeName, ValueType> id) { \
314 return IntType<IntTypeName, ValueType>(arg_val op id.value()); \
323#undef INT_TYPE_ARITHMETIC_OP
331#define INT_TYPE_COMPARISON_OP(op) \
332 template <typename IntTypeName, typename ValueType> \
333 static inline constexpr bool operator op( \
334 IntType<IntTypeName, ValueType> id_1, \
335 IntType<IntTypeName, ValueType> id_2) { \
336 return id_1.value() op id_2.value(); \
338 template <typename IntTypeName, typename ValueType> \
339 static inline constexpr bool operator op( \
340 IntType<IntTypeName, ValueType> id, \
341 typename IntType<IntTypeName, ValueType>::ValueType val) { \
342 return id.value() op val; \
344 template <typename IntTypeName, typename ValueType> \
345 static inline constexpr bool operator op( \
346 typename IntType<IntTypeName, ValueType>::ValueType val, \
347 IntType<IntTypeName, ValueType> id) { \
348 return val op id.value(); \
356#undef INT_TYPE_COMPARISON_OP
const ThisType operator++(int v)
constexpr const ThisType operator~() const
constexpr ValType value() const
INT_TYPE_ASSIGNMENT_OP(+=)
constexpr const ThisType operator-() const
INT_TYPE_ASSIGNMENT_OP * INT_TYPE_ASSIGNMENT_OP(/=);INT_TYPE_ASSIGNMENT_OP(<<=
INT_TYPE_ASSIGNMENT_OP(%=)
static constexpr absl::string_view TypeName()
INT_TYPE_ASSIGNMENT_OP(> >=)
constexpr ValueType value() const
constexpr bool operator!() const
friend H AbslHashValue(H h, const IntType &i)
const ThisType operator--(int v)
constexpr const ThisType operator+() const
ThisType & operator++()
– UNARY OPERATORS -----------------------------------------------------—
constexpr IntType(ValueType value)
C'tor explicitly initializing from a ValueType.
constexpr IntType()
Default constructor initializing value_ to 0.
ThisType & operator=(ValueType arg_value)
IntType< IntTypeName, ValueType > ThisType
INT_TYPE_ASSIGNMENT_OP(-=)
#define INT_TYPE_ASSIGNMENT_OP(op)
#define INT_TYPE_ARITHMETIC_OP(op)
INT_TYPE_COMPARISON_OP(==)
std::ostream & operator<<(std::ostream &os, IntType< IntTypeName, ValueType > arg)
class gtl::IntType ABSL_ATTRIBUTE_PACKED
size_t operator()(const IntType &arg) const