1. Yit Phang Khoo
  2. Adapton.ocaml

Commits

Yit Phang Khoo  committed c6058fa

Add comments to WeakSet.

  • Participants
  • Parent commits 5374e58
  • Branches default

Comments (0)

Files changed (1)

File Source/Adapton/WeakSet.ml

View file
     let fold fn xs acc =
         let acc = ref acc in
         if xs.size <= limit then begin
+            (* as array, fold while compacting the array *)
             let j = ref 0 in
             for i = 0 to xs.size - 1 do
                 match Weak.get xs.array i with
             done;
             xs.size <- !j;
         end else
+            (* as hash table, just fold *)
             for i = 0 to xs.size - 1 do
                 match Weak.get xs.array i with
                     | Some x -> acc := fn x !acc;
             let old_array = xs.array in
             xs.array <- Weak.create (old_size * 2);
             if Weak.length xs.array <= limit then begin
+                (* as array, copy and compact *)
                 let j = ref 0 in
                 for i = 0 to old_size - 1 do
                     match Weak.get old_array i with
                 done;
                 xs.size <- !j
             end else begin
+                (* 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
             end
         in
         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
                 | Some _ -> x'opt
                 | None -> if H.equal x' x then Some x' else None
                     end else
                         merge xs x
         else
+            (* as hash table, perform a lookup with linear probing *)
             let i = H.hash x mod xs.size in
             let rec find j result =
                 let k = (i + j) mod xs.size in