OR-Tools  9.6
ids_validator.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 <stddef.h>
17 
18 #include <algorithm>
19 #include <cstdint>
20 #include <iterator>
21 #include <limits>
22 #include <optional>
23 #include <string>
24 
25 #include "absl/container/flat_hash_set.h"
26 #include "absl/status/status.h"
27 #include "absl/strings/str_cat.h"
28 #include "absl/strings/string_view.h"
29 #include "absl/types/span.h"
33 
35 
36 absl::Status CheckIdsRangeAndStrictlyIncreasing(absl::Span<const int64_t> ids) {
37  int64_t previous{-1};
38  for (int i = 0; i < ids.size(); previous = ids[i], ++i) {
39  if (ids[i] < 0 || ids[i] == std::numeric_limits<int64_t>::max()) {
41  << "Expected ids to be nonnegative and not max(int64_t) but at "
42  "index "
43  << i << " found id: " << ids[i];
44  }
45  if (ids[i] <= previous) {
47  << "Expected ids to be strictly increasing, but at index " << i
48  << " found id: " << ids[i] << " and at index " << i - 1
49  << " found id: " << ids[i - 1];
50  }
51  }
52  return absl::OkStatus();
53 }
54 
55 absl::Status CheckIdsSubset(absl::Span<const int64_t> ids,
56  const IdNameBiMap& universe,
57  std::optional<int64_t> upper_bound) {
58  for (const int64_t id : ids) {
59  if (upper_bound.has_value() && id >= *upper_bound) {
61  << "id " << id
62  << " should be less than upper bound: " << *upper_bound;
63  }
64  if (!universe.HasId(id)) {
65  return util::InvalidArgumentErrorBuilder() << "id " << id << " not found";
66  }
67  }
68  return absl::OkStatus();
69 }
70 
71 absl::Status CheckIdsSubset(absl::Span<const int64_t> ids,
72  const IdNameBiMap& universe,
73  absl::string_view ids_description,
74  absl::string_view universe_description) {
75  for (int i = 0; i < ids.size(); ++i) {
76  const int64_t id = ids[i];
77  if (!universe.HasId(id)) {
79  << "Id: " << id << " (at index: " << i << ") in "
80  << ids_description << " is missing from " << universe_description;
81  }
82  }
83  return absl::OkStatus();
84 }
85 
86 absl::Status CheckIdsIdentical(absl::Span<const int64_t> first_ids,
87  const IdNameBiMap& second_ids,
88  absl::string_view first_description,
89  absl::string_view second_description) {
90  if (first_ids.size() != second_ids.Size()) {
92  << first_description << " has size " << first_ids.size() << ", but "
93  << second_description << " has size " << second_ids.Size();
94  }
95  RETURN_IF_ERROR(CheckIdsSubset(first_ids, second_ids, first_description,
96  second_description));
97  return absl::OkStatus();
98 }
99 
100 } // namespace operations_research::math_opt
int64_t max
Definition: alldiff_cst.cc:140
#define RETURN_IF_ERROR(expr)
absl::Status CheckIdsRangeAndStrictlyIncreasing(absl::Span< const int64_t > ids)
absl::Status CheckIdsSubset(absl::Span< const int64_t > ids, const IdNameBiMap &universe, std::optional< int64_t > upper_bound)
absl::Status CheckIdsIdentical(absl::Span< const int64_t > first_ids, const IdNameBiMap &second_ids, absl::string_view first_description, absl::string_view second_description)
StatusBuilder InvalidArgumentErrorBuilder()
IntVar * upper_bound
Definition: routing.cc:1087