Adapt filter_map_inplace.
parent
23b2edf286
commit
bff08d2763
|
@ -254,19 +254,37 @@ let iter f h =
|
||||||
flip_ongoing_traversal h;
|
flip_ongoing_traversal h;
|
||||||
raise exn
|
raise exn
|
||||||
|
|
||||||
|
let rec filter_map_inplace_bucket f h i prec = function
|
||||||
|
| Empty ->
|
||||||
|
begin match prec with
|
||||||
|
| Empty -> h.data.(i) <- Empty
|
||||||
|
| Cons c -> c.next <- Empty
|
||||||
|
end
|
||||||
|
| (Cons ({key; data; next} as c)) as slot ->
|
||||||
|
begin match f key data with
|
||||||
|
| None ->
|
||||||
|
h.size <- h.size - 1;
|
||||||
|
filter_map_inplace_bucket f h i prec next
|
||||||
|
| Some data ->
|
||||||
|
begin match prec with
|
||||||
|
| Empty -> h.data.(i) <- slot
|
||||||
|
| Cons c -> c.next <- slot
|
||||||
|
end;
|
||||||
|
c.data <- data;
|
||||||
|
filter_map_inplace_bucket f h i slot next
|
||||||
|
end
|
||||||
|
|
||||||
let filter_map_inplace f h =
|
let filter_map_inplace f h =
|
||||||
let rec do_bucket = function
|
|
||||||
| Empty ->
|
|
||||||
Empty
|
|
||||||
| Cons(k, d, rest) ->
|
|
||||||
match f k d with
|
|
||||||
| None -> h.size <- h.size - 1; do_bucket rest
|
|
||||||
| Some new_d -> Cons(k, new_d, do_bucket rest)
|
|
||||||
in
|
|
||||||
let d = h.data in
|
let d = h.data in
|
||||||
for i = 0 to Array.length d - 1 do
|
let old_trav = ongoing_traversal h in
|
||||||
d.(i) <- do_bucket d.(i)
|
if not old_trav then flip_ongoing_traversal h;
|
||||||
done
|
try
|
||||||
|
for i = 0 to Array.length d - 1 do
|
||||||
|
filter_map_inplace_bucket f h i Empty h.data.(i)
|
||||||
|
done
|
||||||
|
with exn when not old_trav ->
|
||||||
|
flip_ongoing_traversal h;
|
||||||
|
raise exn
|
||||||
|
|
||||||
let fold f h init =
|
let fold f h init =
|
||||||
let rec do_bucket b accu =
|
let rec do_bucket b accu =
|
||||||
|
|
Loading…
Reference in New Issue