Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 84 lines (69 sloc) 2.056 kB
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 exception MaxSize
19
20 type 'a t =
21 {
22 mutable array : 'a Weak.t;
23 mutable length : int
24 }
25
26 let make ?size n =
27 let size =
28 match size with
29 Some s -> s
30 | _ -> n in
31 {
32 array = Weak.create size;
33 length = n
34 }
35
36 let create = make
37
38 let get a i =
39 if i < a.length && i >= 0 then Weak.get a.array i
40 else None
41
42 (** problème: set peut renvoyer (sans qu'on le sache) un tableau frais
43 cela est gênant si on dépend d'effets de bord *)
44 let set a i v =
45 let l = Weak.length a.array in
46 if i < l then begin
47 Weak.set a.array i v ;
48 if i >= a.length then a.length <- succ i
49 end
50 else if i < Sys.max_array_length then
51 let n = max i (min Sys.max_array_length (2 * l)) in
52 begin
53 let na = Weak.create n in
54 Weak.blit a.array 0 na 0 l;
55 a.array <- na;
56 a.length <- succ i
57 end
58 else raise MaxSize
59
60 let remove a i =
61 set a i None
62
63 let length a = a.length
64
65 let real_length a = Weak.length a.array
66
67 let fold_left f init a =
68 let rec aux acc i =
69 if i = length a then acc
70 else
71 match Weak.get a.array i with
72 | Some x -> aux (f acc x) (succ i)
73 | None -> aux acc (succ i)
74 in aux init 0
75
76 let fold_left_i f init a =
77 let rec aux acc j i =
78 if j = length a then acc
79 else
80 match Weak.get a.array j with
81 | Some x -> aux (f acc x ~i) (succ j) (succ i)
82 | None -> aux acc (succ j) i
83 in aux init 0 0
Something went wrong with that request. Please try again.