Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
sparse_vector_validator.h
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
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
26namespace operations_research {
27namespace math_opt {
28
29template <typename T>
30absl::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
40template <typename T,
41 typename = std::enable_if_t<!std::is_floating_point<T>::value> >
42absl::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
48template <typename T,
49 typename = std::enable_if_t<!std::is_floating_point<T>::value> >
50absl::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
57template <typename T,
58 typename = std::enable_if_t<std::is_floating_point<T>::value> >
59absl::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
71template <typename T,
72 typename = std::enable_if_t<std::is_floating_point<T>::value> >
73absl::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::Status CheckIdsAndValuesSize(const SparseVectorView< T > &vector_view, absl::string_view value_name="values")
absl::Status CheckValues(const SparseVectorView< T > &vector_view, absl::string_view value_name="values")
absl::Status CheckIdsAndValues(const SparseVectorView< T > &vector_view, absl::string_view value_name="values")
absl::Status CheckScalar(const double value, const DoubleOptions &options)
Checks value is not NaN and satisfies the additional conditions in options.
absl::Status CheckIdsRangeAndStrictlyIncreasing(absl::Span< const int64_t > ids)
In SWIG mode, we don't want anything besides these top-level includes.