Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. open Printf
  2.  
  3. module IntSet =
  4. Set.Make (
  5. struct
  6. type t = int
  7. let compare = compare
  8. end)
  9.  
  10. module IntSetHash =
  11. Hashtbl.Make (
  12. struct
  13. type t = IntSet.t
  14. let equal = IntSet.equal
  15. let hash s = IntSet.fold (fun x a -> a + x) s 0
  16. end)
  17.  
  18. let rec interval a b =
  19. if a >= b then
  20. []
  21. else
  22. a::(interval (a+1) b)
  23.  
  24. let rec insert x ys =
  25. match ys with
  26. [] -> [[x]]
  27. | y::ys' ->
  28. (x::ys)::(List.map (fun rs -> y::rs) (insert x ys'))
  29.  
  30. let rec perm xs =
  31. match xs with
  32. [] -> [[]]
  33. | x::xs' ->
  34. List.flatten (List.map (insert x) (perm xs'))
  35.  
  36. let test create reg iter length =
  37. let ht = create 0 in
  38. List.iter
  39. (fun xs ->
  40. let s = IntSet.of_list xs in
  41. reg ht s true)
  42. (perm (interval 0 5));
  43. printf "----------------------------------------\n";
  44. printf "%d\n" (length ht);
  45. iter
  46. (fun s _ ->
  47. printf "{ ";
  48. IntSet.iter
  49. (fun x -> printf "%d " x)
  50. s;
  51. printf "}\n")
  52. ht
  53.  
  54. let () = test Hashtbl.create Hashtbl.replace Hashtbl.iter Hashtbl.length
  55. let () = test IntSetHash.create IntSetHash.replace IntSetHash.iter IntSetHash.length
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement