DataType:setiterator

From the wonderful RedGuides Wiki

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.


This Data Type is referenced in MQ2Collections, and accessed by Top-Level Object(s): setiterator
setiterator is used as a return type by these members:  [ Toggle ]

Page Member Description
set Find[string] 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.
First 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.

Members

Type Member Description
bool Advance 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 True if the iterator is at the end of the set.
Reset Positions the iterator to the start of the set. True is always returned.
string Value Returns the element of the set under the iterator.

Examples

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.


See also