/
llarray.opa
228 lines (192 loc) · 6.06 KB
/
llarray.opa
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
Copyright © 2011 MLstate
This file is part of Opa.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Native Imperative Array.
*
* @destination public
* @stabilization untested
**/
/**
* {1 About this module}
*
* This module contains an API for manipulating imperative native array.
* You must be careful by using this module, there are some strong constraints:
* + your array will not be serializable. Implementation is available on each side,
* but llarray are not meant to be transported from a side to an other.
* + this structure brings not any guarantee in case of parrallelism, and distribution.
*
* You may use this structure, if you know what you're doing.
* Probably not for casual user.
*
* {1 Where should I start?}
*
* {1 What if I need more?}
**/
/**
* {1 About dependencies of this module}
*
* Be aware of the fact that the opa compiler introduce some @llarray directives.
**/
/**
* {1 Types defined in this module}
**/
/**
* The external type for manipulating low level arrays.
* Implemented on both side.
**/
@opacapi
type llarray('a) = external
/**
* {1 Module for manipulating llarray}
**/
LowLevelArray = {{
size = %% Lowlevelarray.size %% : llarray('a) -> int
/**
* Element are indexed, started from index [0].
* This function is unsafe, and fails in case of index out of bounds.
* @error
**/
get = %% Lowlevelarray.get %% : llarray('a), int -> 'a
/**
* Element are indexed, started from index [0].
* This function is unsafe, and fails in case of index out of bounds.
* @error
**/
set = %% Lowlevelarray.set %% : llarray('a), int, 'a -> void
/**
* [LowLevelArray.create(size,v)] creates an llarray of the size [size] with each cell initialized to [v]
* [llarray] of size zero returned are shared
* You may also use the directive [@llarray(0, 1, 2, 3)] for building
* static and small arrays.
**/
create(size,v) =
bp_create = %% Lowlevelarray.create %% : int, 'a -> llarray('a)
if size==0 then empty else bp_create(size,v)
/* waiting sharing for @llarray to have the sharing invariant of zero size array */
/** A polymorphic array of size zero */
empty = @llarray()
/**
* An alias for {!LowLevelArray.size}
**/
length = size
/**
* {2 Iterators}
**/
iter(f : 'a -> void, a : llarray('a))=
n = length(a)
rec aux(i) = if i == n then void else do f(get(a, i)); aux(succ(i))
aux(0)
iteri(f : int, 'a -> void, a : llarray('a))=
n = length(a)
rec aux(i) = if i == n then void else do f(i, get(a, i)); aux(succ(i))
aux(0)
fold(f, a, acc)=
n = length(a)
rec aux(i, acc) =
if i == n then acc
else
acc = f(get(a, i), acc)
aux(succ(i), acc)
aux(0, acc)
foldi(f, a, acc)=
n = length(a)
rec aux(i, acc) =
if i == n then acc
else
acc = f(i, get(a, i), acc)
aux(succ(i), acc)
aux(0, acc)
/**
* The order of the traversal of the list is not guaranteed
* to be left to right
*/
filter_map_to_list(f:'a -> option('b), array:llarray('a)) : list('b) =
rec aux(f, array, i, acc) =
if i < 0 then
acc
else
acc =
match f(get(array, i)) with
| {none} -> acc
| {some = v} -> [v|acc]
aux(f, array, i-1, acc)
aux(f, array, length(array) - 1, [])
@private
map_aux(a,na,f,i) =
if i == 0 then na
else
v = f(i,get(a,i))
do set(na,i,v)
map_aux(a,na,f,i-1)
/**
* Remap an array using a function taking position and old value and returning new value
* The order in which the function will be called is undefined
*/
mapi(a)(f) =
s = size(a)
if s==0 then empty
else
na = create(s,f(0,get(a,0)))
map_aux(a,na,f,s-1)
@private
of_list_map_aux(a,f,l,i) =
match l
[] -> a
[hd|tl] ->
do set(a,i,f(i,hd))
of_list_map_aux(a,f,tl,i+1)
/**
* Same as mapi but with a list as input
*/
of_list_mapi(l)(f) =
match l
[] -> empty
[hd|tl] ->
a = create(List.length(l),f(0,hd))
of_list_map_aux(a,f,tl,1)
/**
* List to llarray conversion
*/
@expand
of_list(l) = l=l of_list_mapi(l)(_i,x->x)
@private
fold_mapi_aux(a,na,f,n,i,acc) =
if i==n then (na,acc)
else
(na_i,acc) = f(i,get(a,i),acc)
do set(na,i,na_i)
fold_mapi_aux(a,na,f,n,i+1,acc)
/**
* Same as mapi but remapping an element use and propagate an accumulator
* the mapping and propagation of accumulator is done following index order
*/
fold_mapi(a:llarray('a),acc:'acc)(f:int,'a,'acc->('b,'acc)):(llarray('b),'acc) =
s = size(a)
if s==0 then (empty,acc)
else
(na_0,acc) = f(0,get(a,0),acc)
na = create(s,na_0)
fold_mapi_aux(a,na,f,s,1,acc)
@private
aux_for_all(a,f,i) =
a_i = get(a,i)
if i==0 then f(a_i)
else f(a_i) && aux_for_all(a,f,i-1)
@expand
for_all(a)(f) = aux_for_all(a,f,size(a)-1)
init(n)(f) =
if n==0 then empty else
v0 = f(0)
a = create(n,v0)
rec aux(i) =
if i==n then a
else do set(a,i,f(i))
aux(i+1)
aux(1)
concat = %% LowLevelArray.concat %%
}}