1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Luc Maranget, projet Moscova, INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 2002 Institut National de Recherche en Informatique et *)
8 (* en Automatique. *)
9 (* *)
10 (* All rights reserved. This file is distributed under the terms of *)
11 (* the GNU Lesser General Public License version 2.1, with the *)
12 (* special exception on linking described in the file LICENSE. *)
13 (* *)
14 (**************************************************************************)
15
16 type 'a t = {mutable next : int ; mutable data : 'a array}
17
18 let default_size = 32
19 ;;
20
21 let create x = {next = 0 ; data = Array.make default_size x}
22 and reset t = t.next <- 0
23 ;;
24
25 let incr_table table new_size =
26 let t = Array.make new_size table.data.(0) in
27 Array.blit table.data 0 t 0 (Array.length table.data) ;
28 table.data <- t
29
30 let emit table i =
31 let size = Array.length table.data in
32 if table.next >= size then
33 incr_table table (2*size);
34 table.data.(table.next) <- i ;
35 table.next <- table.next + 1
36 ;;
37
38
39 exception Error
40
41 let get t i =
42 if 0 <= i && i < t.next then
43 t.data.(i)
44 else
45 raise Error
46
47 let trim t =
48 let r = Array.sub t.data 0 t.next in
49 reset t ;
50 r
51
52 let iter t f =
53 let size = t.next
54 and data = t.data in
55 for i = 0 to size-1 do
56 f data.(i)
57 done
58
59 let size t = t.next
60