Skip to content

Commit

Permalink
used strongly typed reversible data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jan 21, 2020
1 parent 0c4fdd7 commit e9342c2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
5 changes: 3 additions & 2 deletions ortools/sat/integer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion ortools/sat/integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1121,8 +1121,9 @@ class GenericLiteralWatcher : public SatPropagator {
std::vector<bool> in_queue_;

// Data for each propagator.
DEFINE_INT_TYPE(IdType, int32);
std::vector<int> id_to_level_at_last_call_;
RevVector<int> id_to_greatest_common_level_since_last_call_;
RevVector<IdType, int> id_to_greatest_common_level_since_last_call_;
std::vector<std::vector<ReversibleInterface*>> id_to_reversible_classes_;
std::vector<std::vector<int*>> id_to_reversible_ints_;
std::vector<std::vector<int>> id_to_watch_indices_;
Expand Down
13 changes: 7 additions & 6 deletions ortools/util/rev.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <vector>

#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"

Expand Down Expand Up @@ -83,13 +84,13 @@ class RevRepository : public ReversibleInterface {
};

// A basic reversible vector implementation.
template <class T>
template <class IndexType, class T>
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];
}

Expand Down Expand Up @@ -121,8 +122,8 @@ class RevVector : public ReversibleInterface {

private:
std::vector<int> end_of_level_; // In stack_.
std::vector<std::pair<int, T>> stack_;
std::vector<T> vector_;
std::vector<std::pair<IndexType, T>> stack_;
gtl::ITIVector<IndexType, T> vector_;
};

template <class T>
Expand Down

0 comments on commit e9342c2

Please sign in to comment.