Skip to content

Conversation

cvvergara
Copy link
Member

@cvvergara cvvergara commented Apr 17, 2025

Fixes #2802

Changes proposed in this pull request:

  • Reorganization of the code done

This PR contains the work done on PR #2845 with the following changes

  • Code not modified is returned to the original indentation

@pgRouting/admins

Summary by CodeRabbit

  • Documentation

    • Updated release notes and changelog to document code enhancements in the contraction functions.
    • Added and updated translation strings for new documentation entries.
  • New Features

    • Introduced new methods for managing contracted vertices and forbidden vertices in contraction-related classes.
    • Added new public members to support additional vertex metadata.
  • Refactor

    • Simplified and delegated forbidden vertex and shortcut management to the graph interface.
    • Improved clarity and consistency in contraction-related logic and interfaces.
    • Removed internal shortcut processing and forbidden vertex tracking from contraction classes, centralizing these in the graph.
  • Style

    • Modernized type usage and improved code readability in contraction modules.

Copy link
Contributor

coderabbitai bot commented Apr 17, 2025

Walkthrough

This change refactors and reorganizes the internal C++ code for the pgr_contraction family of functions in pgRouting. It delegates forbidden vertex and shortcut management from individual contraction classes to the graph class, introduces new methods for handling contracted vertices, and improves type usage and code clarity. The update also adds new documentation entries and translation strings to reflect the code enhancement. No public API or function signatures are changed, and the modifications are limited to internal implementation and documentation updates.

Changes

