another attempts for fixing solution limits on sat c++
This commit is contained in:
@@ -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",
|
||||
|
||||
34
ortools/sat/model.cc
Normal file
34
ortools/sat/model.cc
Normal file
@@ -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 <cstddef>
|
||||
|
||||
#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
|
||||
@@ -23,9 +23,7 @@
|
||||
#include <vector>
|
||||
|
||||
#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 <typename T>
|
||||
T* GetOrCreate() {
|
||||
const size_t type_id = gtl::FastTypeId<T>();
|
||||
auto find = singletons_.find(type_id);
|
||||
if (find != singletons_.end()) {
|
||||
return static_cast<T*>(find->second);
|
||||
void* find = GetSingletonOrNullptr(type_id);
|
||||
if (find != nullptr) {
|
||||
return static_cast<T*>(find);
|
||||
}
|
||||
|
||||
// New element.
|
||||
// TODO(user): directly store std::unique_ptr<> in singletons_?
|
||||
T* new_t = MyNew<T>(0);
|
||||
singletons_[type_id] = new_t;
|
||||
AddNewSingleton(new_t, type_id);
|
||||
TakeOwnership(new_t);
|
||||
return new_t;
|
||||
}
|
||||
@@ -132,9 +130,7 @@ class Model {
|
||||
*/
|
||||
template <typename T>
|
||||
const T* Get() const {
|
||||
const auto& it = singletons_.find(gtl::FastTypeId<T>());
|
||||
return it != singletons_.end() ? static_cast<const T*>(it->second)
|
||||
: nullptr;
|
||||
return static_cast<const T*>(GetSingletonOrNullptr(gtl::FastTypeId<T>()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,8 +138,7 @@ class Model {
|
||||
*/
|
||||
template <typename T>
|
||||
T* Mutable() const {
|
||||
const auto& it = singletons_.find(gtl::FastTypeId<T>());
|
||||
return it != singletons_.end() ? static_cast<T*>(it->second) : nullptr;
|
||||
return static_cast<T*>(GetSingletonOrNullptr(gtl::FastTypeId<T>()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,9 +171,7 @@ class Model {
|
||||
*/
|
||||
template <typename T>
|
||||
void Register(T* non_owned_class) {
|
||||
const size_t type_id = gtl::FastTypeId<T>();
|
||||
CHECK(!singletons_.contains(type_id));
|
||||
singletons_[type_id] = non_owned_class;
|
||||
AddNewSingleton(non_owned_class, gtl::FastTypeId<T>());
|
||||
}
|
||||
|
||||
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<T> to a "singleton" of type T.
|
||||
#if defined(_MSC_VER)
|
||||
absl::btree_map</*typeid*/ size_t, void*> singletons_;
|
||||
#else
|
||||
absl::flat_hash_map</*typeid*/ size_t, void*> singletons_;
|
||||
#endif
|
||||
|
||||
struct DeleteInterface {
|
||||
virtual ~DeleteInterface() = default;
|
||||
|
||||
Reference in New Issue
Block a user