File: deriving_interned.ml

package info (click to toggle)
ocaml-deriving-ocsigen 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 628 kB
  • ctags: 1,159
  • sloc: ml: 6,334; makefile: 63; sh: 18
file content (28 lines) | stat: -rw-r--r-- 642 bytes parent folder | download | duplicates (2)
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
(* Copyright Jeremy Yallop 2007.
   This file is free software, distributed under the MIT license.
   See the file COPYING for details.
*)

(* Interned strings *)
module StringMap = Map.Make(String)

(* global state *)
let map = ref StringMap.empty
let counter = ref 0

type t = int * string
    deriving (Show)

let intern s = 
  try StringMap.find s !map
  with Not_found ->
    let fresh = (!counter, String.copy s) in begin
        map := StringMap.add s fresh !map;
        incr counter;
        fresh
      end
        
let to_string (_,s) = String.copy s
let name = snd
let compare (l,_) (r,_) = compare l r
let eq (l,_) (r,_) = l = r