Advertisement
saltycracker

stringHash.ml

Apr 20th, 2020
2,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.12 KB | None | 0 0
  1. let strs =
  2.   [
  3.     "Salty";
  4.     "Cracker";
  5.     "Emily";
  6.     "Angela";
  7.     "Richard";
  8.     "John";
  9.     "Betty";
  10.     "Zebra";
  11.     "Bobby";
  12.     "Kenny";
  13.     "Brenda";
  14.     "Dart";
  15.     "Cat";
  16.     "Dog";
  17.     "Bird";
  18.     "Lion";
  19.     "Tweety";
  20.     "Tank";
  21.     "Car";
  22.     "Gun";
  23.     "Apple";
  24.     "Plum";
  25.     "Run";
  26.     "Peel";
  27.     "See";
  28.     "Sun";
  29.     "Mouse";
  30.     "Speaker";
  31.     "Key";
  32.     "Song";
  33.     "Switch";
  34.     "On";
  35.     "Gun";
  36.     "Apple";
  37.     "Switch";
  38.     "Lion";
  39.     "Gun";
  40.     "Gun";
  41.   ]
  42.  
  43. let arr_size = 24
  44.  
  45. type 'a collisions = 'a list
  46. type 'a hash_table = (('a * 'a collisions) option) array
  47.  
  48. let genhash str size =
  49.   let hash = 5381 in
  50.   (Seq.fold_left
  51.     (
  52.       fun a d ->
  53.         ((Int.shift_left a 5) + a) + (Char.code d)
  54.     )
  55.     hash
  56.     (String.to_seq(str))) mod size
  57.  
  58. let createEmptyHash size:'a hash_table =
  59.   Array.init size (fun x -> None)
  60.  
  61. let display (ht:'a hash_table) fStr =
  62.   Array.iter (
  63.     fun v ->
  64.       match v with
  65.       | None -> print_endline "Nothing here!"
  66.       | Some (s, coll) ->
  67.         (
  68.           Printf.printf "%s\n" (fStr s);
  69.           List.iter (fun s -> Printf.printf "\t%s\n" s) coll
  70.         )
  71.   ) ht
  72.  
  73. let addString ht str size =
  74.   let h_v = genhash str size in
  75.   let elem = Array.get ht h_v in
  76.   match elem with
  77.   | None -> Array.set ht h_v (Some(str, []))
  78.   | Some (s, coll) ->
  79.     if not(String.equal s str)
  80.     then
  81.       if List.exists (fun s -> String.equal s str) coll
  82.       then
  83.         ()
  84.       else
  85.         Array.set ht h_v (Some(s, str::coll))
  86.  
  87. let findValue (ht:string hash_table) str =
  88.   Array.exists
  89.     (
  90.       fun e ->
  91.         match e with
  92.         | None -> false
  93.         | Some (s, coll) ->
  94.           if (String.equal s str)
  95.           then
  96.             true
  97.           else if (List.exists (fun s -> String.equal s str) coll)
  98.           then
  99.             true
  100.           else
  101.             false
  102.  
  103.     ) ht
  104.  
  105. let removeValue (ht:string hash_table) str =
  106.   let exception Done in
  107.   try
  108.   Array.iteri
  109.     (
  110.       fun i e ->
  111.         match e with
  112.         | None -> ()
  113.         | Some (s, coll) ->
  114.           if String.equal s str
  115.           then
  116.             (
  117.               if List.length coll = 0
  118.               then
  119.                 (
  120.                   Array.set ht i None;
  121.                   raise Done
  122.                 )
  123.               else
  124.                 (
  125.                   Array.set ht i (Some(List.hd coll, List.tl coll));
  126.                   raise Done
  127.                 )
  128.             )
  129.           else if List.exists (fun s -> String.equal s str) coll
  130.           then
  131.             (
  132.               Array.set ht i
  133.                 (
  134.                   Some(s, List.filter (fun s -> not(String.equal s str)) coll)
  135.                 );
  136.               raise Done
  137.             )
  138.     ) ht
  139.     with _ -> ()
  140.  
  141. let my_hash = createEmptyHash arr_size
  142.  
  143. let () =
  144.   List.iter
  145.     (fun s -> addString my_hash s arr_size)
  146.     strs
  147.  
  148. let () =
  149.   List.iter
  150.     (
  151.       fun s ->
  152.        removeValue my_hash s
  153.     )
  154.     ["Tank"; "John"; "Bird"; "Lion"; "Switch"; "Apple"; "Gun"]
  155.  
  156. let () = display my_hash (fun t -> t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement