Advertisement
saltycracker

stringHashAndMore4.ml

Apr 23rd, 2020
2,413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 4.84 KB | None | 0 0
  1.  
  2. module type SIn =
  3. sig
  4.  
  5.   type key
  6.   type value
  7.  
  8.   val arr_size: int
  9.   val k_compare: key -> key -> int
  10.   val v_compare: value -> value -> int
  11.   val genHash: key -> int
  12.  
  13. end
  14.  
  15. module type SOut =
  16. sig
  17.  
  18.   type key
  19.   type value
  20.  
  21.   type hash
  22.  
  23.   val create: unit -> hash
  24.   val display: hash -> (key -> string) -> (value -> string) -> unit
  25.   val add: hash -> key -> value -> unit
  26.   val find: hash -> key -> value option
  27.   val remove: hash -> key -> unit
  28.  
  29. end
  30.  
  31.  
  32. module Make(E:SIn):(SOut with type key := E.key with type value := E.value) =
  33. struct
  34.  
  35.   type key = E.key
  36.   type value = E.value
  37.  
  38.   type collisions = (key * value) list
  39.   type node = (key * value * collisions) option
  40.   type hash = node array
  41.  
  42.   let genHash k = E.genHash k
  43.  
  44.   let create ():(hash) =
  45.     Array.init E.arr_size (fun _ -> None)
  46.  
  47.   let display (ht:hash) sKey sValue =
  48.     Array.iter (
  49.       fun e ->
  50.         match e with
  51.         | None -> print_endline "[Nothing here]"
  52.         | Some (k, v, lst) ->
  53.           Printf.printf "->%s=>%s\n" (sKey k) (sValue v);
  54.           List.iter (
  55.             fun (kl, vl) ->
  56.               Printf.printf "--->%s=>%s\n" (sKey kl) (sValue vl);
  57.           ) lst
  58.     ) ht
  59.  
  60.   let rec add_list lst key value =
  61.     match lst with
  62.     | [] -> (key, value)::[]
  63.     | (k, v)::tl ->
  64.       if (E.k_compare k key) = 0 && (E.v_compare v value) = 0
  65.       then
  66.         (k, v)::tl
  67.       else if (E.k_compare k key) = 0
  68.       then
  69.         (k, value)::tl
  70.       else
  71.         (k, v)::add_list tl key value
  72.  
  73.  
  74.   let add ht key value =
  75.     let hv = genHash key in
  76.     let arr_node = Array.get ht hv in
  77.     match arr_node with
  78.     | None -> Array.set ht hv (Some (key, value, []))
  79.     | Some (k, v, lst) when (E.k_compare k key) = 0 && (E.v_compare v value) != 0 ->
  80.       Array.set ht hv (Some (k, value, lst))
  81.     | Some (k, v, lst) when (E.k_compare k key) = 0 && (E.v_compare v value) = 0 ->
  82.       ()
  83.     | Some (k, v, lst) -> Array.set ht hv (Some(k, v, (add_list lst key value)))
  84.  
  85.   let rec find_list lst key =
  86.     match lst with
  87.     | [] -> None
  88.     | (k, v)::tl ->
  89.       if (E.k_compare key k) = 0
  90.       then
  91.         Some v
  92.       else
  93.         find_list tl key
  94.  
  95.   let find (ht:hash) key =
  96.     let hv = genHash key in
  97.     let arr_node = Array.get ht hv in
  98.     match arr_node with
  99.     | None -> None
  100.     | Some (k, v, _) when (E.k_compare k key) = 0 -> Some v
  101.     | Some (_, _, lst) -> find_list lst key
  102.  
  103.   let rec remove_list lst key =
  104.     match lst with
  105.     | [] -> []
  106.     | (k, v)::tl ->
  107.       if (E.k_compare key k) = 0
  108.       then
  109.         tl
  110.       else
  111.         (k, v)::remove_list tl key
  112.  
  113.   let remove (ht:hash) key =
  114.     let hv = genHash key in
  115.     let arr_node = Array.get ht hv in
  116.     match arr_node with
  117.     | None -> ()
  118.     | Some (k, _, []) when (E.k_compare k key) = 0 -> Array.set ht hv None
  119.     | Some (k, v, lst) when (E.k_compare k key) = 0 ->
  120.       let (kl, vl) = List.hd lst in
  121.       Array.set ht hv (Some(kl, vl, (List.tl lst)))
  122.     | Some (k, v, lst) -> Array.set ht hv (Some(k, v, remove_list lst key))
  123.  
  124. end
  125.  
  126. (***************************************************************************)
  127.  
  128.  
  129. let strs =
  130.   [
  131.     ("Salty", 1);
  132.     ("Cracker", 2);
  133.     ("Emily", 3);
  134.     ("Angela", 4);
  135.     ("Richard", 5);
  136.     ("John", 6);
  137.     ("Betty", 7);
  138.     ("Zebra", 8);
  139.     ("Bobby", 9);
  140.     ("Kenny", 10);
  141.     ("Brenda", 11);
  142.     ("Dart", 12);
  143.     ("Cat", 13);
  144.     ("Dog", 14);
  145.     ("Bird", 15);
  146.     ("Lion", 16);
  147.     ("Tweety", 17);
  148.     ("Tank", 18);
  149.     ("Car", 19);
  150.     ("Gun", 20);
  151.     ("Apple", 21);
  152.     ("Plum", 22);
  153.     ("Run", 23);
  154.     ("Peel", 24);
  155.     ("See", 25);
  156.     ("Sun", 26);
  157.     ("Mouse", 27);
  158.     ("Speaker", 28);
  159.     ("Key", 29);
  160.     ("Song", 30);
  161.     ("Switch", 31);
  162.     ("On", 32);
  163.     ("Gun", 33);
  164.     ("Apple", 34);
  165.     ("Switch", 35);
  166.     ("Lion", 36);
  167.     ("Gun", 37);
  168.     ("Gun", 38);
  169.   ]
  170.  
  171. module MyHashStrInt =
  172. struct
  173.  
  174.   type key = string
  175.   type value = int
  176.  
  177.   let arr_size = 24
  178.   let k_compare = String.compare
  179.   let v_compare = Int.compare
  180.   let genHash str =
  181.     let hash = 5381 in
  182.     (Seq.fold_left
  183.        (
  184.          fun a d ->
  185.            ((Int.shift_left a 5) + a) + (Char.code d)
  186.        )
  187.        hash
  188.        (String.to_seq(str))) mod arr_size
  189.  
  190. end
  191.  
  192. module HashSI = Myhash.Make(MyHashStrInt)
  193.  
  194. let ht = HashSI.create()
  195.  
  196. let () =
  197.   List.iter (
  198.     fun (k, v) ->
  199.       HashSI.add ht k v
  200.   ) strs
  201.  
  202. let () = HashSI.display ht (fun t -> t) string_of_int
  203.  
  204. let () = print_endline "\n\n"
  205.  
  206. let () =
  207.   List.iter (
  208.     fun k ->
  209.       HashSI.remove ht k
  210.   ) ["Lion"; "Switch"; "Apple"; "Gun"; ]
  211.  
  212. let () = HashSI.display ht (fun t -> t) string_of_int
  213.  
  214. let () = print_endline "\n\n"
  215.  
  216. let ans = HashSI.find ht "Angela"
  217.  
  218. let () =
  219.   match ans with
  220.   | None -> print_endline "Nothing found!"
  221.   | Some v -> Printf.printf "Found: %d\n" v
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement