From c28a3d32ea935a46614389d01a8c8fc7345f6345 Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Sun, 23 Mar 2025 18:49:11 -0700 Subject: [PATCH] another attempts for fixing solution limits on sat c++ --- ortools/sat/BUILD.bazel | 2 +- ortools/sat/model.cc | 34 ++++++++++++++++++++++++++++++++++ ortools/sat/model.h | 28 ++++++++++------------------ 3 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 ortools/sat/model.cc diff --git a/ortools/sat/BUILD.bazel b/ortools/sat/BUILD.bazel index a6753c7410..197ca0d33f 100644 --- a/ortools/sat/BUILD.bazel +++ b/ortools/sat/BUILD.bazel @@ -43,11 +43,11 @@ cc_library( cc_library( name = "model", + srcs = ["model.cc"], hdrs = ["model.h"], deps = [ "//ortools/base", "//ortools/base:typeid", - "@com_google_absl//absl/container:btree", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/log:check", "@com_google_absl//absl/meta:type_traits", diff --git a/ortools/sat/model.cc b/ortools/sat/model.cc new file mode 100644 index 0000000000..21cf5324df --- /dev/null +++ b/ortools/sat/model.cc @@ -0,0 +1,34 @@ +// Copyright 2010-2025 Google LLC +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "ortools/sat/model.h" + +#include + +#include "absl/log/check.h" + +namespace operations_research { +namespace sat { + +void Model::AddNewSingleton(void* new_element, size_t type_id) { + CHECK(singletons_.emplace(type_id, new_element).second) + << "Duplicate type id: " << type_id; +} + +void* Model::GetSingletonOrNullptr(size_t type_id) const { + const auto it = singletons_.find(type_id); + return it != singletons_.end() ? it->second : nullptr; +} + +} // namespace sat +} // namespace operations_research diff --git a/ortools/sat/model.h b/ortools/sat/model.h index 531074a196..e3099a9d26 100644 --- a/ortools/sat/model.h +++ b/ortools/sat/model.h @@ -23,9 +23,7 @@ #include #include "absl/container/flat_hash_map.h" -#include "absl/container/btree_map.h" #include "absl/log/check.h" -#include "absl/meta/type_traits.h" #include "ortools/base/logging.h" #include "ortools/base/typeid.h" @@ -112,15 +110,15 @@ class Model { template T* GetOrCreate() { const size_t type_id = gtl::FastTypeId(); - auto find = singletons_.find(type_id); - if (find != singletons_.end()) { - return static_cast(find->second); + void* find = GetSingletonOrNullptr(type_id); + if (find != nullptr) { + return static_cast(find); } // New element. // TODO(user): directly store std::unique_ptr<> in singletons_? T* new_t = MyNew(0); - singletons_[type_id] = new_t; + AddNewSingleton(new_t, type_id); TakeOwnership(new_t); return new_t; } @@ -132,9 +130,7 @@ class Model { */ template const T* Get() const { - const auto& it = singletons_.find(gtl::FastTypeId()); - return it != singletons_.end() ? static_cast(it->second) - : nullptr; + return static_cast(GetSingletonOrNullptr(gtl::FastTypeId())); } /** @@ -142,8 +138,7 @@ class Model { */ template T* Mutable() const { - const auto& it = singletons_.find(gtl::FastTypeId()); - return it != singletons_.end() ? static_cast(it->second) : nullptr; + return static_cast(GetSingletonOrNullptr(gtl::FastTypeId())); } /** @@ -176,9 +171,7 @@ class Model { */ template void Register(T* non_owned_class) { - const size_t type_id = gtl::FastTypeId(); - CHECK(!singletons_.contains(type_id)); - singletons_[type_id] = non_owned_class; + AddNewSingleton(non_owned_class, gtl::FastTypeId()); } const std::string& Name() const { return name_; } @@ -198,14 +191,13 @@ class Model { return new T(); } + void AddNewSingleton(void* new_element, size_t type_id); + void* GetSingletonOrNullptr(size_t type_id) const; + const std::string name_; // Map of FastTypeId to a "singleton" of type T. - #if defined(_MSC_VER) - absl::btree_map singletons_; - #else absl::flat_hash_map singletons_; - #endif struct DeleteInterface { virtual ~DeleteInterface() = default;