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"
37absl::Status CheckSortedIdsSubsetWithIndexOffset(
38 const absl::Span<const int64_t> ids,
39 const absl::Span<const int64_t> universe,
const int64_t offset) {
41 int universe_index = 0;
44 while (id_index < ids.size() && universe_index < universe.size()) {
45 if (universe[universe_index] < ids[id_index]) {
47 }
else if (universe[universe_index] == ids[id_index]) {
53 if (id_index < ids.size()) {
55 <<
"Bad id: " << ids[id_index] <<
" (at index: " << id_index + offset
58 return absl::OkStatus();
69 explicit FastIdCheck(
const absl::Span<const int64_t> ids) {
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;
77 ids_ = absl::flat_hash_set<int64_t>(ids.begin(), ids.end());
80 bool contains(int64_t
id)
const {
82 return id >= interval_lb_ &&
id < interval_ub_;
84 return ids_.contains(
id);
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_;
98absl::Status CheckSortedIdsNotBad(
const absl::Span<const int64_t> ids,
99 const absl::Span<const int64_t> bad_list) {
102 while (id_index < ids.size() && bad_index < bad_list.size()) {
103 if (bad_list[bad_index] < ids[id_index]) {
105 }
else if (bad_list[bad_index] > ids[id_index]) {
109 <<
"Bad id: " << ids[id_index] <<
" (at index: " << id_index
113 return absl::OkStatus();
118 int64_t previous{-1};
119 for (
int i = 0; i < ids.size(); previous = ids[i], ++i) {
122 <<
"Expected ids to be nonnegative and not max(int64_t) but at "
124 << i <<
" found id: " << ids[i];
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];
133 return absl::OkStatus();
137 const absl::Span<const int64_t> universe) {
138 RETURN_IF_ERROR(CheckSortedIdsSubsetWithIndexOffset(ids, universe, 0));
139 return absl::OkStatus();
143 const absl::Span<const int64_t> universe) {
145 return absl::OkStatus();
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";
154 return absl::OkStatus();
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";
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: "
170 <<
" but the first new id was: " << new_ids_.front();
172 return absl::OkStatus();
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";
184 return absl::OkStatus();
188 const absl::Span<const int64_t> ids)
const {
193 size_t split_point = ids.size();
194 if (!new_ids_.empty()) {
200 RETURN_IF_ERROR(CheckSortedIdsSubsetWithIndexOffset(ids.subspan(split_point),
201 new_ids_, split_point));
202 return absl::OkStatus();
206 const absl::Span<const int64_t> ids)
const {
208 return absl::OkStatus();
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";
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";
227 return absl::OkStatus();
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;
242 return absl::OkStatus();
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();
255 second_description));
256 return absl::OkStatus();
#define RETURN_IF_ERROR(expr)
int64_t next_free_id() const
bool HasId(int64_t id) const
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
absl::Status IsValid() 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.
StatusBuilder InvalidArgumentErrorBuilder()