Skip to content

Commit c26e90c

Browse files
karenzsheaKaren Shea
authored and
Karen Shea
committed
sort/unique turn penalty lookup
1 parent d4e5710 commit c26e90c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/contractor/contractor.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,29 @@ parse_turn_penalty_lookup_from_csv_files(const std::vector<std::string> &turn_pe
432432

433433
tbb::parallel_for(std::size_t{0}, turn_penalty_filenames.size(), parse_turn_penalty_file);
434434

435+
// With flattened map-ish view of all the files, sort and unique them on from,to,source
436+
// The greater '>' is used here since we want to give files later on higher precedence
437+
const auto sort_by = [](const TurnPenaltySource &lhs, const TurnPenaltySource &rhs) {
438+
return std::tie(lhs.segment.from, lhs.segment.via, lhs.segment.to, lhs.penalty_source.source) >
439+
std::tie(rhs.segment.from, rhs.segment.via, rhs.segment.to, rhs.penalty_source.source);
440+
};
441+
442+
std::stable_sort(begin(map), end(map), sort_by);
443+
444+
// Unique only on from,to to take the source precedence into account and remove duplicates
445+
const auto unique_by = [](const TurnPenaltySource &lhs, const TurnPenaltySource &rhs) {
446+
return std::tie(lhs.segment.from, lhs.segment.via, lhs.segment.to) ==
447+
std::tie(rhs.segment.from, rhs.segment.via, rhs.segment.to);
448+
};
449+
450+
const auto it = std::unique(begin(map), end(map), unique_by);
451+
452+
map.erase(it, end(map));
453+
454+
util::SimpleLogger().Write() << "In total loaded " << turn_penalty_filenames.size()
455+
<< " turn penalty file(s) with a total of " << map.size()
456+
<< " unique values";
457+
435458
return map;
436459
}
437460
} // anon ns

0 commit comments

Comments
 (0)