Advertisement
Guest User

Untitled

a guest
Dec 27th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.87 KB | None | 0 0
  1. open Core
  2.  
  3. type t = int * string [@@deriving show]
  4.  
  5. let mk =
  6.   let table = String.Table.create () in
  7.   let n = ref (-1) in
  8.   function
  9.   | name -> (
  10.     match Hashtbl.find table name with
  11.     | Some x -> (x, name)
  12.     | None -> (
  13.         incr n ;
  14.         match Hashtbl.add table ~key:name ~data:!n with
  15.         | `Duplicate -> failwith "Must not happend since .find failed"
  16.         | `Ok -> (!n, name) ) )
  17.  
  18. let name (_, s) = s
  19.  
  20. let compare s1 s2 = String.compare (snd s1) (snd s2)
  21.  
  22. let equal s1 s2 = (Pervasives.compare (fst s1) (fst s2)) = 0
  23.  
  24. module Ord = struct
  25.   type symbol = t
  26.  
  27.   type t = symbol
  28.  
  29.   let compare (x0, _) (x1, _) = Pervasives.compare x0 x1
  30.  
  31.   let t_of_sexp tuple = Tuple2.t_of_sexp Int.t_of_sexp String.t_of_sexp tuple
  32.  
  33.   let sexp_of_t tuple = Tuple2.sexp_of_t Int.sexp_of_t String.sexp_of_t tuple
  34. end
  35.  
  36. module Table = Map.Make (Ord)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement