Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgr_edwardMoore GSOC-2019 week 11 - Lint/Misc Changes #29

Merged
merged 5 commits into from Aug 8, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 28 additions & 50 deletions include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp
Expand Up @@ -21,28 +21,27 @@ 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*/

#ifndef INCLUDE_MST_PGR_BINARYBREADTHFIRSTSEARCH_HPP_
#define INCLUDE_MST_PGR_BINARYBREADTHFIRSTSEARCH_HPP_
#ifndef INCLUDE_BREADTHFIRSTSEARCH_PGR_BINARYBREADTHFIRSTSEARCH_HPP_
#define INCLUDE_BREADTHFIRSTSEARCH_PGR_BINARYBREADTHFIRSTSEARCH_HPP_
#pragma once

#include <deque>
#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>

#include "cpp_common/basePath_SSEC.hpp"
#include "cpp_common/pgr_base_graph.hpp"
#include "cpp_common/pgr_assert.h"
//******************************************

namespace pgrouting
{
namespace functions
{
namespace pgrouting {
namespace functions {

template <class G>
class Pgr_binaryBreadthFirstSearch
{
public:
class Pgr_binaryBreadthFirstSearch {
public:
typedef typename G::V V;
typedef typename G::E E;
typedef typename G::B_G B_G;
Expand All @@ -53,18 +52,15 @@ class Pgr_binaryBreadthFirstSearch
G &graph,
std::vector<int64_t> start_vertex,
std::vector<int64_t> end_vertex) {

std::deque<Path> paths;

for(auto source : start_vertex) {
for (auto source : start_vertex) {
std::deque<Path> result_paths = one_to_many_binaryBreadthFirstSearch(
graph,
source,
end_vertex
);

end_vertex);
paths.insert(
paths.begin(),
paths.begin(),
std::make_move_iterator(result_paths.begin()),
std::make_move_iterator(result_paths.end()));
}
Expand All @@ -81,20 +77,16 @@ class Pgr_binaryBreadthFirstSearch
return paths;
}

private:

private:
E DEFAULT_EDGE;

std::deque<Path> one_to_many_binaryBreadthFirstSearch(
G &graph,
int64_t start_vertex,
std::vector<int64_t> end_vertex)
{

std::vector<int64_t> end_vertex) {
std::deque<Path> paths;

if (graph.has_vertex(start_vertex) == false)
{
if (graph.has_vertex(start_vertex) == false) {
return paths;
}

Expand All @@ -108,26 +100,22 @@ class Pgr_binaryBreadthFirstSearch
current_cost[bgl_start_vertex] = 0;
dq.push_front(bgl_start_vertex);

while (dq.empty() == false)
{
while (dq.empty() == false) {
int64_t head_vertex = dq.front();

dq.pop_front();

updateVertexCosts(graph, current_cost, from_edge, dq, head_vertex);
}

for (auto target_vertex : end_vertex)
{
if (graph.has_vertex(target_vertex) == false)
{
for (auto target_vertex : end_vertex) {
if (graph.has_vertex(target_vertex) == false) {
continue;
}

int64_t bgl_target_vertex = graph.get_V(target_vertex);

if (from_edge[bgl_target_vertex] == DEFAULT_EDGE)
{
if (from_edge[bgl_target_vertex] == DEFAULT_EDGE) {
continue;
}

Expand All @@ -144,16 +132,14 @@ class Pgr_binaryBreadthFirstSearch
int64_t target,
int64_t bgl_target_vertex,
std::vector<E> &from_edge,
std::vector<double> &current_cost)
{
std::vector<double> &current_cost) {
int64_t current_node = bgl_target_vertex;

Path path = Path(graph[bgl_start_vertex].id, graph[current_node].id);

path.push_back({target, -1, 0, current_cost[current_node]});

do
{
do {
E e = from_edge[current_node];
auto from = graph.source(e);

Expand All @@ -171,43 +157,35 @@ class Pgr_binaryBreadthFirstSearch
std::vector<double> &current_cost,
std::vector<E> &from_edge,
std::deque<int64_t> &dq,
int64_t &head_vertex)
{
int64_t &head_vertex) {
auto out_edges = boost::out_edges(head_vertex, graph.graph);
E e;
EO_i out_i;
EO_i out_end;
V v_source, v_target;

for (boost::tie(out_i, out_end) = out_edges;
out_i != out_end; ++out_i)
{

out_i != out_end; ++out_i) {
e = *out_i;
v_target = graph.target(e);
v_source = graph.source(e);
double edge_cost = graph[e].cost;

if (std::isinf(current_cost[v_target]) or current_cost[v_source] + edge_cost < current_cost[v_target])
{

if (std::isinf(current_cost[v_target]) || current_cost[v_source] + edge_cost < current_cost[v_target]) {
current_cost[v_target] = current_cost[v_source] + edge_cost;

from_edge[v_target] = e;

if (edge_cost != 0)
{
if (edge_cost != 0) {
dq.push_back(v_target);
}
else
{
} else {
dq.push_front(v_target);
}
}
}
}
};
} // namespace functions
} // namespace pgrouting
} // namespace functions
} // namespace pgrouting

#endif // INCLUDE_MST_PGR_BINARYBREADTHFIRSTSEARCH_HPP_
#endif // INCLUDE_BREADTHFIRSTSEARCH_PGR_BINARYBREADTHFIRSTSEARCH_HPP_
24 changes: 9 additions & 15 deletions include/breadthFirstSearch/pgr_breadthFirstSearch.hpp
Expand Up @@ -21,15 +21,16 @@ 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*/

#ifndef INCLUDE_MST_PGR_BREADTHFIRSTSEARCH_HPP_
#define INCLUDE_MST_PGR_BREADTHFIRSTSEARCH_HPP_
#ifndef INCLUDE_BREADTHFIRSTSEARCH_PGR_BREADTHFIRSTSEARCH_HPP_
#define INCLUDE_BREADTHFIRSTSEARCH_PGR_BREADTHFIRSTSEARCH_HPP_
#pragma once

#include <vector>

#include <visitors/edges_order_bfs_visitor.hpp>
#include <boost/graph/breadth_first_search.hpp>

#include <vector>

#include "cpp_common/pgr_base_graph.hpp"
//******************************************

Expand All @@ -38,7 +39,7 @@ namespace functions {

template <class G>
class Pgr_breadthFirstSearch {
public:
public:
typedef typename G::V V;
typedef typename G::E E;
typedef typename G::B_G B_G;
Expand All @@ -48,12 +49,10 @@ class Pgr_breadthFirstSearch {
G &graph,
std::vector<int64_t> start_vertex,
int64_t depth) {

std::vector<pgr_mst_rt> results;
using bfs_visitor = visitors::Edges_order_bfs_visitor<E>;

for (auto source : start_vertex) {

std::vector<E> visited_order;

if (graph.has_vertex(source)) {
Expand All @@ -65,15 +64,11 @@ class Pgr_breadthFirstSearch {
auto single_source_results = get_results(visited_order, source, depth, graph);
results.insert(results.end(), single_source_results.begin(), single_source_results.end());
}

}

return results;

}

private:

private:
template <typename T>
std::vector<pgr_mst_rt> get_results(
T order,
Expand Down Expand Up @@ -105,9 +100,8 @@ class Pgr_breadthFirstSearch {
}
return results;
}

};
} // namespace functions
} // namespace pgrouting
} // namespace functions
} // namespace pgrouting

#endif // INCLUDE_MST_PGR_BREADTHFIRSTSEARCH_HPP_
#endif // INCLUDE_BREADTHFIRSTSEARCH_PGR_BREADTHFIRSTSEARCH_HPP_
Expand Up @@ -27,8 +27,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

********************************************************************PGR-GNU*/

#ifndef INCLUDE_DRIVERS_BINARYBREADTHFIRSTSEARCH_BINARYBREADTHFIRSTSEARCH_DRIVER_H_
#define INCLUDE_DRIVERS_BINARYBREADTHFIRSTSEARCH_BINARYBREADTHFIRSTSEARCH_DRIVER_H_
#ifndef INCLUDE_DRIVERS_BREADTHFIRSTSEARCH_BINARYBREADTHFIRSTSEARCH_DRIVER_H_
#define INCLUDE_DRIVERS_BREADTHFIRSTSEARCH_BINARYBREADTHFIRSTSEARCH_DRIVER_H_
#pragma once

/* for size-t */
Expand Down Expand Up @@ -75,4 +75,4 @@ extern "C" {
}
#endif

#endif // INCLUDE_DRIVERS_BINARYBREADTHFIRSTSEARCH_BINARYBREADTHFIRSTSEARCH_DRIVER_H_
#endif // INCLUDE_DRIVERS_BREADTHFIRSTSEARCH_BINARYBREADTHFIRSTSEARCH_DRIVER_H_
25 changes: 8 additions & 17 deletions src/breadthFirstSearch/binaryBreadthFirstSearch.c
Expand Up @@ -77,8 +77,7 @@ process(
pgr_get_edges(edges_sql, &edges, &total_edges);
PGR_DBG("Total %ld edges in query:", total_edges);

if (total_edges == 0)
{
if (total_edges == 0) {
if (start_vidsArr)
pfree(start_vidsArr);
if (end_vidsArr)
Expand Down Expand Up @@ -110,8 +109,7 @@ process(
time_msg(" processing pgr_binaryBreadthFirstSearch", start_t, clock());
PGR_DBG("Returning %ld tuples", *result_count);

if (err_msg)
{
if (err_msg) {
if (*result_tuples)
pfree(*result_tuples);
}
Expand All @@ -134,8 +132,7 @@ process(
pgr_SPI_finish();
}

PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS)
{
PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS) {
FuncCallContext *funcctx;
TupleDesc tuple_desc;

Expand All @@ -144,8 +141,7 @@ PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS)
size_t result_count = 0;
/**************************************************************************/

if (SRF_IS_FIRSTCALL())
{
if (SRF_IS_FIRSTCALL()) {
MemoryContext oldcontext;
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
Expand Down Expand Up @@ -174,8 +170,7 @@ PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS)
funcctx->max_calls = (uint32_t)result_count;
#endif
funcctx->user_fctx = result_tuples;
if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
{
if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) {
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
Expand All @@ -190,8 +185,7 @@ PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS)
tuple_desc = funcctx->tuple_desc;
result_tuples = (General_path_element_t *)funcctx->user_fctx;

if (funcctx->call_cntr < funcctx->max_calls)
{
if (funcctx->call_cntr < funcctx->max_calls) {
HeapTuple tuple;
Datum result;
Datum *values;
Expand All @@ -214,8 +208,7 @@ PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS)
nulls = palloc(numb * sizeof(bool));

size_t i;
for (i = 0; i < numb; ++i)
{
for (i = 0; i < numb; ++i) {
nulls[i] = false;
}

Expand All @@ -233,9 +226,7 @@ PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS)
tuple = heap_form_tuple(tuple_desc, values, nulls);
result = HeapTupleGetDatum(tuple);
SRF_RETURN_NEXT(funcctx, result);
}
else
{
} else {
/**********************************************************************/

PGR_DBG("Clean up code");
Expand Down