From b922ca75d0fb74798d338d04b052bcb4c4f4bfa5 Mon Sep 17 00:00:00 2001 From: Teon Banek Date: Mon, 2 Mar 2020 11:35:13 +0100 Subject: [PATCH] Add mgp_path_equal to C API Reviewers: ipaljak, mferencevic Reviewed By: ipaljak Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D2697 --- include/mg_procedure.h | 4 ++++ src/query/procedure/mg_procedure_impl.cpp | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/mg_procedure.h b/include/mg_procedure.h index 57120503a..312693ba0 100644 --- a/include/mg_procedure.h +++ b/include/mg_procedure.h @@ -378,6 +378,10 @@ const struct mgp_vertex *mgp_path_vertex_at(const struct mgp_path *path, /// NULL is returned if index is out of range. const struct mgp_edge *mgp_path_edge_at(const struct mgp_path *path, size_t index); + +/// Return non-zero if given paths are equal, otherwise 0. +int mgp_path_equal(const struct mgp_path *p1, const struct mgp_path *p2); + ///@} /// @name Procedure Result diff --git a/src/query/procedure/mg_procedure_impl.cpp b/src/query/procedure/mg_procedure_impl.cpp index e6b96be6e..6b251dea8 100644 --- a/src/query/procedure/mg_procedure_impl.cpp +++ b/src/query/procedure/mg_procedure_impl.cpp @@ -782,6 +782,21 @@ const mgp_edge *mgp_path_edge_at(const mgp_path *path, size_t i) { return &path->edges[i]; } +int mgp_path_equal(const struct mgp_path *p1, const struct mgp_path *p2) { + CHECK(mgp_path_size(p1) == p1->vertices.size() - 1); + CHECK(mgp_path_size(p2) == p2->vertices.size() - 1); + if (mgp_path_size(p1) != mgp_path_size(p2)) return 0; + const auto *start1 = mgp_path_vertex_at(p1, 0); + const auto *start2 = mgp_path_vertex_at(p2, 0); + if (!mgp_vertex_equal(start1, start2)) return 0; + for (size_t i = 0; i < mgp_path_size(p1); ++i) { + const auto *e1 = mgp_path_edge_at(p1, i); + const auto *e2 = mgp_path_edge_at(p2, i); + if (!mgp_edge_equal(e1, e2)) return 0; + } + return 1; +} + /// Plugin Result int mgp_result_set_error_msg(mgp_result *res, const char *msg) {