The DejaVU Framework -- hush 3.1
[.] Papers Tutorials Examples Manuals Interfaces Sources Packages Resources ?

Manual: ITER 4 1996


[.] [man] man1 man2 man3 man4 man5 man6 man7 man8 man9 manl mann ?

NAME

iter -- an abstract iteration class

SYNOPSIS


An iterator provides a convenient way to traverse a container of arbitrary type in a uniform fashion.


slide: SYNOPSIS

INTERFACE


    template<class T>
    class iter {             
<hush/iter.h> <hush/iter.idl>

public: virtual T* operator()(); // delivers (pointer to) element protected: iter(iter<T>*); private: iter<T>* p; // holds real iterator };

slide: INTERFACE

DESCRIPTION


The abstract iter class provides the interface (and delegation mechanism) for concrete iteration classes. By convention, in hush, iterators are obtained from the container by assignment, that is by type coercion. Iterators are provided for dictionaries (dictionary(4)) and lists (list(4)). However, one may also obtain an iterator from strings (see string(4)). Some examples are given below. Beware that the iterator delivers pointers to objects and not references. By (yet another) convention, the template parameter indicates only the type of the object, whereas a pointer to that type is actually delivered.


slide: DESCRIPTION

EXAMPLES



slide: EXAMPLES

List Processing


In the example below an iterator is used to process the elements of a list. See list(4).

    list<int> l;
    int i1 = 1; int i2 = 2; int i3 = 3;
     
    l << i1 << &i2 << &i3; // insert elements
     
    int sum = 0;
  
    iter<int>& it = l;  // convert to iter by assignment
    int* pn = 0;       // declare pointer to element
  
    while ( pn = it() ) {
            sum += *pn;
            }
    cout << "TOTAL: " << sum << endl; 
  
Notice that first an iterator is obtained from the list by assignment and that a pointer variable is used to store the result of evaluating the iterator.
slide: List Processing

Dictionary


In the example below an iterator is obtained from a dictionary (an associative array, see dictionary(4)) to process the stored entries.

    class app { //
    public:
            int value();
    };
    
    int main() {
    
    app i1;
    app i2;
    
    dictionary<app> d;
    
    d["app1"] = &i1;
    d["app2"] = &i2;
    
    app* sp = d["app1"]; // simple retrieval
    
    iter<string>& it = d; // iterator by assignment
    string* k = 0;
    app* ap = 0;
    
    while ( p = it() ) {   // pointer to key
          ap = d[*p];
            cout << ap->value() << endl;
            }
    
    return 0;
    }
  Beware that the iterator delivers pointers to the key.
  To obtain the stored object you must used the (dereferenced) key.
  

slide: Dictionary

String Processing


As an example of an iterator obtained from a string look at the example where a string is split. Each substring may be accessed by iterating over the (split) string.

    string s = "a:b:aa/bb:aaa:bbb://abc";
    s.split(":");
    
    iter<string>& it = s; // iterator by assignment
    string* sp = 0; // pointer to string
    
    while ( sp = it() ) {
            cout << *sp << endl;
            }
  
Iterators may also be used to access the elements from an argc/argv pair. See
string(4).
slide: String Processing

LIBRARY


hush -- file <hush/iter.h>


slide: LIBRARY

SEE ALSO

dictionary(4), list(4), string(4)
[.] Papers Tutorials Examples Manuals Interfaces Sources Packages Resources ?
Hush Online Technology
hush@cs.vu.nl
09/24/99