Next: , Previous: , Up: The Prolog Library   [Contents][Index]


10.28 Mutable, Dense Array Operations—library(mutarray)

Prolog does not have an array type. Instead, one must use lists or trees, with logarithmic access time to the elements at best. This package provides operations on dense, mutable arrays, of elements indexed from 1 and up. It uses an array representation, which admits direct access to the elements and constant-time access to any element. Update operations also take constant time, except under some circumstances when the entire array must be copied, in which case such operations take linear time. Following are the exported predicates, where a mutarray is a mutable (see ref-lte-mut), the value of which is accessed and modified by the operations of this package.

is_mutarray(+Array)

succeeds when Array is a mutarray.

list_to_mutarray(+Items, -Array)

where Items should be a list of values. Array is unified with a new mutarray populated by the given values.

new_mutarray(-Array)

unifies Array with a new mutarray of zero length.

new_mutarray(-Array, +Size)

where Size should be a nonnegative integer. Array is unified with a new mutarray of the given size populated by brand new variables.

mutarray_length(+MutArray, -Length)

Length is unified in constant time with the length of MutArray.

mutarray_set_length(+MutArray, +Length)

sets the new length of mutarray MutArray to Length. If the MutArray becomes shorter, this runs in constant time in the best case. Otherwise, new brand new variables are added to the end of the array in linear time.

mutarray_last(+MutArray, -Last)

Last is unified in constant time with the last element of MutArray. Merely fails if MutArray is of zero length.

mutarray_to_list(+MutArray, -List)

List is unified with the list of elements of MutArray.

mutarray_gen(+MutArray, -N, -Element)

is true when Element is the N:th element of the given MutArray. Iterates over all elements by increasing N.

mutarray_get(+MutArray, +N, -Element)

unifies Element in constant time with the N:th element of MutArray.

mutarray_put(+MutArray, +N, +Element)

sets the N:th element of MutArray to Element.

mutarray_update(+MutArray, +N, -OldElement, +NewElement)

unifies OldElement with the N:th element of MutArray, and sets that element to NewElement.

mutarray_append(+MutArr, +MutArrOrValues)

where MutArrOrValues should be a mutarray or a list of values, which is appended to the end of MutArr.



Send feedback on this subject.