From 0d45764873c0a2ec345b14c3bf750ff1fccb74a9 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Sat, 18 Mar 2023 19:21:34 -0500 Subject: T5089: order nodes on configtree parsing from string This is a conservative application of lexical_numeric ordering for configtree: order nodes on instantiation in 'from_string' --- src/vytree.ml | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src/vytree.ml') diff --git a/src/vytree.ml b/src/vytree.ml index 905f12b..bd73776 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -88,6 +88,14 @@ let rec insert ?(position=Default) ?(children=[]) node path data = let s = Printf.sprintf "Non-existent intermediary node: \'%s\'" name in raise (Insert_error s) +let sorted_children_of_node cmp node = + let names = list_children node in + let names = List.sort cmp names in + List.map (find_or_fail node) names + +let sort_children cmp node = + {node with children = (sorted_children_of_node cmp node)} + (** Given a node N check if it has children with duplicate names, and merge subsequent children's children into the first child by that name. @@ -99,9 +107,11 @@ let rec insert ?(position=Default) ?(children=[]) node path data = may be normal and even expected, such as "ethernet eth0" and "ethernet eth1" in the "curly" format. *) -let merge_children merge_data node = +let merge_children merge_data cmp node = (* Given a node N and a list of nodes NS, find all nodes in NS that - have the same name as N and merge their children into N *) + have the same name as N and merge their children into N, sorting + children by a comparison function cmp (string -> string -> int) on + node names *) let rec merge_into n ns = match ns with | [] -> n @@ -110,6 +120,7 @@ let merge_children merge_data node = let children = List.append n.children n'.children in let data = merge_data n.data n'.data in let n = {n with children=children; data=data} in + let n = sort_children cmp n in merge_into n ns' else merge_into n ns' in @@ -179,11 +190,6 @@ let children_of_path node path = let node' = get node path in list_children node' -let sorted_children_of_node cmp node = - let names = list_children node in - let names = List.sort cmp names in - List.map (find_or_fail node) names - let copy node old_path new_path = if exists node new_path then raise Duplicate_child else let child = get node old_path in -- cgit v1.2.3