Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Bug Fixes

* [#2966](https://github.com/pgRouting/pgrouting/issues/2966): pgr_withPoints does not pick optimal route when fraction = 1

Code enhancement

* [#3025](https://github.com/pgRouting/pgrouting/issues/3025): Ordering functions standardized
Comment on lines +19 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

NEWS.md is auto-generated and should not be edited manually.

The changes to NEWS.md (lines 19–21) modify an auto-generated file. Per pgRouting's workflow, changes to NEWS.md content should be implemented via the generator script tools/release-scripts/notes2news.pl or its source files, not by direct manual edits. Manual changes will be overwritten during the next generation.

Please move the "Code enhancement" entry to the source file that feeds the notes2news.pl script instead of editing NEWS.md directly.

🤖 Prompt for AI Agents
In @NEWS.md around lines 19 - 21, The NEWS.md change is auto-generated and must
not be edited directly; revert the manual edit in NEWS.md, add the "Code
enhancement" entry (including the pgrouting issue link and "Ordering functions
standardized" text) into the source input consumed by the notes2news.pl
generator (referenced as notes2news.pl), then re-run the generator to regenerate
NEWS.md so the entry is preserved via the canonical source instead of a direct
edit.



## pgRouting 4.0

Expand Down
2 changes: 1 addition & 1 deletion doc/src/pgRouting-introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Contributors
This Release Contributors
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Individuals in this release v4.0.0 (in alphabetical order)
Individuals in this release |release| (in alphabetical order)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Mohit Rawat,
Expand Down
5 changes: 5 additions & 0 deletions doc/src/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Bug Fixes

* :issue:`2966`: pgr_withPoints does not pick optimal route when fraction = 1

Code enhancement
...............................................................................

* :issue:`3025`: Ordering functions standardized

.. current

pgRouting 4.0
Expand Down
40 changes: 17 additions & 23 deletions include/c_types/i_rt.h → include/c_common/enums.h
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
/*PGR-GNU*****************************************************************
File: i_rt.h
File: enums.h
Copyright (c) 2015 pgRouting developers
Copyright (c) 2025 pgRouting developers
Mail: [email protected]
Function's developer:
Copyright (c) 2019 Hang Wu
mail: [email protected]
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
/*! @file */

#ifndef INCLUDE_C_TYPES_I_RT_H_
#define INCLUDE_C_TYPES_I_RT_H_
#pragma once

/* for int64_t */
#ifdef __cplusplus
# include <cstdint>
#else
# include <stdint.h>
#endif

struct I_rt {
int64_t id;
********************************************************************PGR-GNU*/

#ifndef INCLUDE_C_COMMON_ENUMS_H_
#define INCLUDE_C_COMMON_ENUMS_H_

enum Which {
/** undirected graph + results: vertex id */
SLOAN = 0, CUTCHILL, KING,
/** directed graph + results: vertex id */
TOPOSORT = 11
};

#endif // INCLUDE_C_TYPES_I_RT_H_
#endif // INCLUDE_C_COMMON_ENUMS_H_
1 change: 1 addition & 0 deletions include/cpp_common/basic_vertex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Basic_vertex {


friend std::ostream& operator<<(std::ostream& log, const Basic_vertex &v);

public:
int64_t id;
};
Expand Down
25 changes: 25 additions & 0 deletions include/cpp_common/to_postgres.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ count_rows(

} // namespace detail

/** @brief Vector of vertices id are saved on a C array
*
* @param[in] graph Created graph with the base Graph
* @param[in] data data[i] -> the ith element of the vector contains the vertex id
* @param[out] result_count The size of the vector
* @param[out] result_tuples The C array of bigint
*
* bigint results[i]
*/
template <class G>
void get_vertexId(
const G &graph,
const std::vector<typename G::V> &data,
size_t &result_count,
int64_t **result_tuples) {
result_count = data.size();
*result_tuples = pgrouting::pgr_alloc(result_count, (*result_tuples));

size_t seq = 0;
for (auto const &v : data) {
(*result_tuples)[seq] = graph.graph[v].id;
++seq;
}
}

/** @brief Stored results on a vector are saved on a C array
*
* @param[in] graph Created graph with the base Graph
Expand Down
63 changes: 0 additions & 63 deletions include/drivers/ordering/cuthillMckeeOrdering_driver.h

This file was deleted.

56 changes: 0 additions & 56 deletions include/drivers/ordering/topologicalSort_driver.h

This file was deleted.

3 changes: 2 additions & 1 deletion include/drivers/ordering_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <string>
#include <cstdint>

#include "c_common/enums.h"
void
do_ordering(
std::string, int,
const std::string&, Which,
int64_t**, size_t*,
char **, char **, char **);

Expand Down
116 changes: 2 additions & 114 deletions include/ordering/cuthillMckeeOrdering.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,128 +31,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#define INCLUDE_ORDERING_CUTHILLMCKEEORDERING_HPP_
#pragma once

#include <algorithm>
#include <vector>
#include <map>
#include <cstdint>

#include <boost/property_map/property_map.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/property_map/vector_property_map.hpp>
#include <boost/type_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/cuthill_mckee_ordering.hpp>

#include "cpp_common/base_graph.hpp"
#include "cpp_common/interruption.hpp"
#include "cpp_common/messages.hpp"

#include "c_types/ii_t_rt.h"

/** @file cuthillMckeeOrdering.hpp
* @brief The main file which calls the respective boost function.
*
* Contains actual implementation of the function and the calling
* of the respective boost function.
*/


namespace pgrouting {
namespace functions {

template <class G>
class CuthillMckeeOrdering : public Pgr_messages {
public:
typedef typename G::V V;
typedef typename G::E E;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
typedef boost::graph_traits<Graph>::vertices_size_type size_type;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;

/** @name CuthillMckeeOrdering
* @{
*
*/

/** @brief cuthillMckeeOrdering function
*
* It does all the processing and returns the results.
*
* @param graph the graph containing the edges
*
* @returns results, when results are found
*
* @see [boost::cuthill_mckee_ordering]
* (https://www.boost.org/libs/graph/doc/cuthill_mckee_ordering.html)
*/

std::vector<II_t_rt>
cuthillMckeeOrdering(G &graph) {
std::vector<II_t_rt>results;

// map which store the indices with their nodes.
auto i_map = boost::get(boost::vertex_index, graph.graph);

// vector which will store the order of the indices.
std::vector<Vertex> inv_perm(boost::num_vertices(graph.graph));

// vector which will store the color of all the vertices in the graph
std::vector <boost::default_color_type> colors(boost::num_vertices(graph.graph));

// An iterator property map which records the color of each vertex
auto color_map = boost::make_iterator_property_map(&colors[0], i_map, colors[0]);

// map which store the degree of each vertex.
auto out_deg = boost::make_out_degree_map(graph.graph);

/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
CHECK_FOR_INTERRUPTS();

try {
boost::cuthill_mckee_ordering(graph.graph, inv_perm.rbegin(), color_map, out_deg);
} catch (boost::exception const& ex) {
(void)ex;
throw;
} catch (std::exception &e) {
(void)e;
throw;
} catch (...) {
throw;
}

results = get_results(inv_perm, graph);

return results;
}

//@}

private:
/** @brief to get the results
*
* Uses the `inv_perm` vector to get the results i.e. the ordering.
*
* @param inv_perm vector which contains the new ordering of indices.
* @param graph the graph containing the edges
*
* @returns `results` vector
*/
std::vector <II_t_rt> get_results(
std::vector <size_type> & inv_perm,
const G &graph) {
std::vector <II_t_rt> results;

for (std::vector<Vertex>::const_iterator i = inv_perm.begin();
i != inv_perm.end(); ++i) {
log << inv_perm[*i] << " ";
auto seq = graph[*i].id;
results.push_back({{seq}, {static_cast<int64_t>(graph.graph[*i].id)}});
seq++;
}
std::vector<pgrouting::UndirectedGraph::V>
cuthillMckeeOrdering(pgrouting::UndirectedGraph &graph);
Comment on lines +41 to +42
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check signatures of other ordering functions for consistency

echo "=== Checking kingOrdering signature ==="
ast-grep --pattern $'std::vector<$_> kingOrdering($$$)'

echo ""
echo "=== Checking sloanOrdering signature ==="
ast-grep --pattern $'std::vector<$_> sloanOrdering($$$)'

echo ""
echo "=== Checking topologicalSort signature ==="
ast-grep --pattern $'std::vector<$_> topologicalSort($$$)'

echo ""
echo "=== Checking cuthillMckeeOrdering signature ==="
ast-grep --pattern $'std::vector<$_> cuthillMckeeOrdering($$$)'

Repository: pgRouting/pgrouting

Length of output: 237


🏁 Script executed:

#!/bin/bash
# Search for header files containing these ordering functions
echo "=== Finding ordering function headers ==="
fd -t f -e hpp . include/ordering/ 2>/dev/null | head -20

echo ""
echo "=== Contents of cuthillMckeeOrdering.hpp ==="
cat include/ordering/cuthillMckeeOrdering.hpp

Repository: pgRouting/pgrouting

Length of output: 1794


🏁 Script executed:

#!/bin/bash
# Search more broadly for function signatures
echo "=== Searching for kingOrdering ==="
rg "kingOrdering" -t hpp --max-count 5

echo ""
echo "=== Searching for sloanOrdering ==="
rg "sloanOrdering" -t hpp --max-count 5

Repository: pgRouting/pgrouting

Length of output: 207


🏁 Script executed:

#!/bin/bash
# Read the kingOrdering header
echo "=== Contents of kingOrdering.hpp ==="
cat include/ordering/kingOrdering.hpp

echo ""
echo "=== Contents of sloanOrdering.hpp ==="
cat include/ordering/sloanOrdering.hpp

Repository: pgRouting/pgrouting

Length of output: 3007


🏁 Script executed:

#!/bin/bash
# Search for function signatures using rg with correct syntax
echo "=== Searching for all ordering function declarations ==="
rg "Ordering\(" include/ordering/ -A 1 -B 1

echo ""
echo "=== Check if any use const parameter ==="
rg "const.*UndirectedGraph" include/ordering/ -A 1

Repository: pgRouting/pgrouting

Length of output: 788


🏁 Script executed:

#!/bin/bash
# Find and examine the implementation files
echo "=== Finding implementation files for ordering functions ==="
fd -t f -e cpp . src/ordering/ 2>/dev/null | head -20

echo ""
echo "=== Check cuthillMckeeOrdering implementation ==="
fd -t f "cuthillMckee" src/

echo ""
echo "=== Check kingOrdering implementation ==="
fd -t f "king" src/ordering/ 2>/dev/null

echo ""
echo "=== Check sloan implementation ==="
fd -t f "sloan" src/ordering/ 2>/dev/null

Repository: pgRouting/pgrouting

Length of output: 643


🏁 Script executed:

#!/bin/bash
# Look at the actual implementations
echo "=== cuthillMckeeOrdering.cpp ==="
find . -name "*cuthillMckee*.cpp" -exec cat {} \; 2>/dev/null | head -100

echo ""
echo "=== Check if graph is modified in any implementation ==="
find . -path "*/ordering/*.cpp" -exec grep -l "graph\." {} \; 2>/dev/null

Repository: pgRouting/pgrouting

Length of output: 3332


🏁 Script executed:

#!/bin/bash
# Check the other implementations to see if they also only read the graph
echo "=== kingOrdering.cpp ==="
cat src/ordering/kingOrdering.cpp | head -100

echo ""
echo "=== sloanOrdering.cpp ==="
cat src/ordering/sloanOrdering.cpp | head -100

echo ""
echo "=== Check if UndirectedGraph methods used in implementations are const-friendly ==="
rg "num_vertices|vertex_index|make_out_degree_map|make_iterator_property_map" include/cpp_common/ -A 2 | head -50

Repository: pgRouting/pgrouting

Length of output: 8994


🏁 Script executed:

#!/bin/bash
# Check if the graph member variable is const-accessible
echo "=== Check base_graph.hpp for const-correctness support ==="
cat include/cpp_common/base_graph.hpp | head -200

echo ""
echo "=== Search for const usage in ordering algorithms ==="
rg "const.*UndirectedGraph" src/ordering/ -A 2

Repository: pgRouting/pgrouting

Length of output: 5905


🏁 Script executed:

#!/bin/bash
# Verify Boost graph algorithms accept const references
echo "=== Check Boost algorithm signatures ==="
web_search "boost::cuthill_mckee_ordering const graph reference"

Repository: pgRouting/pgrouting

Length of output: 171


🌐 Web query:

boost::cuthill_mckee_ordering const reference parameter requirements

💡 Result:

Short answer: the graph parameter may be a const reference (signatures use const Graph& or const IncidenceGraph&). The graph type must model the required graph concepts (IncidenceGraph for the single-component APIs, or VertexListGraph+IncidenceGraph for the vertex-list variants). In addition you must supply appropriate property maps: a writable/work ColorMap (used internally), and a DegreeMap (a readable Vertex->degree map) or a VertexIndexMap for the overloads that infer maps. See Boost Graph Library docs for exact signatures and parameter requirements. [1][2]

Sources:

  • Boost Graph Library — Cuthill–McKee ordering (function signatures and parameter notes). [1]
  • boost/graph/cuthill_mckee_ordering.hpp (implementation / overloads showing const Graph& signatures). [2]

Add const qualifier to graph parameter for all three ordering functions.

The function signatures are consistent across cuthillMckeeOrdering, kingOrdering, and sloanOrdering, all using the same non-const parameter signature. However, the parameter should be const pgrouting::UndirectedGraph &graph since these implementations only read the graph structure and the underlying Boost graph algorithms (boost::cuthill_mckee_ordering, boost::king_ordering, boost::sloan_ordering) support const references. Update all three function declarations and implementations to use the const qualifier for improved const-correctness.

🤖 Prompt for AI Agents
In @include/ordering/cuthillMckeeOrdering.hpp around lines 41 - 42, Change the
three ordering functions to accept the graph by const reference: update the
declarations and definitions of cuthillMckeeOrdering, kingOrdering, and
sloanOrdering to use const pgrouting::UndirectedGraph &graph instead of a
non-const reference, and adjust any internal calls if necessary to use
const-compatible Boost algorithms (e.g., pass graph to
boost::cuthill_mckee_ordering / boost::king_ordering / boost::sloan_ordering
without mutating it); ensure any local variables that previously tried to modify
graph are copied or made non-mutating so the implementations compile with the
new const parameter.


return results;
}
};
} // namespace functions
} // namespace pgrouting

Expand Down
Loading