Skip to content

Commit

Permalink
Merge pull request #75 from stdgraph/idx_adj_list
Browse files Browse the repository at this point in the history
Idx adj list
  • Loading branch information
pratzl committed Nov 15, 2023
2 parents 95f3c4d + 20f379e commit c400ada
Show file tree
Hide file tree
Showing 10 changed files with 1,180 additions and 650 deletions.
30 changes: 10 additions & 20 deletions example/CppCon2022/rr_adaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ class rr_adaptor {
return g.vertices_;
}

friend vertex_id_type
tag_invoke(std::graph::tag_invoke::vertex_id_fn_t, const graph_type& g, std::ranges::iterator_t<vertices_range> ui) {
friend vertex_id_type vertex_id(const graph_type& g, std::ranges::iterator_t<vertices_range> ui) {
return static_cast<vertex_id_type>(ui -
std::ranges::begin(g.vertices_)); // overriden to assure correct type returned
}
Expand All @@ -157,24 +156,21 @@ class rr_adaptor {
return get<0>(to_tuple(uv));
}

friend constexpr vertex_value_type&
tag_invoke(std::graph::tag_invoke::vertex_value_fn_t, graph_type& g, vertex_type& u) {
friend constexpr vertex_value_type& vertex_value(graph_type& g, vertex_type& u) {
size_t uidx = static_cast<size_t>(&u - g.vertices_.data());
return g.vertex_values_[uidx];
}
friend constexpr const vertex_value_type&
tag_invoke(std::graph::tag_invoke::vertex_value_fn_t, const graph_type& g, const vertex_type& u) {
friend constexpr const vertex_value_type& vertex_value(const graph_type& g, const vertex_type& u) {
size_t uidx = static_cast<size_t>(&u - g.vertices_.data());
return g.vertex_values_[uidx];
}

// edge_value(g,uv)
friend constexpr edge_value_type& tag_invoke(std::graph::tag_invoke::edge_value_fn_t, graph_type& g, edge_type& uv) {
friend constexpr edge_value_type& edge_value(graph_type& g, edge_type& uv) {
auto t = to_tuple(uv);
return get<1>(t);
}
friend constexpr const edge_value_type&
tag_invoke(std::graph::tag_invoke::edge_value_fn_t, const graph_type& g, const edge_type& uv) {
friend constexpr const edge_value_type& edge_value(const graph_type& g, const edge_type& uv) {
auto t = to_tuple(uv);
return get<1>(t);
}
Expand Down Expand Up @@ -365,8 +361,7 @@ class rr_adaptor2 {
return g.vertices_;
}

friend vertex_id_type
tag_invoke(std::graph::tag_invoke::vertex_id_fn_t, const graph_type& g, std::ranges::iterator_t<vertices_range> ui) {
friend vertex_id_type vertex_id(const graph_type& g, std::ranges::iterator_t<vertices_range> ui) {
return static_cast<vertex_id_type>(ui -
std::ranges::begin(g.vertices_)); // overriden to assure correct type returned
}
Expand All @@ -392,21 +387,16 @@ class rr_adaptor2 {
return get<0>(to_tuple(uv));
}

friend constexpr vertex_value_type&
tag_invoke(std::graph::tag_invoke::vertex_value_fn_t, graph_type& g, vertex_type& u) {
return get<1>(to_tuple(u));
}
friend constexpr const vertex_value_type&
tag_invoke(std::graph::tag_invoke::vertex_value_fn_t, const graph_type& g, const vertex_type& u) {
friend constexpr vertex_value_type& vertex_value(graph_type& g, vertex_type& u) { return get<1>(to_tuple(u)); }
friend constexpr const vertex_value_type& vertex_value(const graph_type& g, const vertex_type& u) {
return get<1>(to_tuple(u));
}

// edge_value(g,uv)
friend constexpr edge_value_type& tag_invoke(std::graph::tag_invoke::edge_value_fn_t, graph_type& g, edge_type& uv) {
friend constexpr edge_value_type& edge_value(graph_type& g, edge_type& uv) { //
return get<1>(to_tuple(uv));
}
friend constexpr const edge_value_type&
tag_invoke(std::graph::tag_invoke::edge_value_fn_t, const graph_type& g, const edge_type& uv) {
friend constexpr const edge_value_type& edge_value(const graph_type& g, const edge_type& uv) {
return get<1>(to_tuple(uv));
}

Expand Down
2 changes: 1 addition & 1 deletion include/graph/algorithm/mst.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void kruskal(E&& e, // graph
auto outer_compare = [&](auto&& i, auto&& j) { return compare(std::get<2>(i), std::get<2>(j)); };
std::sort(e.begin(), e.end(), outer_compare);

VId N = e.max_vid() + 1;
VId N = static_cast<VId>(e.max_vid() + 1);
std::vector<std::pair<VId, size_t>> subsets(N);
for (VId uid = 0; uid < N; ++uid) {
subsets[uid].first = uid;
Expand Down
31 changes: 9 additions & 22 deletions include/graph/container/compressed_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,13 @@ class csr_row_values {
constexpr const_reference operator[](size_type pos) const { return v_[pos]; }

private:
friend constexpr vertex_value_type&
tag_invoke(::std::graph::tag_invoke::vertex_value_fn_t, graph_type& g, vertex_type& u) {
friend constexpr vertex_value_type& vertex_value(graph_type& g, vertex_type& u) {
static_assert(ranges::contiguous_range<row_index_vector>, "row_index_ must be a contiguous range to evaluate uidx");
auto uidx = g.index_of(u);
csr_row_values& row_vals = g;
return row_vals.v_[uidx];
}
friend constexpr const vertex_value_type&
tag_invoke(::std::graph::tag_invoke::vertex_value_fn_t, const graph_type& g, const vertex_type& u) {
friend constexpr const vertex_value_type& vertex_value(const graph_type& g, const vertex_type& u) {
static_assert(ranges::contiguous_range<row_index_vector>, "row_index_ must be a contiguous range to evaluate uidx");
auto uidx = g.index_of(u);
const csr_row_values& row_vals = g;
Expand Down Expand Up @@ -320,14 +318,12 @@ class csr_col_values {

private:
// edge_value(g,uv)
friend constexpr edge_value_type&
tag_invoke(::std::graph::tag_invoke::edge_value_fn_t, graph_type& g, edge_type& uv) {
friend constexpr edge_value_type& edge_value(graph_type& g, edge_type& uv) {
auto uv_idx = g.index_of(uv);
csr_col_values& col_vals = g;
return col_vals.v_[uv_idx];
}
friend constexpr const edge_value_type&
tag_invoke(::std::graph::tag_invoke::edge_value_fn_t, const graph_type& g, const edge_type& uv) {
friend constexpr const edge_value_type& edge_value(const graph_type& g, const edge_type& uv) {
auto uv_idx = g.index_of(uv);
const csr_col_values& col_vals = g;
return col_vals.v_[uv_idx];
Expand Down Expand Up @@ -757,8 +753,7 @@ class compressed_graph_base
return const_vertices_type(g.row_index_.begin(), g.row_index_.end() - 1); // don't include terminating row
}

friend vertex_id_type
tag_invoke(::std::graph::tag_invoke::vertex_id_fn_t, const compressed_graph_base& g, const_iterator ui) {
friend vertex_id_type vertex_id(const compressed_graph_base& g, const_iterator ui) {
return static_cast<vertex_id_type>(ui - g.row_index_.begin());
}

Expand Down Expand Up @@ -799,12 +794,8 @@ class compressed_graph_base
// target_id(g,uv), target(g,uv)
friend constexpr vertex_id_type target_id(const graph_type& g, const edge_type& uv) noexcept { return uv.index; }

friend constexpr vertex_type&
tag_invoke(::std::graph::tag_invoke::target_fn_t, graph_type& g, edge_type& uv) noexcept {
return g.row_index_[uv.index];
}
friend constexpr const vertex_type&
tag_invoke(::std::graph::tag_invoke::target_fn_t, const graph_type& g, const edge_type& uv) noexcept {
friend constexpr vertex_type& target(graph_type& g, edge_type& uv) noexcept { return g.row_index_[uv.index]; }
friend constexpr const vertex_type& target(const graph_type& g, const edge_type& uv) noexcept {
return g.row_index_[uv.index];
}

Expand Down Expand Up @@ -922,12 +913,8 @@ class compressed_graph : public compressed_graph_base<EV, VV, GV, VId, EIndex, A
: base_type(ilist, alloc) {}

private: // tag_invoke properties
friend constexpr value_type& tag_invoke(::std::graph::tag_invoke::graph_value_fn_t, graph_type& g) {
return g.value_;
}
friend constexpr const value_type& tag_invoke(::std::graph::tag_invoke::graph_value_fn_t, const graph_type& g) {
return g.value_;
}
friend constexpr value_type& graph_value(graph_type& g) { return g.value_; }
friend constexpr const value_type& graph_value(const graph_type& g) { return g.value_; }

private: // Member variables
graph_value_type value_ = graph_value_type();
Expand Down
59 changes: 17 additions & 42 deletions include/graph/container/dynamic_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,10 @@ class dynamic_edge_target {
// target_id(g,uv), target(g,uv)
friend constexpr vertex_id_type target_id(const graph_type& g, const edge_type& uv) noexcept { return uv.target_id_; }

friend constexpr vertex_type&
tag_invoke(::std::graph::tag_invoke::target_fn_t, graph_type& g, edge_type& uv) noexcept {
friend constexpr vertex_type& target(graph_type& g, edge_type& uv) noexcept {
return begin(vertices(g))[uv.target_id_];
}
friend constexpr const vertex_type&
tag_invoke(::std::graph::tag_invoke::target_fn_t, const graph_type& g, const edge_type& uv) noexcept {
friend constexpr const vertex_type& target(const graph_type& g, const edge_type& uv) noexcept {
return begin(vertices(g))[uv.target_id_];
}
};
Expand Down Expand Up @@ -295,16 +293,12 @@ class dynamic_edge_source {

private:
// source_id(g,uv), source(g)
friend constexpr vertex_id_type
tag_invoke(::std::graph::tag_invoke::source_id_fn_t, const graph_type& g, const edge_type& uv) noexcept {
return uv.source_id_;
}
friend constexpr vertex_type&
tag_invoke(::std::graph::tag_invoke::source_fn_t, graph_type& g, edge_type& uv) noexcept {
friend constexpr vertex_id_type source_id(const graph_type& g, const edge_type& uv) noexcept { return uv.source_id_; }

friend constexpr vertex_type& source(graph_type& g, edge_type& uv) noexcept {
return begin(vertices(g))[uv.source_id_];
}
friend constexpr const vertex_type&
tag_invoke(::std::graph::tag_invoke::source_fn_t, const graph_type& g, const edge_type& uv) noexcept {
friend constexpr const vertex_type& source_fn(const graph_type& g, const edge_type& uv) noexcept {
return begin(vertices(g))[uv.source_id_];
}
};
Expand Down Expand Up @@ -383,14 +377,8 @@ class dynamic_edge_value {
value_type value_ = value_type();

private: // tag_invoke properties
friend constexpr value_type&
tag_invoke(::std::graph::tag_invoke::edge_value_fn_t, graph_type& g, edge_type& uv) noexcept {
return uv.value_;
}
friend constexpr const value_type&
tag_invoke(::std::graph::tag_invoke::edge_value_fn_t, const graph_type& g, const edge_type& uv) noexcept {
return uv.value_;
}
friend constexpr value_type& edge_value(graph_type& g, edge_type& uv) noexcept { return uv.value_; }
friend constexpr const value_type& edge_value(const graph_type& g, const edge_type& uv) noexcept { return uv.value_; }
};

/**
Expand Down Expand Up @@ -700,13 +688,11 @@ class dynamic_vertex_base {
}

friend constexpr typename edges_type::iterator
tag_invoke(::std::graph::tag_invoke::find_vertex_edge_fn_t, graph_type& g, vertex_id_type uid, vertex_id_type vid) {
find_vertex_edge(graph_type& g, vertex_id_type uid, vertex_id_type vid) {
return ranges::find(g[uid].edges_, [&g, &vid](const edge_type& uv) -> bool { return target_id(g, uv) == vid; });
}
friend constexpr typename edges_type::const_iterator tag_invoke(::std::graph::tag_invoke::find_vertex_edge_fn_t,
const graph_type& g,
vertex_id_type uid,
vertex_id_type vid) {
friend constexpr typename edges_type::const_iterator
find_vertex_edge(const graph_type& g, vertex_id_type uid, vertex_id_type vid) {
return ranges::find(g[uid].edges_, [&g, &vid](const edge_type& uv) -> bool { return target_id(g, uv) == vid; });
}
};
Expand Down Expand Up @@ -768,13 +754,8 @@ class dynamic_vertex : public dynamic_vertex_base<EV, VV, GV, VId, Sourced, Trai
value_type value_ = value_type();

private: // tag_invoke properties
friend constexpr value_type& tag_invoke(::std::graph::tag_invoke::vertex_value_fn_t, graph_type& g, vertex_type& u) {
return u.value_;
}
friend constexpr const value_type&
tag_invoke(::std::graph::tag_invoke::vertex_value_fn_t, const graph_type& g, const vertex_type& u) {
return u.value_;
}
friend constexpr value_type& vertex_value(graph_type& g, vertex_type& u) { return u.value_; }
friend constexpr const value_type& vertex_value(const graph_type& g, const vertex_type& u) { return u.value_; }
};


Expand Down Expand Up @@ -1260,9 +1241,7 @@ class dynamic_graph_base {
return g.vertices_;
}

friend vertex_id_type tag_invoke(::std::graph::tag_invoke::vertex_id_fn_t,
const dynamic_graph_base& g,
typename vertices_type::const_iterator ui) {
friend vertex_id_type vertex_id(const dynamic_graph_base& g, typename vertices_type::const_iterator ui) {
return static_cast<vertex_id_type>(ui - g.vertices_.begin());
}

Expand Down Expand Up @@ -1633,13 +1612,9 @@ class dynamic_graph : public dynamic_graph_base<EV, VV, GV, VId, Sourced, Traits
private:
value_type value_; ///< Graph value

private: // tag_invoke properties
friend constexpr value_type& tag_invoke(::std::graph::tag_invoke::graph_value_fn_t, graph_type& g) {
return g.value_;
}
friend constexpr const value_type& tag_invoke(::std::graph::tag_invoke::graph_value_fn_t, const graph_type& g) {
return g.value_;
}
private: // tag_invoke properties
friend constexpr value_type& graph_value(graph_type& g) { return g.value_; }
friend constexpr const value_type& graph_value(const graph_type& g) { return g.value_; }
};

/**
Expand Down
10 changes: 2 additions & 8 deletions include/graph/container/utility_edgelist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,9 @@ class utility_edgelist {
return el.storage_;
}

friend constexpr VSourceId&
tag_invoke(::std::graph::edgelist::tag_invoke::source_id_fn_t, utility_edgelist& el, value_type& e) {
return std::get<0>(e);
}
friend constexpr VSourceId& source_id(utility_edgelist& el, value_type& e) { return std::get<0>(e); }

friend constexpr EV&
tag_invoke(::std::graph::edgelist::tag_invoke::edge_value_fn_t, utility_edgelist& el, value_type& e) {
return std::get<2>(e);
}
friend constexpr EV& edge_value(utility_edgelist& el, value_type& e) { return std::get<2>(e); }

storage_type storage_;
VSourceId source_max_;
Expand Down
Loading

0 comments on commit c400ada

Please sign in to comment.