"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 Length[ds] the length of ds Normal[ds] convert ds to a normal expression
Examples
open all close allBasic Examples (2)
A new "OrderedHashSet" can be created with CreateDataStructure:
ds = CreateDataStructure["OrderedHashSet"]Insert an element into the set:
ds["Insert", f[1]]There is one element in the set:
ds["Length"]Test if an expression is stored:
ds["MemberQ", f[1]]If an expression is not stored, False is returned:
ds["MemberQ", f[2]]Remove an element from the set, returning True if the element was successfully removed:
ds["Delete", f[1]]Return an expression version of ds:
Normal[ds]It is fast to insert elements:
ds = CreateDataStructure["OrderedHashSet"];
Do[ds["Insert", i], {i, 100}]A visualization of the data structure can be generated:
ds["Visualization"]The order in which elements are inserted is preserved:
ds["Elements"]Scope (16)
Creation (2)
Create an empty "OrderedHashSet":
CreateDataStructure["OrderedHashSet"]Create a "OrderedHashSet" with initial elements:
CreateDataStructure["OrderedHashSet", {x, y, z}]Information (1)
A new "OrderedHashSet" can be created with CreateDataStructure:
ds = CreateDataStructure["OrderedHashSet"]Information about the data structure ds:
Information[ds]Operations (13)
"Complement" (1)
Create a "OrderedHashSet" with initial elements:
ds = CreateDataStructure["OrderedHashSet", Range[100]]Create a new set containing elements that are members of the original "OrderedHashSet" but not members of the given list:
ds1 = ds["Complement", Range[50]]Verify the elements of the new ordered hash set:
ds1["Elements"]"Copy" (1)
Create a "OrderedHashSet" with initial elements:
ds = CreateDataStructure["OrderedHashSet", Range[4]]ds1 = ds["Copy"]The contents of both arrays are the same:
{Normal[ds], Normal[ds1]}Modifying the copy will not affect the original set:
ds1["Insert", 42];
{Normal[ds], Normal[ds1]}"Elements" (1)
Create a "OrderedHashSet" with initial elements:
ds = CreateDataStructure["OrderedHashSet", Range[10]]ds["Elements"]Normal returns the same list:
Normal[ds]"EmptyQ" (1)
Test whether a "OrderedHashSet" is empty or not:
ds = CreateDataStructure["OrderedHashSet"];
ds["EmptyQ"]After inserting an element, the set is no longer empty:
ds["Insert", f[x]];
ds["EmptyQ"]"Delete" (1)
Delete an element from a "OrderedHashSet", returning True if deletion succeeded:
ds = CreateDataStructure["OrderedHashSet", {f[x], f[y], f[z]}];
ds["Delete", f[x]]Deleting an element not present in the set is not possible, so False is returned:
ds["Delete", f[w]]"DeleteAll" (1)
Delete all elements in a "OrderedHashSet":
ds = CreateDataStructure["OrderedHashSet", Range[300]];
ds["DeleteAll"]ds["EmptyQ"]"Insert" (1)
Insert an element into a "OrderedHashSet", returning True if the insertion succeeded:
ds = CreateDataStructure["OrderedHashSet"];
ds["Insert", f[x]]Inserting the same element again is not possible, so False is returned:
ds["Insert", f[x]]"Intersection" (1)
Create a "OrderedHashSet" with initial elements:
ds = CreateDataStructure["OrderedHashSet", Range[100]]Create a new set containing only those elements common to both the original "OrderedHashSet" and a given list:
ds1 = ds["Intersection", Range[50]]Verify the elements of the new ordered hash set:
ds1["Elements"]"Length" (1)
Create a "OrderedHashSet" with initial elements:
ds = CreateDataStructure["OrderedHashSet", Range[100]]Get the number of elements in the set:
ds["Length"]Length gives the same value:
Length[ds]"MemberQ" (1)
Test whether an element is present in a "OrderedHashSet":
ds = CreateDataStructure["OrderedHashSet", {f[x], f[y], f[z]}]{ds["MemberQ", f[x]], ds["MemberQ", f[w]]}"Pop" (1)
Create a "OrderedHashSet" with initial elements:
ds = CreateDataStructure["OrderedHashSet", {f[x], f[y], f[z]}];
ds["Elements"]ds["Pop"]"Union" (1)
Create a "OrderedHashSet" with initial elements:
ds = CreateDataStructure["OrderedHashSet", Range[10]]Create a new set containing elements present both in the original "OrderedHashSet" and a given list:
ds1 = ds["Union", Range[10, 20]]Verify the elements of the new hash set:
ds1["Elements"]"Visualization" (1)
Create a "OrderedHashSet" with initial elements:
ds = CreateDataStructure["OrderedHashSet", Range[300]]Visualize the contents of the hash set:
ds["Visualization"]Applications (2)
Stable Deduplication (1)
Create a list of random elements:
list = RandomChoice[{"A", "B", "C", "D"}, 20]Remove duplicate elements from the list while preserving the order of first occurrence:
ds = CreateDataStructure["OrderedHashSet"];
ds["Union", list];ds["Elements"]Compare with DeleteDuplicates:
DeleteDuplicates[list]Properties & Relations (5)
InputForm (1)
InputForm returns the serialized contents of the "OrderedHashSet":
InputForm[CreateDataStructure["OrderedHashSet", Range[10]]]This serialized form can be used to recreate the data structure:
DataStructure["OrderedHashSet", {"Data" -> Range[10]}]Length (1)
Length can be used to get the number of elements in a "OrderedHashSet":
ds = CreateDataStructure["OrderedHashSet", 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 "OrderedHashSet":
ds = CreateDataStructure["OrderedHashSet", 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 hash sets contain identical elements in the same order:
ds1 = CreateDataStructure["OrderedHashSet", Range[10]];
ds2 = CreateDataStructure["OrderedHashSet", Range[10]];
ds1 === ds2"HashSet" (1)
Many algorithms that work for "HashSet" also work for "OrderedHashSet".
Unlike "HashSet", the order in which elements are inserted into an "OrderedHashSet" is preserved.
Neat Examples (1)
Create an ordered set of divisors of the first integer:
ds = CreateDataStructure["OrderedHashSet", Divisors[45]]Find the common divisors of two integers by intersecting with the divisors of the second:
ds["Intersection", Divisors[78]]//NormalExtract the greatest common divisor as the last element:
Last[%]Verify the result using the built-in function:
GCD[45, 78]See Also
Association Dispatch Hash DownValues CreateDataStructure DataStructure
Data Structures: HashSet HashTable OrderedHashTable DisjointSet SortedMultiset
Related Guides
History
Introduced in 2020 (12.1)