build with ocamlbuild by default
[deriving.git] / lib / interned.ml
blobd3f198f19168517b48cf6f379f919feb57fc0e54
1 (*pp deriving *)
3 (* Copyright Jeremy Yallop 2007.
4 This file is free software, distributed under the MIT license.
5 See the file COPYING for details.
6 *)
8 (* Interned strings *)
9 module StringMap = Map.Make(String)
11 (* global state *)
12 let map = ref StringMap.empty
13 let counter = ref 0
15 type t = int * string
16 deriving (Show)
18 let intern s =
19 try StringMap.find s !map
20 with Not_found ->
21 let fresh = (!counter, String.copy s) in begin
22 map := StringMap.add s fresh !map;
23 incr counter;
24 fresh
25 end
27 let to_string (_,s) = String.copy s
28 let name = snd
29 let compare (l,_) (r,_) = compare l r
30 let eq (l,_) (r,_) = l = r