Skip to content

SetIterator: a forward iterator over sets

TypePun edited this page Feb 1, 2019 · 5 revisions

SetIterator

A setiterator implements a forward iterator over the set type. A forward iterator is an iterator that can only be incremented. Invoking Advance on the iterator will position the iterator on the next element with a value lexicographically greater than the current element. If there is no next element, the iterator will be positioned on the end of the set and IsEnd will be true.


Declaring a setiterator

/declare si setiterator

The type name is case sensitive. That is, setiterator will work, but SetIterator, SETITERATOR or any other permutation will not create the type.

Acquiring a setiterator

setiterators are reference types and are initialized by invoking either the First or Find methods on a set.

Methods

Name Arguments Returns Comments
Reset None Bool Positions the iterator to the start of the set. True is always returned.
Advance None Bool The iterator is moved to the next item in the set, if one exists. True is returned if the iterator was advanced and False otherwise.
IsEnd None Bool True if the iterator is at the end of the set.
Value None String Returns the element of the set under the iterator.

Example usage

Sub SetIteratorTest
    /declare s set

    /echo 'Starting Set Iterator 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]}) {
        /echo 'Set Add of C failed.'    
        /endmacro
    }
    /if (!${s.Add[D]}) {
        /echo 'Set Add of D failed.'
        /endmacro
    }
    /if (!${s.Add[E]}) {
        /echo 'Set Add of E failed.'    
        /endmacro
    }

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

    /echo 'Acquire an iterator to the start of the set.'

    | Get an iterator to the first element and output each
    | element in the set.
    /declare si setiterator
    /vardata si s.First

    /while (!${si.IsEnd}) {
        /echo ${si.Value}
        /if (${si.Advance}) {
            /echo 'Iterator advanced to next element.'
        } else {
            /echo 'Iterator not advanced. IsEnd: ${si.IsEnd}.'
            /endmacro
        }
    }

    | Test Reset and do it again.
    /echo 'Testing Reset.'

    /if (${si.Reset}) {
        /echo 'Iterator Reset.'
    } else {
        /echo 'Iterator could not be reset. IsEnd: ${si.IsEnd}.'
    }

    /while (!${si.IsEnd}) {
        /echo ${si.Value}
        /if (${si.Advance}) {
            /echo 'Iterator advanced to next element.'
        } else {
            /echo 'Iterator not advanced. IsEnd: ${si.IsEnd}.'
            /endmacro
        }
    }

    /echo 'Calling Find[C] on the set.'

    | Acquire an iterator using Find to C.
    /vardata si s.Find[C]

    /while (!${si.IsEnd}) {
        /echo ${si.Value}
        /if (${si.Advance}) {
            /echo 'Iterator advanced to next element.'
        } else {
            /echo 'Iterator not advanced. IsEnd: ${si.IsEnd}.'
            /endmacro
        }
    }

    /echo 'Calling Find[Z] on the set.'

    | Acquire an iterator using Find to Z.
    /vardata si s.Find[Z]

    /if (${si.IsEnd}) {
        /echo 'IsEnd for Find[Z]: ${si.IsEnd}.'
    } else {
        /echo 'IsEnd is FALSE for Find[Z].'
        /endmacro
    }

    /echo 'Ending Set Iterator Test'
    
    /return

Notes

  • If Advance returns False, IsEnd will be True.
  • If IsEnd is True, then Value is undefined.
Clone this wiki locally