45 #ifndef OR_TOOLS_MATH_OPT_CORE_SPARSE_VECTOR_VIEW_H_ 46 #define OR_TOOLS_MATH_OPT_CORE_SPARSE_VECTOR_VIEW_H_ 56 #include "google/protobuf/message.h" 57 #include "absl/container/flat_hash_map.h" 58 #include "absl/types/span.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::RepeatedPtrField<T>& values) {
157 template <
typename SparseVectorProto,
158 typename T = sparse_value_type<SparseVectorProto>>
167 template <
typename T>
168 SparseVectorView<T>::const_iterator::const_iterator(
169 const SparseVectorView<T>* view,
bool at_end)
172 index_ = view_->ids_size();
176 template <
typename T>
177 typename SparseVectorView<T>::const_iterator::value_type
179 return {view_->ids(index_), view_->values(index_)};
182 template <
typename T>
190 template <
typename T>
194 return index_ != other.index_;
197 template <
typename T>
204 template <
typename T>
210 template <
typename T>
211 template <
typename IndexType>
213 absl::flat_hash_map<IndexType, T> result;
214 CHECK_EQ(ids_size(), values_size());
215 result.reserve(ids_size());
216 for (
const auto& [
id,
value] : *
this) {
225 #endif // OR_TOOLS_MATH_OPT_CORE_SPARSE_VECTOR_VIEW_H_ absl::Span< const int64_t > ids() const
absl::Span< const T > values() const
int64_t ids(int index) const
SparseVectorView< T > MakeView(absl::Span< const int64_t > ids, const Collection &values)
const_iterator end() const
const T & values(int index) const
absl::flat_hash_map< IndexType, T > as_map()
SparseVectorView::value_type value_type
const_iterator & operator++()
void InsertOrDie(Collection *const collection, const typename Collection::value_type &value)
std::pair< int64_t, T > value_type
SparseVectorView(absl::Span< const int64_t > ids, absl::Span< const T > values)
LinearExpr operator *(LinearExpr lhs, double rhs)
std::forward_iterator_tag iterator_category
bool operator!=(const STLCountingAllocator< T, A > &a, const STLCountingAllocator< T, A > &b)
#define CHECK_EQ(val1, val2)
#define DCHECK_EQ(val1, val2)
const_iterator begin() const
Collection of objects used to extend the Constraint Solver library.
bool operator!=(const const_iterator &other) const
typename std::remove_reference< decltype(SparseVector().values())>::type::value_type sparse_value_type
#define DCHECK_LT(val1, val2)
value_type operator *() const