Skip to content

Commit

Permalink
Merge pull request #4 from codex-semantics-library/remove_dissappeared
Browse files Browse the repository at this point in the history
Remove Dissappeared exception
  • Loading branch information
dlesbre authored May 2, 2024
2 parents 5b06e5e + c9bcae2 commit 115a5c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- Fixed a bug where NodeWithId wasn't incrementing ids properly
- `zarith` is no longer a dependency, used GCC's `__builtin_clz` as a faster
method of finding an integer's highest bit.
- Fixed a bug where `pop_minimum` and `pop_maximum` could throw a private exception
`Dissappeared` when using `WeakNode`.

# v0.9.0 - 2024-04-18

Expand Down
52 changes: 20 additions & 32 deletions patriciaTree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -754,41 +754,29 @@ module MakeCustomHeterogeneous

let remove to_remove m = removeint (Key.to_int to_remove) m

(* This exception is triggered if an operation cannot be completed
because a weak key disappeared. Probably the simplest way to deal
with this operation is to "compact" (remove the dead keys) and
retry. *)
exception Disappeared

let rec pop_min_nonempty m = match NODE.view m with
| Leaf{key;value} -> KeyValue(key,value),empty
| Branch{prefix;branching_bit;tree0;tree1} ->
let res,tree0' = pop_min_nonempty tree0 in
let restree =
if tree0' == empty then tree1
else branch ~prefix ~branching_bit ~tree0:tree0' ~tree1
in (res,restree)
| Empty ->
(* Can only happen in weak sets and maps. *)
raise Disappeared
let pop_unsigned_minimum m = match NODE.view m with
let rec pop_unsigned_minimum m = match NODE.view m with
| Empty -> None
| _ -> Some(pop_min_nonempty m)

let rec pop_max_nonempty m = match NODE.view m with
| Leaf{key;value} -> KeyValue(key,value),empty
| Leaf{key;value} -> Some (KeyValue(key,value),empty)
| Branch{prefix;branching_bit;tree0;tree1} ->
let res,tree1' = pop_max_nonempty tree1 in
let restree =
if tree1' == empty then tree0
else branch ~prefix ~branching_bit ~tree0 ~tree1:tree1'
in (res,restree)
(* Can only happen in weak sets and maps. *)
| Empty -> raise Disappeared

let pop_unsigned_maximum m = match NODE.view m with
match pop_unsigned_minimum tree0 with
| None -> pop_unsigned_minimum tree1
| Some(res,tree0') ->
let restree =
if is_empty tree0' then tree1
else branch ~prefix ~branching_bit ~tree0:tree0' ~tree1
in Some(res,restree)

let rec pop_unsigned_maximum m = match NODE.view m with
| Empty -> None
| _ -> Some(pop_max_nonempty m)
| Leaf{key;value} -> Some (KeyValue(key,value),empty)
| Branch{prefix;branching_bit;tree0;tree1} ->
match pop_unsigned_maximum tree1 with
| None -> pop_unsigned_maximum tree0
| Some(res,tree1') ->
let restree =
if is_empty tree1' then tree0
else branch ~prefix ~branching_bit ~tree0 ~tree1:tree1'
in Some(res,restree)

let insert: type a map. a Key.t -> ((a,map) Value.t option -> (a,map) Value.t) -> map t -> map t =
fun thekey f t ->
Expand Down

0 comments on commit 115a5c9

Please sign in to comment.