"ExtensibleVector" (Data Structure)
"ExtensibleVector"
represents a dynamically extensible vector where the elements are general expressions.
Details


- An extensible vector is useful for continuously appending and prepending elements, as well as for efficient element extraction and updating.
-
CreateDataStructure[ "ExtensibleVector"] create a new empty "ExtensibleVector" CreateDataStructure[ "ExtensibleVector",elems] create a new "ExtensibleVector"containing elems Typed[x,"ExtensibleVector"] give x the type "ExtensibleVector" - For a data structure of type "ExtensibleVector", the following operations can be used:
-
ds["Append",x] append x to ds time: O(1) ds["Copy"] return a copy of ds time: O(n) ds["Drop",i] drop the i part of ds
time: O(n) ds["DropAll"] drop all the elements from ds time: O(n) ds["DropLast"] drop the last element of ds time: O(1) 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["Insert",x,i ] insert x into ds at position i time: O(1) ds["JoinBack",elems ] join elems to the back of ds time: O(nelems) ds["JoinFront",elems ] join elems to the front of ds time: O(nelems) ds["Length",x] the number of elements stored in ds time: O(1) ds["Part",i] give the i part of ds
time: O(1) ds["Prepend",x] prepend x to ds time: O(1) ds["SetPart",i,elem] update the i part of ds
time: O(1) ds["SwapPart",i,j] swap the i and j
parts of ds
time: 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 val
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)Summary of the most common use cases
A new "ExtensibleVector" can be created with CreateDataStructure:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-ck3oom


https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-84r1md


https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-k7y0gt


https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-tnm4iz


https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-kfcutb


https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-2ymbbr


https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-3dkxgf

Return an expression version of ds:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-gzceda


https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-mfljzp
A visualization of the data structure can be generated:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-uvwa1b


https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-volwmf

Scope (1)Survey of the scope of standard use cases
Information (1)
A new "ExtensibleVector" can be created with CreateDataStructure:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-olwaxi

Information about the data structure ds:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-5du3om

Applications (11)Sample problems that can be solved with this function
Reverse (1)
A simple function that carries out an in-place reverse of a "ExtensibleVector":

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-k1geif
A sample data structure to use:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-ok3cxr

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-v6s7yl

Bubble Sort (1)
Bubble sort is a simple sorting algorithm that is easy to code but typically has poor performance:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-qfxr7a
A sample data structure to use:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-6iij4g

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-ch3pow

The data structure is sorted in place:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-jwd12t

Insertion Sort (1)
Insertion sort is a simple sorting algorithm that is easy to code and typically has poor performance for large arrays, but can be useful for small arrays:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-uuea2a
A sample data structure to use:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-rnvca7

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-qbnzb1

The data structure is sorted in place:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-f26i23

Quicksort (1)
Quicksort is an efficient sorting algorithm. This basic implementation uses nested functions:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-0b81qa
A sample data structure to use:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-ys4psf

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-2jbmri

The data structure is sorted in place:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-pd9hqf

Merge Sort (1)
Merge sort is an efficient sorting algorithm. This is a simple implementation that uses a workspace:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-8w3b15

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-gks9ef

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-8uh2mt
A sample data structure to use:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-jfrp9i

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-q8dqik

The data structure is sorted in place:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-f2jg3j

Heapsort (1)
Heapsort is an efficient sorting algorithm that works in place by constructing a heap:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-lbn5i7

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-wkx57e
A sample data structure to use:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-0qbb4d
The elements are sorted in place:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-dcq0in

Timsort (1)
Timsort is a hybrid and efficient sorting algorithm:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-utwzvk

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-xxk8ku

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-8u8nt2
A sample data structure to use:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-3867c2
The elements are sorted in place:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-e3olpb

Fisher–Yates Shuffle (1)
The Fisher–Yates shuffle generates a random permutation of a finite sequence:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-ypbhro
A sample array to use for the shuffle:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-fsx11v

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-p7mn4c

Palindrome (1)
Quickselect (1)
Quickselect is a selection algorithm related to the Quicksort algorithm. It returns the k smallest element in a list:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-oem0o9

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-owe0hl

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-fn1ton
A sample data structure to use:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-wbvmlh
The third smallest element is 7:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-kqha7h

Heap (1)
A heap is a tree-based data structure where the value stored at children is smaller than that stored in their parents (for a min heap). It is commonly used to implement a priority queue and typically implemented with an array rather than a tree.
The following converts an array into a heap:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-qkp2f3

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-6gbdr5

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-xf77l5

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-o02ioi

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-5jwtn1
A sample data structure to use:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-xehzyv
This converts the array into a heap; it is hard to see the heap property:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-pp3guy

This function generates a tree:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-854ywl
A visualization makes it easier to see the min heap property:

https://wolfram.com/xid/0l9u54bv5xcg6m6wyodxn26i-86qyu2

Properties & Relations (2)Properties of the function, and connections to other functions
"FixedArray" (1)
Many algorithms that work for "ExtensibleVector" also work for "FixedArray".
"DynamicArray" (1)
Many algorithms that work for "ExtensibleVector" also work for "DynamicArray".