OR-Tools  9.6
sparse_vector_validator.h
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 
14 #ifndef OR_TOOLS_MATH_OPT_VALIDATORS_SPARSE_VECTOR_VALIDATOR_H_
15 #define OR_TOOLS_MATH_OPT_VALIDATORS_SPARSE_VECTOR_VALIDATOR_H_
16 #include <type_traits>
17 
18 #include "absl/status/status.h"
19 #include "absl/strings/str_cat.h"
20 #include "absl/strings/string_view.h"
25 
26 namespace operations_research {
27 namespace math_opt {
28 
29 template <typename T>
30 absl::Status CheckIdsAndValuesSize(const SparseVectorView<T>& vector_view,
31  absl::string_view value_name = "values") {
32  if (vector_view.ids_size() != vector_view.values_size()) {
33  return absl::InvalidArgumentError(absl::StrCat(
34  "Ids size= ", vector_view.ids_size(), " should be equal to ",
35  value_name, " size= ", vector_view.values_size()));
36  }
37  return absl::OkStatus();
38 }
39 
40 template <typename T,
42 absl::Status CheckValues(const SparseVectorView<T>& vector_view,
43  absl::string_view value_name = "values") {
44  RETURN_IF_ERROR(CheckIdsAndValuesSize(vector_view, value_name));
45  return absl::OkStatus();
46 }
47 
48 template <typename T,
50 absl::Status CheckIdsAndValues(const SparseVectorView<T>& vector_view,
51  absl::string_view value_name = "values") {
53  RETURN_IF_ERROR(CheckValues(vector_view, value_name));
54  return absl::OkStatus();
55 }
56 
57 template <typename T,
59 absl::Status CheckValues(const SparseVectorView<T>& vector_view,
60  const DoubleOptions& options,
61  absl::string_view value_name = "values") {
62  RETURN_IF_ERROR(CheckIdsAndValuesSize(vector_view, value_name));
63  for (int i = 0, length = vector_view.values_size(); i < length; ++i) {
64  RETURN_IF_ERROR(CheckScalar(vector_view.values(i), options))
65  << absl::StrCat(" in: ", value_name, " for id: ", vector_view.ids(i),
66  " (at index: ", i, ")");
67  }
68  return absl::OkStatus();
69 }
70 
71 template <typename T,
73 absl::Status CheckIdsAndValues(const SparseVectorView<T>& vector_view,
74  const DoubleOptions& options,
75  absl::string_view value_name = "values") {
77  RETURN_IF_ERROR(CheckValues(vector_view, options, value_name));
78  return absl::OkStatus();
79 }
80 
81 } // namespace math_opt
82 } // namespace operations_research
83 
84 #endif // OR_TOOLS_MATH_OPT_VALIDATORS_SPARSE_VECTOR_VALIDATOR_H_
#define RETURN_IF_ERROR(expr)
absl::Span< const int64_t > ids() const
int64_t value
absl::Status CheckIdsAndValuesSize(const SparseVectorView< T > &vector_view, absl::string_view value_name="values")
absl::Status CheckScalar(const double value, const DoubleOptions &options)
absl::Status CheckValues(const SparseVectorView< T > &vector_view, absl::string_view value_name="values")
absl::Status CheckIdsRangeAndStrictlyIncreasing(absl::Span< const int64_t > ids)
absl::Status CheckIdsAndValues(const SparseVectorView< T > &vector_view, absl::string_view value_name="values")
Collection of objects used to extend the Constraint Solver library.