[go: up one dir, main page]

File: inttbl.ml

package info (click to toggle)
cduce 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,180 kB
  • ctags: 3,176
  • sloc: ml: 20,028; xml: 5,546; makefile: 427; sh: 133
file content (57 lines) | stat: -rw-r--r-- 1,161 bytes parent folder | download | duplicates (8)
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
56
57
module type S = sig
  type key
  type 'a t

  val create: unit -> 'a t
  val fold: 'a t -> (key -> 'a -> 'b -> 'b) -> 'b -> 'b
  val clear: 'a t -> unit
  val add: 'a t -> key -> 'a -> unit
  val find: 'a t -> key -> 'a
  val mem: 'a t -> key -> bool
  val remove: 'a t -> key -> unit
end

type key = int
type 'a t = 'a option array ref

let create () = ref (Array.create 16 None)

let clear t = t := Array.create 16 None

let fold t f x =
  let rec aux i x =
    if i < 0 then x 
    else
      let x = 
	match !t.(i) with
	  | Some y -> f i y x
	  | None -> x 
      in
      aux (pred i) x
  in
  aux (pred (Array.length !t)) x

let add t i x =
  let l = Array.length !t in
  if i >= l then (
    let n = max (i + 1) (l * 2) in
    let a = Array.create n None in
    Array.blit !t 0 a 0 l;
    t := a;
  );
  (!t).(i) <- Some x

let remove t i =
  if (i <= Array.length !t) then (!t).(i) <- None

let find t i =
  if i >= Array.length !t then raise Not_found
  else match (!t).(i) with
    | None -> raise Not_found
    | Some x -> x
    
let mem t i =
  if i >= Array.length !t then false
  else match (!t).(i) with
    | None -> false
    | Some _ -> true