File: stkarray.dats

package info (click to toggle)
ats2-lang 0.4.2-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 40,500 kB
  • sloc: ansic: 389,898; makefile: 7,136; javascript: 1,852; lisp: 811; sh: 657; php: 573; python: 387; perl: 365
file content (325 lines) | stat: -rw-r--r-- 5,948 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
(***********************************************************************)
(*                                                                     *)
(*                         Applied Type System                         *)
(*                                                                     *)
(***********************************************************************)

(*
** ATS/Postiats - Unleashing the Potential of Types!
** Copyright (C) 2011-2013 Hongwei Xi, ATS Trustful Software, Inc.
** All rights reserved
**
** ATS is free software;  you can  redistribute it and/or modify it under
** the terms of  the GNU GENERAL PUBLIC LICENSE (GPL) as published by the
** Free Software Foundation; either version 3, or (at  your  option)  any
** later version.
** 
** ATS is distributed in the hope that it will be useful, but WITHOUT ANY
** WARRANTY; without  even  the  implied  warranty  of MERCHANTABILITY or
** FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
** for more details.
** 
** You  should  have  received  a  copy of the GNU General Public License
** along  with  ATS;  see the  file COPYING.  If not, please write to the
** Free Software Foundation,  51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*)

(* ****** ****** *)

(* Author: Hongwei Xi *)
(* Start time: September, 2013 *)
(* Authoremail: hwxi AT cs DOT bu DOT edu *)

(* ****** ****** *)
//
#define
ATS_PACKNAME
"ATSLIB.libats.stkarray"
//
#define // HX: no dynloading
ATS_DYNLOADFLAG 0 // at run-time
//
#define // HX: prefix for external
ATS_EXTERN_PREFIX "atslib_" // names
//  
(* ****** ****** *)

staload
UN = "prelude/SATS/unsafe.sats"

(* ****** ****** *)

staload "libats/SATS/stkarray.sats"

(* ****** ****** *)

implement
{a}(*tmp*)
stkarray_make_cap
  (cap) = stk where
{
//
val A = arrayptr_make_uninitized<a>(cap)
//
val
(pfat, pfgc | p) = ptr_alloc<stkarray_tsize>()
//
val (pfngc | stk) =
  stkarray_make_ngc_tsz{a}(pfat | p, A, cap, sizeof<a>)
//
prval ((*void*)) = mfree_gcngc_v_nullify(pfgc, pfngc)
//
} // end of [stkarray_make_cap]

(* ****** ****** *)

local

extern
fun
stkarray_get_size_tsz
  {a:vt0p}{m,n:int}
(
  stk: !stkarray (a, m, n), sizeof_t(a)
) :<> size_t(n) = "mac#%" // end-of-fun
extern
fun
stkarray_get_capacity_tsz
  {a:vt0p}{m,n:int}
(
  stk: !stkarray (a, m, n), sizeof_t(a)
) :<> size_t(m) = "mac#%" // end-of-fun

in (* in of [local] *)
//
implement
{a}(*tmp*)
stkarray_get_size
  (stk) =
  stkarray_get_size_tsz{a}(stk, sizeof<a>)
//
implement
{a}(*tmp*)
stkarray_get_capacity
  (stk) =
  stkarray_get_capacity_tsz{a}(stk, sizeof<a>)
//
end // end of [local]

(* ****** ****** *)
//
implement
{}(*tmp*)
fprint_stkarray$sep
  (out) = fprint (out, "<-")
//
implement
{a}(*tmp*)
fprint_stkarray
  (out, stk) = let
//
val n =
stkarray_get_size<a>(stk)
//
prval
[n:int]
EQINT() = eqint_make_guint(n)
//
implement
fprint_array$sep<>
  (out) = fprint_stkarray$sep<>(out)
//
val
p_beg =
stkarray_get_ptrbeg{a}(stk)
//
val
(pf,fpf|p_beg) =
$UN.ptr_vtake{array(a,n)}(p_beg)
//
val () = fprint_array<a>(out, !p_beg, n)
//
prval () = fpf(pf) // end of [prval]
//
in
  // nothing
end // end of [fprint_stkarray]
//
(* ****** ****** *)

implement
{a}(*tmp*)
fprint_stkarray_sep
  (out, stk, sep) = let
//
implement
{}(*tmp*)
fprint_stkarray$sep
  (out) = fprint_string(out, sep)
//
in
  fprint_stkarray<a> (out, stk)
end // end of [fprint_stkarray_sep]

(* ****** ****** *)

extern fun
stkarray_get_ptrcur{a:vt0p}
  {m,n:int} (stk: !stkarray (INV(a), m, n)):<> ptr = "mac#%"
// end of [stkarray_get_ptrcur]
extern fun
stkarray_set_ptrcur{a:vt0p}
  {m,n:int} (stk: !stkarray (INV(a), m, n), ptr):<!wrt> void = "mac#%"
// end of [stkarray_set_ptrcur]

(* ****** ****** *)

implement
{a}(*tmp*)
stkarray_insert
  {m,n}(stk, x0) = let
//
val p_cur =
  stkarray_get_ptrcur{a}(stk)
//
val ((*void*)) =
  $UN.ptr0_set<a>(p_cur, x0)
val ((*void*)) =
  stkarray_set_ptrcur{a}(stk, ptr_succ<a>(p_cur))
//
prval () = __assert (stk) where
{
//
extern
praxi
__assert
  (!stkarray(a, m, n) >> stkarray(a, m, n+1)): void
//
} (* end of [prval] *)
//
in
  // nothing
end // end of [stkarray_insert]

(* ****** ****** *)

implement
{a}(*tmp*)
stkarray_insert_opt
  (stk, x0) = let
//
val
isnot = stkarray_isnot_full{a}(stk)
//
in
//
if
isnot
then let
  val () = stkarray_insert<a>(stk, x0) in None_vt()
end else Some_vt{a}(x0)
//
end // end of [stkarray_insert_opt]

(* ****** ****** *)

implement
{a}(*tmp*)
stkarray_takeout
  {m,n} (stk) = x0 where
{
//
val p_cur =
  stkarray_get_ptrcur{a}(stk)
//
val p1_cur = ptr_pred<a>(p_cur)
//
val x0 = $UN.ptr0_get<a>(p1_cur)
val () = stkarray_set_ptrcur{a}(stk, p1_cur)
//
prval () = __assert (stk) where
{
//
extern
praxi
__assert
  (!stkarray(a, m, n) >> stkarray(a, m, n-1)): void
//
} (* end of [prval] *)
//
} // end of [stkarray_takeout]

(* ****** ****** *)

implement
{a}(*tmp*)
stkarray_takeout_opt
  (stk) = let
//
val
isnot = stkarray_isnot_nil{a}(stk)
//
in
//
if
isnot
then let
//
  val x0 =
  stkarray_takeout<a>(stk) in Some_vt{a}(x0)
//
end else None_vt((*void*))
//
end // end of [stkarray_takeout_opt]

(* ****** ****** *)

implement
{a}{env}
stkarray_foreach$cont(x, env) = true

implement
{a}(*tmp*)
stkarray_foreach
  (stk) = let
  var env: void = () in
  stkarray_foreach_env<a><void>(stk, env)
end // end of [stkarray_foreach]

(* ****** ****** *)

implement
{a}{env}
stkarray_foreach_env
  (stk, env) = let
//
implement
array_rforeach$cont<a><env>
  (x, env) =
  stkarray_foreach$cont<a><env>(x, env)
implement
array_rforeach$fwork<a><env>
  (x, env) =
  stkarray_foreach$fwork<a><env>(x, env)
//
val n =
stkarray_get_size<a>(stk)
//
prval
[
n:int
] EQINT() = eqint_make_guint(n)
//
val p0 = stkarray_get_ptrbeg{a}(stk)
val (pf,fpf|p0) = $UN.ptr0_vtake{array(a,n)}(p0)
//
val res = array_rforeach_env<a><env>(!p0, n, env)
//
in
  let prval () = fpf(pf) in res end
end // end of [stkarray_foreach_env]

(* ****** ****** *)

(* end of [stkarray.dats] *)