source.ml 822 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. open Base
  2. type source = int
  3. type value = int
  4. let time = 0
  5. let next_id = ref 1
  6. let create () =
  7. let s = !next_id in
  8. Int.incr next_id ; s
  9. let to_string = function
  10. | 0 -> "Time"
  11. | n -> Printf.sprintf "<src:%d>" n
  12. module Cmp : Comparable.S with type t = source = Int
  13. include Cmp
  14. type set = (t, comparator_witness) Set.t
  15. type 'a map = (t, 'a, comparator_witness) Map.t
  16. let empty_set = Set.empty (module Cmp)
  17. let empty_map = Map.empty (module Cmp)
  18. let singleton s = Set.singleton (module Cmp) s
  19. module State = struct
  20. type t = value map
  21. let empty = empty_map
  22. let set i v m = Map.set m ~key:i ~data:v
  23. let of_time ts = set time ts empty
  24. let get_exn i m = match Map.find m i with
  25. | Some(v) -> v
  26. | None -> failwith "source unset"
  27. end
  28. module Private = struct
  29. let next_id ~diff = !next_id + diff
  30. end