Module
datastructs/array - A thread safe variable array implementation for mzscheme
Synopsis
(require (lib "array.scm" "datastructs")) >(define a (array 'a 'b 'c)) >(array-set! (array-set! a 3 "hi!") 4 100) #<struct:array-type> >(array-set! a 6 'this-will-yield-an-error) index 6 is out of bound (0..5). >(array-ref a -1) index -1 is out of bound (0..4). >(array-ref a 0) 'a >(array-add! a 'hi) #<struct:array-type> >(array-insert! a 'inserted 2) #<struct:array-type> >(array->list a) (a b inserted c "hi!" 100 hi) >(array->vector a) #7(a b inserted c "hi!" 100 hi) >(array-foreach (lambda (element) (display element)(newline)) a) a b inserted c hi! 100 hi #<struct:array-type> >(array->list (array-map (lambda (e) (if (number? e) (* e e) e)) a)) (a b inserted c "hi!" 10000 hi 0) >(array->list a) (a b inserted c "hi!" 100 hi) >(array-remove! a 2) #<struct:array-type> >(array->list a) (a b c "hi!" 100 hi) >(array-space a) 6 >(array-space (array-add! a 4)) 14 >(array-length a) 7 >(array? a) #t >(array? 3) #t >(list->array '(6 7 8 9)) #<struct:array-type> >(vector->array (vector 1 2 3)) #<struct:array-type>
Description
The array module of the datastructs collection implements dynamic arrays.
This implementation is analogue in use to vectors in scheme with the exception,
that the array is automatically resized, when for a array-set!
operation an
index is used that is equal to the current array-length
.
These dynamic arrays are thread safe, i.e. when concurrently used, the array-set!
,
array-remove!
and array-insert!
operations will be run in a critical section.
API
Construction
Introspection
(array? obj) : boolean
Checks if the given scheme object obj
is of type array
. Returns #t
if so, #f, otherwise.
(array-length A) : number
Returns the number of elements in array A
.
(array-space A) : number
Returns the number of reserved elements in the underlying datastructure for array A
.
Mutation
(array-ref A i) : <element>
Returns element i
of array A
. If i<0
or i>=(array-length A)
, then
an out of bound error will be raised.
(array-set! A i e) : array
Sets element i
of array A
to e
. If i<0
or i>(array-length A)
, then
an out of bound error will be signaled. Returns A
.
(array-insert! A e i) : array
Note the reversed element and index arguments! Inserts element e
at position
i
in array A
. If i<0
or i>(array-length A)
, then
an out of bound error will be raised. Returns A
.
(array-add! A e) : array
This operation is equivalent to (array-set! A (array-length A) e)
. Returns A.
(array-remove! A i) : array
Removes element i
of array A
. If i<0
or i>=(array-length A)
, then
an out of bound error will be raised. Returns A
.
Conversion
(array->list A) ; list
Returns a list containing all elements of A
in order of index.
(array->vector A) : vector
Returns a vector containing all elements of A
in order of index.
(list->array L) : array
Returns an array containing all elements of L
in list order.
(vector->array V) : array
Returns an array containing all elements of V
in order of index.
Iteration
(array-foreach func A) : array
Calls func
with argument (array-ref A i)
for i=0..(- (array-length A) 1)
.
Returns A
.
(array-map func A) : array
Calls func
with argument (array-ref A i)
for i=0..(- (array-length A) 1)
and
builds up a new array with the results of func
. Returns the new array.
(array-map! func A) : array
Calls func
with argument (array-ref A i)
for i=0..(- (array-length A) 1)
and
performs (array-set! A i (func (array-ref A i)))
for each i
. Returns A
.
Note, each array-set!
operation is atomic. The whole array-map!
is not atomic.
Info
(c) 2005 Hans Oesterholt-Dijkema. Distributed under LGPL. Contact: send email to hans in domain elemental-programming.org. Homepage: http://www.elemental-programming.org.
$Id: array.pod,v 1.2 2006/06/05 10:27:02 hoesterholt Exp $