OR-Tools  9.3
ids_validator.h
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
14#ifndef OR_TOOLS_MATH_OPT_VALIDATORS_IDS_VALIDATOR_H_
15#define OR_TOOLS_MATH_OPT_VALIDATORS_IDS_VALIDATOR_H_
16
17#include <stdint.h>
18
19#include "absl/status/status.h"
20#include "absl/strings/string_view.h"
21#include "absl/types/span.h"
23
24namespace operations_research {
25namespace math_opt {
26
27// Checks that the input ids are in [0, max(int64_t)) range and that their are
28// strictly increasing.
29absl::Status CheckIdsRangeAndStrictlyIncreasing(absl::Span<const int64_t> ids);
30
31// Checks that the elements of ids are a subset of universe.
32//
33// Assumed: ids and universe are sorted in increasing order, repeats allowed.
34absl::Status CheckSortedIdsSubset(const absl::Span<const int64_t> ids,
35 const absl::Span<const int64_t> universe);
36
37// Checks that the elements of ids are a subset of universe.
38//
39// Assumed: universe are sorted in strictly increasing order (no repeats). No
40// assumptions on ids.
41absl::Status CheckUnsortedIdsSubset(const absl::Span<const int64_t> ids,
42 const absl::Span<const int64_t> universe);
43
44// Checks that the elements of ids are a subset of universe. Elements of ids
45// do not need to be sorted or distinct.
46absl::Status CheckIdsSubset(absl::Span<const int64_t> ids,
47 const IdNameBiMap& universe,
48 absl::string_view ids_description,
49 absl::string_view universe_description);
50
51// first_ids and second_ids must include distinct ids.
52absl::Status CheckIdsIdentical(absl::Span<const int64_t> first_ids,
53 const IdNameBiMap& second_ids,
54 absl::string_view first_description,
55 absl::string_view second_description);
56
57// Provides a unified view of the id sets:
58// * NOT_DELETED = old - deleted
59// * FINAL = old - deleted + new
60// so users can validate if a list of ids (sorted or unsorted) is a subset of
61// either of the sets above.
62//
63// Implementation note: this class does not allocate by default, but some
64// functions will allocate at most O(#deleted + #new).
66 public:
67 // deleted_ids and new_ids must be sorted with unique strictly increasing
68 // entries.
70 const absl::Span<const int64_t> deleted_ids,
71 const absl::Span<const int64_t> new_ids)
72 : old_ids_(old_ids), deleted_ids_(deleted_ids), new_ids_(new_ids) {}
73
74 // Returns true if the sets of ids passed to the constructor are valid.
75 absl::Status IsValid() const;
76
77 // Checks that ids is a subset of NOT_DELETED = old_ids_ - deleted_ids_.
78 //
79 // ids must be sorted in increasing order (repeats are allowed).
81 const absl::Span<const int64_t> ids) const;
82
83 // Checks that ids is a subset of FINAL = old_ids_ - deleted_ids_ + new_ids_.
84 //
85 // ids must be sorted in increasing order (repeats are allowed).
86 absl::Status CheckSortedIdsSubsetOfFinal(
87 const absl::Span<const int64_t> ids) const;
88
89 // Checks that ids is a subset of FINAL = old_ids_ - deleted_ids_ + new_ids_.
90 //
91 // If ids is sorted, prefer CheckSortedIdsSubsetOfFinal.
92 absl::Status CheckIdsSubsetOfFinal(const absl::Span<const int64_t> ids) const;
93
94 private:
95 // NOT OWNED
96 const IdNameBiMap& old_ids_;
97 const absl::Span<const int64_t> deleted_ids_;
98 const absl::Span<const int64_t> new_ids_;
99};
100
101} // namespace math_opt
102} // namespace operations_research
103
104#endif // OR_TOOLS_MATH_OPT_VALIDATORS_IDS_VALIDATOR_H_
absl::Status CheckSortedIdsSubsetOfFinal(const absl::Span< const int64_t > ids) const
absl::Status CheckIdsSubsetOfFinal(const absl::Span< const int64_t > ids) const
IdUpdateValidator(const IdNameBiMap &old_ids, const absl::Span< const int64_t > deleted_ids, const absl::Span< const int64_t > new_ids)
Definition: ids_validator.h:69
absl::Status CheckSortedIdsSubsetOfNotDeleted(const absl::Span< const int64_t > ids) const
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.