Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
string_array.h
Go to the documentation of this file.
1// Copyright 2010-2024 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_UTIL_STRING_ARRAY_H_
15#define OR_TOOLS_UTIL_STRING_ARRAY_H_
16
17#include <string>
18#include <vector>
19
20#include "absl/strings/string_view.h"
21
22namespace operations_research {
23// ---------- Pretty Print Helpers ----------
24
25// See the straightforward (and unique) usage of this macro below.
26#define RETURN_STRINGIFIED_VECTOR(vector, separator, method) \
27 std::string out; \
28 for (int i = 0; i < vector.size(); ++i) { \
29 if (i > 0) out += separator; \
30 out += vector[i] method; \
31 } \
32 return out
33
34// Converts a vector into a string by calling the given method (or simply
35// getting the given string member), on all elements, and concatenating
36// the obtained strings with the given separator.
37
38// Join v[i].DebugString().
39template <class T>
40std::string JoinDebugString(const std::vector<T>& v,
41 absl::string_view separator) {
42 RETURN_STRINGIFIED_VECTOR(v, separator, .DebugString());
43}
44
45// Join v[i]->DebugString().
46template <class T>
47std::string JoinDebugStringPtr(const std::vector<T>& v,
48 absl::string_view separator) {
49 RETURN_STRINGIFIED_VECTOR(v, separator, ->DebugString());
50}
51
52// Join v[i]->name().
53template <class T>
54std::string JoinNamePtr(const std::vector<T>& v, absl::string_view separator) {
55 RETURN_STRINGIFIED_VECTOR(v, separator, ->name());
56}
57
58// Join v[i]->name.
59template <class T>
60std::string JoinNameFieldPtr(const std::vector<T>& v,
61 absl::string_view separator) {
62 RETURN_STRINGIFIED_VECTOR(v, separator, ->name);
63}
64
65#undef RETURN_STRINGIFIED_VECTOR
66
67} // namespace operations_research
68#endif // OR_TOOLS_UTIL_STRING_ARRAY_H_
const std::string name
A name for logging purposes.
In SWIG mode, we don't want anything besides these top-level includes.
std::string JoinDebugStringPtr(const std::vector< T > &v, absl::string_view separator)
Join v[i]->DebugString().
std::string JoinNameFieldPtr(const std::vector< T > &v, absl::string_view separator)
Join v[i]->name.
std::string JoinNamePtr(const std::vector< T > &v, absl::string_view separator)
Join v[i]->name().
std::string JoinDebugString(const std::vector< T > &v, absl::string_view separator)
Join v[i].DebugString().
#define RETURN_STRINGIFIED_VECTOR(vector, separator, method)
-------— Pretty Print Helpers -------—