Skip to content

Commit

Permalink
[Improvement][C++] Use inherit to implement EdgesCollection (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
acezen committed Oct 11, 2023
1 parent 7d9a9ac commit 6b8cd62
Show file tree
Hide file tree
Showing 21 changed files with 339 additions and 395 deletions.
9 changes: 0 additions & 9 deletions .github/workflows/java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ jobs:
mvn spotless:check
popd
- name: Build and Install cpp
run: |
mkdir build
pushd build
cmake ../cpp
make -j$(nproc)
sudo make install
popd
- name: Run test
run: |
export JAVA_HOME=${JAVA_HOME_11_X64}
Expand Down
9 changes: 4 additions & 5 deletions cpp/examples/bfs_father_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main(int argc, char* argv[]) {
GAR_NAMESPACE::ConstructVerticesCollection(graph_info, label);
ASSERT(maybe_vertices.status().ok());
auto& vertices = maybe_vertices.value();
int num_vertices = vertices.size();
int num_vertices = vertices->size();
std::cout << "num_vertices: " << num_vertices << std::endl;

// get the "person_knows_person" edges of graph
Expand All @@ -46,8 +46,7 @@ int main(int argc, char* argv[]) {
graph_info, src_label, edge_label, dst_label,
GAR_NAMESPACE::AdjListType::unordered_by_source);
ASSERT(!maybe_edges.has_error());
auto& edges = std::get<GAR_NAMESPACE::EdgesCollection<
GAR_NAMESPACE::AdjListType::unordered_by_source>>(maybe_edges.value());
auto& edges = maybe_edges.value();

// run bfs algorithm
GAR_NAMESPACE::IdType root = 0;
Expand All @@ -57,7 +56,7 @@ int main(int argc, char* argv[]) {
distance[i] = (i == root ? 0 : -1);
pre[i] = (i == root ? root : -1);
}
auto it_begin = edges.begin(), it_end = edges.end();
auto it_begin = edges->begin(), it_end = edges->end();
for (int iter = 0;; iter++) {
GAR_NAMESPACE::IdType count = 0;
for (auto it = it_begin; it != it_end; ++it) {
Expand Down Expand Up @@ -122,7 +121,7 @@ int main(int argc, char* argv[]) {
if (pre[i] == -1) {
ASSERT(array_builder2.AppendNull().ok());
} else {
auto it = vertices.find(pre[i]);
auto it = vertices->find(pre[i]);
auto father_id = it.property<int64_t>("id").value();
ASSERT(array_builder2.Append(father_id).ok());
}
Expand Down
7 changes: 3 additions & 4 deletions cpp/examples/bfs_pull_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main(int argc, char* argv[]) {
GAR_NAMESPACE::ConstructVerticesCollection(graph_info, label);
ASSERT(maybe_vertices.status().ok());
auto& vertices = maybe_vertices.value();
int num_vertices = vertices.size();
int num_vertices = vertices->size();
std::cout << "num_vertices: " << num_vertices << std::endl;

// construct edges collection
Expand All @@ -44,15 +44,14 @@ int main(int argc, char* argv[]) {
graph_info, src_label, edge_label, dst_label,
GAR_NAMESPACE::AdjListType::ordered_by_dest);
ASSERT(!maybe_edges.has_error());
auto& edges = std::get<GAR_NAMESPACE::EdgesCollection<
GAR_NAMESPACE::AdjListType::ordered_by_dest>>(maybe_edges.value());
auto& edges = maybe_edges.value();

// run bfs algorithm
GAR_NAMESPACE::IdType root = 0;
std::vector<int32_t> distance(num_vertices);
for (GAR_NAMESPACE::IdType i = 0; i < num_vertices; i++)
distance[i] = (i == root ? 0 : -1);
auto it_begin = edges.begin(), it_end = edges.end();
auto it_begin = edges->begin(), it_end = edges->end();
auto it = it_begin;
for (int iter = 0;; iter++) {
GAR_NAMESPACE::IdType count = 0;
Expand Down
7 changes: 3 additions & 4 deletions cpp/examples/bfs_push_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main(int argc, char* argv[]) {
GAR_NAMESPACE::ConstructVerticesCollection(graph_info, label);
ASSERT(maybe_vertices.status().ok());
auto& vertices = maybe_vertices.value();
int num_vertices = vertices.size();
int num_vertices = vertices->size();
std::cout << "num_vertices: " << num_vertices << std::endl;

// construct edges collection
Expand All @@ -44,15 +44,14 @@ int main(int argc, char* argv[]) {
graph_info, src_label, edge_label, dst_label,
GAR_NAMESPACE::AdjListType::ordered_by_source);
ASSERT(!maybe_edges.has_error());
auto& edges = std::get<GAR_NAMESPACE::EdgesCollection<
GAR_NAMESPACE::AdjListType::ordered_by_source>>(maybe_edges.value());
auto& edges = maybe_edges.value();

// run bfs algorithm
GAR_NAMESPACE::IdType root = 0;
std::vector<int32_t> distance(num_vertices);
for (GAR_NAMESPACE::IdType i = 0; i < num_vertices; i++)
distance[i] = (i == root ? 0 : -1);
auto it_begin = edges.begin(), it_end = edges.end();
auto it_begin = edges->begin(), it_end = edges->end();
auto it = it_begin;
for (int iter = 0;; iter++) {
GAR_NAMESPACE::IdType count = 0;
Expand Down
7 changes: 3 additions & 4 deletions cpp/examples/bfs_stream_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main(int argc, char* argv[]) {
GAR_NAMESPACE::ConstructVerticesCollection(graph_info, label);
ASSERT(maybe_vertices.status().ok());
auto& vertices = maybe_vertices.value();
int num_vertices = vertices.size();
int num_vertices = vertices->size();
std::cout << "num_vertices: " << num_vertices << std::endl;

// construct edges collection
Expand All @@ -44,15 +44,14 @@ int main(int argc, char* argv[]) {
graph_info, src_label, edge_label, dst_label,
GAR_NAMESPACE::AdjListType::unordered_by_source);
ASSERT(!maybe_edges.has_error());
auto& edges = std::get<GAR_NAMESPACE::EdgesCollection<
GAR_NAMESPACE::AdjListType::unordered_by_source>>(maybe_edges.value());
auto& edges = maybe_edges.value();

// run bfs algorithm
GAR_NAMESPACE::IdType root = 0;
std::vector<int32_t> distance(num_vertices);
for (GAR_NAMESPACE::IdType i = 0; i < num_vertices; i++)
distance[i] = (i == root ? 0 : -1);
auto it_begin = edges.begin(), it_end = edges.end();
auto it_begin = edges->begin(), it_end = edges->end();
for (int iter = 0;; iter++) {
GAR_NAMESPACE::IdType count = 0;
for (auto it = it_begin; it != it_end; ++it) {
Expand Down
13 changes: 6 additions & 7 deletions cpp/examples/bgl_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int main(int argc, char* argv[]) {
GAR_NAMESPACE::ConstructVerticesCollection(graph_info, label);
ASSERT(maybe_vertices.status().ok());
auto& vertices = maybe_vertices.value();
int num_vertices = vertices.size();
int num_vertices = vertices->size();
std::cout << "num_vertices: " << num_vertices << std::endl;

// construct edges collection
Expand All @@ -51,8 +51,7 @@ int main(int argc, char* argv[]) {
graph_info, src_label, edge_label, dst_label,
GAR_NAMESPACE::AdjListType::ordered_by_source);
ASSERT(!maybe_edges.has_error());
auto& edges = std::get<GAR_NAMESPACE::EdgesCollection<
GAR_NAMESPACE::AdjListType::ordered_by_source>>(maybe_edges.value());
auto& edges = maybe_edges.value();

// define the Graph type
typedef boost::adjacency_list<
Expand All @@ -68,8 +67,8 @@ int main(int argc, char* argv[]) {
// declare a graph object with (num_vertices) vertices and a edge iterator
std::vector<std::pair<GAR_NAMESPACE::IdType, GAR_NAMESPACE::IdType>>
edges_array;
auto it_end = edges.end();
for (auto it = edges.begin(); it != it_end; ++it) {
auto it_end = edges->end();
for (auto it = edges->begin(); it != it_end; ++it) {
edges_array.push_back(std::make_pair(it.source(), it.destination()));
}
Graph g(edges_array.begin(), edges_array.end(), num_vertices);
Expand All @@ -84,8 +83,8 @@ int main(int argc, char* argv[]) {
boost::property_map<Graph, boost::vertex_name_t>::type id =
get(boost::vertex_name_t(), g);

auto v_it_end = vertices.end();
for (auto it = vertices.begin(); it != v_it_end; ++it) {
auto v_it_end = vertices->end();
for (auto it = vertices->begin(); it != v_it_end; ++it) {
// FIXME(@acezen): double free error when get string property
boost::put(id, it.id(), it.property<int64_t>("id").value());
}
Expand Down
12 changes: 5 additions & 7 deletions cpp/examples/cc_push_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main(int argc, char* argv[]) {
GAR_NAMESPACE::ConstructVerticesCollection(graph_info, label);
ASSERT(maybe_vertices.status().ok());
auto& vertices = maybe_vertices.value();
int num_vertices = vertices.size();
int num_vertices = vertices->size();
std::cout << "num_vertices: " << num_vertices << std::endl;

// construct edges collection
Expand All @@ -45,14 +45,12 @@ int main(int argc, char* argv[]) {
graph_info, src_label, edge_label, dst_label,
GAR_NAMESPACE::AdjListType::ordered_by_source);
ASSERT(!expect1.has_error());
auto& edges1 = std::get<GAR_NAMESPACE::EdgesCollection<
GAR_NAMESPACE::AdjListType::ordered_by_source>>(expect1.value());
auto& edges1 = expect1.value();
auto expect2 = GAR_NAMESPACE::ConstructEdgesCollection(
graph_info, src_label, edge_label, dst_label,
GAR_NAMESPACE::AdjListType::ordered_by_dest);
ASSERT(!expect2.has_error());
auto& edges2 = std::get<GAR_NAMESPACE::EdgesCollection<
GAR_NAMESPACE::AdjListType::ordered_by_dest>>(expect2.value());
auto& edges2 = expect2.value();

// run cc algorithm
std::vector<GAR_NAMESPACE::IdType> component(num_vertices);
Expand All @@ -62,8 +60,8 @@ int main(int argc, char* argv[]) {
active[0].push_back(true);
active[1].push_back(false);
}
auto begin1 = edges1.begin(), end1 = edges1.end();
auto begin2 = edges2.begin(), end2 = edges2.end();
auto begin1 = edges1->begin(), end1 = edges1->end();
auto begin2 = edges2->begin(), end2 = edges2->end();
auto it1 = begin1;
auto it2 = begin2;
int count = num_vertices;
Expand Down
7 changes: 3 additions & 4 deletions cpp/examples/cc_stream_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main(int argc, char* argv[]) {
GAR_NAMESPACE::ConstructVerticesCollection(graph_info, label);
ASSERT(maybe_vertices.status().ok());
auto& vertices = maybe_vertices.value();
int num_vertices = vertices.size();
int num_vertices = vertices->size();
std::cout << "num_vertices: " << num_vertices << std::endl;

// construct edges collection
Expand All @@ -45,14 +45,13 @@ int main(int argc, char* argv[]) {
graph_info, src_label, edge_label, dst_label,
GAR_NAMESPACE::AdjListType::ordered_by_source);
ASSERT(!maybe_edges.has_error());
auto& edges = std::get<GAR_NAMESPACE::EdgesCollection<
GAR_NAMESPACE::AdjListType::ordered_by_source>>(maybe_edges.value());
auto& edges = maybe_edges.value();

// run cc algorithm
std::vector<GAR_NAMESPACE::IdType> component(num_vertices);
for (GAR_NAMESPACE::IdType i = 0; i < num_vertices; i++)
component[i] = i;
auto it_begin = edges.begin(), it_end = edges.end();
auto it_begin = edges->begin(), it_end = edges->end();
for (int iter = 0;; iter++) {
std::cout << "iter " << iter << std::endl;
bool flag = false;
Expand Down
7 changes: 3 additions & 4 deletions cpp/examples/pagerank_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main(int argc, char* argv[]) {
GAR_NAMESPACE::ConstructVerticesCollection(graph_info, label);
ASSERT(maybe_vertices.status().ok());
auto& vertices = maybe_vertices.value();
int num_vertices = vertices.size();
int num_vertices = vertices->size();
std::cout << "num_vertices: " << num_vertices << std::endl;

// construct edges collection
Expand All @@ -45,8 +45,7 @@ int main(int argc, char* argv[]) {
graph_info, src_label, edge_label, dst_label,
GAR_NAMESPACE::AdjListType::ordered_by_source);
ASSERT(!maybe_edges.has_error());
auto& edges = std::get<GAR_NAMESPACE::EdgesCollection<
GAR_NAMESPACE::AdjListType::ordered_by_source>>(maybe_edges.value());
auto& edges = maybe_edges.value();

// run pagerank algorithm
const double damping = 0.85;
Expand All @@ -59,7 +58,7 @@ int main(int argc, char* argv[]) {
pr_next[i] = 0;
out_degree[i] = 0;
}
auto it_begin = edges.begin(), it_end = edges.end();
auto it_begin = edges->begin(), it_end = edges->end();
for (auto it = it_begin; it != it_end; ++it) {
GAR_NAMESPACE::IdType src = it.source();
out_degree[src]++;
Expand Down
Loading

0 comments on commit 6b8cd62

Please sign in to comment.