From 89139ffe3c457f2b323cd2ce2589bdd528178ee5 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Mon, 2 Dec 2024 16:38:33 +0100 Subject: [PATCH] feat: add a filter --- src/findpath.ml | 13 +++++++++++-- src/findpath.mli | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/findpath.ml b/src/findpath.ml index d3219b5..90b07d5 100644 --- a/src/findpath.ml +++ b/src/findpath.ml @@ -2,10 +2,16 @@ open Graph let child_nodes (gr : 'a graph) (a: id) = List.map (fun x -> x.tgt) (out_arcs gr a) -let find_path (gr: 'a graph) (begin_node: id) (end_node: id) = +let find_path (gr: 'a graph) (begin_node: id) (end_node: id) (filter: 'a arc -> bool)= let rec find_way s acu = let s_child = child_nodes gr s in + let arc x1 x2 = + match find_arc gr x1 x2 with + | Some x -> x + | None -> raise Not_found + in + if s == end_node then acu else @@ -15,7 +21,10 @@ let find_path (gr: 'a graph) (begin_node: id) (end_node: id) = | [] -> raise Not_found | x::rest -> try - find_way x (acu@[x]) + if (filter (arc s x)) then + find_way x (acu@[x]) + else + fp rest with | Not_found -> fp rest in fp next_rec diff --git a/src/findpath.mli b/src/findpath.mli index 421aa23..ce1ca5e 100644 --- a/src/findpath.mli +++ b/src/findpath.mli @@ -4,4 +4,4 @@ open Graph val child_nodes: 'a graph -> id -> id list (* search path between 2 node and return the path : a list of node *) -val find_path: 'a graph -> id -> id -> id list \ No newline at end of file +val find_path: 'a graph -> id -> id -> ('a arc -> bool) -> id list \ No newline at end of file