45#ifndef OR_TOOLS_MATH_OPT_CORE_SPARSE_VECTOR_VIEW_H_
46#define OR_TOOLS_MATH_OPT_CORE_SPARSE_VECTOR_VIEW_H_
54#include "absl/container/flat_hash_map.h"
55#include "absl/types/span.h"
56#include "google/protobuf/message.h"
62#include "ortools/math_opt/sparse_containers.pb.h"
69template <
typename SparseVector>
111 bool at_end =
false);
124 absl::Span<const int64_t>
ids()
const {
return ids_; }
127 absl::Span<const T>
values()
const {
return values_; }
136 template <
typename KeyType,
typename Storage>
137 absl::flat_hash_map<KeyType, T>
as_map(
const Storage* storage);
140 absl::Span<const int64_t> ids_;
141 absl::Span<const T> values_;
147template <
typename Collection,
typename T =
typename Collection::value_type>
149 const Collection& values) {
158 const google::protobuf::RepeatedField<int64_t>& ids,
159 const google::protobuf::RepeatedPtrField<T>& values) {
166template <
typename SparseVectorProto,
184SparseVectorView<T>::const_iterator::const_iterator(
185 const SparseVectorView<T>* view,
bool at_end)
188 index_ = view_->ids_size();
193typename SparseVectorView<T>::const_iterator::reference
194SparseVectorView<T>::const_iterator::operator*()
const {
195 return {view_->ids(index_), view_->values(index_)};
208 DCHECK_LT(index_, view_->ids_size());
216 DCHECK_EQ(view_, other.view_);
217 return index_ == other.index_;
223 return !(*
this == other);
229 DCHECK_EQ(ids_size(), values_size());
235 DCHECK_EQ(ids_size(), values_size());
240template <
typename KeyType,
typename Storage>
242 const Storage* storage) {
243 absl::flat_hash_map<KeyType, T> result;
244 CHECK_EQ(ids_size(), values_size());
245 result.reserve(ids_size());
246 for (
const auto& [
id,
value] : *
this) {
internal::ArrowOperatorProxy< reference > operator->() const
const_iterator & operator++()
SparseVectorView::value_type value_type
reference operator*() const
bool operator!=(const const_iterator &other) const
bool operator==(const const_iterator &other) const
std::forward_iterator_tag iterator_category
const_iterator begin() const
absl::flat_hash_map< KeyType, T > as_map(const Storage *storage)
absl::Span< const int64_t > ids() const
SparseVectorView()=default
const T & values(int index) const
const_iterator end() const
SparseVectorView(absl::Span< const int64_t > ids, absl::Span< const T > values)
absl::Span< const T > values() const
std::pair< int64_t, T > value_type
int64_t ids(int index) const
void InsertOrDie(Collection *const collection, const typename Collection::value_type &value)
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)
In SWIG mode, we don't want anything besides these top-level includes.
std::vector< int64_t > ids
Should be sorted (in increasing order) with all elements distinct.
std::vector< T > values
Must have equal length to ids.