File: table.ml

package info (click to toggle)
advi 1.6.0-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 17,416 kB
  • ctags: 2,825
  • sloc: ml: 12,261; sh: 1,500; ansic: 935; makefile: 738; perl: 57; tcl: 10
file content (55 lines) | stat: -rw-r--r-- 1,984 bytes parent folder | download | duplicates (4)
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
49
50
51
52
53
54
55
(***********************************************************************)
(*                                                                     *)
(*                             Active-DVI                              *)
(*                                                                     *)
(*                   Projet Cristal, INRIA Rocquencourt                *)
(*                                                                     *)
(*  Copyright 2002 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the GNU Lesser General Public License.          *)
(*                                                                     *)
(*  Jun Furuse, Didier Rmy and Pierre Weis.                           *)
(*  Contributions by Roberto Di Cosmo, Didier Le Botlan,               *)
(*  Xavier Leroy, and Alan Schmitt.                                    *)
(*                                                                     *)
(*  Based on Mldvi by Alexandre Miquel.                                *)
(***********************************************************************)

(* $Id: table.ml,v 1.5 2003/10/23 13:17:21 weis Exp $ *)

type 'a status =
   | Unknown
   | Known of 'a
   | Error of exn;;

type 'a t = {
   table : 'a status array;
   build : int -> 'a;
   (* extension for japanese characters (id > 255) *)
   hash : (int, 'a) Hashtbl.t;
};;

let make f = {
  table = Array.make 256 Unknown;
  build = f;
  hash = Hashtbl.create 1031;
};;

let get tbl n =
  if n < 0 || n > 0xFF then begin
    try Hashtbl.find tbl.hash n with Not_found ->
      let v = tbl.build n in
      Hashtbl.add tbl.hash n v;
      v
  end else
  let table = tbl.table in
  match table.(n) with
  | Known v -> v
  | Error e -> raise e
  | Unknown ->
      try
        let v = tbl.build n in
        table.(n) <- Known v; v
      with e ->
        table.(n) <- Error e;
        raise e;;