Skip to content

Commit 5e67e76

Browse files
committed
routing: Use a static function to return a const reference to an empty vector.
1 parent 3209652 commit 5e67e76

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

ortools/routing/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ cc_library(
305305
"//ortools/util:time_limit",
306306
"@abseil-cpp//absl/algorithm:container",
307307
"@abseil-cpp//absl/base:core_headers",
308+
"@abseil-cpp//absl/base:no_destructor",
308309
"@abseil-cpp//absl/container:btree",
309310
"@abseil-cpp//absl/container:flat_hash_map",
310311
"@abseil-cpp//absl/container:flat_hash_set",

ortools/routing/routing.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <vector>
3737

3838
#include "absl/algorithm/container.h"
39+
#include "absl/base/no_destructor.h"
3940
#include "absl/container/flat_hash_map.h"
4041
#include "absl/container/flat_hash_set.h"
4142
#include "absl/flags/flag.h"
@@ -217,6 +218,12 @@ void Model::SetSweepArranger(SweepArranger* sweep_arranger) {
217218

218219
SweepArranger* Model::sweep_arranger() const { return sweep_arranger_.get(); }
219220

221+
/* static */
222+
const std::vector<int>& Model::NodeNeighborsByCostClass::GetEmptyNeighbors() {
223+
static const absl::NoDestructor<std::vector<int>> kEmptyNeighbors;
224+
return *kEmptyNeighbors;
225+
}
226+
220227
void Model::NodeNeighborsByCostClass::ComputeNeighbors(
221228
const NodeNeighborsParameters& params) {
222229
auto [num_neighbors, add_vehicle_starts_to_neighbors,

ortools/routing/routing.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ class OR_DLL Model {
16931693
/// is a neighborhood arc for 'cost_class'.
16941694
const std::vector<int>& GetIncomingNeighborsOfNodeForCostClass(
16951695
int cost_class, int node_index) const {
1696-
if (routing_model_.IsStart(node_index)) return empty_neighbors_;
1696+
if (routing_model_.IsStart(node_index)) return GetEmptyNeighbors();
16971697

16981698
if (node_index_to_incoming_neighbors_by_cost_class_.empty()) {
16991699
DCHECK(IsFullNeighborhood());
@@ -1702,7 +1702,7 @@ class OR_DLL Model {
17021702
const std::vector<std::vector<int>>& node_index_to_incoming_neighbors =
17031703
node_index_to_incoming_neighbors_by_cost_class_[cost_class];
17041704
if (node_index_to_incoming_neighbors.empty()) {
1705-
return empty_neighbors_;
1705+
return GetEmptyNeighbors();
17061706
}
17071707
return node_index_to_incoming_neighbors[node_index];
17081708
}
@@ -1712,7 +1712,7 @@ class OR_DLL Model {
17121712
/// arc for 'cost_class'.
17131713
const std::vector<int>& GetOutgoingNeighborsOfNodeForCostClass(
17141714
int cost_class, int node_index) const {
1715-
if (routing_model_.IsEnd(node_index)) return empty_neighbors_;
1715+
if (routing_model_.IsEnd(node_index)) return GetEmptyNeighbors();
17161716

17171717
if (node_index_to_outgoing_neighbors_by_cost_class_.empty()) {
17181718
DCHECK(IsFullNeighborhood());
@@ -1721,7 +1721,7 @@ class OR_DLL Model {
17211721
const std::vector<std::vector<int>>& node_index_to_outgoing_neighbors =
17221722
node_index_to_outgoing_neighbors_by_cost_class_[cost_class];
17231723
if (node_index_to_outgoing_neighbors.empty()) {
1724-
return empty_neighbors_;
1724+
return GetEmptyNeighbors();
17251725
}
17261726
return node_index_to_outgoing_neighbors[node_index];
17271727
}
@@ -1744,12 +1744,9 @@ class OR_DLL Model {
17441744
bool IsFullNeighborhood() const { return full_neighborhood_; }
17451745

17461746
private:
1747+
static const std::vector<int>& GetEmptyNeighbors();
1748+
17471749
const Model& routing_model_;
1748-
#if __cplusplus >= 202002L && !defined(__APPLE__)
1749-
static constexpr std::vector<int> empty_neighbors_ = {};
1750-
#else
1751-
inline static const std::vector<int> empty_neighbors_ = {};
1752-
#endif
17531750

17541751
std::vector<std::vector<std::vector<int>>>
17551752
node_index_to_incoming_neighbors_by_cost_class_;

0 commit comments

Comments
 (0)