14"""helpers methods for the cp_model module."""
17from typing
import Any, Union
21INT_MIN = -9223372036854775808
22INT_MAX = 9223372036854775807
23INT32_MIN = -2147483648
28 """Checks if the x is a boolean."""
29 if isinstance(x, bool):
31 if isinstance(x, np.bool_):
37 """Checks if the x is 0 or 0.0."""
38 if isinstance(x, numbers.Integral):
40 if isinstance(x, numbers.Real):
41 return float(x) == 0.0
46 """Checks if x is 1 or 1.0."""
47 if isinstance(x, numbers.Integral):
49 if isinstance(x, numbers.Real):
50 return float(x) == 1.0
55 """Checks if x is -1 or -1.0 ."""
56 if isinstance(x, numbers.Integral):
58 if isinstance(x, numbers.Real):
59 return float(x) == -1.0
64 """Asserts that x is integer and x is in [min_int_64, max_int_64] and returns it casted to an int."""
65 if not isinstance(x, numbers.Integral):
66 raise TypeError(f
"Not an integer: {x} of type {type(x)}")
68 if x_as_int < INT_MIN
or x_as_int > INT_MAX:
69 raise OverflowError(f
"Does not fit in an int64_t: {x}")
74 """Asserts that x is integer and x is in [min_int_32, max_int_32] and returns it casted to an int."""
75 if not isinstance(x, numbers.Integral):
76 raise TypeError(f
"Not an integer: {x} of type {type(x)}")
78 if x_as_int < INT32_MIN
or x_as_int > INT32_MAX:
79 raise OverflowError(f
"Does not fit in an int32_t: {x}")
84 """Asserts that x is 0 or 1 and returns it as an int."""
85 if not isinstance(x, numbers.Integral):
86 raise TypeError(f
"Not a boolean: {x} of type {type(x)}")
88 if x_as_int < 0
or x_as_int > 1:
89 raise TypeError(f
"Not a boolean: {x}")
94 """Asserts that x is a number and returns it casted to an int or a float."""
95 if isinstance(x, numbers.Integral):
97 if isinstance(x, numbers.Real):
99 raise TypeError(f
"Not a number: {x} of type {type(x)}")
103 """Restrict v within [INT_MIN..INT_MAX] range."""
112 """Saturated arithmetics. Returns x - y truncated to the int64_t range."""
118 if x == INT_MAX
or x == INT_MIN:
119 raise OverflowError(
"Integer NaN: subtracting INT_MAX or INT_MIN to itself")
121 if x == INT_MAX
or x == INT_MIN: