Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ build --enable_platform_specific_config
# Options for Linux.
###############################################################################

build:linux --cxxopt=-std=c++17 --host_cxxopt=-std=c++17
build:linux --cxxopt=-std=c++20 --host_cxxopt=-std=c++20
build:linux --cxxopt=-Wno-sign-compare --host_cxxopt=-Wno-sign-compare
build:linux --cxxopt=-Wno-range-loop-construct --host_cxxopt=-Wno-range-loop-construct

Expand All @@ -45,7 +45,7 @@ build:linux --cxxopt=-Wno-range-loop-construct --host_cxxopt=-Wno-range-loop-con
# Sets the default Apple platform to macOS.
build --apple_platform_type=macos
build:macos --features=-supports_dynamic_linker
build:macos --cxxopt=-std=c++17 --host_cxxopt=-std=c++17
build:macos --cxxopt=-std=c++20 --host_cxxopt=-std=c++20
build:macos --cxxopt=-Wno-sign-compare --host_cxxopt=-Wno-sign-compare
build:macos --cxxopt=-Wno-dangling-field --host_cxxopt=-Wno-dangling-field
build:macos --cxxopt=-Wno-range-loop-construct --host_cxxopt=-Wno-range-loop-construct
Expand Down
6 changes: 1 addition & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ message(STATUS "${PROJECT_NAME} version: ${PROJECT_VERSION} release: ${RELEASE}"
#message(STATUS "minor: ${PROJECT_VERSION_MINOR}")
#message(STATUS "patch: ${PROJECT_VERSION_PATCH}")

if(MSVC)
set(CMAKE_CXX_STANDARD 20)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

Expand Down
32 changes: 6 additions & 26 deletions bazel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,35 +72,15 @@ OR-Tools depends on several mandatory libraries.

## Compilation

You must compile OR-Tools using at least C++17 (C++20 on windows):

* on UNIX:

```sh
bazel build -c opt --cxxopt=-std=c++17 ...
```

* on Windows when using MSVC:

```sh
bazel build -c opt --cxxopt="/std:c++20" ...
```
```sh
bazel build -c opt //ortools/... //examples/...
```

## Testing

You may run tests using:

* on UNIX:

```sh
bazel test -c opt --cxxopt=-std=c++17 ...
```

* on Windows when using MSVC:

```sh
bazel test -c opt --cxxopt="/std:c++20" ...
```
```sh
bazel test -c opt //ortools/... //examples/...
```

## Integration

Expand Down
11 changes: 5 additions & 6 deletions cmake/dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,8 @@ endif()
# ##############################################################################
# Coinutils
# ##############################################################################
# Coin-OR does not support C++17/C++20 (use of 'register' storage class specifier)

# Coin-OR uses the 'register' storage class specifier which requires C++11.
set(CMAKE_CXX_STANDARD 11)
if(WIN32)
set(BUILD_SHARED_LIBS OFF)
Expand Down Expand Up @@ -532,14 +533,12 @@ if(BUILD_Cbc)
message(CHECK_PASS "fetched")
endif()

# End of Coin-OR dependencies, setting the properties back to their previous
# values.
set(CMAKE_CXX_STANDARD 20)
if(WIN32)
set(BUILD_SHARED_LIBS ON)
endif()
if(MSVC)
set(CMAKE_CXX_STANDARD 20)
else()
set(CMAKE_CXX_STANDARD 17)
endif()

###############
## TESTING ##
Expand Down
6 changes: 0 additions & 6 deletions ortools/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ cc_library(
],
)

cc_library(
name = "array",
hdrs = ["array.h"],
deps = ["@abseil-cpp//absl/utility"],
)

cc_library(
name = "base",
srcs = ["version.cc"],
Expand Down
56 changes: 0 additions & 56 deletions ortools/base/array.h

This file was deleted.

2 changes: 1 addition & 1 deletion ortools/base/filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include <algorithm>
#include <exception> // IWYU pragma: keep
#include <filesystem> // NOLINT(build/c++17)
#include <filesystem> // NOLINT
#include <regex> // NOLINT
#include <string>
#include <string_view>
Expand Down
6 changes: 1 addition & 5 deletions ortools/cpp/CMakeLists.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ message(STATUS "${PROJECT_NAME} version: ${PROJECT_VERSION}")
#message(STATUS "minor: ${PROJECT_VERSION_MINOR}")
#message(STATUS "patch: ${PROJECT_VERSION_PATCH}")

if(MSVC)
set(CMAKE_CXX_STANDARD 20)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH True)
Expand Down
3 changes: 0 additions & 3 deletions ortools/math_opt/elemental/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ cc_library(
":arrays",
":elements",
":symmetry",
"//ortools/base:array",
"@abseil-cpp//absl/strings:string_view",
],
)
Expand Down Expand Up @@ -323,7 +322,6 @@ cc_test(
srcs = ["arrays_test.cc"],
deps = [
":arrays",
"//ortools/base:array",
"//ortools/base:gmock_main",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/strings",
Expand Down Expand Up @@ -459,7 +457,6 @@ cc_library(
visibility = ["//ortools/math_opt:__subpackages__"],
deps = [
":tagged_id",
"//ortools/base:array",
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/strings:string_view",
],
Expand Down
3 changes: 0 additions & 3 deletions ortools/math_opt/elemental/arrays.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace operations_research::math_opt {
// ```
template <int n, typename Fn>
constexpr decltype(auto) ApplyOnIndexRange(Fn&& fn) {
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
return [&fn]<int... is>(std::integer_sequence<int, is...>) mutable {
return fn.template operator()<is...>();
}(std::make_integer_sequence<int, n>());
Expand All @@ -55,15 +54,13 @@ constexpr decltype(auto) ApplyOnIndexRange(Fn&& fn) {
template <int n, typename Fn>
constexpr decltype(auto) ForEachIndex(Fn&& fn) {
return ApplyOnIndexRange<n>(
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
[&fn]<int... is>() { return (fn.template operator()<is>(), ...); });
}

// Calls `fn` of each element of `tuple`, and returns the result of the
// last invocation.
template <typename Fn, typename Tuple>
constexpr decltype(auto) ForEach(Fn&& fn, Tuple&& tuple) {
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
return std::apply([&fn]<typename... Ts>(
Ts&&... ts) { return (fn(std::forward<Ts>(ts)), ...); },
std::forward<Tuple>(tuple));
Expand Down
41 changes: 11 additions & 30 deletions ortools/math_opt/elemental/arrays_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "gtest/gtest.h"
#include "ortools/base/array.h"
#include "ortools/base/gmock.h"

namespace operations_research::math_opt {
Expand All @@ -35,109 +34,92 @@ using ::testing::ElementsAre;
template <auto a>
constexpr int Sum() {
return ApplyOnIndexRange<std::size(a)>(
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
[]<int... i>() { return (a[i] + ... + 0); });
}

// Same as `Sum`, but starts at 1.
template <auto a>
constexpr int SumPlusOne() {
return ApplyOnIndexRange<std::size(a)>(
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
[]<int... i>() { return (a[i] + ... + 1); });
}

#if __cplusplus >= 202002L
// NOLINTBEGIN(clang-diagnostic-pre-c++20-compat)
TEST(ApplyOnIndexRangeTest, Sum) {
EXPECT_EQ(Sum<gtl::to_array({5, 3, 1})>(), 9);
EXPECT_EQ(SumPlusOne<gtl::to_array({5, 3, 1})>(), 10);
EXPECT_EQ(Sum<std::to_array({5, 3, 1})>(), 9);
EXPECT_EQ(SumPlusOne<std::to_array({5, 3, 1})>(), 10);
}
// NOLINTEND(clang-diagnostic-pre-c++20-compat)
#endif

// Returns the weighted sum of the elements of an array-like object `a`, where
// weights are indices.
template <auto a>
constexpr double ScaledSum() {
return ApplyOnIndexRange<std::size(a)>(
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
[]<int... i>() { return ((i * a[i]) + ... + 0.0); });
}

#if __cplusplus >= 202002L
// NOLINTBEGIN(clang-diagnostic-pre-c++20-compat)
TEST(ApplyOnIndexRangeTest, ScaledSum) {
EXPECT_EQ(ScaledSum<gtl::to_array({5, 3, 1})>(), 5.0);
EXPECT_EQ(ScaledSum<std::to_array({5, 3, 1})>(), 5.0);
}
// NOLINTEND(clang-diagnostic-pre-c++20-compat)
#endif

// Returns the number of even elements in an array-like object `a`.
template <auto a>
constexpr int CountEven() {
return ApplyOnIndexRange<std::size(a)>(
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
[]<int... i>() { return ((a[i] % 2 == 0 ? 1 : 0) + ... + 0); });
}

#if __cplusplus >= 202002L
// NOLINTBEGIN(clang-diagnostic-pre-c++20-compat)
TEST(ApplyOnIndexRangeTest, CountEven) {
EXPECT_EQ(CountEven<gtl::to_array({5, 4, 8, 1, 10})>(), 3);
EXPECT_EQ(CountEven<std::to_array({5, 4, 8, 1, 10})>(), 3);
}
// NOLINTEND(clang-diagnostic-pre-c++20-compat)
#endif

// Returns array of doubles of the same size as `a`, where each element has been
// halved.
template <auto a>
constexpr std::array<double, std::size(a)> Half() {
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
return ApplyOnIndexRange<std::size(a)>([]<int... i>() {
return std::array<double, std::size(a)>(
{(static_cast<double>(a[i]) / 2.0)...});
});
}

#if __cplusplus >= 202002L
// NOLINTBEGIN(clang-diagnostic-pre-c++20-compat)
TEST(ApplyOnIndexRangeTest, Half) {
EXPECT_THAT(Half<gtl::to_array({5, 4, 8, 1, 10})>(),
EXPECT_THAT(Half<std::to_array({5, 4, 8, 1, 10})>(),
ElementsAre(2.5, 2.0, 4.0, 0.5, 5.0));
}
// NOLINTEND(clang-diagnostic-pre-c++20-compat)
#endif

// Returns true of all elements of `a` are even.
template <auto a>
constexpr int AllEven() {
return ApplyOnIndexRange<std::size(a)>(
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
[]<int... i>() { return (((a[i] % 2) == 0) && ...); });
}

// Returns true of any element of `a` is even.
template <auto a>
constexpr int AnyEven() {
return ApplyOnIndexRange<std::size(a)>(
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
[]<int... i>() { return (((a[i] % 2) == 0) || ...); });
}

#if __cplusplus >= 202002L
// NOLINTBEGIN(clang-diagnostic-pre-c++20-compat)
TEST(ApplyOnIndexRangeTest, Even) {
EXPECT_FALSE(AllEven<gtl::to_array({5, 4, 8, 1, 10})>());
EXPECT_TRUE(AnyEven<gtl::to_array({5, 4, 8, 1, 10})>());
EXPECT_FALSE(AllEven<std::to_array({5, 4, 8, 1, 10})>());
EXPECT_TRUE(AnyEven<std::to_array({5, 4, 8, 1, 10})>());

EXPECT_TRUE(AllEven<gtl::to_array({8, 2, 6})>());
EXPECT_TRUE(AnyEven<gtl::to_array({8, 2, 6})>());
EXPECT_TRUE(AllEven<std::to_array({8, 2, 6})>());
EXPECT_TRUE(AnyEven<std::to_array({8, 2, 6})>());

EXPECT_FALSE(AllEven<gtl::to_array({3, 7, 1})>());
EXPECT_FALSE(AnyEven<gtl::to_array({3, 7, 1})>());
EXPECT_FALSE(AllEven<std::to_array({3, 7, 1})>());
EXPECT_FALSE(AnyEven<std::to_array({3, 7, 1})>());
}
// NOLINTEND(clang-diagnostic-pre-c++20-compat)
#endif

// A example of a more complex reduce operation using `ForEachIndex`. Here, we
Expand All @@ -153,7 +135,6 @@ TEST(ForEachIndexTest, CollectTest) {

EXPECT_THAT(
ForEachIndex<21>(
// NOLINTNEXTLINE(clang-diagnostic-pre-c++20-compat)
[&may_fail, failed_indices = std::vector<int>()]<int i>() mutable
-> const std::vector<int>& {
if (!may_fail(i).ok()) {
Expand Down
Loading
Loading