Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*PGR-GNU*****************************************************************
File: pgr_componentV_t.h
File: pgr_componentSV_rt.h

Copyright (c) 2015 Celia Virginia Vergara Castillo
Mail: vicky_vergara@hotmail.com
Expand All @@ -23,8 +23,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
/*! @file */

#ifndef INCLUDE_C_TYPES_PGR_COMPONENTV_T_H_
#define INCLUDE_C_TYPES_PGR_COMPONENTV_T_H_
#ifndef INCLUDE_C_TYPES_PGR_COMPONENTSV_RT_H_
#define INCLUDE_C_TYPES_PGR_COMPONENTSV_RT_H_
#pragma once


Expand Down Expand Up @@ -59,6 +59,6 @@ typedef struct {
int64_t component;
int n_seq;
int64_t node;
} pgr_componentV_t;
} pgr_componentsV_rt;

#endif // INCLUDE_C_TYPES_PGR_COMPONENTV_T_H_
#endif // INCLUDE_C_TYPES_PGR_COMPONENTSV_RT_H_
89 changes: 29 additions & 60 deletions include/components/pgr_components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <boost/graph/strong_components.hpp>

#include <vector>
#include <map>
#include <utility>
#include <algorithm>

Expand All @@ -57,75 +56,42 @@ class Pgr_components {
typedef typename G::V V;

//! Connected Components Vertex Version
std::vector<pgr_componentV_t> connectedComponentsV(
std::vector<pgr_componentsV_rt> connectedComponentsV(
G &graph);

//! Strongly Connected Components Vertex Version
std::vector<pgr_componentV_t> strongComponentsV(
std::vector<pgr_componentsV_rt> strongComponentsV(
G &graph);

private:
//! Compare two pgr_componentV_t structs
static bool sort_cmp(
const pgr_componentV_t &a,
const pgr_componentV_t &b);

//! Generate Results, Vertex Version
std::vector<pgr_componentV_t> generate_resultsV(
std::vector<pgr_componentsV_rt> generate_resultsV(
G &graph,
std::vector< V >);

//! Generate Map V_to_id
std::map< V, int64_t > V_to_id;
void generate_map(
std::map< int64_t, V > id_to_V);
};


/******************** IMPLEMENTTION ******************/

//! Compare two pgr_componentV_t structs
template < class G >
bool
Pgr_components< G >::sort_cmp(
const pgr_componentV_t &a,
const pgr_componentV_t &b) {
if (a.component == b.component)
return a.node < b.node;
return a.component < b.component;
}

//! Generate map V_to_id
template < class G >
void
Pgr_components< G >::generate_map(
std::map< int64_t, V > id_to_V) {
V_to_id.clear();
for (auto iter : id_to_V) {
V_to_id.insert(std::make_pair(iter.second, iter.first));
}
}

//! Generate Results, Vertex Version
template < class G >
std::vector<pgr_componentV_t>
std::vector<pgr_componentsV_rt>
Pgr_components< G >::generate_resultsV(
G &graph,
std::vector< V > components) {
// generate V_to_id
generate_map(graph.vertices_map);

// generate results
auto totalNodes = num_vertices(graph.graph);

std::vector< pgr_componentV_t > results;
std::vector< pgr_componentsV_rt > results;
results.resize(totalNodes);

std::vector< int64_t > result_comp;
result_comp.resize(0);
size_t temp_size = 0;
for (auto i = 0; i < totalNodes; i++) {
results[i].node = V_to_id.find(i)->second;
for (V i = 0; i < totalNodes; i++) {
results[i].node = graph[i].id;
if (components[i] >= temp_size) {
result_comp.push_back(results[i].node);
temp_size++;
Expand All @@ -136,51 +102,54 @@ Pgr_components< G >::generate_resultsV(
}

// generate component number
for (auto i = 0; i < totalNodes; i++) {
for (V i = 0; i < totalNodes; i++) {
results[i].component = result_comp[components[i]];
}

// sort results and generate n_seq
std::sort(results.begin(), results.end(), sort_cmp);
for (auto i = 0; i < totalNodes; i++) {
if (i == 0 || results[i].component != results[i - 1].component) {
results[i].n_seq = 1;
} else {
results[i].n_seq = results[i - 1].n_seq + 1;
}
}
std::sort(results.begin(), results.end(),
[](const pgr_componentsV_rt &left, const pgr_componentsV_rt &right) {
return left.node < right.node; });

std::stable_sort(results.begin(), results.end(),
[](const pgr_componentsV_rt &left, const pgr_componentsV_rt &right) {
return left.component < right.component; });

auto current = results[0].component;
int seq(0);
for (auto &result: results) {
result.n_seq = result.component == current ? ++seq : seq = 1;
current = result.component;
}

return results;
}

//! Connected Components Vertex Version
template < class G >
std::vector<pgr_componentV_t>
std::vector<pgr_componentsV_rt>
Pgr_components< G >::connectedComponentsV(
G &graph) {
// perform the algorithm
std::vector< V > components(num_vertices(graph.graph));
boost::connected_components(graph.graph, &components[0]);

//get the results
std::vector<pgr_componentV_t> results = generate_resultsV(graph, components);

// get the results
return results;
return generate_resultsV(graph, components);
}

//! Strongly Connected Components Vertex Version
template < class G >
std::vector<pgr_componentV_t>
std::vector<pgr_componentsV_rt>
Pgr_components< G >::strongComponentsV(
G &graph) {
// perform the algorithm
std::vector< V > components(num_vertices(graph.graph));
boost::strong_components(graph.graph, &components[0]);
boost::strong_components(graph.graph,
boost::make_iterator_property_map(components.begin(), get(boost::vertex_index, graph.graph)));

// get the results
std::vector<pgr_componentV_t> results = generate_resultsV(graph, components);

return results;
return generate_resultsV(graph, components);
}

#endif // INCLUDE_COMPONENTS_PGR_COMPONENTS_HPP_
4 changes: 2 additions & 2 deletions include/drivers/components/connectedComponentsV_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#pragma once

#include "c_types/pgr_edge_t.h"
#include "c_types/pgr_componentV_t.h"
#include "c_types/pgr_componentsV_rt.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -49,7 +49,7 @@ extern "C" {
do_pgr_connectedComponentsV(
pgr_edge_t *data_edges,
size_t total_edges,
pgr_componentV_t **return_tuples,
pgr_componentsV_rt **return_tuples,
size_t *return_count,
char ** log_msg,
char ** notice_msg,
Expand Down
4 changes: 2 additions & 2 deletions include/drivers/components/strongComponentsV_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#pragma once

#include "c_types/pgr_edge_t.h"
#include "c_types/pgr_componentV_t.h"
#include "c_types/pgr_componentsV_rt.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -49,7 +49,7 @@ extern "C" {
do_pgr_strongComponentsV(
pgr_edge_t *data_edges,
size_t total_edges,
pgr_componentV_t **return_tuples,
pgr_componentsV_rt **return_tuples,
size_t *return_count,
char ** log_msg,
char ** notice_msg,
Expand Down
6 changes: 3 additions & 3 deletions src/components/src/connectedComponentsV.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static
void
process(
char* edges_sql,
pgr_componentV_t **result_tuples,
pgr_componentsV_rt **result_tuples,
size_t *result_count) {
/*
* https://www.postgresql.org/docs/current/static/spi-spi-connect.html
Expand Down Expand Up @@ -146,7 +146,7 @@ PGDLLEXPORT Datum connectedComponentsV(PG_FUNCTION_ARGS) {
/**************************************************************************/
/* MODIFY AS NEEDED */
/* */
pgr_componentV_t *result_tuples = NULL;
pgr_componentsV_rt *result_tuples = NULL;
size_t result_count = 0;
/* */
/**************************************************************************/
Expand Down Expand Up @@ -203,7 +203,7 @@ PGDLLEXPORT Datum connectedComponentsV(PG_FUNCTION_ARGS) {

funcctx = SRF_PERCALL_SETUP();
tuple_desc = funcctx->tuple_desc;
result_tuples = (pgr_componentV_t*) funcctx->user_fctx;
result_tuples = (pgr_componentsV_rt*) funcctx->user_fctx;

if (funcctx->call_cntr < funcctx->max_calls) {
HeapTuple tuple;
Expand Down
8 changes: 4 additions & 4 deletions src/components/src/connectedComponentsV_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

template < class G >
static
std::vector<pgr_componentV_t>
std::vector<pgr_componentsV_rt>
pgr_connectedComponentsV(
G &graph) {
std::vector<pgr_componentV_t> results;
std::vector<pgr_componentsV_rt> results;
Pgr_components< G > fn_components;
return fn_components.connectedComponentsV(graph);
}
Expand All @@ -63,7 +63,7 @@ void
do_pgr_connectedComponentsV(
pgr_edge_t *data_edges,
size_t total_edges,
pgr_componentV_t **return_tuples,
pgr_componentsV_rt **return_tuples,
size_t *return_count,
char ** log_msg,
char ** notice_msg,
Expand All @@ -81,7 +81,7 @@ do_pgr_connectedComponentsV(

graphType gType = UNDIRECTED;

std::vector<pgr_componentV_t> results;
std::vector<pgr_componentsV_rt> results;

log << "Working with Undirected Graph\n";
pgrouting::UndirectedGraph undigraph(gType);
Expand Down
6 changes: 3 additions & 3 deletions src/components/src/strongComponentsV.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static
void
process(
char* edges_sql,
pgr_componentV_t **result_tuples,
pgr_componentsV_rt **result_tuples,
size_t *result_count) {
/*
* https://www.postgresql.org/docs/current/static/spi-spi-connect.html
Expand Down Expand Up @@ -146,7 +146,7 @@ PGDLLEXPORT Datum strongComponentsV(PG_FUNCTION_ARGS) {
/**************************************************************************/
/* MODIFY AS NEEDED */
/* */
pgr_componentV_t *result_tuples = NULL;
pgr_componentsV_rt *result_tuples = NULL;
size_t result_count = 0;
/* */
/**************************************************************************/
Expand Down Expand Up @@ -203,7 +203,7 @@ PGDLLEXPORT Datum strongComponentsV(PG_FUNCTION_ARGS) {

funcctx = SRF_PERCALL_SETUP();
tuple_desc = funcctx->tuple_desc;
result_tuples = (pgr_componentV_t*) funcctx->user_fctx;
result_tuples = (pgr_componentsV_rt*) funcctx->user_fctx;

if (funcctx->call_cntr < funcctx->max_calls) {
HeapTuple tuple;
Expand Down
8 changes: 4 additions & 4 deletions src/components/src/strongComponentsV_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

template < class G >
static
std::vector<pgr_componentV_t>
std::vector<pgr_componentsV_rt>
pgr_strongComponentsV(
G &graph) {
std::vector<pgr_componentV_t> results;
std::vector<pgr_componentsV_rt> results;
Pgr_components< G > fn_components;
return fn_components.strongComponentsV(graph);
}
Expand All @@ -63,7 +63,7 @@ void
do_pgr_strongComponentsV(
pgr_edge_t *data_edges,
size_t total_edges,
pgr_componentV_t **return_tuples,
pgr_componentsV_rt **return_tuples,
size_t *return_count,
char ** log_msg,
char ** notice_msg,
Expand All @@ -81,7 +81,7 @@ do_pgr_strongComponentsV(

graphType gType = DIRECTED;

std::vector<pgr_componentV_t> results;
std::vector<pgr_componentsV_rt> results;

log << "Working with Directed Graph\n";
pgrouting::DirectedGraph digraph(gType);
Expand Down