"OrderedHashSet" (Data Structure)
"OrderedHashSet"
represents a set where the members are general expressions, membership is computed by using a hash function and the order in which members are inserted is preserved.
Details


- An ordered hash set is useful for efficient insertion and removal as well as membership testing where it is important to preserve the order of insertion:
-
CreateDataStructure["OrderedHashSet"] create a new empty "OrderedHashSet" CreateDataStructure["OrderedHashSet",elems] create a new "OrderedHashSet" containing elems Typed[x,"OrderedHashSet"] give x the type "OrderedHashSet" - For a data structure of type "OrderedHashSet", the following operations can be used:
-
ds["Complement",list] remove elements from ds that appear in list time: O(n) 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["Delete",x] delete x from ds; return True if x was actually an element time: O(1) ds["DeleteAll"] delete all the elements from ds time: O(n) ds["Insert",x] add x to the set and return True if addition succeeded time: O(1) ds["Intersection",list] remove elements from ds that do not appear in list time: O(n) ds["Length"] returns the number of elements stored in ds time: O(1) ds["MemberQ",x] True, if x is an element of ds time: O(1) ds["Pop"] remove an element from ds and return it time: O(1) ds["Union",list] add elements to ds that appear in list time: O(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)Summary of the most common use cases
A new "OrderedHashSet" can be created with CreateDataStructure:

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


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

There is one element in the set:

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

Test if an expression is stored:

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

If an expression is not stored, False is returned:

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

Remove an element from the set. If something was actually removed, return True:

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

Return an expression version of ds:

https://wolfram.com/xid/0ipeb3mtlox8s5rpy4mbnkwyhib-dbekgb

It is fast to insert elements:

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

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

The order in which elements are inserted is preserved:

https://wolfram.com/xid/0ipeb3mtlox8s5rpy4mbnkwyhib-53ezb4

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

https://wolfram.com/xid/0ipeb3mtlox8s5rpy4mbnkwyhib-wn7q0m

Information about the data structure ds:

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

Set Operations (1)
"OrderedHashSet" supports some core set operations. A new empty "OrderedHashSet" can be created with CreateDataStructure:

https://wolfram.com/xid/0ipeb3mtlox8s5rpy4mbnkwyhib-ubf2je


https://wolfram.com/xid/0ipeb3mtlox8s5rpy4mbnkwyhib-vyu2r7
The "Union" operation adds elements that were not originally in the data structure:

https://wolfram.com/xid/0ipeb3mtlox8s5rpy4mbnkwyhib-ma3q43

The "Complement" operation removes elements from the data structure:

https://wolfram.com/xid/0ipeb3mtlox8s5rpy4mbnkwyhib-gtty7m

The "Intersection" operation leaves common elements in the data structure:

https://wolfram.com/xid/0ipeb3mtlox8s5rpy4mbnkwyhib-5omvlp
