OR-Tools  9.6
sparse_coefficient_map.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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_STORAGE_SPARSE_COEFFICIENT_MAP_H_
15 #define OR_TOOLS_MATH_OPT_STORAGE_SPARSE_COEFFICIENT_MAP_H_
16 
17 #include <utility>
18 
19 #include "absl/container/flat_hash_map.h"
20 #include "absl/meta/type_traits.h"
22 #include "ortools/math_opt/sparse_containers.pb.h"
24 
26 
27 // Represents a sparse collection of linear terms: {double_i x VariableId_i}_i.
28 // All VariableIds not represented in the collection are implicitly treated as
29 // having zero coefficient.
30 //
31 // Internally it is a lightweight wrapper around absl::flat_hash_map that only
32 // explicitly stores nonzero elements.
34  public:
35  SparseCoefficientMap() = default;
36 
37  inline explicit SparseCoefficientMap(
38  absl::flat_hash_map<VariableId, double> terms);
39 
40  // Returns 0.0 by default if no value is set.
41  inline double get(const VariableId id) const;
42 
43  // Returns true if the stored value changes.
44  inline bool set(const VariableId id, const double coeff);
45 
46  const absl::flat_hash_map<VariableId, double>& terms() const {
47  return terms_;
48  }
49 
50  inline void clear();
51 
52  // Has no effect if id is not set.
53  inline void erase(VariableId id);
54 
55  SparseDoubleVectorProto Proto() const;
56 
57  private:
58  absl::flat_hash_map<VariableId, double> terms_;
59 };
60 
62 // Inline implementations
64 
66  absl::flat_hash_map<VariableId, double> terms)
67  : terms_(std::move(terms)) {
68  absl::erase_if(terms_, [](const auto& term) { return term.second == 0.0; });
69 }
70 
71 double SparseCoefficientMap::get(const VariableId id) const {
72  const auto it = terms_.find(id);
73  if (it == terms_.end()) {
74  return 0.0;
75  }
76  return it->second;
77 }
78 
79 bool SparseCoefficientMap::set(const VariableId id, const double coeff) {
80  if (coeff == 0.0) {
81  return terms_.erase(id) > 0;
82  }
83  const auto [iterator, inserted] = terms_.try_emplace(id, coeff);
84  if (inserted) {
85  return true;
86  }
87  if (iterator->second != coeff) {
88  iterator->second = coeff;
89  return true;
90  }
91  return false;
92 }
93 
94 void SparseCoefficientMap::clear() { terms_.clear(); }
95 
96 void SparseCoefficientMap::erase(const VariableId id) { terms_.erase(id); }
97 
98 } // namespace operations_research::math_opt
99 
100 #endif // OR_TOOLS_MATH_OPT_STORAGE_SPARSE_COEFFICIENT_MAP_H_
const absl::flat_hash_map< VariableId, double > & terms() const
bool set(const VariableId id, const double coeff)