Skip to content

Commit 3d0d015

Browse files
committed
base: fix map_util and mathutil
1 parent a1fc6be commit 3d0d015

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

ortools/base/map_util.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,23 @@
1919
#include "ortools/base/logging.h"
2020

2121
namespace gtl {
22+
template <typename M>
23+
using MapUtilValueT = typename M::value_type;
24+
template <typename M>
25+
using MapUtilKeyT = typename MapUtilValueT<M>::first_type;
26+
template <typename M>
27+
using MapUtilMappedT = typename MapUtilValueT<M>::second_type;
28+
2229
// Perform a lookup in a std::map or std::unordered_map.
2330
// If the key is present in the map then the value associated with that
2431
// key is returned, otherwise the value passed as a default is returned.
2532
//
2633
// Prefer the two-argument form unless you need to specify a custom default
2734
// value (i.e., one that is not equal to a value-initialized instance).
28-
template <class Collection>
29-
const typename Collection::value_type::second_type& FindWithDefault(
30-
const Collection& collection,
31-
const typename Collection::value_type::first_type& key,
32-
const typename Collection::value_type::second_type& value) {
35+
template <typename Collection, typename KeyType = MapUtilKeyT<Collection>>
36+
const MapUtilMappedT<Collection>& FindWithDefault(
37+
const Collection& collection, const KeyType& key,
38+
const MapUtilMappedT<Collection>& value) {
3339
typename Collection::const_iterator it = collection.find(key);
3440
if (it == collection.end()) {
3541
return value;
@@ -40,12 +46,11 @@ const typename Collection::value_type::second_type& FindWithDefault(
4046
// Returns a const reference to the value associated with the given key if it
4147
// exists, otherwise returns a const reference to a value-initialized object
4248
// that is never destroyed.
43-
template <class Collection>
44-
const typename Collection::value_type::second_type& FindWithDefault(
45-
const Collection& collection,
46-
const typename Collection::value_type::first_type& key) {
47-
static const typename Collection::value_type::second_type* const
48-
default_value = new typename Collection::value_type::second_type{};
49+
template <class Collection, typename KeyType = MapUtilKeyT<Collection>>
50+
const MapUtilMappedT<Collection>& FindWithDefault(const Collection& collection,
51+
const KeyType& key) {
52+
static const MapUtilMappedT<Collection>* const default_value =
53+
new MapUtilMappedT<Collection>{};
4954
typename Collection::const_iterator it = collection.find(key);
5055
if (it == collection.end()) {
5156
return *default_value;

ortools/base/mathutil.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <algorithm>
2020
#include <cstdint>
21+
#include <limits>
2122
#include <vector>
2223

2324
#include "absl/base/casts.h"
@@ -141,7 +142,7 @@ class MathUtil {
141142
// rounding_value must be greater than zero.
142143
template <typename IntType>
143144
static IntType RoundUpTo(IntType input_value, IntType rounding_value) {
144-
static_assert(MathLimits<IntType>::kIsInteger,
145+
static_assert(std::numeric_limits<IntType>::is_integer,
145146
"RoundUpTo() operation type is not integer");
146147
DCHECK_GE(input_value, 0);
147148
DCHECK_GT(rounding_value, 0);
@@ -161,19 +162,21 @@ class MathUtil {
161162
// 1. If x is NaN, the result is zero.
162163
//
163164
// 2. If the truncated form of x is above the representable range of IntOut,
164-
// the result is MathLimits<IntOut>::kMax.
165+
// the result is std::numeric_limits<IntOut>::max().
165166
//
166167
// 3. If the truncated form of x is below the representable range of IntOut,
167-
// the result is MathLimits<IntOut>::kMin.
168+
// the result is std::numeric_limits<IntOut>::lowest().
168169
//
169170
// Note that cases #2 and #3 cover infinities as well as finite numbers.
170171
//
171172
// The range of FloatIn must include the range of IntOut, otherwise
172173
// the results are undefined.
173174
template <class IntOut, class FloatIn>
174175
static IntOut SafeCast(FloatIn x) {
175-
COMPILE_ASSERT(!MathLimits<FloatIn>::kIsInteger, FloatIn_is_integer);
176-
COMPILE_ASSERT(MathLimits<IntOut>::kIsInteger, IntOut_is_not_integer);
176+
COMPILE_ASSERT(!std::numeric_limits<FloatIn>::is_integer,
177+
FloatIn_is_integer);
178+
COMPILE_ASSERT(std::numeric_limits<IntOut>::is_integer,
179+
IntOut_is_not_integer);
177180
COMPILE_ASSERT(std::numeric_limits<IntOut>::radix == 2, IntOut_is_base_2);
178181

179182
// Special case NaN, for which the logic below doesn't work.
@@ -182,13 +185,14 @@ class MathUtil {
182185
}
183186

184187
// Negative values all clip to zero for unsigned results.
185-
if (!MathLimits<IntOut>::kIsSigned && x < 0) {
188+
if (!std::numeric_limits<IntOut>::is_signed && x < 0) {
186189
return 0;
187190
}
188191

189192
// Handle infinities.
190193
if (MathLimits<FloatIn>::IsInf(x)) {
191-
return x < 0 ? MathLimits<IntOut>::kMin : MathLimits<IntOut>::kMax;
194+
return x < 0 ? std::numeric_limits<IntOut>::lowest()
195+
: std::numeric_limits<IntOut>::max();
192196
}
193197

194198
// Set exp such that x == f * 2^exp for some f with |f| in [0.5, 1.0),
@@ -207,7 +211,8 @@ class MathUtil {
207211
}
208212

209213
// Handle numbers with magnitude >= 2^N.
210-
return x < 0 ? MathLimits<IntOut>::kMin : MathLimits<IntOut>::kMax;
214+
return x < 0 ? std::numeric_limits<IntOut>::lowest()
215+
: std::numeric_limits<IntOut>::max();
211216
}
212217

213218
// --------------------------------------------------------------------
@@ -218,15 +223,17 @@ class MathUtil {
218223
// return type. In those cases, Round has undefined
219224
// behavior. SafeRound returns 0 when the argument is
220225
// NaN, and returns the closest possible integer value otherwise (i.e.
221-
// MathLimits<IntOut>::kMax for large positive values, and
222-
// MathLimits<IntOut>::kMin for large negative values).
226+
// std::numeric_limits<IntOut>::max() for large positive values, and
227+
// std::numeric_limits<IntOut>::lowest() for large negative values).
223228
// The range of FloatIn must include the range of IntOut, otherwise
224229
// the results are undefined.
225230
// --------------------------------------------------------------------
226231
template <class IntOut, class FloatIn>
227232
static IntOut SafeRound(FloatIn x) {
228-
COMPILE_ASSERT(!MathLimits<FloatIn>::kIsInteger, FloatIn_is_integer);
229-
COMPILE_ASSERT(MathLimits<IntOut>::kIsInteger, IntOut_is_not_integer);
233+
COMPILE_ASSERT(!std::numeric_limits<FloatIn>::is_integer,
234+
FloatIn_is_integer);
235+
COMPILE_ASSERT(std::numeric_limits<IntOut>::is_integer,
236+
IntOut_is_not_integer);
230237

231238
if (MathLimits<FloatIn>::IsNaN(x)) {
232239
return 0;

ortools/linear_solver/model_validator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class LazyMPModelNameToIndexMaps {
471471
map = &general_constraint_name_to_index_.value();
472472
break;
473473
}
474-
const int index = gtl::FindWithDefault(*map, std::string(name), -2);
474+
const int index = gtl::FindWithDefault(*map, name, -2);
475475
if (index == -2) return absl::NotFoundError("name not found");
476476
if (index == -1) return absl::InvalidArgumentError("name is not unique");
477477
return index;

0 commit comments

Comments
 (0)