Skip to content

Commit

Permalink
std::string -> absl::string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Sep 23, 2023
1 parent a7b26f6 commit d73f533
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 19 deletions.
20 changes: 10 additions & 10 deletions ortools/flatzinc/cp_model_fz_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "absl/container/flat_hash_set.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/types/span.h"
#include "google/protobuf/text_format.h"
#include "ortools/base/iterator_adaptors.h"
#include "ortools/flatzinc/checker.h"
Expand Down Expand Up @@ -73,8 +74,8 @@ struct CpModelProtoWithMapping {
// Create and return the indices of the IntervalConstraint corresponding
// to the flatzinc "interval" specified by a start var and a size var.
// This method will cache intervals with the key <start, size>.
std::vector<int> CreateIntervals(const std::vector<int>& starts,
const std::vector<VarOrValue>& sizes);
std::vector<int> CreateIntervals(absl::Span<const int> starts,
absl::Span<const VarOrValue> sizes);

// Create and return the indices of the IntervalConstraint corresponding
// to the flatzinc "interval" specified by a start var and a size var.
Expand All @@ -84,7 +85,7 @@ struct CpModelProtoWithMapping {
// stating that the interval will be performed if and only if the size is
// greater than 0.
std::vector<int> CreateNonZeroOrOptionalIntervals(
const std::vector<int>& starts, const std::vector<VarOrValue>& sizes);
absl::Span<const int> starts, absl::Span<const VarOrValue> sizes);

// Create and return the index of the optional IntervalConstraint
// corresponding to the flatzinc "interval" specified by a start var, the
Expand All @@ -104,7 +105,7 @@ struct CpModelProtoWithMapping {
// Helpers to fill a ConstraintProto.
void FillAMinusBInDomain(const std::vector<int64_t>& domain,
const fz::Constraint& fz_ct, ConstraintProto* ct);
void FillLinearConstraintWithGivenDomain(const std::vector<int64_t>& domain,
void FillLinearConstraintWithGivenDomain(absl::Span<const int64_t> domain,
const fz::Constraint& fz_ct,
ConstraintProto* ct);
void FillConstraint(const fz::Constraint& fz_ct, ConstraintProto* ct);
Expand All @@ -114,7 +115,7 @@ struct CpModelProtoWithMapping {
// Translates the flatzinc search annotations into the CpModelProto
// search_order field.
void TranslateSearchAnnotations(
const std::vector<fz::Annotation>& search_annotations,
absl::Span<const fz::Annotation> search_annotations,
SolverLogger* logger);

// The output proto.
Expand Down Expand Up @@ -293,7 +294,7 @@ int CpModelProtoWithMapping::GetOrCreateOptionalInterval(int start_var,
}

std::vector<int> CpModelProtoWithMapping::CreateIntervals(
const std::vector<int>& starts, const std::vector<VarOrValue>& sizes) {
absl::Span<const int> starts, absl::Span<const VarOrValue> sizes) {
std::vector<int> intervals;
for (int i = 0; i < starts.size(); ++i) {
intervals.push_back(
Expand All @@ -303,7 +304,7 @@ std::vector<int> CpModelProtoWithMapping::CreateIntervals(
}

std::vector<int> CpModelProtoWithMapping::CreateNonZeroOrOptionalIntervals(
const std::vector<int>& starts, const std::vector<VarOrValue>& sizes) {
absl::Span<const int> starts, absl::Span<const VarOrValue> sizes) {
std::vector<int> intervals;
for (int i = 0; i < starts.size(); ++i) {
const int opt_var = NonZeroLiteralFrom(sizes[i]);
Expand Down Expand Up @@ -394,7 +395,7 @@ void CpModelProtoWithMapping::FillAMinusBInDomain(
}

void CpModelProtoWithMapping::FillLinearConstraintWithGivenDomain(
const std::vector<int64_t>& domain, const fz::Constraint& fz_ct,
absl::Span<const int64_t> domain, const fz::Constraint& fz_ct,
ConstraintProto* ct) {
auto* arg = ct->mutable_linear();
for (const int64_t domain_bound : domain) arg->add_domain(domain_bound);
Expand Down Expand Up @@ -1097,8 +1098,7 @@ void CpModelProtoWithMapping::FillReifOrImpliedConstraint(
}

void CpModelProtoWithMapping::TranslateSearchAnnotations(
const std::vector<fz::Annotation>& search_annotations,
SolverLogger* logger) {
absl::Span<const fz::Annotation> search_annotations, SolverLogger* logger) {
std::vector<fz::Annotation> flat_annotations;
for (const fz::Annotation& annotation : search_annotations) {
fz::FlattenAnnotations(annotation, &flat_annotations);
Expand Down
5 changes: 3 additions & 2 deletions ortools/flatzinc/model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "ortools/base/stl_util.h"
#include "ortools/util/logging.h"

Expand Down Expand Up @@ -200,7 +201,7 @@ bool Domain::IntersectWithInterval(int64_t interval_min, int64_t interval_max) {
return false;
}

bool Domain::IntersectWithListOfIntegers(const std::vector<int64_t>& integers) {
bool Domain::IntersectWithListOfIntegers(absl::Span<const int64_t> integers) {
if (is_interval) {
const int64_t dmin =
values.empty() ? std::numeric_limits<int64_t>::min() : values[0];
Expand Down Expand Up @@ -370,7 +371,7 @@ bool Domain::Contains(int64_t value) const {

namespace {
bool IntervalOverlapValues(int64_t lb, int64_t ub,
const std::vector<int64_t>& values) {
absl::Span<const int64_t> values) {
for (int64_t value : values) {
if (lb <= value && value <= ub) {
return true;
Expand Down
3 changes: 2 additions & 1 deletion ortools/flatzinc/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "ortools/base/logging.h"
#include "ortools/base/types.h"
#include "ortools/graph/iterators.h"
Expand Down Expand Up @@ -94,7 +95,7 @@ struct Domain {
bool IntersectWithSingleton(int64_t value);
bool IntersectWithDomain(const Domain& domain);
bool IntersectWithInterval(int64_t interval_min, int64_t interval_max);
bool IntersectWithListOfIntegers(const std::vector<int64_t>& integers);
bool IntersectWithListOfIntegers(absl::Span<const int64_t> integers);
bool IntersectWithFloatDomain(const Domain& domain);

// Returns true iff the value did belong to the domain, and was removed.
Expand Down
4 changes: 3 additions & 1 deletion ortools/graph/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

#include <vector>

#include "absl/types/span.h"

namespace util {

bool IsSubsetOf0N(const std::vector<int>& v, int n) {
bool IsSubsetOf0N(absl::Span<const int> v, int n) {
std::vector<bool> mask(n, false);
for (const int i : v) {
if (i < 0 || i >= n || mask[i]) return false;
Expand Down
7 changes: 4 additions & 3 deletions ortools/graph/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "absl/container/btree_map.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/inlined_vector.h"
#include "absl/types/span.h"
#include "ortools/base/hash.h"
#include "ortools/base/map_util.h"
#include "ortools/graph/connected_components.h"
Expand Down Expand Up @@ -85,7 +86,7 @@ std::unique_ptr<Graph> RemapGraph(const Graph& graph,
// be done in O(num new nodes + num new arcs) but with a higher constant.
template <class Graph>
std::unique_ptr<Graph> GetSubgraphOfNodes(const Graph& graph,
const std::vector<int>& nodes);
absl::Span<const int> nodes);

// This can be used to view a directed graph (that supports reverse arcs)
// from graph.h as un undirected graph: operator[](node) returns a
Expand Down Expand Up @@ -141,7 +142,7 @@ std::vector<int> GetWeaklyConnectedComponents(const Graph& graph) {
// Returns true iff the given vector is a subset of [0..n-1], i.e.
// all elements i are such that 0 <= i < n and no two elements are equal.
// "n" must be >= 0 or the result is undefined.
bool IsSubsetOf0N(const std::vector<int>& v, int n);
bool IsSubsetOf0N(absl::Span<const int> v, int n);

// Returns true iff the given vector is a permutation of [0..size()-1].
inline bool IsValidPermutation(const std::vector<int>& v) {
Expand Down Expand Up @@ -295,7 +296,7 @@ std::unique_ptr<Graph> RemapGraph(const Graph& old_graph,

template <class Graph>
std::unique_ptr<Graph> GetSubgraphOfNodes(const Graph& old_graph,
const std::vector<int>& nodes) {
absl::Span<const int> nodes) {
typedef typename Graph::NodeIndex NodeIndex;
typedef typename Graph::ArcIndex ArcIndex;
DCHECK(IsSubsetOf0N(nodes, old_graph.num_nodes())) << "Invalid subset";
Expand Down
1 change: 1 addition & 0 deletions ortools/linear_solver/linear_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
#include <cstdint>
#include <functional>
#include <limits>
#include <map>
#include <memory>
#include <optional>
#include <ostream>
Expand Down
128 changes: 126 additions & 2 deletions ortools/util/strong_integers.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

#include <cstdint>
#include <iosfwd>
#include <limits>
#include <ostream> // NOLINT

#include "absl/strings/str_format.h"
Expand Down Expand Up @@ -346,12 +347,135 @@ H AbslHashValue(H h, const StrongInt64<StrongIntegerName>& i) {
// -- STD HASHING SUPPORT -----------------------------------------------------
namespace std {
template <typename Tag>
struct hash<operations_research::StrongIndex<Tag> >
struct hash<operations_research::StrongIndex<Tag>>
: ::operations_research::StrongIndex<Tag>::Hasher {};

template <typename Tag>
struct hash<operations_research::StrongInt64<Tag> >
struct hash<operations_research::StrongInt64<Tag>>
: ::operations_research::StrongInt64<Tag>::Hasher {};
} // namespace std

// -- STD NUMERIC_LIMITS SUPPORT ----------------------------------------------
namespace std {

template <typename Tag>
struct numeric_limits<operations_research::StrongIndex<Tag>> {
private:
using StrongIntT = operations_research::StrongIndex<Tag>;
using NativeTypeT = typename operations_research::StrongIndex<Tag>::ValueType;

public:
// NOLINTBEGIN(google3-readability-class-member-naming)
static constexpr bool is_specialized = true;
static constexpr bool is_signed = numeric_limits<NativeTypeT>::is_signed;
static constexpr bool is_integer = numeric_limits<NativeTypeT>::is_integer;
static constexpr bool is_exact = numeric_limits<NativeTypeT>::is_exact;
static constexpr bool has_infinity =
numeric_limits<NativeTypeT>::has_infinity;
static constexpr bool has_quiet_NaN =
numeric_limits<NativeTypeT>::has_quiet_NaN;
static constexpr bool has_signaling_NaN =
numeric_limits<NativeTypeT>::has_signaling_NaN;
static constexpr float_denorm_style has_denorm =
numeric_limits<NativeTypeT>::has_denorm;
static constexpr bool has_denorm_loss =
numeric_limits<NativeTypeT>::has_denorm_loss;
static constexpr float_round_style round_style =
numeric_limits<NativeTypeT>::round_style;
static constexpr bool is_iec559 = numeric_limits<NativeTypeT>::is_iec559;
static constexpr bool is_bounded = numeric_limits<NativeTypeT>::is_bounded;
static constexpr bool is_modulo = numeric_limits<NativeTypeT>::is_modulo;
static constexpr int digits = numeric_limits<NativeTypeT>::digits;
static constexpr int digits10 = numeric_limits<NativeTypeT>::digits10;
static constexpr int max_digits10 = numeric_limits<NativeTypeT>::max_digits10;
static constexpr int radix = numeric_limits<NativeTypeT>::radix;
static constexpr int min_exponent = numeric_limits<NativeTypeT>::min_exponent;
static constexpr int min_exponent10 =
numeric_limits<NativeTypeT>::min_exponent10;
static constexpr int max_exponent = numeric_limits<NativeTypeT>::max_exponent;
static constexpr int max_exponent10 =
numeric_limits<NativeTypeT>::max_exponent10;
static constexpr bool traps = numeric_limits<NativeTypeT>::traps;
static constexpr bool tinyness_before =
numeric_limits<NativeTypeT>::tinyness_before;
// NOLINTEND(google3-readability-class-member-naming)

static constexpr StrongIntT(min)() {
return StrongIntT(numeric_limits<NativeTypeT>::min());
}
static constexpr StrongIntT lowest() {
return StrongIntT(numeric_limits<NativeTypeT>::lowest());
}
static constexpr StrongIntT(max)() {
return StrongIntT(numeric_limits<NativeTypeT>::max());
}
static constexpr StrongIntT epsilon() { return StrongIntT(); }
static constexpr StrongIntT round_error() { return StrongIntT(); }
static constexpr StrongIntT infinity() { return StrongIntT(); }
static constexpr StrongIntT quiet_NaN() { return StrongIntT(); }
static constexpr StrongIntT signaling_NaN() { return StrongIntT(); }
static constexpr StrongIntT denorm_min() { return StrongIntT(); }
};

template <typename Tag>
struct numeric_limits<operations_research::StrongInt64<Tag>> {
private:
using StrongIntT = operations_research::StrongInt64<Tag>;
using NativeTypeT = typename operations_research::StrongInt64<Tag>::ValueType;

public:
// NOLINTBEGIN(google3-readability-class-member-naming)
static constexpr bool is_specialized = true;
static constexpr bool is_signed = numeric_limits<NativeTypeT>::is_signed;
static constexpr bool is_integer = numeric_limits<NativeTypeT>::is_integer;
static constexpr bool is_exact = numeric_limits<NativeTypeT>::is_exact;
static constexpr bool has_infinity =
numeric_limits<NativeTypeT>::has_infinity;
static constexpr bool has_quiet_NaN =
numeric_limits<NativeTypeT>::has_quiet_NaN;
static constexpr bool has_signaling_NaN =
numeric_limits<NativeTypeT>::has_signaling_NaN;
static constexpr float_denorm_style has_denorm =
numeric_limits<NativeTypeT>::has_denorm;
static constexpr bool has_denorm_loss =
numeric_limits<NativeTypeT>::has_denorm_loss;
static constexpr float_round_style round_style =
numeric_limits<NativeTypeT>::round_style;
static constexpr bool is_iec559 = numeric_limits<NativeTypeT>::is_iec559;
static constexpr bool is_bounded = numeric_limits<NativeTypeT>::is_bounded;
static constexpr bool is_modulo = numeric_limits<NativeTypeT>::is_modulo;
static constexpr int digits = numeric_limits<NativeTypeT>::digits;
static constexpr int digits10 = numeric_limits<NativeTypeT>::digits10;
static constexpr int max_digits10 = numeric_limits<NativeTypeT>::max_digits10;
static constexpr int radix = numeric_limits<NativeTypeT>::radix;
static constexpr int min_exponent = numeric_limits<NativeTypeT>::min_exponent;
static constexpr int min_exponent10 =
numeric_limits<NativeTypeT>::min_exponent10;
static constexpr int max_exponent = numeric_limits<NativeTypeT>::max_exponent;
static constexpr int max_exponent10 =
numeric_limits<NativeTypeT>::max_exponent10;
static constexpr bool traps = numeric_limits<NativeTypeT>::traps;
static constexpr bool tinyness_before =
numeric_limits<NativeTypeT>::tinyness_before;
// NOLINTEND(google3-readability-class-member-naming)

static constexpr StrongIntT(min)() {
return StrongIntT(numeric_limits<NativeTypeT>::min());
}
static constexpr StrongIntT lowest() {
return StrongIntT(numeric_limits<NativeTypeT>::lowest());
}
static constexpr StrongIntT(max)() {
return StrongIntT(numeric_limits<NativeTypeT>::max());
}
static constexpr StrongIntT epsilon() { return StrongIntT(); }
static constexpr StrongIntT round_error() { return StrongIntT(); }
static constexpr StrongIntT infinity() { return StrongIntT(); }
static constexpr StrongIntT quiet_NaN() { return StrongIntT(); }
static constexpr StrongIntT signaling_NaN() { return StrongIntT(); }
static constexpr StrongIntT denorm_min() { return StrongIntT(); }
};

} // namespace std

#endif // OR_TOOLS_UTIL_STRONG_INTEGERS_H_

0 comments on commit d73f533

Please sign in to comment.