"SortedMultiset" (Data Structure)
"SortedMultiset"
represents a multiset where the members are general expressions and are kept in a sorted order.
Details


- A sorted multiset is useful for efficient insertion and removal as well as membership testing where the same element can appear more than once:
-
CreateDataStructure["SortedMultiset"] create a new empty "SortedMultiset" that uses canonical ordering CreateDataStructure["SortedMultiset",elems] create a new "SortedMultiset" with elements elems CreateDataStructure["SortedMultiset",elems,p] create a new "SortedMultiset" with specified elements and ordering function p Typed[x,"SortedMultiset"] give x the type "SortedMultiset" - For a data structure of type "SortedMultiset", the following operations can be used:
-
ds["Cases",elem] returns members of the multiset that match elem time: O(log n) ds["Count", elem] returns the number of elements that match elem time: O(log n) ds["Delete",x] delete one instance of x from ds; return True if x was actually an element time: O(log n) ds["DeleteAll"] delete all the elements from ds time: O(n) ds["DeleteCases",x] delete all instances of x from ds; return True if x was actually an element time: O(log n) ds["Elements"] return a list of the elements of ds time: O(n) ds["EmptyQ"] True, if ds has no members time: O(1) ds["Insert",x] add x to the multiset time: O(log n) ds["Length"] returns the number of members stored in ds time: O(1) ds["MemberQ",x] True, if x is a member of ds 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 (3)Summary of the most common use cases
A new "HashSet" can be created with CreateDataStructure:

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


https://wolfram.com/xid/0o85higkmkcvnt89474kc3fc8o76-84r1md
There is one element in the set:

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

Test if an expression is stored:

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

If an expression is not stored, False is returned:

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

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

https://wolfram.com/xid/0o85higkmkcvnt89474kc3fc8o76-3dkxgf
Now there are two elements in the set:

https://wolfram.com/xid/0o85higkmkcvnt89474kc3fc8o76-l1py1j

Return an expression version of ds:

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


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

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

This shows how the data is sorted and contains multiple elements.
An ordering function can be given for a sorted multiset:

https://wolfram.com/xid/0o85higkmkcvnt89474kc3fc8o76-tm669

https://wolfram.com/xid/0o85higkmkcvnt89474kc3fc8o76-46x6du
These elements are all equal according to the ordering function:

https://wolfram.com/xid/0o85higkmkcvnt89474kc3fc8o76-yo6xa5


https://wolfram.com/xid/0o85higkmkcvnt89474kc3fc8o76-vheel1


https://wolfram.com/xid/0o85higkmkcvnt89474kc3fc8o76-n32j59

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

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

Information about the data structure ds:

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

Possible Issues (1)Common pitfalls and unexpected behavior
Set Membership (1)
If an ordering function is given, this is used to specify set membership:

https://wolfram.com/xid/0o85higkmkcvnt89474kc3fc8o76-kcurc1
This returns True because the ordering function only looks at the first element:

https://wolfram.com/xid/0o85higkmkcvnt89474kc3fc8o76-b6b4af
