Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
glpk_sparse_vector.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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
15
16#include <functional>
17#include <vector>
18
19#include "absl/log/check.h"
21
23
25 : capacity_(capacity),
26 index_to_entry_(capacity + 1, kNotPresent),
27 indices_(capacity + 1, -1),
28 values_(capacity + 1, 0.0) {
29 CHECK_GE(capacity, 0);
30}
31
33 // Resets the elements of the index_to_entry_ map we have modified.
34 for (int i = 1; i <= size_; ++i) {
35 index_to_entry_[indices_[i]] = kNotPresent;
36 }
37
38 // Cleanup the used items to make sure we don't reuse those values by mistake
39 // later.
40 for (int i = 1; i <= size_; ++i) {
41 indices_[i] = -1;
42 values_[i] = 0.0;
43 }
44
45 size_ = 0;
46}
47
49 std::function<int(int* indices, double* values)> getter) {
50 CHECK(getter != nullptr);
51
52 Clear();
53
54 size_ = getter(indices_.data(), values_.data());
55
56 CHECK_GE(size_, 0);
57 CHECK_LE(size_, capacity_);
58
59 // We don't know if the GLPK API has written to the first element but we reset
60 // those values anyway.
61 indices_[0] = -1;
62 values_[0] = 0.0;
63
64 // Update index_to_entry_.
65 for (int i = 1; i <= size_; ++i) {
66 const int index = indices_[i];
67 CHECK_GE(index, 1);
68 CHECK_LE(index, capacity_);
69 CHECK_EQ(index_to_entry_[index], kNotPresent) << "duplicated: " << index;
70 index_to_entry_[index] = i;
71 }
72}
73
74} // namespace operations_research::math_opt
int capacity() const
Returns the capacity (the size of the vector if it was dense).
void Load(std::function< int(int *indices, double *values)> getter)
int index
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28