Skip to content

Commit ea13e24

Browse files
committed
Add Picos_htbl.mem
1 parent 502b073 commit ea13e24

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

lib/picos_htbl/picos_htbl.ml

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,31 +300,36 @@ let[@inline] get t =
300300

301301
(* *)
302302

303-
let rec assoc_exn t key = function
304-
| Nil -> raise_notrace Not_found
305-
| Cons r -> if t r.key key then r.value else assoc_exn t key r.rest
303+
let rec assoc_node t key = function
304+
| Nil -> (Nil : (_, _, [< `Nil | `Cons ]) tdt)
305+
| Cons r as cons -> if t r.key key then cons else assoc_node t key r.rest
306306

307-
let find_exn t key =
307+
let find_node t key =
308308
(* Reads can proceed in parallel with writes. *)
309309
let r = Atomic.get t in
310310
let h = r.hash key in
311311
let mask = Array.length r.buckets - 1 in
312312
let i = h land mask in
313313
match Atomic.get (Array.unsafe_get r.buckets i) with
314-
| B Nil -> raise_notrace Not_found
315-
| B (Cons cons_r) ->
316-
if r.equal cons_r.key key then cons_r.value
317-
else assoc_exn r.equal key cons_r.rest
314+
| B Nil -> Nil
315+
| B (Cons cons_r as cons) ->
316+
if r.equal cons_r.key key then cons
317+
else assoc_node r.equal key cons_r.rest
318318
| B (Resize resize_r) ->
319319
(* A resize is in progress. The spine of the resize still holds what was
320320
in the bucket before resize reached that bucket. *)
321-
assoc_exn r.equal key resize_r.spine
321+
assoc_node r.equal key resize_r.spine
322322

323323
(* *)
324324

325-
let rec mem t key = function
326-
| Nil -> false
327-
| Cons r -> t key r.key || mem t key r.rest
325+
let find_exn t key =
326+
match find_node t key with
327+
| Nil -> raise_notrace Not_found
328+
| Cons r -> r.value
329+
330+
let mem t key = find_node t key != Nil
331+
332+
(* *)
328333

329334
let rec try_add t key value backoff =
330335
let r = get t in
@@ -339,7 +344,7 @@ let rec try_add t key value backoff =
339344
adjust_estimated_size t r mask 1
340345
else try_add t key value (Backoff.once backoff)
341346
| B (Cons _ as before) ->
342-
if mem r.equal key before then false
347+
if assoc_node r.equal key before != Nil then false
343348
else
344349
let after = Cons { key; value; rest = before } in
345350
if Atomic.compare_and_set b (B before) (B after) then

lib/picos_htbl/picos_htbl.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ val find_exn : ('k, 'v) t -> 'k -> 'v
2323
(** [find_exn htbl key] returns the current binding of [key] in the hash table
2424
[htbl] or raises {!Not_found} if no such binding exists. *)
2525

26+
val mem : ('k, 'v) t -> 'k -> bool
27+
(** [mem htbl key] determines whether the hash table [htbl] has a binding for
28+
the [key]. *)
29+
2630
val try_add : ('k, 'v) t -> 'k -> 'v -> bool
2731
(** [try_add htbl key value] tries to add a new binding of [key] to [value] to
2832
the hash table [htbl]. Returns [true] on success and [false] in case the

0 commit comments

Comments
 (0)