HashMapIter.h

Classes

ConstHashMapIter -- Step through a const HashMap (full description)
HashMapIter -- Step through a non-const HashMap (full description)

template<class key, class val> class ConstHashMapIter

Interface

Public Members
void toStart()
void operator++()
void operator++(int)
const key &getKey() const
const val &getVal() const
Bool atEnd() const
Bool atStart() const
Bool isValid() const
ConstHashMapIter(const HashMap<key,val> &st)
virtual ConstHashMapIter<key,val> &operator=(const val<key,val> &other)
ConstHashMapIter(const ConstHashMapIter<key,val> &st)
virtual ConstHashMapIter<key,val> &operator=(const ConstHashMapIter<key,val> &other)
ConstHashMapIter() : Container(0), curBucket(0), atEnd_(False)
const val &defaultVal() const
const val &operator()(const key &ky) const
Bool isDefined(const key &ky) const
uInt ndefined() const
const HashMap<key,val> &container() const
virtual ~ConstHashMapIter()
Protected Members
void step()
See Also
Related IO functions

Description

Review Status

Date Reviewed:
yyyy/mm/dd

Synopsis

This class is an iterator, and it used to step through const HashMaps. This is useful when one wishes to find each of the user defined mappings in a particular map.

Example

    #include <casa/Containers/HashMap.h>
    #include <casa/BasicSL/String.h>
    #include <casa/iostream.h>
   
    main() {
      HashMap<String,Int> hash;
    
      hash.define("one",1);
      hash.define("two",2);
      hash.define("three",3);
      hash.define("four",4);
      hash.define("five",5);
      hash.define("six",6);
    
      ConstHashMapIter<String,Int> iter(hash);
      for ( iter.toStart(); ! iter.atEnd(); iter++ )
          cout << iter.getVal() << ": " << iter.getKey() << endl;
    }

Motivation

Sometimes one needs to step through the defined elements of an associative array. The user should be told when iterator does not modify the underlying data structure. The standard C++ const is not sufficient because while the internal state of the iterator changes, the underlying data structure is not modified. For this reason, both const and non-const versions of the iterator are useful.

Member Description

void toStart()

Move the iterator to the start of the Map.

void operator++()
void operator++(int)

Advance to the next element of the Map.

const key &getKey() const
const val &getVal() const

Get the key or value for the current position in the Map.

Bool atEnd() const
Bool atStart() const

Check to see if the iterator position is at the end or beginning of the Map.

Bool isValid() const

Check to see if the iterator is in a valid state.

ConstHashMapIter(const HashMap<key,val> &st)

Constructs a Map iterator from a Map (with reference semantics).

virtual ConstHashMapIter<key,val> &operator=(const val<key,val> &other)

Assign one map iterator to a map (with reference semantics).

ConstHashMapIter(const ConstHashMapIter<key,val> &st)

Constructs a Map iterator from another iterator (with reference semantics).

virtual ConstHashMapIter<key,val> &operator=(const ConstHashMapIter<key,val> &other)

Assign one map iterator to another iterator (with reference semantics).

ConstHashMapIter() : Container(0), curBucket(0), atEnd_(False)

Default constructor creates an invalid Map iterator.

const val &defaultVal() const

Returns the default value for the Map on which this iterator is operating if it is a valid iterator, otherwise it throws an exception.

const val &operator()(const key &ky) const

Allows mapping functions to be performed with the map on which this iterator operates. If this iterator is invalid, then an exception will be thrown.

Bool isDefined(const key &ky) const

Allows one to check to see if a given key is defined in the map which this iterator tracks. If this iterator is invalid, then an exception will be thrown.

uInt ndefined() const

Returns the number of user defined mappings

const HashMap<key,val> &container() const

Returns the container on which this iterator is operating.

virtual ~ConstHashMapIter()

dtor

void step()


template<class key, class val> class HashMapIter : public ConstHashMapIter<key,val>

Interface

