Skip to content

Commit

Permalink
feat: use dichotomi to implement NSet.add
Browse files Browse the repository at this point in the history
Use dichotomi to find where to add the new element and then filled the
new array with Array.blit
  • Loading branch information
FardaleM committed Oct 11, 2024
1 parent 34fbbc3 commit 0d2cc0a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/common/Type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,18 @@ end = struct
if !i2 < l2 then Array.blit t2 !i2 t (!i2 + l1) (l2 - !i2);
t

let add elt t = union (singleton elt) t
let add elt t =
let new_t = Array.make (length t + 1) elt in
begin match CCArray.bsearch ~cmp:Base.compare elt t with
| `Just_after i | `At i ->
Array.blit t 0 new_t 0 (i + 1);
Array.blit t (i + 1) new_t (i+2) (length t - i - 1)
| `All_lower -> Array.blit t 0 new_t 0 (length t)
| `All_bigger -> Array.blit t 0 new_t 1 (length t)
| `Empty -> ()
end;
new_t

let fold fn t acc = CCArray.fold_left (CCFun.flip fn) acc t
let map fn t = of_iter @@ Iter.map fn @@ to_iter t

Expand Down

0 comments on commit 0d2cc0a

Please sign in to comment.