WOLFRAM

"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 elemtime: O(log n)
    ds["Count", elem]returns the number of elements that match elemtime: O(log n)
    ds["Delete",x]delete one instance of x from ds; return True if x was actually an elementtime: O(log n)
    ds["DeleteAll"]delete all the elements from dstime: O(n)
    ds["DeleteCases",x]delete all instances of x from ds; return True if x was actually an elementtime: O(log n)
    ds["Elements"]return a list of the elements of dstime: O(n)
    ds["EmptyQ"]True, if ds has no memberstime: O(1)
    ds["Insert",x]add x to the multisettime: O(log n)
    ds["Length"]returns the number of members stored in dstime: O(1)
    ds["MemberQ",x]True, if x is a member of dstime: O(log n)
    ds["Visualization"]return a visualization of dstime: O(n)
  • The following functions are also supported:
  • dsi===dsjTrue, 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 all

Basic Examples  (3)Summary of the most common use cases

A new "HashSet" can be created with CreateDataStructure:

Out[1]=1

Insert into the set:

There is one element in the set:

Out[3]=3

Test if an expression is stored:

Out[4]=4

If an expression is not stored, False is returned:

Out[5]=5

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

Now there are two elements in the set:

Out[7]=7

Return an expression version of ds:

Out[8]=8

Insert a number of elements:

A visualization of the data structure can be generated:

Out[2]=2

This shows how the data is sorted and contains multiple elements.

An ordering function can be given for a sorted multiset:

Insert two elements:

These elements are all equal according to the ordering function:

Out[3]=3

Remove an element:

Out[4]=4
Out[5]=5

Scope  (1)Survey of the scope of standard use cases

Information  (1)

A new "SortedMultiset" can be created with CreateDataStructure:

Out[1]=1

Information about the data structure ds:

Out[2]=2

Possible Issues  (1)Common pitfalls and unexpected behavior

Set Membership  (1)

If an ordering function is given, this is used to specify set membership:

This returns True because the ordering function only looks at the first element:

Out[2]=2