45 #ifndef OR_TOOLS_MATH_OPT_SPARSE_VECTOR_VIEW_H_
46 #define OR_TOOLS_MATH_OPT_SPARSE_VECTOR_VIEW_H_
54 #include "absl/container/flat_hash_map.h"
55 #include "absl/types/span.h"
56 #include "google/protobuf/message.h"
60 #include "ortools/math_opt/sparse_containers.pb.h"
67 template <
typename SparseVector>
69 decltype(SparseVector().values())>::type::value_type;
107 bool at_end =
false);
114 : ids_(std::move(
ids)), values_(std::move(
values)) {}
117 inline const_iterator
begin()
const;
118 inline const_iterator
end()
const;
120 absl::Span<const int64_t>
ids()
const {
return ids_; }
123 absl::Span<const T>
values()
const {
return values_; }
128 template <
typename IndexType>
129 absl::flat_hash_map<IndexType, T>
as_map();
132 absl::Span<const int64_t> ids_;
133 absl::Span<const T> values_;
139 template <
typename Collection,
typename T =
typename Collection::value_type>
141 const Collection& values) {
148 template <
typename T>
150 const google::protobuf::RepeatedField<int64_t>& ids,
151 const google::protobuf::RepeatedPtrField<T>& values) {
158 template <
typename SparseVectorProto,
159 typename T = sparse_value_type<SparseVectorProto>>
168 template <
typename T>
169 SparseVectorView<T>::const_iterator::const_iterator(
170 const SparseVectorView<T>* view,
bool at_end)
173 index_ = view_->ids_size();
177 template <
typename T>
178 typename SparseVectorView<T>::const_iterator::value_type
180 return {view_->ids(index_), view_->values(index_)};
183 template <
typename T>
191 template <
typename T>
195 return index_ != other.index_;
198 template <
typename T>
205 template <
typename T>
211 template <
typename T>
212 template <
typename IndexType>
214 absl::flat_hash_map<IndexType, T> result;
215 CHECK_EQ(ids_size(), values_size());
216 result.reserve(ids_size());
217 for (
const auto& [
id,
value] : *
this) {
#define CHECK_EQ(val1, val2)
#define DCHECK_LT(val1, val2)
#define DCHECK_EQ(val1, val2)
bool operator!=(const const_iterator &other) const
std::forward_iterator_tag iterator_category
SparseVectorView::value_type value_type
const_iterator & operator++()
value_type operator*() const
const_iterator begin() const
SparseVectorView(absl::Span< const int64_t > ids, absl::Span< const T > values)
std::pair< int64_t, T > value_type
absl::Span< const T > values() const
const T & values(int index) const
const_iterator end() const
absl::Span< const int64_t > ids() const
absl::flat_hash_map< IndexType, T > as_map()
int64_t ids(int index) const
void InsertOrDie(Collection *const collection, const typename Collection::value_type &value)
bool operator!=(const STLCountingAllocator< T, A > &a, const STLCountingAllocator< T, A > &b)
typename std::remove_reference< decltype(SparseVector().values())>::type::value_type sparse_value_type
SparseVectorView< T > MakeView(absl::Span< const int64_t > ids, const Collection &values)
Collection of objects used to extend the Constraint Solver library.
LinearExpr operator*(LinearExpr lhs, double rhs)