"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,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 ds time: O(log n) ds["ReplacePart",i,x] return a new immutable vector with the i part replaced by x time: 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 allclose allBasic Examples (2)
A new "ImmutableVector" can be created with CreateDataStructure:
The length of the original vector did not change:
Return expression version of ds:
It is efficient to append elements:
Scope (2)
Efficiency (1)
"ImmutableVector" is efficient at appending elements:
Regular list expressions are not as fast for appending:
Association is also fast for appending, but gives a key rather than an index view into the data:
Information (1)
A new "ImmutableVector" can be created with CreateDataStructure: