File: unique.ml

package info (click to toggle)
botch 0.24-6.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,084,624 kB
  • sloc: xml: 11,924,892; ml: 4,489; python: 3,890; sh: 1,268; makefile: 334
file content (48 lines) | stat: -rw-r--r-- 1,569 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(**************************************************************************)
(*                                                                        *)
(*  Copyright (C) 2012 Pietro Abate <pietro.abate@pps.jussieu.fr>         *)
(*                                                                        *)
(*  This library is free software: you can redistribute it and/or modify  *)
(*  it under the terms of the GNU Lesser General Public License as        *)
(*  published by the Free Software Foundation, either version 3 of the    *)
(*  License, or (at your option) any later version.  A special linking    *)
(*  exception to the GNU Lesser General Public License applies to this    *)
(*  library, see the COPYING file for more information.                   *)
(**************************************************************************)

module type UniqueType = sig
  type t
  type v
  val create : v -> t
  val value : t -> v
  val uid : t -> int
end

module type OrderedType = sig type t val compare : t -> t -> int end

(* This module gives to each value a single id *)
module Make ( S : OrderedType ) : UniqueType with type v = S.t = struct

  module VMap = Map.Make(S)

  type v = S.t
  type t = Hash of (v * int)

  let gentag =
    let r = ref 0 in
    fun () -> incr r; !r

  let cons = ref VMap.empty

  let create v =
    try Hash(v,VMap.find v !cons) with
    Not_found -> begin
      let i = gentag () in
      cons := VMap.add v i !cons;
      Hash (v,i)
    end

  let value = function Hash (v,_) -> v
  let uid = function Hash (_,i) -> i

end