From 80686581f4730428dd089578c698a5d8a0b9cd6c Mon Sep 17 00:00:00 2001 From: Mizux Seiha Date: Tue, 25 Feb 2025 16:04:20 +0100 Subject: [PATCH] backport from main --- ortools/algorithms/binary_search.h | 36 +++++++++------ ortools/algorithms/binary_search_test.cc | 47 ++++++++++++++++++++ ortools/algorithms/dynamic_partition_test.cc | 2 + ortools/algorithms/hungarian_test.cc | 39 ++++++++-------- ortools/algorithms/knapsack_solver_test.cc | 2 +- ortools/base/BUILD.bazel | 6 +-- ortools/base/adjustable_priority_queue-inl.h | 2 +- ortools/base/adjustable_priority_queue.h | 2 - ortools/base/constant_divisor_test.cc | 2 +- ortools/base/gzipfile.h | 2 +- ortools/base/int_type.h | 21 +++------ ortools/base/logging.cc | 3 -- ortools/base/macros.h | 18 +------- ortools/base/mathutil.cc | 2 +- ortools/base/mathutil.h | 12 ----- ortools/base/memutil.h | 4 +- ortools/base/murmur.h | 3 ++ ortools/base/parse_test_proto.h | 5 +-- ortools/base/parse_text_proto.h | 11 ++--- ortools/base/proto_enum_utils.h | 4 +- ortools/base/protobuf_util.h | 11 ++--- ortools/base/recordio.h | 4 +- ortools/base/stl_util.h | 5 +-- ortools/base/strong_int.h | 17 ++++--- ortools/base/timer.cc | 2 + ortools/base/timer.h | 7 ++- ortools/base/top_n.h | 3 -- ortools/base/typeid.h | 1 + ortools/flatzinc/parser.yy.cc | 1 + ortools/init/init.cc | 5 ++- 30 files changed, 149 insertions(+), 130 deletions(-) diff --git a/ortools/algorithms/binary_search.h b/ortools/algorithms/binary_search.h index d9621694ff..27e818158a 100644 --- a/ortools/algorithms/binary_search.h +++ b/ortools/algorithms/binary_search.h @@ -19,6 +19,7 @@ #include #include +#include "absl/base/log_severity.h" #include "absl/functional/function_ref.h" #include "absl/log/check.h" #include "absl/numeric/int128.h" @@ -26,22 +27,29 @@ #include "ortools/base/logging.h" namespace operations_research { - +// Finds a point in [x_true, x_false) where f changes from true to false. +// If check_bounds is true, it will CHECK that f(x_true) = true and +// f(x_false) = false. +// // EXAMPLE: // // Finds the value x in [0,Pi/2] such that cos(x)=2sin(x): // BinarySearch(/*x_true=*/0.0, /*x_false=*/M_PI/2, // [](double x) { return cos(x) >= 2*sin(x); }); // -// Note that x_true > x_false is supported: it works either way. +// MONOTONIC FUNCTIONS: Suppose f is a monotonic boolean function. See below for +// the NON-MONOTONIC case. // -// Ideally, f is a monotonic boolean function, such that: -// - f(x_true) = true -// - f(x_false) = false -// - there exists X such that f(x)=true for all x between x_true and X, and -// f(x)=false for all x between X and x_false. +// If x_true < x_false, this returns X such that: +// - x_true < X < x_false, +// - f((x_true, X]) = true (i.e. for all x in (x_true, X], f(x) = true), +// - f((X, x_false)) = false (i.e. for all x in (X, x_false), f(x) = false) +// or this returns x_true if such an X does not exist. // -// In those conditions, this returns that value X (note that f(X) is true). -// See below for the NON-MONOTONIC case. +// If x_true > x_false, this function returns X such that: +// - x_false < X < x_true +// - f((x_false, X)) = false +// - f([X, x_true)) = true +// or this return x_true if such an X does not exist. // // Also note that 'Point' may be floating-point types: the function will still // converge when the midpoint can't be distinguished from one of the limits, @@ -68,7 +76,7 @@ namespace operations_research { // Note also that even if f() is non-deterministic, i.e. f(X) can sometimes // return true and sometimes false for the same X, then the binary search will // still finish, but it's hard to say anything about the returned X. -template +template Point BinarySearch(Point x_true, Point x_false, std::function f); // Used by BinarySearch(). This is just (x+y)/2, with some DCHECKs to catch @@ -207,10 +215,12 @@ Point BinarySearchMidpoint(Point x, Point y) { return midpoint; } -template +template Point BinarySearch(Point x_true, Point x_false, std::function f) { - DCHECK(f(x_true)) << x_true; - DCHECK(!f(x_false)) << x_false; + if constexpr (check_bounds) { + CHECK(f(x_true)) << x_true; + CHECK(!f(x_false)) << x_false; + } int num_iterations = 0; constexpr int kMaxNumIterations = 1000000; while (true) { diff --git a/ortools/algorithms/binary_search_test.cc b/ortools/algorithms/binary_search_test.cc index 3f2c6620f9..ea2c8a729b 100644 --- a/ortools/algorithms/binary_search_test.cc +++ b/ortools/algorithms/binary_search_test.cc @@ -175,6 +175,53 @@ TEST(BinarySearchDeathTest, DiesIfEitherBoundaryConditionViolatedInFastbuild) { ""); } +TEST(BinarySearchDeathTest, DiesIfBoundCheckIsEnabledAndABoundIsViolated) { + // EXPECT_DEATH does not work with 2-parameter templates. + auto bs = [](int x_true, int x_false, auto f) { + return BinarySearch(x_true, x_false, f); + }; + EXPECT_DEATH(bs(/*x_true=*/0, /*x_false=*/42, [](int x) { return x < 999; }), + ""); + EXPECT_DEATH(bs(/*x_true=*/0, /*x_false=*/42, [](int x) { return x < 0; }), + ""); + EXPECT_DEATH(bs(/*x_true=*/0, /*x_false=*/42, [](int x) { return x > 20; }), + ""); +} + +TEST(BinarySearchTest, NoDeathIfBoundCheckIsDisabled) { + // EXPECT_EQ does not work with 2-parameter templates. + auto bs = [](int x_true, int x_false, auto f) { + return BinarySearch(x_true, x_false, f); + }; + + // f is true on the whole interval ]0, 42[: return last value 41. + EXPECT_EQ(bs(/*x_true=*/0, /*x_false=*/42, [](int x) { return x < 999; }), + 41); + // f is true on reversed interval ]42, 0[: return last value 1. + EXPECT_EQ(bs(/*x_true=*/42, /*x_false=*/0, [](int x) { return x < 999; }), 1); + // f is false on the whole interval ]0, 42[: return x_true bound 0. + EXPECT_EQ(bs(/*x_true=*/0, /*x_false=*/42, [](int x) { return x < 0; }), 0); + // f is false on reversed interval ]42, 0[: return x_true bound 42. + EXPECT_EQ(bs(/*x_true=*/0, /*x_false=*/42, [](int x) { return x < 0; }), 0); + // f is false on x_true, true on x_false. + // No DCHECK trigger, instead return a transition point of the function + // f': x_true -> true, x_false -> false, x_true < x < x_false -> f(x). + // The transitions are: 0 (true) -> 1 (false) and 41 (true) -> 42 (false). + { + const int x_transition = + bs(/*x_true=*/0, /*x_false=*/42, [](int x) { return x > 20; }); + EXPECT_TRUE(x_transition == 0 || x_transition == 41); + } + // f is false on x_true, true on x_false, with a reversed interval. + // No DCHECK trigger, return one of the transition points. + // The transitions are: 42 (true) -> 41 (false) and 1 (true) -> 0 (false). + { + const int x_transition = + bs(/*x_true=*/42, /*x_false=*/0, [](int x) { return x < 20; }); + EXPECT_TRUE(x_transition == 42 || x_transition == 1); + } +} + } // namespace // Examples of cases where one needs to override BinarySearchMidpoint() to get diff --git a/ortools/algorithms/dynamic_partition_test.cc b/ortools/algorithms/dynamic_partition_test.cc index aeacd0686b..c9d47d1af2 100644 --- a/ortools/algorithms/dynamic_partition_test.cc +++ b/ortools/algorithms/dynamic_partition_test.cc @@ -19,8 +19,10 @@ #include #include +#include "absl/base/log_severity.h" #include "absl/random/bit_gen_ref.h" #include "absl/random/random.h" +#include "absl/types/span.h" #include "gtest/gtest.h" #include "ortools/base/gmock.h" #include "ortools/base/stl_util.h" diff --git a/ortools/algorithms/hungarian_test.cc b/ortools/algorithms/hungarian_test.cc index d722d900be..842ec9e2ab 100644 --- a/ortools/algorithms/hungarian_test.cc +++ b/ortools/algorithms/hungarian_test.cc @@ -20,13 +20,12 @@ #include #include +#include "absl/base/macros.h" #include "absl/container/flat_hash_map.h" #include "absl/random/distributions.h" #include "absl/types/span.h" #include "gtest/gtest.h" -#include "ortools/base/macros.h" #include "ortools/base/map_util.h" -#include "ortools/base/types.h" namespace operations_research { @@ -94,24 +93,24 @@ TEST(LinearAssignmentTest, InvalidMatrix) { TestMinimization(cost_nan, 0, expected_agents, expected_tasks); } -#define MATRIX_TEST \ - { \ - std::vector> cost(kMatrixHeight); \ - for (int row = 0; row < kMatrixHeight; ++row) { \ - cost[row].resize(kMatrixWidth); \ - for (int col = 0; col < kMatrixWidth; ++col) { \ - cost[row][col] = kCost[row][col]; \ - } \ - } \ - EXPECT_EQ(arraysize(expected_agents_for_min), \ - arraysize(expected_tasks_for_min)); \ - EXPECT_EQ(arraysize(expected_agents_for_max), \ - arraysize(expected_tasks_for_max)); \ - const int assignment_size = arraysize(expected_agents_for_max); \ - TestMinimization(cost, assignment_size, expected_agents_for_min, \ - expected_tasks_for_min); \ - TestMaximization(cost, assignment_size, expected_agents_for_max, \ - expected_tasks_for_max); \ +#define MATRIX_TEST \ + { \ + std::vector> cost(kMatrixHeight); \ + for (int row = 0; row < kMatrixHeight; ++row) { \ + cost[row].resize(kMatrixWidth); \ + for (int col = 0; col < kMatrixWidth; ++col) { \ + cost[row][col] = kCost[row][col]; \ + } \ + } \ + EXPECT_EQ(ABSL_ARRAYSIZE(expected_agents_for_min), \ + ABSL_ARRAYSIZE(expected_tasks_for_min)); \ + EXPECT_EQ(ABSL_ARRAYSIZE(expected_agents_for_max), \ + ABSL_ARRAYSIZE(expected_tasks_for_max)); \ + const int assignment_size = ABSL_ARRAYSIZE(expected_agents_for_max); \ + TestMinimization(cost, assignment_size, expected_agents_for_min, \ + expected_tasks_for_min); \ + TestMaximization(cost, assignment_size, expected_agents_for_max, \ + expected_tasks_for_max); \ } // Test on a 1x1 matrix diff --git a/ortools/algorithms/knapsack_solver_test.cc b/ortools/algorithms/knapsack_solver_test.cc index bbcbc82766..1589f20e43 100644 --- a/ortools/algorithms/knapsack_solver_test.cc +++ b/ortools/algorithms/knapsack_solver_test.cc @@ -17,8 +17,8 @@ #include #include +#include "absl/base/macros.h" #include "gtest/gtest.h" -#include "ortools/base/macros.h" #include "ortools/util/time_limit.h" namespace operations_research { diff --git a/ortools/base/BUILD.bazel b/ortools/base/BUILD.bazel index 9b24f1a603..8f80104ea7 100644 --- a/ortools/base/BUILD.bazel +++ b/ortools/base/BUILD.bazel @@ -44,7 +44,6 @@ cc_library( hdrs = [ "commandlineflags.h", "init_google.h", - "logging.h", "stl_logging.h", "types.h", "version.h", @@ -63,7 +62,6 @@ cc_library( deps = [ ":commandlineflags", ":logging", - ":macros", ":types", "@com_google_absl//absl/base", "@com_google_absl//absl/container:flat_hash_map", @@ -73,7 +71,10 @@ cc_library( "@com_google_absl//absl/flags:flag", "@com_google_absl//absl/flags:parse", "@com_google_absl//absl/flags:usage", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", "@com_google_absl//absl/log:die_if_null", + "@com_google_absl//absl/log:globals", "@com_google_absl//absl/log:initialize", "@com_google_absl//absl/strings", "@com_google_absl//absl/synchronization", @@ -617,7 +618,6 @@ cc_library( srcs = ["timer.cc"], hdrs = ["timer.h"], deps = [ - ":macros", "@com_google_absl//absl/log:check", "@com_google_absl//absl/time", ], diff --git a/ortools/base/adjustable_priority_queue-inl.h b/ortools/base/adjustable_priority_queue-inl.h index 427e52bb19..7e952074ec 100644 --- a/ortools/base/adjustable_priority_queue-inl.h +++ b/ortools/base/adjustable_priority_queue-inl.h @@ -14,6 +14,6 @@ #ifndef OR_TOOLS_BASE_ADJUSTABLE_PRIORITY_QUEUE_INL_H_ #define OR_TOOLS_BASE_ADJUSTABLE_PRIORITY_QUEUE_INL_H_ -#include "ortools/base/adjustable_priority_queue.h" +#include "ortools/base/adjustable_priority_queue.h" // IWYU pragma: export #endif // OR_TOOLS_BASE_ADJUSTABLE_PRIORITY_QUEUE_INL_H_ diff --git a/ortools/base/adjustable_priority_queue.h b/ortools/base/adjustable_priority_queue.h index dd3fbae997..66c44331da 100644 --- a/ortools/base/adjustable_priority_queue.h +++ b/ortools/base/adjustable_priority_queue.h @@ -20,8 +20,6 @@ #include #include -#include "ortools/base/macros.h" - template class LowerPriorityThan { public: diff --git a/ortools/base/constant_divisor_test.cc b/ortools/base/constant_divisor_test.cc index eaa1687480..a1e42cc218 100644 --- a/ortools/base/constant_divisor_test.cc +++ b/ortools/base/constant_divisor_test.cc @@ -68,7 +68,7 @@ TEST(ConstantDivisorTemplateTest, Simple) { } TEST(ConstantDivisorUint64Test, Bugs) { - // If forumula (27) from p231 is ever implemented, these divisors will break + // If formula (27) from p231 is ever implemented, these divisors will break // if a >= is accidentally used instead of >. EXPECT_EQ(uint64_t{828560257293048160}, ConstantDivisor(21).div(uint64_t{17399765403154011380u})); diff --git a/ortools/base/gzipfile.h b/ortools/base/gzipfile.h index 7037de3b62..dad2f0852f 100644 --- a/ortools/base/gzipfile.h +++ b/ortools/base/gzipfile.h @@ -28,7 +28,7 @@ enum class AppendedStreams { kIgnoreAppendedData, }; -// Return a readonly file that contains a uncompressed version of +// Return a read-only file that contains a uncompressed version of // another File. // // If "ownership == TAKE_OWNERSHIP", the file takes ownership of diff --git a/ortools/base/int_type.h b/ortools/base/int_type.h index 84f79669af..246376def8 100644 --- a/ortools/base/int_type.h +++ b/ortools/base/int_type.h @@ -147,14 +147,12 @@ #include -#include #include #include // NOLINT #include -#include "absl/base/port.h" +#include "absl/base/attributes.h" #include "absl/strings/string_view.h" -#include "ortools/base/macros.h" namespace gtl { @@ -198,8 +196,13 @@ class IntType { } }; + template + friend H AbslHashValue(H h, const IntType& i) { + return H::combine(std::move(h), i.value()); + } + public: - // Default c'tor initializing value_ to 0. + // Default constructor initializing value_ to 0. constexpr IntType() : value_(0) {} // C'tor explicitly initializing from a ValueType. constexpr explicit IntType(ValueType value) : value_(value) {} @@ -273,9 +276,6 @@ class IntType { private: // The integer value of type ValueType. ValueType value_; - - COMPILE_ASSERT(std::is_integral::value, - invalid_integer_type_for_id_type_); } ABSL_ATTRIBUTE_PACKED; // -- NON-MEMBER STREAM OPERATORS ---------------------------------------------- @@ -357,11 +357,4 @@ INT_TYPE_COMPARISON_OP(>=); // NOLINT } // namespace gtl -// Allows it to be used as a key to hashable containers. -namespace std { -template -struct hash > - : gtl::IntType::Hasher {}; -} // namespace std - #endif // OR_TOOLS_BASE_INT_TYPE_H_ diff --git a/ortools/base/logging.cc b/ortools/base/logging.cc index 9afc09e06a..8e127ba59b 100644 --- a/ortools/base/logging.cc +++ b/ortools/base/logging.cc @@ -15,7 +15,6 @@ #include // for std::call_once and std::once_flag. // NOLINT -#include "absl/flags/flag.h" #include "absl/flags/usage.h" #include "absl/log/globals.h" #include "absl/log/initialize.h" @@ -34,6 +33,4 @@ void FixFlagsAndEnvironmentForSwig() { absl::EnableLogPrefix(false); } -void KeepAbslSymbols() { absl::SetFlag(&FLAGS_stderrthreshold, 0); } - } // namespace operations_research diff --git a/ortools/base/macros.h b/ortools/base/macros.h index 0a67d92e6d..98ef0f7118 100644 --- a/ortools/base/macros.h +++ b/ortools/base/macros.h @@ -14,9 +14,7 @@ #ifndef OR_TOOLS_BASE_MACROS_H_ #define OR_TOOLS_BASE_MACROS_H_ -#include // for size_t. - -#include "ortools/base/base_export.h" // for OR_DLL +#include "ortools/base/base_export.h" // IWYU pragma: export #define COMPILE_ASSERT(x, msg) @@ -26,18 +24,4 @@ const bool DEBUG_MODE = false; const bool DEBUG_MODE = true; #endif // NDEBUG -// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. -// It goes in the private: declarations in a class. -#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ - TypeName(const TypeName&) = delete; \ - TypeName& operator=(const TypeName&) = delete - -template -char (&ArraySizeHelper(T (&array)[N]))[N]; -#ifndef COMPILER_MSVC -template -char (&ArraySizeHelper(const T (&array)[N]))[N]; -#endif -#define arraysize(array) (sizeof(ArraySizeHelper(array))) - #endif // OR_TOOLS_BASE_MACROS_H_ diff --git a/ortools/base/mathutil.cc b/ortools/base/mathutil.cc index 4ed88ffd27..d3d8a87117 100644 --- a/ortools/base/mathutil.cc +++ b/ortools/base/mathutil.cc @@ -11,12 +11,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "absl/log/check.h" #if defined(_MSC_VER) #define _USE_MATH_DEFINES #include #endif -#include "ortools/base/logging.h" #include "ortools/base/mathutil.h" namespace operations_research { diff --git a/ortools/base/mathutil.h b/ortools/base/mathutil.h index 22745af560..dce9c77ee4 100644 --- a/ortools/base/mathutil.h +++ b/ortools/base/mathutil.h @@ -24,7 +24,6 @@ #include "absl/base/casts.h" #include "ortools/base/logging.h" -#include "ortools/base/macros.h" namespace operations_research { class MathUtil { @@ -173,12 +172,6 @@ class MathUtil { // the results are undefined. template static IntOut SafeCast(FloatIn x) { - COMPILE_ASSERT(!std::numeric_limits::is_integer, - FloatIn_is_integer); - COMPILE_ASSERT(std::numeric_limits::is_integer, - IntOut_is_not_integer); - COMPILE_ASSERT(std::numeric_limits::radix == 2, IntOut_is_base_2); - // Special case NaN, for which the logic below doesn't work. if (std::isnan(x)) { return 0; @@ -230,11 +223,6 @@ class MathUtil { // -------------------------------------------------------------------- template static IntOut SafeRound(FloatIn x) { - COMPILE_ASSERT(!std::numeric_limits::is_integer, - FloatIn_is_integer); - COMPILE_ASSERT(std::numeric_limits::is_integer, - IntOut_is_not_integer); - if (std::isnan(x)) { return 0; } else { diff --git a/ortools/base/memutil.h b/ortools/base/memutil.h index e9ea03ef4c..f9a8accbad 100644 --- a/ortools/base/memutil.h +++ b/ortools/base/memutil.h @@ -17,10 +17,8 @@ #include #include -#include "absl/strings/internal/memutil.h" - namespace strings { -char* memdup(const char* s, size_t slen) { +inline char* memdup(const char* s, size_t slen) { void* copy; if ((copy = malloc(slen)) == nullptr) return nullptr; memcpy(copy, s, slen); diff --git a/ortools/base/murmur.h b/ortools/base/murmur.h index 26aaea1836..d89df5d65f 100644 --- a/ortools/base/murmur.h +++ b/ortools/base/murmur.h @@ -14,6 +14,9 @@ #ifndef OR_TOOLS_BASE_MURMUR_H_ #define OR_TOOLS_BASE_MURMUR_H_ +#include +#include + #include "ortools/base/hash.h" namespace util_hash { diff --git a/ortools/base/parse_test_proto.h b/ortools/base/parse_test_proto.h index e1934ce0bc..0553e1a221 100644 --- a/ortools/base/parse_test_proto.h +++ b/ortools/base/parse_test_proto.h @@ -14,9 +14,8 @@ #ifndef OR_TOOLS_BASE_PARSE_TEST_PROTO_H_ #define OR_TOOLS_BASE_PARSE_TEST_PROTO_H_ -#include -#include #include +#include #include "google/protobuf/message.h" #include "google/protobuf/text_format.h" @@ -32,7 +31,7 @@ class ParseProtoHelper { template operator T() { // NOLINT(runtime/explicit) T result; - const bool ok = ::google::protobuf::TextFormat::TextFormat::ParseFromString( + const bool ok = google::protobuf::TextFormat::TextFormat::ParseFromString( asciipb_, &result); EXPECT_TRUE(ok) << "Failed to parse text proto: " << asciipb_; return result; diff --git a/ortools/base/parse_text_proto.h b/ortools/base/parse_text_proto.h index cf352fe1fc..cc3c0411e3 100644 --- a/ortools/base/parse_text_proto.h +++ b/ortools/base/parse_text_proto.h @@ -14,9 +14,10 @@ #ifndef OR_TOOLS_BASE_PARSE_TEXT_PROTO_H_ #define OR_TOOLS_BASE_PARSE_TEXT_PROTO_H_ +#include #include -#include "absl/log/absl_check.h" +#include "absl/log/check.h" #include "google/protobuf/message.h" #include "google/protobuf/text_format.h" @@ -24,13 +25,13 @@ namespace google::protobuf::contrib::parse_proto { template bool ParseTextProto(const std::string& input, T* proto) { - return ::google::protobuf::TextFormat::ParseFromString(input, proto); + return google::protobuf::TextFormat::ParseFromString(input, proto); } template T ParseTextOrDie(const std::string& input) { T result; - ABSL_CHECK(ParseTextProto(input, &result)); + CHECK(ParseTextProto(input, &result)); return result; } @@ -42,7 +43,7 @@ class ParseProtoHelper { template operator T() { // NOLINT(runtime/explicit) T result; - const bool ok = ::google::protobuf::TextFormat::TextFormat::ParseFromString( + const bool ok = google::protobuf::TextFormat::TextFormat::ParseFromString( asciipb_, &result); CHECK(ok) << "Failed to parse text proto: " << asciipb_; return result; @@ -54,7 +55,7 @@ class ParseProtoHelper { } // namespace text_proto_internal -text_proto_internal::ParseProtoHelper ParseTextProtoOrDie( +inline text_proto_internal::ParseProtoHelper ParseTextProtoOrDie( std::string_view input) { return text_proto_internal::ParseProtoHelper(input); } diff --git a/ortools/base/proto_enum_utils.h b/ortools/base/proto_enum_utils.h index 11be37db27..a78dd61a72 100644 --- a/ortools/base/proto_enum_utils.h +++ b/ortools/base/proto_enum_utils.h @@ -27,11 +27,9 @@ // #include -#include -#include "absl/types/span.h" #include "google/protobuf/descriptor.pb.h" - +#include "google/protobuf/repeated_field.h" namespace google::protobuf::contrib::utils { using google::protobuf::GetEnumDescriptor; diff --git a/ortools/base/protobuf_util.h b/ortools/base/protobuf_util.h index 8433ec008e..a964f1eeb4 100644 --- a/ortools/base/protobuf_util.h +++ b/ortools/base/protobuf_util.h @@ -15,15 +15,14 @@ #define OR_TOOLS_BASE_PROTOBUF_UTIL_H_ #include +#include +#include "absl/log/check.h" #include "google/protobuf/repeated_field.h" #include "google/protobuf/repeated_ptr_field.h" #include "google/protobuf/text_format.h" -#include "ortools/base/logging.h" -namespace google { -namespace protobuf { -namespace util { +namespace google::protobuf::util { // RepeatedPtrField version. template inline void Truncate(RepeatedPtrField* array, int new_size) { @@ -108,8 +107,6 @@ T ParseTextOrDie(const std::string& input) { return result; } -} // namespace util -} // namespace protobuf -} // namespace google +} // namespace google::protobuf::util #endif // OR_TOOLS_BASE_PROTOBUF_UTIL_H_ diff --git a/ortools/base/recordio.h b/ortools/base/recordio.h index b2be8bcbb7..af7bcbb787 100644 --- a/ortools/base/recordio.h +++ b/ortools/base/recordio.h @@ -14,7 +14,7 @@ #ifndef OR_TOOLS_BASE_RECORDIO_H_ #define OR_TOOLS_BASE_RECORDIO_H_ -#include +#include #include #include "ortools/base/file.h" @@ -35,7 +35,7 @@ class RecordWriter { // Magic number when reading and writing protocol buffers. static const int kMagicNumber; - explicit RecordWriter(File* const file); + explicit RecordWriter(File* file); template bool WriteProtocolMessage(const P& proto) { diff --git a/ortools/base/stl_util.h b/ortools/base/stl_util.h index 26705aae16..57bb82cbf3 100644 --- a/ortools/base/stl_util.h +++ b/ortools/base/stl_util.h @@ -19,21 +19,18 @@ #include #include -#include #include #include #include #include #include #include -#include #include #include -#include +#include "absl/base/attributes.h" #include "absl/meta/type_traits.h" #include "absl/strings/internal/resize_uninitialized.h" -#include "ortools/base/macros.h" namespace gtl { namespace internal { diff --git a/ortools/base/strong_int.h b/ortools/base/strong_int.h index 41d2c75a94..ac5ff1a5a9 100644 --- a/ortools/base/strong_int.h +++ b/ortools/base/strong_int.h @@ -88,7 +88,7 @@ // The class also defines a hash functor that allows the StrongInt to be used // as key to hashable containers such as hash_map and hash_set. // -// We suggest using the StrongIntIndexedContainer wrapper around google3's +// We suggest using the StrongIntIndexedContainer wrapper around // FixedArray and STL vector (see int-type-indexed-container.h) if an StrongInt // is intended to be used as an index into these containers. These wrappers are // indexed in a type-safe manner using StrongInts to ensure type-safety. @@ -147,15 +147,18 @@ #include +#include #include #include +#include +#include #include // NOLINT #include +#include "absl/base/attributes.h" #include "absl/base/port.h" #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" -#include "ortools/base/macros.h" namespace util_intops { @@ -200,8 +203,13 @@ class StrongInt { } }; + template + friend H AbslHashValue(H h, const StrongInt& i) { + return H::combine(std::move(h), i.value()); + } + public: - // Default c'tor initializing value_ to 0. + // Default ctor initializing value_ to 0. constexpr StrongInt() : value_(0) {} // C'tor explicitly initializing from a ValueType. constexpr explicit StrongInt(ValueType value) : value_(value) {} @@ -285,9 +293,6 @@ class StrongInt { private: // The integer value of type ValueType. ValueType value_; - - COMPILE_ASSERT(std::is_integral::value, - invalid_integer_type_for_id_type_); } ABSL_ATTRIBUTE_PACKED; // -- NON-MEMBER STREAM OPERATORS ---------------------------------------------- diff --git a/ortools/base/timer.cc b/ortools/base/timer.cc index 999a44042b..9fdbed9d8e 100644 --- a/ortools/base/timer.cc +++ b/ortools/base/timer.cc @@ -13,6 +13,8 @@ #include "ortools/base/timer.h" +#include "absl/log/check.h" + ScopedWallTime::ScopedWallTime(double* aggregate_time) : aggregate_time_(aggregate_time), timer_() { DCHECK(aggregate_time != nullptr); diff --git a/ortools/base/timer.h b/ortools/base/timer.h index 70ad2eb9a6..2989d58b1e 100644 --- a/ortools/base/timer.h +++ b/ortools/base/timer.h @@ -16,10 +16,8 @@ #include -#include "absl/log/check.h" #include "absl/time/clock.h" #include "absl/time/time.h" -#include "ortools/base/macros.h" class WallTimer { public: @@ -83,6 +81,9 @@ typedef CycleTimer SimpleCycleTimer; // Conversion routines between CycleTimer::GetCycles and actual times. class CycleTimerBase { public: + CycleTimerBase(const CycleTimerBase&) = delete; + CycleTimerBase& operator=(const CycleTimerBase&) = delete; + static int64_t SecondsToCycles(double s) { return static_cast(s * 1e9); } @@ -109,7 +110,5 @@ class ScopedWallTime { // When the instance was created. WallTimer timer_; - - DISALLOW_COPY_AND_ASSIGN(ScopedWallTime); }; #endif // OR_TOOLS_BASE_TIMER_H_ diff --git a/ortools/base/top_n.h b/ortools/base/top_n.h index a2fa1b95e3..76746571fe 100644 --- a/ortools/base/top_n.h +++ b/ortools/base/top_n.h @@ -34,11 +34,8 @@ #include #include -#include #include -#include "ortools/base/logging.h" - namespace operations_research { namespace gtl { // Cmp is an stl binary predicate. Note that Cmp is the "greater" predicate, diff --git a/ortools/base/typeid.h b/ortools/base/typeid.h index 94e9c66f5d..8a4b314f76 100644 --- a/ortools/base/typeid.h +++ b/ortools/base/typeid.h @@ -14,6 +14,7 @@ #ifndef OR_TOOLS_BASE_TYPEID_H_ #define OR_TOOLS_BASE_TYPEID_H_ +#include namespace gtl { template inline size_t FastTypeId() { diff --git a/ortools/flatzinc/parser.yy.cc b/ortools/flatzinc/parser.yy.cc index d6943c77b9..5bd90d2ab6 100644 --- a/ortools/flatzinc/parser.yy.cc +++ b/ortools/flatzinc/parser.yy.cc @@ -712,6 +712,7 @@ static const flex_int32_t yy_rule_can_match_eol[32] = { #include #include +#include "absl/log/check.h" #include "absl/strings/numbers.h" #include "ortools/flatzinc/parser.tab.hh" #if defined(_MSC_VER) diff --git a/ortools/init/init.cc b/ortools/init/init.cc index e80ce87a4f..4813b4a4b9 100644 --- a/ortools/init/init.cc +++ b/ortools/init/init.cc @@ -13,6 +13,9 @@ #include "ortools/init/init.h" +#include + +#include "absl/base/log_severity.h" #include "absl/flags/flag.h" #include "absl/flags/usage.h" #include "absl/log/globals.h" @@ -28,7 +31,7 @@ void CppBridge::InitLogging(const std::string& usage) { } void CppBridge::SetFlags(const CppFlags& flags) { - absl::SetFlag(&FLAGS_stderrthreshold, flags.stderrthreshold); + absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo); absl::EnableLogPrefix(flags.log_prefix); if (!flags.cp_model_dump_prefix.empty()) { absl::SetFlag(&FLAGS_cp_model_dump_prefix, flags.cp_model_dump_prefix);