"ImmutableVector" (Data Structure)
"ImmutableVector"
represents an immutable vector where modifications generate a new data structure and the elements are general expressions.
Details
- An immutable vector is useful for efficiently adding and removing elements without modifying the data structure in place.
- An immutable vector uses structural sharing, which results in performance-bound guarantees.
-
CreateDataStructure["ImmutableVector"] create a new empty "ImmutableVector" CreateDataStructure["ImmutableVector",elems] create a new "ImmutableVector" containing elems Typed[x,"ImmutableVector"] give x the type "ImmutableVector" - For a data structure of type "ImmutableVector", the following operations can be used:
-
ds["Append",x] return a new immutable vector with x appended to ds time: O(log n) ds["DropLast"] return a new immutable vector with the last element of ds dropped time: O(log 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["JoinBack",elems] join elems to the back of ds time: O(nelems) ds["Length"] the number of elements stored in ds time: O(1) ds["Part",i] give the i
part of dstime: O(log n) ds["ReplacePart",ix] return a new immutable vector with the i
part replaced by xtime: O(log n) ds["Visualization"] return a visualization of ds time: O(n) - The following functions are also supported:
-
dsi===dsj True, if dsi equals dsj FullForm[ds] full form of ds Information[ds] information about ds InputForm[ds] input form of ds Normal[ds] convert ds to a normal expression
Examples
open all close allBasic Examples (2)
A new "ImmutableVector" can be created with CreateDataStructure:
ds = CreateDataStructure["ImmutableVector"]ds1 = ds["Append", f[1]]ds1["Length"]The length of the original vector did not change:
ds["Length"]ds1["Part", 1]ds2 = ds1["ReplacePart", 1 -> g[1]]ds1["Part", 1]ds2["Part", 1]Return expression version of ds:
Normal[ds2]It is efficient to append elements:
ds = CreateDataStructure["ImmutableVector"];
Do[ds = ds["Append", i], {i, 1, 1000}]A visualization of the data structure can be generated:
ds["Visualization"]ds["Fold", Plus, 0]Scope (2)
Efficiency (1)
"ImmutableVector" is efficient at appending elements:
ds = CreateDataStructure["ImmutableVector"];
Do[ds = ds["Append", i], {i, 1, 50000}]//AbsoluteTimingRegular list expressions are not as fast for appending:
list = {};
Do[list = Append[list, i], {i, 1, 50000}]//AbsoluteTimingAssociation is also fast for appending, but gives a key rather than an index view into the data:
assoc = <||>;
Do[assoc = Append[assoc, i -> i], {i, 1, 50000}]//AbsoluteTimingInformation (1)
A new "ImmutableVector" can be created with CreateDataStructure:
ds = CreateDataStructure["ImmutableVector"]Information about the data structure ds:
Information[ds]See Also
CreateDataStructure DataStructure
Data Structures: FixedArray LinkedList DoublyLinkedList RingBuffer
Related Guides
History
Introduced in 2021 (12.3)