From e9342c2bc59a606bf34c144668325409062b0c0b Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Tue, 21 Jan 2020 11:00:12 +0100 Subject: [PATCH] used strongly typed reversible data structures --- ortools/sat/integer.cc | 5 +++-- ortools/sat/integer.h | 3 ++- ortools/util/rev.h | 13 +++++++------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ortools/sat/integer.cc b/ortools/sat/integer.cc index 7400987c420..a7d66943464 100644 --- a/ortools/sat/integer.cc +++ b/ortools/sat/integer.cc @@ -1686,11 +1686,12 @@ bool GenericLiteralWatcher::Propagate(Trail* trail) { // Before we propagate, make sure any reversible structure are up to date. // Note that we never do anything expensive more than once per level. { - const int low = id_to_greatest_common_level_since_last_call_[id]; + const int low = + id_to_greatest_common_level_since_last_call_[IdType(id)]; const int high = id_to_level_at_last_call_[id]; if (low < high || level > low) { // Equivalent to not all equal. id_to_level_at_last_call_[id] = level; - id_to_greatest_common_level_since_last_call_[id] = level; + id_to_greatest_common_level_since_last_call_[IdType(id)] = level; for (ReversibleInterface* rev : id_to_reversible_classes_[id]) { if (low < high) rev->SetLevel(low); if (level > low) rev->SetLevel(level); diff --git a/ortools/sat/integer.h b/ortools/sat/integer.h index 5b4bce94c7e..7b8098e1f34 100644 --- a/ortools/sat/integer.h +++ b/ortools/sat/integer.h @@ -1121,8 +1121,9 @@ class GenericLiteralWatcher : public SatPropagator { std::vector in_queue_; // Data for each propagator. + DEFINE_INT_TYPE(IdType, int32); std::vector id_to_level_at_last_call_; - RevVector id_to_greatest_common_level_since_last_call_; + RevVector id_to_greatest_common_level_since_last_call_; std::vector> id_to_reversible_classes_; std::vector> id_to_reversible_ints_; std::vector> id_to_watch_indices_; diff --git a/ortools/util/rev.h b/ortools/util/rev.h index 489ad6461aa..ea490bd3842 100644 --- a/ortools/util/rev.h +++ b/ortools/util/rev.h @@ -18,6 +18,7 @@ #include #include "absl/container/flat_hash_map.h" +#include "ortools/base/int_type_indexed_vector.h" #include "ortools/base/logging.h" #include "ortools/base/map_util.h" @@ -83,13 +84,13 @@ class RevRepository : public ReversibleInterface { }; // A basic reversible vector implementation. -template +template class RevVector : public ReversibleInterface { public: - const T& operator[](int index) const { return vector_[index]; } - T& operator[](int index) { + const T& operator[](IndexType index) const { return vector_[index]; } + T& operator[](IndexType index) { // Save on the stack first. - stack_.push_back({index, vector_[index]}); + if (!end_of_level_.empty()) stack_.push_back({index, vector_[index]}); return vector_[index]; } @@ -121,8 +122,8 @@ class RevVector : public ReversibleInterface { private: std::vector end_of_level_; // In stack_. - std::vector> stack_; - std::vector vector_; + std::vector> stack_; + gtl::ITIVector vector_; }; template