mutable array : H.t Weak.t;

- let ~~create n = { size=0; extent=0; array=Weak.create (max 1 n) }~~

+ let is_array xs = Weak.length xs.array <= limit

- let clear xs = if xs.size <= limit then xs.size <- 0 else xs.array <- Weak.create xs.size

+ let create n = { extent=0; array=Weak.create (max 1 n) }

+ let clear xs = if is_array xs then xs.extent <- 0 else xs.array <- Weak.create (Weak.length xs.array)

- if ~~xs.size <= limit~~ then begin

+ if is_array xs then begin

(* as array, fold while compacting the array *)

- for i = 0 to xs.~~size~~ - 1 do

+ for i = 0 to xs.extent - 1 do

match Weak.get xs.array i with

(* as hash table, just fold *)

- for i = 0 to ~~xs.size~~ - 1 do

+ for i = 0 to Weak.length xs.array - 1 do

match Weak.get xs.array i with

| Some x -> acc := fn x !acc;

- let old_size = ~~xs.size~~ in

+ let old_size = Weak.length xs.array in

let old_array = xs.array in

xs.array <- Weak.create (old_size * 2);

- if ~~Weak.length xs.array <= limit then begi~~n

(* as array, copy and compact *)

for i = 0 to old_size - 1 do

match Weak.get old_array i with

- | Some _ as x'opt -> Weak.set xs.array !j x'opt; incr j

+ Weak.set xs.array xs.extent x'opt;

+ xs.extent <- xs.extent + 1

(* as hash table, reinsert *)

- xs.size <- Weak.length xs.array;

for i = 0 to old_size - 1 do

match Weak.get old_array i with

| Some x -> ignore (merge xs x)

- if ~~xs.size <= limit~~ then

(* as array, use fold to find and compact *)

let x'opt = fold begin fun x' x'opt -> match x'opt with

- if Weak.length xs.array <= xs.size then resize ();

- if xs.size < limit then begin

- Weak.set xs.array xs.size (Some x);

- xs.size <- xs.size + 1;

+ if xs.extent >= Weak.length xs.array then resize ();

+ if is_array xs then begin

+ Weak.set xs.array xs.extent (Some x);

+ xs.extent <- xs.extent + 1;

(* as hash table, perform a lookup with linear probing *)

- let i = H.hash x mod xs.size in

- let window = max limit (xs.size / 4) in

+ let size = Weak.length xs.array in

+ let i = H.hash x mod size in

+ let window = max limit (size / 4) in

- let k = (i + j) mod ~~xs.~~size in

+ let k = (i + j) mod size in

match Weak.get xs.array k with

| Some x' when H.equal x x' -> x'

| Some _ -> find (j + 1) result

- let k = (i + j) mod ~~xs.~~size in

+ let k = (i + j) mod size in

match Weak.get xs.array k with

| None -> xs.extent <- max j xs.extent; Weak.set xs.array k (Some x); x