"FixedArray" (Data Structure)
"FixedArray"
represents an array of fixed length where the elements are general expressions.
Details
- A fixed-length array is useful for efficient element extraction and updating:
-
CreateDataStructure[ "FixedArray",n] create a new "FixedArray" with n elements CreateDataStructure[ "FixedArray",x,n] create a new "FixedArray" of length n initialized to x CreateDataStructure[ "FixedArray",elems] create a new "FixedArray" containing elems Typed[x,"FixedArray"] give x the type "FixedArray" - For a data structure of type "FixedArray", the following operations can be used:
-
ds["Copy"] return a copy of ds time: O(n) ds["Elements"] return a list of the elements of ds time: O(n) ds["EmptyQ"] True, if ds has no elements time: O(1) ds["Fold",fun] apply fun to the elements of ds, accumulating a result time: O(n) ds["Fold",fun,init] apply fun to the elements of ds, starting with init, accumulating a result time: O(n) ds["Length"] number of elements stored in ds time: O(1) ds["Part",i] give the i
part of dstime: O(1) ds["SetPart",i,elem] update the i
part of dstime: O(1) ds["SwapPart",i,j] swap the i
and j
parts of dstime: O(1) ds["Visualization"] return a visualization of ds time: O(n) - The following functions are also supported:
-
dsi===dsj True, if dsi equals dsj ds["Part",i]=val set i
element of ds to valFullForm[ds] full form of ds Information[ds] information about ds InputForm[ds] input form of ds Length[ds] number of elements in ds Normal[ds] convert ds to a normal expression
Examples
open all close allBasic Examples (2)
A new "FixedArray" can be created with CreateDataStructure:
ds = CreateDataStructure["FixedArray", 8]ds["Length"]The elements are initialized to Null:
ds["Elements"]ds["Part", 1] = f[2]ds["Part", 1]Normal[ds]It is fast to update elements:
ds = CreateDataStructure["FixedArray", 1000];
Do[ds["SetPart", i, i], {i, 1000}]A visualization of the array can be generated:
ds["Visualization"]ds["Fold", Plus]Scope (13)
Creation (3)
Create a "FixedArray" with 12 elements:
ds = CreateDataStructure["FixedArray", 12]By default, all elements are initialized to Null:
ds["Elements"]Create a "FixedArray" with 12 elements with a default initial value:
ds = CreateDataStructure["FixedArray", -1, 12]Get the contents of the array:
ds["Elements"]Create a "FixedArray" initialized with the contents of an array:
ds = CreateDataStructure["FixedArray", {x, y, z}]Get the contents of the array:
ds["Elements"]Information (1)
A new "FixedArray" can be created with CreateDataStructure:
ds = CreateDataStructure["FixedArray", 8]Information about the data structure ds:
Information[ds]Operations (9)
"Copy" (1)
Create a "FixedArray" with initial values:
ds = CreateDataStructure["FixedArray", {1, 2, 3}]ds1 = ds["Copy"]The contents of both arrays are the same:
{Normal[ds], Normal[ds1]}Modifying the copy will not affect the original array:
ds1["SetPart", 2, 42];
{Normal[ds], Normal[ds1]}"Elements" (1)
Create a "FixedArray" with initial values:
ds = CreateDataStructure["FixedArray", Range[10]];
ds["Visualization"]ds["Elements"]Normal returns the same list:
Normal[ds]"EmptyQ" (1)
Test whether a "FixedArray" is empty or not:
ds = CreateDataStructure["FixedArray", 0];
ds["EmptyQ"]"Fold" (1)
Create a "FixedArray" with initial values:
ds = CreateDataStructure["FixedArray", Range[10]]Combine all elements of the array using Plus, producing their total sum:
ds["Fold", Plus]You can also specify an initial value to start the accumulation:
ds["Fold", Plus, 42]"Length" (1)
Create a "FixedArray" with initial values:
ds = CreateDataStructure["FixedArray", Range[5]]Get the number of elements in the array:
ds["Length"]Length gives the same value:
Length[ds]"Part" (1)
Create a "FixedArray" with initial values:
ds = CreateDataStructure["FixedArray", Range[10]]Get the fifth element of the array:
ds["Part", 5]"SetPart" (1)
Create a "FixedArray" with initial values:
ds = CreateDataStructure["FixedArray", Range[10]]Set the fifth element of the array to a different value:
ds["SetPart", 5, x]ds["Visualization"]The following syntax is also valid:
ds["Part", 6] = y;
ds["Visualization"]"SwapPart" (1)
Create a "FixedArray" with initial values:
ds = CreateDataStructure["FixedArray", Range[10]]Swap the values of the elements in the fifth and sixth positions:
ds["SwapPart", 5, 6]ds["Visualization"]"Visualization" (1)
Create a "FixedArray" with initial values:
ds = CreateDataStructure["FixedArray", Range[10]]Visualize the contents of the array:
ds["Visualization"]Applications (1)
Sliding Window Computation (1)
Use a "FixedArray" to maintain a sliding window over streaming data:
buffer = CreateDataStructure["FixedArray", ConstantArray[0, 5]]Insert new elements by shifting and overwriting:
push[array : DataStructure["FixedArray", _], x_] :=
With[{len = Length[array]},
Do[array["SetPart", i, array["Part", i + 1]], {i, 1, len - 1}];
array["SetPart", len, x]
]Scan[push[buffer, #]&, Range[8]];
buffer["Elements"]Properties & Relations (5)
InputForm (1)
InputForm returns the serialized contents of the "FixedArray":
InputForm[CreateDataStructure["FixedArray", Range[10]]]This serialized form can be used to recreate the data structure:
DataStructure["FixedArray", {"Data" -> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}]Length (1)
Length can be used to get the number of elements in a "FixedArray":
ds = CreateDataStructure["FixedArray", Range[10]];
Length[ds]The same can be achieved with the "Length" operation:
ds["Length"]Normal (1)
Normal can be used to get the elements of a "FixedArray":
ds = CreateDataStructure["FixedArray", Range[10]];
Normal[ds]The same can be achieved with the "Elements" operation:
ds["Elements"]SameQ (1)
SameQ can be used to test whether two arrays contain identical elements in the same order:
ds1 = CreateDataStructure["FixedArray", Range[10]];
ds2 = CreateDataStructure["FixedArray", Range[10]];
ds1 === ds2If one element is changed, the arrays are no longer the same:
ds1["SetPart", 5, 42];
ds1 === ds2"DynamicArray" (1)
Many algorithms that work for "DynamicArray" also work for "FixedArray".
Unlike "DynamicArray", the size of a "FixedArray" cannot be modified.
Possible Issues (2)
The number of elements of the array needs to be specified as a positive integer:
ds = CreateDataStructure["FixedArray", 1.2]You cannot set parts of the array that do not exist:
ds = CreateDataStructure["FixedArray", 16]ds["SetPart", 17, "test"]Interactive Examples (1)
Compute and store individual plots in a data structure and use Manipulate to interactively display them:
ds = CreateDataStructure["FixedArray", 16]Do[ds["SetPart", i, Plot[Sin[i x] / x, {x, 0, 10}, Filling -> Axis, PlotRange -> 1]], {i, ds["Length"]}]Manipulate[ds["Part", i], {i, 1, ds["Length"], 1}]Neat Examples (2)
Fibonacci (1)
Compute the first 16 elements of the Fibonacci sequence:
ds = CreateDataStructure["FixedArray", 16]Set the first two elements to 1, then compute the remaining elements:
ds["SetPart", 1, 1];
ds["SetPart", 2, 1];
Do[ds["SetPart", i, ds["Part", i - 2] + ds["Part", i - 1]], {i, 3, ds["Length"]}]ds["Visualization"]Histogram (1)
A function that computes a histogram of the different color values of an image:
redBin = CreateDataStructure["FixedArray", ConstantArray[0, 256]];
greenBin = CreateDataStructure["FixedArray", ConstantArray[0, 256]];
blueBin = CreateDataStructure["FixedArray", ConstantArray[0, 256]];
indexOf[x_] := Floor[255 x] + 1
computeHistogram[{r_, g_, b_}] :=
Module[{},
redBin["SetPart", indexOf[r], redBin["Part", indexOf[r]] + 1];
greenBin["SetPart", indexOf[b], greenBin["Part", indexOf[b]] + 1];
blueBin["SetPart", indexOf[g], blueBin["Part", indexOf[g]] + 1];
]Run the function on the pixels of an image:
ImageScan[computeHistogram, [image]]BarChart[Transpose[Normal /@ {redBin, greenBin, blueBin}], PlotRange -> All, ChartStyle -> {Red, Green, Blue}, BarSpacing -> None]See Also
CreateDataStructure DataStructure
Data Structures: ExtensibleVector DynamicArray LinkedList DoublyLinkedList RingBuffer
Related Guides
History
Introduced in 2020 (12.1)