Skip to content

Commit

Permalink
feat(Trie): add Trie.untag
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Sep 30, 2022
1 parent dc2b50c commit 4671c83
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Trie.ml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ let[@inline] retag t : _ t -> _ t =
| None -> None
| Some (d, _) -> non_empty @@ mk_node' d (Some t)

let[@inline] untag t = retag () t

let retag_subtree path t (v : _ t) : _ t = update_subtree path (retag t) v

let rec iter_tag_node (f : 'a -> unit) (t : 'a tag_node) =
Expand Down
3 changes: 3 additions & 0 deletions src/Trie.mli
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ val of_seq_with_merger : ?prefix:bwd_path -> (bwd_path -> 'data * 'tag -> 'data
(** [retag tag t] changes all tags within [t] to [tag] in O(1) time. The data remain intact. *)
val retag : 'tag -> ('data, _) t -> ('data, 'tag) t

(** [untag t] is [retag () t]. *)
val untag : ('data, _) t -> ('data, unit) t

(** [retag_subtree tag path t] changes all tags within the subtrie rooted at [path] to [tag] efficiently. The data remain intact. *)
val retag_subtree : path -> 'tag -> ('data, 'tag) t -> ('data, 'tag) t

Expand Down
1 change: 1 addition & 0 deletions test/ListAsTrie.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ let of_seq s = Seq.fold_left (union_singleton ~prefix:Emp (fun _ _ y -> y)) empt
let of_seq_with_merger ?(prefix=Emp) m s = Seq.fold_left (union_singleton ~prefix m) empty s

let retag t l = List.map (fun (p, (d, _)) -> p, (d, t)) l
let untag l = retag () l
let retag_subtree pre t l =
List.map (fun ((p, (d, _)) as b) -> if Option.is_some (split_path pre p) then p, (d, t) else b) l
let set_of_tags cmp l = List.to_seq @@ List.sort_uniq cmp @@ List.map (fun (_, (_, t)) -> t) l

0 comments on commit 4671c83

Please sign in to comment.