OR-Tools  9.2
name_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 <cstdint>
17#include <string>
18
20#include "absl/container/flat_hash_map.h"
21#include "absl/status/status.h"
22#include "absl/strings/str_cat.h"
23#include "absl/strings/string_view.h"
24#include "absl/types/span.h"
30
31namespace operations_research {
32namespace math_opt {
33
34absl::Status CheckNameVector(
35 const SparseVectorView<const std::string*>& name_vector,
36 const bool check_unique) {
37 if (name_vector.values().empty()) {
38 // Names are optional.
39 return absl::OkStatus();
40 }
41 RETURN_IF_ERROR(CheckIdsAndValuesSize(name_vector, "names"));
42 absl::flat_hash_map<absl::string_view, int64_t> used_variable_names;
43 if (check_unique) {
44 for (const auto [id, name_pointer] : name_vector) {
45 const std::string& name = *name_pointer;
46 if (!name.empty()) {
47 if (!gtl::InsertIfNotPresent(&used_variable_names, {name, id})) {
48 return absl::InvalidArgumentError(
49 absl::StrCat("Found name: ", name, " twice, for ids ", id,
50 " and ", used_variable_names.at(name)));
51 }
52 }
53 }
54 }
55 return absl::OkStatus();
56}
57
58absl::Status CheckNewNames(
59 const IdNameBiMap& old_names,
60 const SparseVectorView<const std::string*>& new_names) {
61 if (old_names.Empty()) {
62 return absl::OkStatus();
63 }
64 for (const auto [id, name_pointer] : new_names) {
65 const std::string& new_name = *name_pointer;
66 if (!new_name.empty() && old_names.HasName(new_name)) {
67 return absl::InvalidArgumentError(
68 absl::StrCat("Found name: ", new_name, " twice, for ids ", id,
69 " and ", old_names.nonempty_name_to_id().at(new_name)));
70 }
71 }
72 return absl::OkStatus();
73}
74
75} // namespace math_opt
76} // namespace operations_research
bool HasName(absl::string_view name) const
const absl::flat_hash_map< absl::string_view, int64_t > & nonempty_name_to_id() const
Definition: model_summary.h:79
const std::string name
bool InsertIfNotPresent(Collection *const collection, const typename Collection::value_type &value)
Definition: map_util.h:122
absl::Status CheckIdsAndValuesSize(const SparseVectorView< T > &vector_view, absl::string_view value_name="values")
absl::Status CheckNewNames(const IdNameBiMap &old_names, const SparseVectorView< const std::string * > &new_names)
absl::Status CheckNameVector(const SparseVectorView< const std::string * > &name_vector, const bool check_unique)
Collection of objects used to extend the Constraint Solver library.
#define RETURN_IF_ERROR(expr)
Definition: status_macros.h:29