Skip to content

Set: a collection of unique, unordered values

TypePun edited this page Oct 10, 2019 · 9 revisions

Set

A set is a collection of unordered unique values. In practice, the values in this set are ordered lexicographically.

Adding an item that is already in a set does not alter the set.

First and Find return iterators that are owned by the set. Multiple calls to First and/or Find will each return a new iterator that invalidates the previous iterator. If you want to maintain multiple iterators over a set you must call the Clone method on the returned iterator. See: setiterator, Clone method, for details.


Declaring a set

/declare s set

The type name is case sensitive. That is, set will work, but Set, SET or any other permutation will not create the type.

Methods

Name Arguments Returns Comments
Count None Integer Number of items added to the set.
Clear None Bool Removes all items from the set. True is always returned.
Contains String Bool True is returned if the item is in the set.
Add Sequence Bool True is returned if the item(s) were added to the set.
Remove String Bool True is returned if the item was removed from the set.
First None setiterator A setiterator is returned on the set where the current element under the iterator is the first element in the set if the set has elements or an empty iterator if the set is empty.
Find String setiterator A setiterator is returned on the set where the current element under the iterator is the item if the item is in the set and an empty iterator if it is not.

Example usage

Sub SetTest
    /declare s set

    /echo 'Starting Set Test'
    /echo 'Count of entries in Set: ${s.Count}'

    | Add entries to the set.
    /echo 'Adding items to the set.

    /if (!${s.Add[A]}) {
        /echo 'Set Add of A failed.'
        /endmacro
    }
    /if (!${s.Add[B]}) {
        /echo 'Set Add of B failed.'
        /endmacro
    }
    /if (!${s.Add[C,D,E]}) {
        /echo 'Set Add of C,D,E failed.'    
        /endmacro
    }

    /declare count int
    /varset count ${s.Count}
    /if (${count} != 5) {
        /echo 'Set count is: ${count} and should be 5.'
        /endmacro
    }

    | Now remove A and E.
    /if (!${s.Remove[A]}) {
        /echo 'Remove of A failed.'
        /endmacro
    }
    /if (!${s.Remove[E]}) {
        /echo 'Remove of E failed.'
        /endmacro
    }
    
    /varset count ${s.Count}
    /if (${count} != 3) {
        /echo 'Set count is: ${count} and should be 3.'
        /endmacro
    }

    | Verify that the set contains B, C and D.
    /if (${s.Contains[B]}) {
        /echo 'Set Contains B.'
    } else {
        /echo 'Set does not contain B.'
        /endmacro
    }
    /if (${s.Contains[C]}) {
        /echo 'Set Contains C.'
    } else {
        /echo 'Set does not contain C.'
        /endmacro
    }
    /if (${s.Contains[D]}) {
        /echo 'Set Contains D.'
    } else {
        /echo 'Set does not contain D.'
        /endmacro
    }

    | And verify it does NOT contain A and E.
    /if (${s.Contains[A]}) {
        /echo 'Set should not contain A!'
        /endmacro
    } else {
        /echo 'Set does not contain A.'
    }
    /if (${s.Contains[E]}) {
        /echo 'Set should not contain E!'
        /endmacro
    } else {
        /echo 'Set does not contain E.'
    }

    | Try and Add an element more than once.    
    /if (!${s.Add[B]}) {
        /echo 'Set Add of B failed.'
        /endmacro
    }

    | Adding the same item should not modify the set.
    /varset count ${s.Count}
    /if (${count} != 3) {
        /echo 'Set count is: ${count} and should be 3.'
        /endmacro
    }
    
    | Clear the set. The count should be zero and it
    | should not contain any elements.
    /echo 'Set Clear: ${s.Clear}.'
    /echo 'Set Count: ${s.Count}.'
    
    | And verify it does NOT contain B.
    /if (${s.Contains[B]}) {
        /echo 'Set should not contain B!'
        /endmacro
    } else {
        /echo 'Set does not contain B.'
    }
    
    /echo 'Ending Set Test'
    
    /return

Notes

None.

Clone this wiki locally