1% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved. 2% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk. 3\newcommand{\FlexibleArrayXmpTitle}{FlexibleArray} 4\newcommand{\FlexibleArrayXmpNumber}{9.26} 5% 6% ===================================================================== 7\begin{page}{FlexibleArrayXmpPage}{9.26 FlexibleArray} 8% ===================================================================== 9\beginscroll 10 11% 12% 13% 14% 15% 16 17 18The \spadtype{FlexibleArray} domain constructor creates 19one-dimensional arrays of elements of the same type. 20%-% \HDindex{array!flexible}{FlexibleArrayXmpPage}{9.26}{FlexibleArray} 21Flexible arrays are an attempt to provide a data type that has the 22%-% \HDindex{flexible array}{FlexibleArrayXmpPage}{9.26}{FlexibleArray} 23best features of both one-dimensional arrays (fast, random access 24to elements) and lists (flexibility). 25They are implemented by a fixed block of storage. 26When necessary for expansion, a new, larger block of storage is 27allocated and the elements from the old storage area are copied 28into the new block. 29 30Flexible arrays have available most of the operations provided by 31\spadtype{OneDimensionalArray} (see \downlink{`OneDimensionalArray'}{OneDimensionalArrayXmpPage}\ignore{OneDimensionalArray} 32and \downlink{`Vector'}{VectorXmpPage}\ignore{Vector}). 33Since flexible arrays are also of category 34\spadtype{ExtensibleLinearAggregate}, they have operations 35\spadfunX{concat}, \spadfunX{delete}, \spadfunX{insert}, 36\spadfunX{merge}, \spadfunX{remove}, \spadfunX{removeDuplicates}, 37and \spadfunX{select}. 38In addition, the operations \spadfun{physicalLength} and 39\spadfunX{physicalLength} provide user-control over expansion and 40contraction. 41 42\xtc{ 43A convenient way to create a flexible array is to apply 44the operation \spadfun{flexibleArray} to a list of values. 45}{ 46\spadpaste{flexibleArray [i for i in 1..6]} 47} 48\xtc{ 49Create a flexible array of six zeroes. 50}{ 51\spadpaste{f : FARRAY INT := new(6,0)\bound{f}} 52} 53\xtc{ 54For i = 1..6, 55set the \eth{\smath{i}} element to \smath{i}. 56Display \spad{f}. 57}{ 58\spadpaste{for i in 1..6 repeat f.i := i; f\bound{f1}\free{f}} 59} 60\xtc{ 61Initially, the physical length is the same as the number of elements. 62}{ 63\spadpaste{physicalLength f\free{f1}} 64} 65\xtc{ 66Add an element to the end of \spad{f}. 67}{ 68\spadpaste{concat!(f,11)\bound{f2}\free{f1}} 69} 70\xtc{ 71See that its physical length has grown. 72}{ 73\spadpaste{physicalLength f\free{f2}} 74} 75\xtc{ 76Make \spad{f} grow to have room for \spad{15} elements. 77}{ 78\spadpaste{physicalLength!(f,15)\bound{f3}\free{f2}} 79} 80\xtc{ 81Concatenate the elements of \spad{f} to itself. 82The physical length allows room for three more values at the end. 83}{ 84\spadpaste{concat!(f,f)\bound{f4}\free{f3}} 85} 86\xtc{ 87Use \spadfunX{insert} to add an element to the front of 88a flexible array. 89}{ 90\spadpaste{insert!(22,f,1)\bound{f5}\free{f4}} 91} 92\xtc{ 93Create a second flexible array from \spad{f} consisting of the 94elements from index 10 forward. 95}{ 96\spadpaste{g := f(10..)\bound{g}\free{f5}} 97} 98\xtc{ 99Insert this array at the front of \spad{f}. 100}{ 101\spadpaste{insert!(g,f,1)\bound{g1}\free{g f5}} 102} 103\xtc{ 104Merge the flexible array \spad{f} into \spad{g} after sorting each in place. 105}{ 106\spadpaste{merge!(sort! f, sort! g)\bound{f6}\free{g f5}} 107} 108\xtc{ 109Remove duplicates in place. 110}{ 111\spadpaste{removeDuplicates! f\bound{f7}\free{f6}} 112} 113\xtc{ 114Remove all odd integers. 115}{ 116\spadpaste{select!(i +-> even? i,f)\bound{f8}\free{f7}} 117} 118\xtc{ 119All these operations have shrunk the physical length of \spad{f}. 120}{ 121\spadpaste{physicalLength f\free{b8}} 122} 123\xtc{ 124To force \Language{} not to shrink flexible arrays call the 125\spadfun{shrinkable} operation with the argument \spad{false}. 126You must package call this operation. 127The previous value is returned. 128}{ 129\spadpaste{shrinkable(false)$FlexibleArray(Integer)} 130} 131 132\endscroll 133\autobuttons 134\end{page} 135% 136