Skip to content

Sort locations in timetable#200

Open
CmdrTMir wants to merge 26 commits intomotis-project:masterfrom
CmdrTMir:sort-loc
Open

Sort locations in timetable#200
CmdrTMir wants to merge 26 commits intomotis-project:masterfrom
CmdrTMir:sort-loc

Conversation

@CmdrTMir
Copy link

Sortiert die locations in timetable, anhand der Anzahl der Routen pro location.

tt.location_areas_ = sorted_loc_area;
tt.route_location_seq_ = sorted_route_loc_seq;

tt.permutate_locations(first_idx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has to be in init_finish (and probably better optional)

static vector<location_idx_t> mapping_vec;

vector<location_idx_t> create_mapping_vec(vector<location_idx_t> vec)
static vector<location_idx_t> create_mapping_vec(vector<location_idx_t> vec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to .cc file

(if in header, then use inline instead of static but if not a template and not performance sensitive -> move to .cc file)

static vector<location_idx_t> mapping_vec;

static vector<location_idx_t> create_mapping_vec(vector<location_idx_t> vec)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always use formatter (format on save tool in Clion, but every editor has support for this)

clang-format-18
image

image



template<typename T>
T apply_permutation_vec(T input)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always const&

return result_vec;
}

static void build_permutation_vec(vecvec<location_idx_t, route_idx_t> order, uint32_t first_idx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +5 to +6
vector<location_idx_t> permutation_;
static vector<location_idx_t> mapping_vec;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no global variables

}
if (opt.permutate_loc_) {
auto first_idx_size_t = special_stations_names.size();
uint32_t first_idx = static_cast<uint32_t>(first_idx_size_t);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint32_t first_idx = static_cast<uint32_t>(first_idx_size_t);
auto const first_idx = static_cast<std::uint32_t>(first_idx_size_t);

auto const progress_tracker = utl::get_active_progress_tracker();
auto timezones = tz_map{};
auto agencies = read_agencies(tt, timezones, load(kAgencyFile).data());
// stops = hash_map<string(id), location_idx_t>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Comment on lines +435 to +473
auto sorted_loc_routes = apply_permutation_vec(location_routes_);
auto sorted_names = apply_permutation_vec(locations_.names_);
auto sorted_ids = apply_permutation_vec(locations_.ids_);
auto sorted_coords = apply_permutation_vec(locations_.coordinates_);
auto sorted_src = apply_permutation_vec(locations_.src_);
auto sorted_transfertime = apply_permutation_vec(locations_.transfer_time_);
auto sorted_types = apply_permutation_vec(locations_.types_);
auto sorted_timezones =
apply_permutation_vec(locations_.location_timezones_);
auto sorted_parents =
apply_permutation_and_mapping_vec(locations_.parents_);
auto sorted_equivalences =
apply_permutation_multimap(locations_.equivalences_);
auto sorted_children = apply_permutation_multimap(locations_.children_);
auto sorted_pre_footpaths_out = apply_permutation_multimap_foot(
locations_.preprocessing_footpaths_out_);
auto sorted_pre_footpaths_in =
apply_permutation_multimap_foot(locations_.preprocessing_footpaths_in_);
auto sorted_loc_area = apply_permutation_vec(location_areas_);
auto sorted_route_loc_seq =
apply_permutation_to_route_loc_seq(route_location_seq_);

locations_.location_id_to_idx_ = std::move(sorted_loc_id_to_idx);
locations_.names_ = std::move(sorted_names);
locations_.ids_ = std::move(sorted_ids);
locations_.coordinates_ = std::move(sorted_coords);
locations_.src_ = std::move(sorted_src);
locations_.transfer_time_ = std::move(sorted_transfertime);
locations_.types_ = std::move(sorted_types);
locations_.location_timezones_ = std::move(sorted_timezones);
locations_.parents_ = std::move(sorted_parents);
locations_.equivalences_ = std::move(sorted_equivalences);
locations_.children_ = std::move(sorted_children);
locations_.preprocessing_footpaths_out_ =
std::move(sorted_pre_footpaths_out);
locations_.preprocessing_footpaths_in_ = std::move(sorted_pre_footpaths_in);
location_areas_ = std::move(sorted_loc_area);
route_location_seq_ = std::move(sorted_route_loc_seq);
location_routes_ = std::move(sorted_loc_routes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why variables and not just assignments?

vector<location_idx_t> location_permutation =
build_permutation_vec(location_routes_, first_idx);

vector<std::pair<location_id, location_idx_t>> unsorted;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


template <typename T>
inline T apply_permutation_vec(T const& input) {
T sorted;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T sorted;
auto sorted = T{};

inline T apply_permutation_vec(T const& input) {
T sorted;
for (auto i = 0U; i < input.size(); ++i) {
auto temp = input.at(permutation_[i]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto temp = input.at(permutation_[i]);
auto temp = input[permutation_[i]];

no check necessary

Comment on lines +28 to +38
vecvec<route_idx_t, stop::value_type> apply_permutation_to_route_loc_seq(
vecvec<route_idx_t, stop::value_type> const& input);

vector_map<location_idx_t, location_idx_t> apply_permutation_and_mapping_vec(
vector_map<location_idx_t, location_idx_t> const& input);

mutable_fws_multimap<location_idx_t, location_idx_t> apply_permutation_multimap(
mutable_fws_multimap<location_idx_t, location_idx_t> const& input);

mutable_fws_multimap<location_idx_t, footpath> apply_permutation_multimap_foot(
mutable_fws_multimap<location_idx_t, footpath> const& input);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why in header? only used locally in .cc file

vecvec<location_idx_t, route_idx_t> const& order, uint32_t const first_idx);

template <typename T>
inline T apply_permutation_vec(T const& input) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why in header? only used locally in .cc file

void write(std::filesystem::path const&) const;
static cista::wrapped<timetable> read(std::filesystem::path const&);

void permutate_locations(uint32_t first_idx) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be a separate function in a separate file

permutate_locations.h: permutate_locations(timetable&);
permutate_locations.cc: permutate_locations(timetable&) { ... }

init_finish() { ... permutate_locations(tt); }

bool adjust_footpaths_{true};
bool merge_dupes_intra_src_{true};
bool merge_dupes_inter_src_{true};
bool permutate_loc_{true};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it have any downsides? if no, no option needed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

siehe E-Mail

bool adjust_footpaths = false,
bool merge_dupes_intra_src = false,
bool merge_dupes_inter_src = false,
bool permutate_loc = false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it have any downsides? if no, no option needed?

#include "nigiri/timetable.h"
#include "nigiri/types.h"

#include <algorithm>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include order (common -> specific)

if (opt.permutate_loc_) {
auto first_idx_size_t = special_stations_names.size();
auto const first_idx = static_cast<std::uint32_t>(first_idx_size_t);
permutate_locations(tt, first_idx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first idx can be a constant? it's a detail anyway, no need to have it do the caller?

for (auto i = 0U; i < n; ++i) {
result[permutation[i].v_] = location_idx_t{i};
}
vector<location_idx_t> mapping_vec;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left side = auto

please fix the remaining places also

Suggested change
vector<location_idx_t> mapping_vec;
auto mapping_vec = vector<location_idx_t>{};


namespace nigiri {

auto create_mapping_vec(vector<location_idx_t> const& permutation) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto create_mapping_vec(vector<location_idx_t> const& permutation) {
std::vector<location_idx_t> create_mapping_vec(vector<location_idx_t> const& permutation) {

explicit return types = better readability

vector<location_idx_t> mapping_vec) {
vector_map<location_idx_t, location_idx_t> sorted;
for (auto i = 0U; i < input.size(); ++i) {
auto temp = input.at(permutation[i]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto temp = input.at(permutation[i]);
auto temp = input[permutation[i]];

we should be 100% sure that permutation[i] is within bounds, right?


auto apply_permutation_and_mapping_vec(
vector_map<location_idx_t, location_idx_t> const& input,
vector<location_idx_t> permutation,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
vector<location_idx_t> permutation,
vector<location_idx_t> permutation,

never pass vectors by value if you don't actually need a copy. why copy here?
applies everywhere!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants