% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved. % !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk. \newcommand{\FlexibleArrayXmpTitle}{FlexibleArray} \newcommand{\FlexibleArrayXmpNumber}{9.26} % % ===================================================================== \begin{page}{FlexibleArrayXmpPage}{9.26 FlexibleArray} % ===================================================================== \beginscroll The \spadtype{FlexibleArray} domain constructor creates one-dimensional arrays of elements of the same type. %-% \HDindex{array!flexible}{FlexibleArrayXmpPage}{9.26}{FlexibleArray} Flexible arrays are an attempt to provide a data type that has the %-% \HDindex{flexible array}{FlexibleArrayXmpPage}{9.26}{FlexibleArray} best features of both one-dimensional arrays (fast, random access to elements) and lists (flexibility). They are implemented by a fixed block of storage. When necessary for expansion, a new, larger block of storage is allocated and the elements from the old storage area are copied into the new block. Flexible arrays have available most of the operations provided by \spadtype{OneDimensionalArray} (see \downlink{`OneDimensionalArray'}{OneDimensionalArrayXmpPage}\ignore{OneDimensionalArray} and \downlink{`Vector'}{VectorXmpPage}\ignore{Vector}). Since flexible arrays are also of category \spadtype{ExtensibleLinearAggregate}, they have operations \spadfunX{concat}, \spadfunX{delete}, \spadfunX{insert}, \spadfunX{merge}, \spadfunX{remove}, \spadfunX{removeDuplicates}, and \spadfunX{select}. In addition, the operations \spadfun{physicalLength} and \spadfunX{physicalLength} provide user-control over expansion and contraction. \xtc{ A convenient way to create a flexible array is to apply the operation \spadfun{flexibleArray} to a list of values. }{ \spadpaste{flexibleArray [i for i in 1..6]} } \xtc{ Create a flexible array of six zeroes. }{ \spadpaste{f : FARRAY INT := new(6,0)\bound{f}} } \xtc{ For \texht{$i=1\ldots 6$}{i = 1..6}, set the \eth{\smath{i}} element to \smath{i}. Display \spad{f}. }{ \spadpaste{for i in 1..6 repeat f.i := i; f\bound{f1}\free{f}} } \xtc{ Initially, the physical length is the same as the number of elements. }{ \spadpaste{physicalLength f\free{f1}} } \xtc{ Add an element to the end of \spad{f}. }{ \spadpaste{concat!(f,11)\bound{f2}\free{f1}} } \xtc{ See that its physical length has grown. }{ \spadpaste{physicalLength f\free{f2}} } \xtc{ Make \spad{f} grow to have room for \spad{15} elements. }{ \spadpaste{physicalLength!(f,15)\bound{f3}\free{f2}} } \xtc{ Concatenate the elements of \spad{f} to itself. The physical length allows room for three more values at the end. }{ \spadpaste{concat!(f,f)\bound{f4}\free{f3}} } \xtc{ Use \spadfunX{insert} to add an element to the front of a flexible array. }{ \spadpaste{insert!(22,f,1)\bound{f5}\free{f4}} } \xtc{ Create a second flexible array from \spad{f} consisting of the elements from index 10 forward. }{ \spadpaste{g := f(10..)\bound{g}\free{f5}} } \xtc{ Insert this array at the front of \spad{f}. }{ \spadpaste{insert!(g,f,1)\bound{g1}\free{g f5}} } \xtc{ Merge the flexible array \spad{f} into \spad{g} after sorting each in place. }{ \spadpaste{merge!(sort! f, sort! g)\bound{f6}\free{g f5}} } \xtc{ Remove duplicates in place. }{ \spadpaste{removeDuplicates! f\bound{f7}\free{f6}} } \xtc{ Remove all odd integers. }{ \spadpaste{select!(i +-> even? i,f)\bound{f8}\free{f7}} } \xtc{ All these operations have shrunk the physical length of \spad{f}. }{ \spadpaste{physicalLength f\free{b8}} } \xtc{ To force \Language{} not to shrink flexible arrays call the \spadfun{shrinkable} operation with the argument \axiom{false}. You must package call this operation. The previous value is returned. }{ \spadpaste{shrinkable(false)\$FlexibleArray(Integer)} } \endscroll \autobuttons \end{page} %