summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2026-02-03 19:40:05 -0600
committerJohn Estabrook <jestabro@vyos.io>2026-02-06 14:42:47 -0600
commit97d2ca7fce11621fa4dd9e4bac0a219c34e397be (patch)
tree6a79ec981f40e5706da25e0ea68d95a95b0d7dec /src
parentb4f79c7428c26815da0c4bb192f29b5b2b5a34b4 (diff)
downloadvyos1x-config-97d2ca7fce11621fa4dd9e4bac0a219c34e397be.tar.gz
vyos1x-config-97d2ca7fce11621fa4dd9e4bac0a219c34e397be.zip
T8232: add extended fold over tree function to track level
The function fold_tree_with_path_cont allows the called function to return a list of values, reset at each return to local root. The motivating example is to carry a stack of boolean values to ignore paths below a certain level.
Diffstat (limited to 'src')
-rw-r--r--src/vytree.ml25
-rw-r--r--src/vytree.mli3
2 files changed, 28 insertions, 0 deletions
diff --git a/src/vytree.ml b/src/vytree.ml
index 7c4e4fb..6ecc867 100644
--- a/src/vytree.ml
+++ b/src/vytree.ml
@@ -306,3 +306,28 @@ let fold_tree_with_path f (p', a) t =
List.fold_left (fold_func f) (f (p, a) t) c in
(Util.drop_first p), snd res
in snd (fold_func f (p', a) t)
+
+(** Allow function called in fold to maintain a list of values for each
+ depth level of tree. A simple example is for the the function to cons a
+ boolean value to the list v at each call of the depth-first traversal;
+ at the return to local root, the value for that level is restored.
+ Note that if the function returns the empty list, this function reduces
+ to fold_tree_with_path.
+ *)
+
+let fold_tree_with_path_and_list f ((p', v), a) t =
+ let rec fold_func f ((p', v), a) t =
+ let p =
+ match name_of_node t with
+ | "" -> p'
+ | name -> name :: p'
+ in
+ let children = children_of_node t in
+ match children with
+ | [] -> let res =
+ f ((p, v), a) t in
+ (Util.drop_first p, Util.drop_first (snd (fst res))), snd res
+ | c -> let res =
+ List.fold_left (fold_func f) (f ((p, v), a) t) c in
+ (Util.drop_first p, Util.drop_first (snd (fst res))), snd res
+ in snd (fold_func f ((p', v), a) t)
diff --git a/src/vytree.mli b/src/vytree.mli
index 023d967..448aa84 100644
--- a/src/vytree.mli
+++ b/src/vytree.mli
@@ -96,3 +96,6 @@ val is_terminal_path : 'a t -> string list -> bool
[@@alert exn "Vytree.Empty_path"]
val fold_tree_with_path: (string list * 'acc -> 'b t -> string list * 'acc) -> string list * 'acc -> 'b t -> 'acc
+
+val fold_tree_with_path_and_list: ((string list * bool list) * 'acc -> 'b t ->
+ (string list * bool list) * 'acc) -> (string list * bool list) * 'acc -> 'b t -> 'acc