Files/Paths Change Summary
NEWS.md, doc/src/release_notes.rst, locale/en/LC_MESSAGES/pgrouting_doc_strings.po, locale/pot/pgrouting_doc_strings.pot Added documentation and translation entries describing code factorization enhancements for pgr_contraction (issue #2802).
include/contraction/contractionGraph.hpp, include/contraction/contract.hpp, include/contraction/deadEndContraction.hpp, include/contraction/linearContraction.hpp, src/contraction/contractGraph_driver.cpp Refactored contraction logic: moved forbidden vertex and shortcut management to the graph class, updated method signatures, removed redundant state from contraction classes, and improved code clarity.
include/cpp_common/ch_edge.hpp, src/common/ch_edge.cpp Added methods to CH_edge for setting and adding contracted vertices.
include/cpp_common/ch_vertex.hpp, src/common/ch_vertex.cpp Added new members and methods to CH_vertex for managing contracted vertices and vertex metadata.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant pgr_contraction (family)
    participant ContractionGraph
    participant CH_edge
    participant CH_vertex

    User->>pgr_contraction (family): Initiate contraction
    pgr_contraction (family)->>ContractionGraph: set_forbidden_vertices()
    pgr_contraction (family)->>ContractionGraph: is_forbidden(), is_dead_end()
    pgr_contraction (family)->>ContractionGraph: process_shortcut(u, v, w)
    ContractionGraph->>CH_edge: set_contracted_vertices()/add_contracted_vertices()
    ContractionGraph->>CH_vertex: set_contracted_vertices()/add_contracted_vertex_id()
    ContractionGraph-->>pgr_contraction (family): get_shortcuts(), get_modified_vertices()
Loading

Poem

In tunnels of code, the rabbits did hop,
Refactoring logic, they just couldn’t stop!
Forbidden and shortcuts now live in one place,
With vertices tracked at a much quicker pace.
The garden of graphs is more tidy and neat—
This bunny’s refactor is truly a treat!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Free

📥 Commits

Reviewing files that changed from the base of the PR and between 0adb2a4 and ce88994.

📒 Files selected for processing (1)
  • NEWS.md (1 hunks)

Note

🎁 Summarized by CodeRabbit Free

Your organization has reached its limit of developer seats under the Pro Plan. For new users, CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please add seats to your subscription by visiting https://app.coderabbit.ai/login.If you believe this is a mistake and have available seats, please assign one to the pull request author through the subscription management page using the link above.

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@cvvergara cvvergara requested a review from robe2 April 17, 2025 14:39
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 10

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c1b2e74 and e6cf116.

📒 Files selected for processing (13)
  • NEWS.md (1 hunks)
  • doc/src/release_notes.rst (1 hunks)
  • include/contraction/contract.hpp (1 hunks)
  • include/contraction/contractionGraph.hpp (3 hunks)
  • include/contraction/deadEndContraction.hpp (3 hunks)
  • include/contraction/linearContraction.hpp (3 hunks)
  • include/cpp_common/ch_edge.hpp (1 hunks)
  • include/cpp_common/ch_vertex.hpp (1 hunks)
  • locale/en/LC_MESSAGES/pgrouting_doc_strings.po (2 hunks)
  • locale/pot/pgrouting_doc_strings.pot (2 hunks)
  • src/common/ch_edge.cpp (2 hunks)
  • src/common/ch_vertex.cpp (2 hunks)
  • src/contraction/contractGraph_driver.cpp (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
doc/src/release_notes.rst (1)
Learnt from: cvvergara
PR: pgRouting/pgrouting#2744
File: doc/src/release_notes.rst:94-94
Timestamp: 2025-02-06T20:50:07.967Z
Learning: In pgRouting, changes to functions can happen at two levels:
1. SQL level - removal/addition of function signatures
2. C/C++ level - deprecation/changes to the underlying implementation
These are documented separately in the release notes.
🧬 Code Graph Analysis (4)
src/common/ch_edge.cpp (3)
src/common/ch_vertex.cpp (2)
  • set_contracted_vertices (42-45)
  • set_contracted_vertices (42-43)
include/cpp_common/ch_vertex.hpp (1)
  • m_contracted_vertices (62-62)
include/cpp_common/ch_edge.hpp (1)
  • m_contracted_vertices (60-60)
src/common/ch_vertex.cpp (2)
include/cpp_common/ch_vertex.hpp (6)
  • CH_vertex (47-47)
  • CH_vertex (48-50)
  • m_contracted_vertices (62-62)
  • vid (56-56)
  • vid (58-58)
  • vid (58-58)
src/common/ch_edge.cpp (2)
  • set_contracted_vertices (34-37)
  • set_contracted_vertices (34-35)
include/cpp_common/ch_vertex.hpp (5)
include/cpp_common/ch_edge.hpp (4)
  • other (52-52)
  • v (54-54)
  • m_contracted_vertices (60-60)
  • os (63-63)
include/cpp_common/basic_vertex.hpp (2)
  • other (59-61)
  • other (59-59)
include/cpp_common/identifiers.hpp (7)
  • other (96-98)
  • other (96-96)
  • other (129-133)
  • other (129-130)
  • other (172-176)
  • os (240-248)
  • os (241-241)
include/contraction/contractionGraph.hpp (10)
  • v (70-82)
  • v (70-70)
  • v (247-263)
  • v (247-247)
  • v (287-292)
  • v (287-287)
  • v (298-307)
  • v (298-298)
  • os (138-159)
  • os (138-140)
src/common/ch_vertex.cpp (2)
  • os (78-84)
  • os (78-78)
include/contraction/deadEndContraction.hpp (2)
include/cpp_common/ch_vertex.hpp (1)
  • v (55-55)
include/contraction/contractionGraph.hpp (16)
  • v (70-82)
  • v (70-70)
  • v (247-263)
  • v (247-247)
  • v (287-292)
  • v (287-287)
  • v (298-307)
  • v (298-298)
  • u (90-132)
  • u (91-91)
  • u (187-189)
  • u (187-187)
  • u (214-245)
  • u (214-217)
  • u (365-387)
  • u (365-365)
🪛 markdownlint-cli2 (0.17.2)
NEWS.md

60-60: Emphasis used instead of a heading
null

(MD036, no-emphasis-as-heading)

⏰ Context from checks skipped due to timeout of 90000ms (17)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Ubuntu Boost (83)
  • GitHub Check: Ubuntu Boost (79)
  • GitHub Check: Ubuntu Boost (80)
  • GitHub Check: Ubuntu Boost (78)
  • GitHub Check: Ubuntu Boost (86)
  • GitHub Check: Ubuntu Boost (56)
  • GitHub Check: Ubuntu Boost (84)
  • GitHub Check: Ubuntu Boost (77)
  • GitHub Check: Check queries
  • GitHub Check: Ubuntu Boost (68)
  • GitHub Check: Ubuntu Boost (75)
  • GitHub Check: Ubuntu Boost (76)
  • GitHub Check: Ubuntu clang
  • GitHub Check: macos (macos-14)
  • GitHub Check: macos (macos-latest)
  • GitHub Check: documentation (en)
🔇 Additional comments (16)
locale/en/LC_MESSAGES/pgrouting_doc_strings.po (2)

11-11: Verify the POT-Creation-Date.

The POT-Creation-Date is set to "2025-04-17", which appears to be a future date. Please verify if this is intentional or if it should be adjusted to the current date.


4011-4017: LGTM! Documentation updates for code enhancement.

The new message entries properly document the code enhancements related to issue #2802 for the pgr_contraction family of functions. This aligns well with the PR objectives of code reorganization without altering functionality.

doc/src/release_notes.rst (1)

90-94: Add “C/C++ code enhancement” section for issue #2802
The new rubric and entry correctly document the internal code refactor for the pgr_contraction family under pgRouting 3.8.0. The link and description align with the existing format.

NEWS.md (1)

60-64: Document internal code factorization in NEWS.md
The bold heading and bullet item for #2802 appropriately mirror the release notes, ensuring that both RST and Markdown changelogs stay in sync.

🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

60-60: Emphasis used instead of a heading
null

(MD036, no-emphasis-as-heading)

locale/pot/pgrouting_doc_strings.pot (2)

11-11: Updated POT-Creation-Date to reflect recent changes.

The POT creation date has been updated to April 17, 2025, indicating this is a recent update to the translation template file.


3586-3591: New documentation entries for code enhancements.

These new entries document the code factorization work done on the pgr_contraction family of functions (issue #2802). This properly reflects the internal code improvements in the documentation and translation strings.

The addition of these entries is important for maintaining documentation completeness, as it informs users and translators about the code improvements that have been made, even though they don't change the public API.

include/cpp_common/ch_edge.hpp (2)

50-51: Method signature looks good, interface extension is consistent.

The addition of the set_contracted_vertices method provides a clean way to replace the entire collection of contracted vertices at once. This complements the existing API that allows for individual vertex additions.


56-56: Method provides batch addition capability, good enhancement.

This method allows adding multiple contracted vertices at once, which is more efficient than adding them one by one. The signature is consistent with the similar operation in the CH_vertex class.

include/contraction/contract.hpp (1)

109-109: Improved architecture - centralized forbidden vertex management.

This change properly delegates the responsibility of managing forbidden vertices to the graph object instead of the contractor. This centralization aligns with good design principles by keeping related functionality together and reduces duplication across different contractor implementations.

src/common/ch_edge.cpp (2)

34-37: Implementation for set_contracted_vertices looks correct.

The implementation directly assigns the provided identifiers collection to the member variable, which is the expected behavior for a setter method. This matches the implementation pattern used in the CH_vertex class.


77-79: Implementation for add_contracted_vertices looks correct.

The implementation uses the += operator to append the provided identifiers to the existing collection, which is consistent with other vertex addition methods in this class. The pattern is clean and efficient.

src/contraction/contractGraph_driver.cpp (1)

81-82: Code simplification through proper encapsulation.

Replacing direct access to graph internals with calls to dedicated methods improves encapsulation and maintainability. This change removes implementation details from this file and delegates responsibility to the graph class, which is the proper owner of this functionality.

src/common/ch_vertex.cpp (1)

37-40: Default‑ctor looks good but add a short‑circuit return
The explicit initialisation of vertex_order and metric to -1 removes UB that would appear later when these fields are read. Nice!

include/contraction/deadEndContraction.hpp (1)

79-81:

❓ Verification inconclusive

Check the result of get_min_cost_edge
get_min_cost_edge returns (edge, found) but the found flag is ignored.
If no edge exists between u and v, found is false and edge contains default values; you will still merge its (empty) contracted‑vertex list into graph[u].
While harmless today, this silently masks topology errors and could break when additional properties are copied later.

-const auto& e = graph.get_min_cost_edge(u, v);
-graph[u].contracted_vertices() += std::get<0>(e).contracted_vertices();
-graph[u].add_contracted_vertex(graph[v]);
+auto [minEdge, found] = graph.get_min_cost_edge(u, v);
+if (found) {
+    graph[u].contracted_vertices() += minEdge.contracted_vertices();
+    graph[u].add_contracted_vertex(graph[v]);
+}

Please confirm that silently skipping missing edges is acceptable; otherwise the loop should throw or log.


Check the found flag from get_min_cost_edge
get_min_cost_edge(u, v) returns (edge, found). If found is false, you’ll still merge a default‐constructed edge’s contracted‑vertex list, silently masking missing links. Either skip the merge, log a warning, or throw an exception.

  • File: include/contraction/deadEndContraction.hpp (lines 79–81)
-const auto& e = graph.get_min_cost_edge(u, v);
-graph[u].contracted_vertices() += std::get<0>(e).contracted_vertices();
-graph[u].add_contracted_vertex(graph[v]);
+auto [minEdge, found] = graph.get_min_cost_edge(u, v);
+if (found) {
+    graph[u].contracted_vertices() += minEdge.contracted_vertices();
+    graph[u].add_contracted_vertex(graph[v]);
+} else {
+    // TODO: log a warning or throw to avoid masking missing edges
+}
include/cpp_common/ch_vertex.hpp (1)

51-52: Keep signature in sync with implementation
After changing the .cpp version, remember to update the declaration:

-void set_contracted_vertices(Identifiers<int64_t>&);
+void set_contracted_vertices(const Identifiers<int64_t>&);
include/contraction/contractionGraph.hpp (1)

90-132: get_min_cost_edge aggregates contracted vertices from all parallel edges

contracted_vertices is filled inside the loop before the minimum‑cost edge is known.
If multiple parallel edges (u→v) exist, vertices contracted on non‑minimal edges leak into the result.

-    contracted_vertices += this->graph[e].contracted_vertices();
-    if (this->graph[e].cost < min_cost) {
-        min_cost = this->graph[e].cost;
-        edge = this->graph[e];
-        found = true;
-    }
+    const auto &cv = this->graph[e].contracted_vertices();
+    if (this->graph[e].cost < min_cost) {
+        min_cost         = this->graph[e].cost;
+        edge             = this->graph[e];
+        contracted_vertices = cv;         // keep only the winning edge’s vertices
+        found            = true;
+    } else {
+        contracted_vertices += cv;        // optional: keep union of all, if intended
+    }

Please double‑check the intended behaviour and adjust accordingly.

@cvvergara cvvergara merged commit f29f83f into pgRouting:main Apr 17, 2025
1 check passed
@cvvergara cvvergara deleted the issue-2802-code-reorganization-on-pgr_contraction-3.8 branch April 17, 2025 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code reorganization on pgr_contraction
3 participants