-
Notifications
You must be signed in to change notification settings - Fork 6
2018 001 Addition of monomorphic buffers
Author: John Reppy
Last revised: May 17, 2018
Status: proposed
Discussion: issue #24
This proposal is a revision of 2015-004 Addition of Buffer module, which generalizes the interface to any element type that has monomorphic vectors, arrays, and slices.
signature MONO_BUFFER
structure Word8Buffer : MONO_BUFFER
where type elem = Word8.word
and type vector = Word8Vector.vector
and type slice = Word8VectorSlice.slice
and type array = Word8Array.array
and type array_slice = Word8ArraySlice.slice
structure CharBuffer : MONO_BUFFER
where type elem = Char.char
and type vector = CharVector.vector
and type slice = CharVectorSlice.slice
and type array = CharArray.array
and type array_slice = CharArraySlice.slicetype buf
type elem
type vector
type slice
type array
type array_slice
val maxLen : int
val new : int -> buf
val contents : buf -> vector
val copy : {src : buf, dst : array, di : int} -> unit
val length : buf -> int
val sub : but * int -> elem
val clear : buf -> unit
val reset : buf -> unit
val reserve : buf * int -> unit
val add1 : buf * elem -> unit
val addVec : buf * vector -> unit
val addSlice : buf * slice -> unit
val addArr : buf * array -> unit
val addArrSlice : buf * array_slice -> unit-
type bufA type of mutable buffers that allows efficient addition of elements at the end and then extraction as a monomorphic vector. It provides accumulative concatenation of strings in quasi-linear time (instead of quadratic time when vector are concatenated pairwise).
-
type elemThe type of buffer elements.
-
type vectorThe type of vectors of elements.
-
type sliceThe type of vector-slices of elements.
-
type arrayThe type of arrays of elements.
-
type array_sliceThe type of array-slices of elements.
-
maxLenis the maximum number of elements that a buffer can hold.
-
new hintcreates a new empty buffer. The argument
hintis used as the initial internal storage capacity for the buffer contents. Ahintof0causes the implementation to use an implementation-dependent default size.Raises
Sizeifhint < 0orhint > maxLen.An implementation should guarantee that if no more than
hintelements are added to the buffer, then the internal storage should not need to be reallocated. -
contents bufreturns the current contents of
bufas a vector value. -
copy {src, dst, di}copies the contents of the buffer into the array
dstwith the buffer element at positionibeing copied to the position(di + i)indst. If the contents of the buffer overflows the array, then theSubscriptexception is raised anddstis not modified. -
length bufreturns the length of the contents of
buf. -
sub (buf, i)returns the buffer element at position
i. Ifi < 0orlength buf <= i, then theSubscriptexception is raised. -
clear bufemptys the buffer
buf(i.e., sets the length of the contents to zero). -
reset bufemptys the buffer
bufand resets its internal storage to the initial size. -
reserve (buf, amount)extends the capacity of the buffer by the specified amount. Note that the total capacity cannot exceed
maxLen.Raises the
Sizeexception ifamount < 0. -
add1 (buf, elem)appends the element
elemto the bufferbuf.This function raises
Subscriptif adding the element causes the buffer contents to exceedmaxLenelements. -
addVec (buf, vec)appends the vector
vecto the bufferbuf.This function raises
Subscriptif adding the vector causes the buffer contents to exceedmaxLenelements. -
addSlice (buf, slice)appends the vector-slice
sliceto the bufferbuf.This function raises
Subscriptif adding the slice causes the buffer contents to exceedmaxLenelements. -
addArr (buf, arr)appends the contents of the array
arrto the bufferbuf.This function raises
Subscriptif adding the array's elements causes the buffer contents to exceedmaxLenelements. -
addArrSlice (buf, slice)appends the contents of the array-slice
sliceto the bufferbuf.This function raises
Subscriptif adding the slice's elements causes the buffer contents to exceedmaxLenelements.
This abstraction allows one to efficiently build up vectors of values (e.g., strings for text output or bytevectors for object serialization). While it is possible to implement this abstraction using the existing Basis Library mechanisms (e.g., see the sample implementation), implementations may be able to take advantage of internal features to maximize performace.
Adopting this proposal should not affect existing programs.
This module is a natural candidate for inclusion in the Basis Library. Similar structures
have been found to be useful in other programming languages, for instance
StringBuilder in Java and the
Buffer module in O'Caml.
-
[2018-05-17] Added
copyandsubfunctions. -
[2018-05-10] Revised proposal.
-
[2015-08-11] Original string-buffer proposal by Ken Friis Larsen (2015-004 Addition of Buffer module).
