OR-Tools  9.1
model_summary.h
Go to the documentation of this file.
1// Copyright 2010-2021 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_MATH_OPT_CORE_MODEL_SUMMARY_H_
15#define OR_TOOLS_MATH_OPT_CORE_MODEL_SUMMARY_H_
16
17#include <cstdint>
18#include <initializer_list>
19#include <list>
20#include <string>
21#include <utility>
22
24#include "absl/container/flat_hash_map.h"
25#include "absl/strings/string_view.h"
28
29namespace operations_research {
30namespace math_opt {
31
32// Maintains a bidirectional mapping between names and ids, e.g. as used for
33// variables and linear constraints.
34//
35// The following invariants are enforced:
36// * Ids must be unique and increasing (in insertion order).
37// * Names must be either empty or unique.
39 public:
40 IdNameBiMap() = default;
41
42 // This constructor CHECKs that the input ids are sorted in increasing
43 // order. This constructor is expected to be used only for unit tests of
44 // validation code.
45 IdNameBiMap(std::initializer_list<std::pair<int64_t, absl::string_view>> ids);
46
47 // Will CHECK fail if id is <= largest_id().
48 // Will CHECK fail if id is present or if name is nonempty and present.
49 inline void Insert(int64_t id, std::string name);
50
51 // Will CHECK fail if id is not present.
52 inline void Erase(int64_t id);
53
54 inline bool HasId(int64_t id) const;
55 inline bool HasName(absl::string_view name) const;
56 inline bool Empty() const;
57 inline int Size() const;
58 inline int64_t LargestId() const;
59
60 // Iteration order is in increasing id order.
62 return id_to_name_;
63 }
64 const absl::flat_hash_map<absl::string_view, int64_t>& nonempty_name_to_id()
65 const {
66 return nonempty_name_to_id_;
67 }
68
69 private:
70 // Pointer stability for name strings and iterating in insertion order are
71 // both needed (so we do not use flat_hash_map).
73 absl::flat_hash_map<absl::string_view, int64_t> nonempty_name_to_id_;
74};
75
79};
80
82// Inline function implementations
84
85void IdNameBiMap::Insert(const int64_t id, std::string name) {
86 if (!id_to_name_.empty()) {
87 CHECK_GT(id, LargestId()) << name;
88 }
89 const auto [it, success] = id_to_name_.emplace(id, std::move(name));
90 CHECK(success) << "id: " << id;
91 const absl::string_view name_view(it->second);
92 if (!name_view.empty()) {
93 gtl::InsertOrDie(&nonempty_name_to_id_, name_view, id);
94 }
95}
96
97void IdNameBiMap::Erase(const int64_t id) {
98 const auto it = id_to_name_.find(id);
99 CHECK(it != id_to_name_.end()) << id;
100 const absl::string_view name_view(it->second);
101 if (!name_view.empty()) {
102 CHECK_EQ(1, nonempty_name_to_id_.erase(name_view))
103 << "name: " << name_view << " id: " << id;
104 }
105 id_to_name_.erase(it);
106}
107bool IdNameBiMap::HasId(const int64_t id) const {
108 return id_to_name_.contains(id);
109}
110bool IdNameBiMap::HasName(const absl::string_view name) const {
111 CHECK(!name.empty());
112 return nonempty_name_to_id_.contains(name);
113}
114
115bool IdNameBiMap::Empty() const { return id_to_name_.empty(); }
116
117int IdNameBiMap::Size() const { return id_to_name_.size(); }
118
119int64_t IdNameBiMap::LargestId() const {
120 CHECK(!Empty());
121 return id_to_name_.back().first;
122}
123
124} // namespace math_opt
125} // namespace operations_research
126
127#endif // OR_TOOLS_MATH_OPT_CORE_MODEL_SUMMARY_H_
#define CHECK(condition)
Definition: base/logging.h:491
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:698
#define CHECK_GT(val1, val2)
Definition: base/logging.h:703
bool HasName(absl::string_view name) const
const gtl::linked_hash_map< int64_t, std::string > & id_to_name() const
Definition: model_summary.h:61
void Insert(int64_t id, std::string name)
Definition: model_summary.h:85
const absl::flat_hash_map< absl::string_view, int64_t > & nonempty_name_to_id() const
Definition: model_summary.h:64
const std::string name
void InsertOrDie(Collection *const collection, const typename Collection::value_type &value)
Definition: map_util.h:154
Collection of objects used to extend the Constraint Solver library.