OR-Tools  9.3
ids_validator.cc
Go to the documentation of this file.
1// Copyright 2010-2021 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 <string>
23
24#include "absl/container/flat_hash_set.h"
25#include "absl/status/status.h"
26#include "absl/strings/str_cat.h"
27#include "absl/strings/string_view.h"
28#include "absl/types/span.h"
32
33namespace operations_research {
34namespace math_opt {
35
36namespace {
37absl::Status CheckSortedIdsSubsetWithIndexOffset(
38 const absl::Span<const int64_t> ids,
39 const absl::Span<const int64_t> universe, const int64_t offset) {
40 int id_index = 0;
41 int universe_index = 0;
42 // NOTE(user): in the common case where ids and/or universe is consecutive,
43 // we can avoid iterating though the list and do interval based checks.
44 while (id_index < ids.size() && universe_index < universe.size()) {
45 if (universe[universe_index] < ids[id_index]) {
46 ++universe_index;
47 } else if (universe[universe_index] == ids[id_index]) {
48 ++id_index;
49 } else {
50 break;
51 }
52 }
53 if (id_index < ids.size()) {
55 << "Bad id: " << ids[id_index] << " (at index: " << id_index + offset
56 << ") found";
57 }
58 return absl::OkStatus();
59}
60
61// Given a sorted, strictly increasing set of ids, provides `contains()` to
62// check if another id is in the set in O(1) time.
63//
64// Implementation note: when ids are consecutive, they are stored as a single
65// interval [lb, ub), otherwise they are stored as a hash table of integers.
66class FastIdCheck {
67 public:
68 // ids must be sorted with unique strictly increasing entries.
69 explicit FastIdCheck(const absl::Span<const int64_t> ids) {
70 if (ids.empty()) {
71 interval_mode_ = true;
72 } else if (ids.size() == ids.back() + 1 - ids.front()) {
73 interval_mode_ = true;
74 interval_lb_ = ids.front();
75 interval_ub_ = ids.back() + 1;
76 } else {
77 ids_ = absl::flat_hash_set<int64_t>(ids.begin(), ids.end());
78 }
79 }
80 bool contains(int64_t id) const {
81 if (interval_mode_) {
82 return id >= interval_lb_ && id < interval_ub_;
83 } else {
84 return ids_.contains(id);
85 }
86 }
87
88 private:
89 bool interval_mode_ = false;
90 int64_t interval_lb_ = 0;
91 int64_t interval_ub_ = 0;
92 absl::flat_hash_set<int64_t> ids_;
93};
94
95// Checks that the elements of ids and bad_list have no overlap.
96//
97// Assumed: ids and bad_list are sorted in increasing order, repeats allowed.
98absl::Status CheckSortedIdsNotBad(const absl::Span<const int64_t> ids,
99 const absl::Span<const int64_t> bad_list) {
100 int id_index = 0;
101 int bad_index = 0;
102 while (id_index < ids.size() && bad_index < bad_list.size()) {
103 if (bad_list[bad_index] < ids[id_index]) {
104 ++bad_index;
105 } else if (bad_list[bad_index] > ids[id_index]) {
106 ++id_index;
107 } else {
109 << "Bad id: " << ids[id_index] << " (at index: " << id_index
110 << ") found";
111 }
112 }
113 return absl::OkStatus();
114}
115} // namespace
116
117absl::Status CheckIdsRangeAndStrictlyIncreasing(absl::Span<const int64_t> ids) {
118 int64_t previous{-1};
119 for (int i = 0; i < ids.size(); previous = ids[i], ++i) {
120 if (ids[i] < 0 || ids[i] == std::numeric_limits<int64_t>::max()) {
122 << "Expected ids to be nonnegative and not max(int64_t) but at "
123 "index "
124 << i << " found id: " << ids[i];
125 }
126 if (ids[i] <= previous) {
128 << "Expected ids to be strictly increasing, but at index " << i
129 << " found id: " << ids[i] << " and at index " << i - 1
130 << " found id: " << ids[i - 1];
131 }
132 }
133 return absl::OkStatus();
134}
135
136absl::Status CheckSortedIdsSubset(const absl::Span<const int64_t> ids,
137 const absl::Span<const int64_t> universe) {
138 RETURN_IF_ERROR(CheckSortedIdsSubsetWithIndexOffset(ids, universe, 0));
139 return absl::OkStatus();
140}
141
142absl::Status CheckUnsortedIdsSubset(const absl::Span<const int64_t> ids,
143 const absl::Span<const int64_t> universe) {
144 if (ids.empty()) {
145 return absl::OkStatus();
146 }
147 const FastIdCheck id_check(universe);
148 for (int i = 0; i < ids.size(); ++i) {
149 if (!id_check.contains(ids[i])) {
151 << "Bad id: " << ids[i] << " (at index: " << i << ") not found";
152 }
153 }
154 return absl::OkStatus();
155}
156
157absl::Status IdUpdateValidator::IsValid() const {
158 for (int i = 0; i < deleted_ids_.size(); ++i) {
159 const int64_t deleted_id = deleted_ids_[i];
160 if (!old_ids_.HasId(deleted_id)) {
162 << "Tried to delete id: " << deleted_id << " (at index: " << i
163 << ") but it was not present";
164 }
165 }
166 if (!new_ids_.empty() && new_ids_.front() < old_ids_.next_free_id()) {
168 << "All new ids should be greater or equal to the first unused id: "
169 << old_ids_.next_free_id()
170 << " but the first new id was: " << new_ids_.front();
171 }
172 return absl::OkStatus();
173}
174
176 const absl::Span<const int64_t> ids) const {
177 RETURN_IF_ERROR(CheckSortedIdsNotBad(ids, deleted_ids_)) << " was deleted";
178 for (int i = 0; i < ids.size(); ++i) {
179 if (!old_ids_.HasId(ids[i])) {
181 << "Bad id: " << ids[i] << " (at index: " << i << ") not found";
182 }
183 }
184 return absl::OkStatus();
185}
186
188 const absl::Span<const int64_t> ids) const {
189 // Implementation:
190 // * Partition ids into "old" and "new"
191 // * Check that the old ids are in old_ids_ but not deleted_ids_.
192 // * Check that the new ids are in new_ids_.
193 size_t split_point = ids.size();
194 if (!new_ids_.empty()) {
195 split_point = std::distance(
196 ids.begin(), std::lower_bound(ids.begin(), ids.end(), new_ids_[0]));
197 }
199 CheckSortedIdsSubsetOfNotDeleted(ids.subspan(0, split_point)));
200 RETURN_IF_ERROR(CheckSortedIdsSubsetWithIndexOffset(ids.subspan(split_point),
201 new_ids_, split_point));
202 return absl::OkStatus();
203}
204
206 const absl::Span<const int64_t> ids) const {
207 if (ids.empty()) {
208 return absl::OkStatus();
209 }
210 const FastIdCheck deleted_fast(deleted_ids_);
211 const FastIdCheck new_fast(new_ids_);
212 for (int i = 0; i < ids.size(); ++i) {
213 const int64_t id = ids[i];
214 if (!new_ids_.empty() && id >= new_ids_[0]) {
215 if (!new_fast.contains(id)) {
217 << "Bad id: " << id << " (at index: " << i << ") not found";
218 }
219 } else if (!old_ids_.HasId(id)) {
221 << "Bad id: " << id << " (at index: " << i << ") not found";
222 } else if (deleted_fast.contains(id)) {
224 << "Bad id: " << id << " (at index: " << i << ") was deleted";
225 }
226 }
227 return absl::OkStatus();
228}
229
230absl::Status CheckIdsSubset(absl::Span<const int64_t> ids,
231 const IdNameBiMap& universe,
232 absl::string_view ids_description,
233 absl::string_view universe_description) {
234 for (int i = 0; i < ids.size(); ++i) {
235 const int64_t id = ids[i];
236 if (!universe.HasId(id)) {
238 << "Id: " << id << " (at index: " << i << ") in "
239 << ids_description << " is missing from " << universe_description;
240 }
241 }
242 return absl::OkStatus();
243}
244
245absl::Status CheckIdsIdentical(absl::Span<const int64_t> first_ids,
246 const IdNameBiMap& second_ids,
247 absl::string_view first_description,
248 absl::string_view second_description) {
249 if (first_ids.size() != second_ids.Size()) {
251 << first_description << " has size " << first_ids.size() << ", but "
252 << second_description << " has size " << second_ids.Size();
253 }
254 RETURN_IF_ERROR(CheckIdsSubset(first_ids, second_ids, first_description,
255 second_description));
256 return absl::OkStatus();
257}
258
259} // namespace math_opt
260} // namespace operations_research
int64_t max
Definition: alldiff_cst.cc:140
#define RETURN_IF_ERROR(expr)
absl::Status CheckSortedIdsSubsetOfFinal(const absl::Span< const int64_t > ids) const
absl::Status CheckIdsSubsetOfFinal(const absl::Span< const int64_t > ids) const
absl::Status CheckSortedIdsSubsetOfNotDeleted(const absl::Span< const int64_t > ids) const
double lower_bound
absl::Status CheckUnsortedIdsSubset(const absl::Span< const int64_t > ids, const absl::Span< const int64_t > universe)
absl::Status CheckIdsRangeAndStrictlyIncreasing(absl::Span< const int64_t > ids)
absl::Status CheckSortedIdsSubset(const absl::Span< const int64_t > ids, const absl::Span< const int64_t > universe)
absl::Status CheckIdsSubset(absl::Span< const int64_t > ids, const IdNameBiMap &universe, absl::string_view ids_description, absl::string_view universe_description)
absl::Status CheckIdsIdentical(absl::Span< const int64_t > first_ids, const IdNameBiMap &second_ids, absl::string_view first_description, absl::string_view second_description)
Collection of objects used to extend the Constraint Solver library.
StatusBuilder InvalidArgumentErrorBuilder()
double distance