@@ -30,6 +30,18 @@ static bool check_duplicates(std::span<const AdjustmentArc> arcs) {
3030}
3131#endif
3232
33+ void AdjustmentMatrix::normalize_arcs () {
34+ if (_type == AdjustmentMatrixType::symmetric) {
35+ for (auto & a : _arcs) {
36+ if (a.from > a.to ) { std::swap (a.from , a.to ); }
37+ }
38+ }
39+ std::sort (
40+ _arcs.begin (), _arcs.end (), [](const auto & a, const auto & b) -> bool {
41+ return a.from < b.from || (a.from == b.from && a.to < b.to );
42+ });
43+ }
44+
3345/* assume that the matrix is sorted */
3446/* need to check that:
3547 * - No duplicates
@@ -55,7 +67,7 @@ static bool is_valid(std::span<const AdjustmentArc> arcs) {
5567 return a.size () == b.size ();
5668 });
5769
58- return check_duplicates (arcs) && (decreasing != equal);
70+ return check_duplicates (arcs) && (( decreasing != increasing) != equal);
5971#else
6072 std::unordered_set<
6173 std::pair<decltype (AdjustmentArc::from), decltype (AdjustmentArc::to)> >
@@ -82,7 +94,8 @@ void AdjustmentMatrix::read_arcs(CSVReader& reader,
8294 _region_count = area_names.size ();
8395#ifdef __cpp_lib_ranges_zip
8496 auto reverse_area_name_map =
85- std::views::zip (area_names, std::views::iota (0ul , area_names.size ()))
97+ std::views::zip (std::views::reverse (area_names),
98+ std::views::iota (0ul , area_names.size ()))
8699 | std::ranges::to<std::unordered_map>();
87100#else
88101 std::unordered_map<std::string, size_t > reverse_area_name_map;
@@ -104,11 +117,6 @@ void AdjustmentMatrix::read_arcs(CSVReader& reader,
104117 .dist = row.get <double >(dist_key)});
105118 }
106119 } catch (std::exception& err) { throw CSVValueError{err.what ()}; }
107-
108- std::sort (
109- _arcs.begin (), _arcs.end (), [](const auto & a, const auto & b) -> bool {
110- return a.from < b.from || (a.from == b.from && a.to < b.to );
111- });
112120}
113121
114122AdjustmentMatrix::AdjustmentMatrix (const std::filesystem::path& filename,
@@ -152,6 +160,9 @@ AdjustmentMatrix::AdjustmentMatrix(std::istringstream&& matstream,
152160 }
153161
154162 _type = determine_matrix_symmetry (_arcs, _region_count);
163+
164+ normalize_arcs ();
165+ if (!is_valid (_arcs)) { _type = AdjustmentMatrixType::invalid; }
155166 if (_type == AdjustmentMatrixType::invalid) {
156167 throw std::runtime_error{" Adjustment matrix is invalid" };
157168 }
@@ -186,9 +197,6 @@ AdjustmentMatrixType AdjustmentMatrix::determine_matrix_symmetry(
186197 } else if (arcs.size () == (region_count * (region_count - 1 ))) {
187198 type = AdjustmentMatrixType::nonsymmetric;
188199 }
189- if (type == AdjustmentMatrixType::invalid || !is_valid (arcs)) {
190- return AdjustmentMatrixType::invalid;
191- }
192200 return type;
193201}
194202} // namespace lagrange
0 commit comments