1 (* Copyright (C
) 1999-2006 Henry Cejtin
, Matthew Fluet
, Suresh
2 * Jagannathan
, and Stephen Weeks
.
4 * MLton is released under a BSD
-style license
.
5 * See the file MLton
-LICENSE for details
.
8 structure Buffer
: BUFFER
=
11 datatype 'a t
= T
of {dummy
: 'a
,
17 elts
= ref (Array
.array (1, dummy
)),
20 fun length (T
{length
, ...}) = !length
22 fun reset (T
{length
, ...}) = length
:= 0
24 fun last (T
{elts
, length
= ref n
, ...}) =
27 else SOME (Array
.sub (!elts
, n
- 1))
29 val growFactor
: int = 2
31 fun ensureFree (T
{dummy
, elts
, length
, ...}, amount
: int): unit
=
33 val maxLength
= Array
.length (!elts
)
35 if amount
<= maxLength
- !length
39 val n
= Int.max (maxLength
* growFactor
, !length
+ amount
)
42 elts
:= Array
.tabulate (n
, fn i
=>
49 fun add (v
as T
{elts
, length
, ...}, e
) =
51 ; Array
.update (!elts
, !length
, e
)
54 fun toVector (T
{elts
, length
, ...}): 'a vector
=
58 Vector.tabulate (!length
, fn i
=> Array
.sub (elts
, i
))
61 fun layout layoutElt b
= Vector.layout
layoutElt (toVector b
)