diff options
author | John Estabrook <jestabro@vyos.io> | 2023-03-14 12:57:31 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2023-03-23 13:42:52 -0500 |
commit | 06bfbc2334723b52414b047c6c8f8c18cbc5bda6 (patch) | |
tree | 53d42694ccd6b90dcd8d87b271eeacd5cfde9345 /src | |
parent | 334819524a78c920b0184f6f6a99daabf57c520e (diff) | |
download | vyos1x-config-06bfbc2334723b52414b047c6c8f8c18cbc5bda6.tar.gz vyos1x-config-06bfbc2334723b52414b047c6c8f8c18cbc5bda6.zip |
T5087: add optional arg for lexical insertion of nodes
Diffstat (limited to 'src')
-rw-r--r-- | src/vylist.ml | 6 | ||||
-rw-r--r-- | src/vylist.mli | 1 | ||||
-rw-r--r-- | src/vytree.ml | 4 | ||||
-rw-r--r-- | src/vytree.mli | 2 |
4 files changed, 11 insertions, 2 deletions
diff --git a/src/vylist.ml b/src/vylist.ml index cd4a32e..c64dd89 100644 --- a/src/vylist.ml +++ b/src/vylist.ml @@ -28,6 +28,12 @@ let rec insert_after p x xs = | y :: ys -> if (p y) then y :: x :: ys else y :: (insert_after p x ys) +let rec insert_compare p x xs = + match xs with + | [] -> [x] + | y :: ys -> if (p x y <= 0) then x :: y :: ys + else y :: (insert_compare p x ys) + let complement xs ys = let rec aux xs ys = match xs, ys with diff --git a/src/vylist.mli b/src/vylist.mli index 9135bf6..b74a537 100644 --- a/src/vylist.mli +++ b/src/vylist.mli @@ -3,5 +3,6 @@ val remove : ('a -> bool) -> 'a list -> 'a list val replace : ('a -> bool) -> 'a -> 'a list -> 'a list val insert_before : ('a -> bool) -> 'a -> 'a list -> 'a list val insert_after : ('a -> bool) -> 'a -> 'a list -> 'a list +val insert_compare : ('a -> 'a -> int) -> 'a -> 'a list -> 'a list val complement : 'a list -> 'a list -> 'a list val in_list : 'a list -> 'a -> bool diff --git a/src/vytree.ml b/src/vytree.ml index cda12f0..905f12b 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -4,7 +4,7 @@ type 'a t = { children: 'a t list } [@@deriving yojson] -type position = Before of string | After of string | End | Default +type position = Before of string | After of string | Lexical | End | Default exception Empty_path exception Duplicate_child @@ -27,6 +27,8 @@ let insert_immediate ?(position=Default) node name data children = | End -> node.children @ [new_node] | Before s -> Vylist.insert_before (fun x -> x.name = s) new_node node.children | After s -> Vylist.insert_after (fun x -> x.name = s) new_node node.children + | Lexical -> + Vylist.insert_compare (fun x y -> Util.lexical_numeric_compare x.name y.name) new_node node.children in { node with children = children' } let delete_immediate node name = diff --git a/src/vytree.mli b/src/vytree.mli index 451e130..a3a6154 100644 --- a/src/vytree.mli +++ b/src/vytree.mli @@ -5,7 +5,7 @@ exception Duplicate_child exception Nonexistent_path exception Insert_error of string -type position = Before of string | After of string | End | Default +type position = Before of string | After of string | Lexical | End | Default val make : 'a -> string -> 'a t val make_full : 'a -> string -> ('a t) list -> 'a t |