Skip to content

Commit 65c15eb

Browse files
committed
base: export update from google3
1 parent ba338d4 commit 65c15eb

14 files changed

Lines changed: 152 additions & 177 deletions

ortools/base/BUILD.bazel

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ cc_library(
145145
"//conditions:default": [],
146146
}),
147147
deps = [
148-
":strong_vector",
149-
"@abseil-cpp//absl/container:inlined_vector",
148+
":intops",
149+
":strong_int",
150150
],
151151
)
152152

@@ -162,6 +162,7 @@ cc_test(
162162
":dump_vars",
163163
":strong_int",
164164
":strong_vector",
165+
"@abseil-cpp//absl/numeric:int128",
165166
"@googletest//:gtest_main",
166167
],
167168
)
@@ -271,11 +272,6 @@ cc_library(
271272
],
272273
)
273274

274-
cc_library(
275-
name = "memfile",
276-
hdrs = ["memfile.h"],
277-
)
278-
279275
cc_library(
280276
name = "memutil",
281277
hdrs = ["memutil.h"],
@@ -287,11 +283,6 @@ cc_library(
287283
deps = ["//ortools/base:hash"],
288284
)
289285

290-
cc_library(
291-
name = "mutable_memfile",
292-
hdrs = ["mutable_memfile.h"],
293-
)
294-
295286
cc_library(
296287
name = "numbers",
297288
srcs = ["numbers.cc"],

ortools/base/dump_vars.h

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@
3939
#ifndef ORTOOLS_BASE_DUMP_VARS_H_
4040
#define ORTOOLS_BASE_DUMP_VARS_H_
4141

42+
#include <cstddef>
43+
#include <iterator>
4244
#include <optional>
4345
#include <ostream>
4446
#include <sstream>
4547
#include <string>
48+
#include <string_view>
49+
#include <type_traits>
4650
#include <utility>
4751
#include <vector>
4852

49-
#include "absl/container/inlined_vector.h"
50-
#include "ortools/base/strong_vector.h"
53+
#include "ortools/base/strong_int.h"
5154

5255
/* need extra level to force extra eval */
5356
#define DUMP_FOR_EACH_N0(F)
@@ -110,42 +113,44 @@
110113
namespace operations_research::base {
111114
namespace internal_dump_vars {
112115

113-
// needed by routing
114116
template <typename T>
115-
std::ostream& operator<<(std::ostream& os,
116-
const ::absl::InlinedVector<T, 8>& vec) {
117-
for (T it : vec) {
118-
os << ::std::to_string(it) << ',';
117+
decltype(auto) FormatValue(const T& val) {
118+
if constexpr (std::is_floating_point_v<std::remove_cvref_t<T>>) {
119+
return ::std::to_string(val);
120+
} else {
121+
return val;
119122
}
120-
return os;
121123
}
122124

123-
// needed by algorithms tests
124125
template <typename T>
125-
std::ostream& operator<<(std::ostream& os, const ::std::vector<T>& vec) {
126-
for (T it : vec) {
127-
os << ::std::to_string(it) << ',';
126+
concept Range = requires(T& t) {
127+
{ std::begin(t) } -> std::input_iterator;
128+
{ std::end(t) } -> std::sentinel_for<decltype(std::begin(t))>;
129+
};
130+
131+
template <typename T>
132+
requires Range<T> && (!std::is_convertible_v<T, std::string_view>)
133+
std::ostream& operator<<(std::ostream& os, const T& t) {
134+
for (const auto& value : t) {
135+
os << FormatValue(value) << ',';
128136
}
129137
return os;
130138
}
131139

132140
template <typename T>
133141
std::ostream& operator<<(std::ostream& os, const ::std::optional<T>& opt) {
134-
if (opt.has_value())
135-
os << ::std::to_string(opt.value());
136-
else
142+
if (opt.has_value()) {
143+
os << FormatValue(opt.value());
144+
} else {
137145
os << "(none)";
146+
}
138147
return os;
139148
}
140149

141-
// needed by graph tests
142150
template <typename T, typename U>
143151
std::ostream& operator<<(std::ostream& os,
144-
const ::util_intops::StrongVector<T, U>& vec) {
145-
for (U it : vec) {
146-
os << ::std::to_string(it) << ',';
147-
}
148-
return os;
152+
const util_intops::StrongInt<T, U>& t) {
153+
return os << t.value();
149154
}
150155

151156
using DumpNames = ::std::vector<::std::string>;
@@ -155,12 +160,12 @@ struct print_fields {
155160

156161
template <class T>
157162
void operator()(const T& t) {
158-
os << names[n++] << kv_sep << t;
163+
os << names[n++] << kv_sep << FormatValue(t);
159164
}
160165

161166
template <class T1, class T2, class... Ts>
162167
void operator()(const T1& t1, const T2& t2, const Ts&... ts) {
163-
os << names[n++] << kv_sep << t1 << field_sep;
168+
os << names[n++] << kv_sep << FormatValue(t1) << field_sep;
164169
(*this)(t2, ts...);
165170
}
166171

ortools/base/dump_vars_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <utility>
2323
#include <vector>
2424

25+
#include "absl/numeric/int128.h"
2526
#include "gtest/gtest.h"
2627
#include "ortools/base/strong_int.h"
2728
#include "ortools/base/strong_vector.h"
@@ -190,6 +191,19 @@ TEST(DumpVars, LazyEvaluation) {
190191
}
191192
}
192193

194+
TEST(DumpVars, Int128) {
195+
absl::int128 i128 = 123456789;
196+
absl::uint128 ui128 = 987654321;
197+
EXPECT_EQ("i128 = 123456789", ToString(DUMP_VARS(i128)));
198+
EXPECT_EQ("ui128 = 987654321", ToString(DUMP_VARS(ui128)));
199+
200+
std::vector<absl::int128> vec = {i128};
201+
EXPECT_EQ("vec = 123456789,", ToString(DUMP_VARS(vec)));
202+
203+
std::optional<absl::uint128> opt = ui128;
204+
EXPECT_EQ("opt = 987654321", ToString(DUMP_VARS(opt)));
205+
}
206+
193207
TEST(DumpVars, TemporaryLifetime) {
194208
EXPECT_EQ(R"(std::string_view(std::string("hello")) = hello)",
195209
ToString(DUMP_VARS(std::string_view(std::string("hello")))));

ortools/base/file.cc

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ namespace {
4949
enum class Format { NORMAL_FILE, GZIP_FILE, BZIP2_FILE };
5050

5151
static Format GetFormatFromName(absl::string_view name) {
52-
const int size = name.size();
53-
if (size > 4 && name.substr(size - 3) == ".gz") {
52+
if (name.ends_with(".gz")) {
5453
return Format::GZIP_FILE;
55-
} else if (size > 5 && name.substr(size - 4) == ".bz2") {
54+
} else if (name.ends_with(".bz2")) {
5655
return Format::BZIP2_FILE;
5756
} else {
5857
return Format::NORMAL_FILE;
@@ -258,33 +257,29 @@ File* File::OpenOrDie(absl::string_view file_name, absl::string_view mode) {
258257
}
259258

260259
File* File::Open(absl::string_view file_name, absl::string_view mode) {
261-
std::string null_terminated_name = std::string(file_name);
262-
std::string null_terminated_mode = std::string(mode);
260+
const std::string filename = std::string(file_name);
261+
std::string mode_str(mode);
263262
#if defined(_MSC_VER)
264-
if (null_terminated_mode == "r") {
265-
null_terminated_mode = "rb";
266-
} else if (null_terminated_mode == "w") {
267-
null_terminated_mode = "wb";
263+
if (mode_str == "r") {
264+
mode_str = "rb";
265+
} else if (mode_str == "w") {
266+
mode_str = "wb";
268267
}
269268
#endif
270-
const Format format = GetFormatFromName(file_name);
271-
switch (format) {
269+
switch (GetFormatFromName(file_name)) {
272270
case Format::NORMAL_FILE: {
273-
FILE* c_file =
274-
fopen(null_terminated_name.c_str(), null_terminated_mode.c_str());
271+
FILE* c_file = fopen(filename.c_str(), mode_str.c_str());
275272
if (c_file == nullptr) return nullptr;
276273
return new CFile(c_file, file_name);
277274
}
278275
case Format::GZIP_FILE: {
279-
gzFile gz_file =
280-
gzopen(null_terminated_name.c_str(), null_terminated_mode.c_str());
281-
if (!gz_file) return nullptr;
276+
gzFile gz_file = gzopen(filename.c_str(), mode_str.c_str());
277+
if (gz_file == nullptr) return nullptr;
282278
return new GzFile(gz_file, file_name);
283279
}
284280
case Format::BZIP2_FILE: {
285-
BZFILE* bz_file = BZ2_bzopen(null_terminated_name.c_str(),
286-
null_terminated_mode.c_str());
287-
if (!bz_file) return nullptr;
281+
BZFILE* bz_file = BZ2_bzopen(filename.c_str(), mode_str.c_str());
282+
if (bz_file == nullptr) return nullptr;
288283
return new Bz2File(bz_file, file_name);
289284
}
290285
}
@@ -325,6 +320,7 @@ absl::string_view File::filename() const { return name_; }
325320
void File::Init() {}
326321

327322
namespace file {
323+
328324
absl::Status Open(absl::string_view file_name, absl::string_view mode, File** f,
329325
Options options) {
330326
if (options == Defaults()) {
@@ -333,7 +329,7 @@ absl::Status Open(absl::string_view file_name, absl::string_view mode, File** f,
333329
return absl::OkStatus();
334330
}
335331
}
336-
return absl::Status(absl::StatusCode::kInvalidArgument,
332+
return absl::Status(absl::StatusCode::kNotFound,
337333
absl::StrCat("Could not open '", file_name, "'"));
338334
}
339335

@@ -371,7 +367,7 @@ absl::Status GetContents(absl::string_view file_name, std::string* output,
371367

372368
file->Close(options).IgnoreError(); // Even if ReadToString() fails!
373369

374-
return absl::Status(absl::StatusCode::kInvalidArgument,
370+
return absl::Status(absl::StatusCode::kNotFound,
375371
absl::StrCat("Could not read from '", file_name, "'."));
376372
}
377373

@@ -490,7 +486,7 @@ absl::Status SetBinaryProto(absl::string_view file_name,
490486

491487
absl::Status Delete(absl::string_view path, Options options) {
492488
if (options == Defaults()) {
493-
std::string null_terminated_path = std::string(path);
489+
const std::string null_terminated_path = std::string(path);
494490
if (remove(null_terminated_path.c_str()) == 0) return absl::OkStatus();
495491
}
496492
return absl::Status(absl::StatusCode::kInvalidArgument,
@@ -499,7 +495,7 @@ absl::Status Delete(absl::string_view path, Options options) {
499495

500496
absl::Status Exists(absl::string_view path, Options options) {
501497
if (options == Defaults()) {
502-
std::string null_terminated_path = std::string(path);
498+
const std::string null_terminated_path = std::string(path);
503499
if (access(null_terminated_path.c_str(), F_OK) == 0) {
504500
return absl::OkStatus();
505501
}

ortools/base/filesystem.cc

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "ortools/base/filesystem.h"
1515

1616
#include <algorithm>
17+
#include <cstdint>
1718
#include <exception> // IWYU pragma: keep
1819
#include <filesystem> // NOLINT
1920
#include <regex> // NOLINT
@@ -23,25 +24,20 @@
2324
#include <vector>
2425

2526
#include "absl/status/status.h"
27+
#include "absl/status/statusor.h"
28+
#include "absl/strings/str_cat.h"
2629
#include "absl/strings/str_replace.h"
2730
#include "ortools/base/file.h"
2831

2932
namespace fs = std::filesystem;
3033

31-
// Converts a absl::string_view into an object compatible with std::filesystem.
32-
#ifdef ABSL_USES_STD_STRING_VIEW
33-
#define SV_ABSL_TO_STD(X) X
34-
#else
35-
#define SV_ABSL_TO_STD(X) std::string(X)
36-
#endif
37-
3834
namespace file {
3935

4036
absl::Status Match(std::string_view pattern, std::vector<std::string>* result,
4137
const file::Options& options) {
4238
try {
43-
const auto search_dir = fs::path(SV_ABSL_TO_STD(pattern)).parent_path();
44-
const auto filename = fs::path(SV_ABSL_TO_STD(pattern)).filename().string();
39+
const auto search_dir = fs::path(pattern).parent_path();
40+
const auto filename = fs::path(pattern).filename().string();
4541
std::string regexp_filename =
4642
absl::StrReplaceAll(filename, {{".", "\\."}, {"*", ".*"}, {"?", "."}});
4743
std::regex regexp_pattern(regexp_filename);
@@ -89,4 +85,34 @@ absl::Status RecursivelyCreateDir(std::string_view path,
8985
}
9086
}
9187

88+
absl::StatusOr<int64_t> GetSize(std::string_view path,
89+
const file::Options& options) {
90+
(void)options;
91+
try {
92+
std::filesystem::path p(path);
93+
std::error_code ec;
94+
const bool is_dir = std::filesystem::is_directory(p, ec);
95+
if (ec) {
96+
if (ec == std::errc::no_such_file_or_directory) {
97+
return absl::NotFoundError(ec.message());
98+
}
99+
return absl::InvalidArgumentError(ec.message());
100+
}
101+
if (is_dir) {
102+
return absl::FailedPreconditionError(
103+
absl::StrCat(path, " is a directory."));
104+
}
105+
const uint64_t size = std::filesystem::file_size(p, ec);
106+
if (ec) {
107+
if (ec == std::errc::no_such_file_or_directory) {
108+
return absl::NotFoundError(ec.message());
109+
}
110+
return absl::InvalidArgumentError(ec.message());
111+
}
112+
return static_cast<int64_t>(size);
113+
} catch (const std::exception& e) {
114+
return absl::NotFoundError(e.what());
115+
}
116+
}
117+
92118
} // namespace file

ortools/base/filesystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <vector>
2020

2121
#include "absl/status/status.h"
22+
#include "absl/status/statusor.h"
2223
#include "ortools/base/file.h"
2324

2425
namespace file {
@@ -31,6 +32,9 @@ absl::Status IsDirectory(std::string_view path, const file::Options& options);
3132
absl::Status RecursivelyCreateDir(std::string_view path,
3233
const file::Options& options);
3334

35+
absl::StatusOr<int64_t> GetSize(absl::string_view path,
36+
const file::Options& options);
37+
3438
} // namespace file
3539

3640
#endif // ORTOOLS_BASE_FILESYSTEM_H_

0 commit comments

Comments
 (0)