Fermi Physics Class Libraries Module: Timer Classes

ZOOM ZOOM

IteratorFilter

IteratorFilter Class Sub-package

This mini-package provides a simple class, IteratorFilter, that provides a simple means of selecting certain of a container's members while traversing (iterating through) the container.

This package may be obtained from the following sources:


Description
User Interface
Implementation

Walter Brown, Module Coordinator
Fermilab Physics Class Library Task Force

Comments, questions about getting started, feature needs, and bug/annoyance reports may be sent to zoom-support@fnal.gov or zoom@fnal.gov.

Description

This module provides a class designed to simplify a common idiom, traversing a container, selecting ("filtering" )from the container only those members of current interest. This idiom is typically expressed as follows:

Use of this IteratorFilter<> template class will slightly simplify this idiom's expression, albeit with a bit more preparation needed.

User Interface

To use the IteratorFilter facilities, a user first includes the header file:

The code guard for the mini-module is ITERATORFILTER_H; this symbol may also be used as an external code guard.

Subsequently, the user may instantiate and employ one or more IteratorFilters, as in the following simple example:

As demonstrated, the IteratorFilter template requires two arguments: the data type of the iterator used to traverse the container (here, It, a typedef for std::vector<int>::const_iterator), and the data type of the function or functor used to filter the contained data (here, FilterFctn).

The actual use of the IteratorFilter<> object is confined to the predicate of the for loop, (here, p(i)). The filter (1) adjusts (by side effect) its iterator parameter so as to refer to the next container element (if any) that meets the selection criteria embedded in the filter function. The result of the filter is a boolean whose truth value indicates the success of the filtering operation.

Test programs that validate the behavior of the IteratorFilter<> template is available in the programs zmtIFtest1.cc, zmtIFtest2.cc, and zmtIFtest3.cc, all furnished as part of this module. The above example is abstracted from zmtIFtest2.cc.

Implementation

The sole header file needed by the user is ZMtools/IteratorFilter.h. This file contains the complete implementation in the form of a template class, IteratorFilter<>, together with a specialization of this template that handles the case of a zero-sized container.

ZMtools main page - ZOOM Home Page - Fermilab at Work - Fermilab Home


Walter Brown
Last modified: May 16, 2000.