OR-Tools  9.6
glpk_sparse_vector.cc
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 
15 
16 #include <functional>
17 #include <vector>
18 
19 #include "ortools/base/logging.h"
20 
22 
24  : capacity_(capacity),
25  index_to_entry_(capacity + 1, kNotPresent),
26  indices_(capacity + 1, -1),
27  values_(capacity + 1, 0.0) {
28  CHECK_GE(capacity, 0);
29 }
30 
32  // Resets the elements of the index_to_entry_ map we have modified.
33  for (int i = 1; i <= size_; ++i) {
34  index_to_entry_[indices_[i]] = kNotPresent;
35  }
36 
37  // Cleanup the used items to make sure we don't reuse those values by mistake
38  // later.
39  for (int i = 1; i <= size_; ++i) {
40  indices_[i] = -1;
41  values_[i] = 0.0;
42  }
43 
44  size_ = 0;
45 }
46 
48  std::function<int(int* indices, double* values)> getter) {
49  CHECK(getter != nullptr);
50 
51  Clear();
52 
53  size_ = getter(indices_.data(), values_.data());
54 
55  CHECK_GE(size_, 0);
56  CHECK_LE(size_, capacity_);
57 
58  // We don't know if the GLPK API has written to the first element but we reset
59  // those values anyway.
60  indices_[0] = -1;
61  values_[0] = 0.0;
62 
63  // Update index_to_entry_.
64  for (int i = 1; i <= size_; ++i) {
65  const int index = indices_[i];
66  CHECK_GE(index, 1);
67  CHECK_LE(index, capacity_);
68  CHECK_EQ(index_to_entry_[index], kNotPresent) << "duplicated: " << index;
69  index_to_entry_[index] = i;
70  }
71 }
72 
73 } // namespace operations_research::math_opt
void Load(std::function< int(int *indices, double *values)> getter)
int index
int64_t capacity