Public Members
val &getVal()
virtual const val &getVal() const
val &define(const key &k, const val &v)
void remove(const key &k)
const val &defaultVal() const
val &defaultVal()
void clear()
const val &operator()(const key &ky) const
val &operator()(const key &ky)
HashMapIter(HashMap<key,val> &st) : ConstHashMapIter<key,val>(st)
HashMapIter(const HashMapIter<key,val> &other) : ConstHashMapIter<key,val>(other)
HashMapIter() : ConstHashMapIter<key,val>()
virtual HashMapIter<key,val> &operator=(val<key,val> &other)
virtual HashMapIter<key,val> &operator=(const HashMapIter<key,val> &other)
HashMap<key,val> &container()
const HashMap<key,val> &container() const
~HashMapIter()
Protected Members
ConstHashMapIter<key,val> &operator=(const val<key,val> &)
ConstHashMapIter<key,val> &operator=(const ConstHashMapIter<key,val> &)
See Also
Related IO functions

Description

Review Status

Date Reviewed:
yyyy/mm/dd

Synopsis

This class is an iterator, and it used to step through non-const HashMaps. This is useful when one wishes to find each of the user defined mappings in a particular map.

Example

       #include <aips/Containers/HashMap.h>
       #include <casa/BasicSL/String.h>
    #include <iostream>
      
       main() {
         HashMap<String,Int> hash;
       
         hash.define("one",1);
         hash.define("two",2);
         hash.define("three",3);
         hash.define("four",4);
         hash.define("five",5);
         hash.define("six",6);
       
         HashMapIter<String,Int> iter(hash);
         for ( iter.toStart(); ! iter.atEnd(); iter++ )
             cout << iter.getVal() << ": " << iter.getKey() << endl;
       }
    

Motivation

Same as ConstHashMapIter, but allows for modification of the underlying structure.

Member Description

val &getVal()
virtual const val &getVal() const

Get the key or value for the current position in the Map.

val &define(const key &k, const val &v)
void remove(const key &k)

These functions allow for the definition and removal of key/value relations. The "define(key &, value &)" function defines a key/value relation, and "remove(key &)" function removes a relation if it has been previously defined.

const val &defaultVal() const
val &defaultVal()

This returns the default value for the map that this iterator is tracking. With a non-const iterator the default value can be changed.

void clear()

Clear all of the mappings.

const val &operator()(const key &ky) const
val &operator()(const key &ky)

Allows mapping functions to be performed with the map on which this iterator operates. If this iterator is invalid, then an exception will be thrown. With a non-const operator, the value can be changed.

HashMapIter(HashMap<key,val> &st) : ConstHashMapIter<key,val>(st)

This allows a MapIter to be constructed from a Map. When created the new MapIter maintains a reference to the original Map. If the Map to which this MapIter points is deleted, then the MapIter is marked as invalid.

HashMapIter(const HashMapIter<key,val> &other) : ConstHashMapIter<key,val>(other)

This allows a MapIter to be constructed from another MapIter. When created the new MapIter maintains a reference to the Map which the MapIter parameter tracked. If this Map is deleted, then this MapIter is marked as invalid.

HashMapIter() : ConstHashMapIter<key,val>()

Default constructor creates an invalid Map iterator.

virtual HashMapIter<key,val> &operator=(val<key,val> &other)

This assignment operator allows the Map which this MapIter tracks to be changed. After a call to this operator, the MapIter will track the Map parameter.

virtual HashMapIter<key,val> &operator=(const HashMapIter<key,val> &other)

This assignment operator allows the Map which this MapIter tracks to be changed. After a call to this operator, this MapIter will track the Map which the MapIter parameter tracks, i.e. it will contain a reference to this new Map.

HashMap<key,val> &container()
const HashMap<key,val> &container() const

Returns the container on which this iterator is operating.

~HashMapIter()

dtor

ConstHashMapIter<key,val> &operator=(const val<key,val> &)
ConstHashMapIter<key,val> &operator=(const ConstHashMapIter<key,val> &)

These assignment operators are private and ONLY throw an exception to prevent incorrect assignments to a non-const iterator.