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:
fsgi02.fnal.gov),
in directories rooted at
/afs/fnal.gov/files/reports/working-groups/fpcltf/ZMtools.
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.
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:
// Container-related information:
typedef /* ... */ Container;
typedef Container::const_iterator Iter;
typedef Container::const_reference Ref;
Container c;
// Filtering information:
extern bool meetsCriteria( Ref );
// Traversal:
for ( Iter i( c.begin() ); i != c.end(); ++i ) {
if ( meetsCriteria(*i) ) {
// ... process *i as desired ...
}
}
To use the IteratorFilter facilities, a user first includes the header file:
#include "ZMtools/IteratorFilter.h"
Subsequently, the user may instantiate and employ one or more IteratorFilters, as in the following simple example:
// Specify container information:
int const NA( 20 );
typedef std::vector<int> A;
typedef A::const_iterator It;
// Specify filter function information:
typedef bool (*FilterFctn) ( int );
bool odd( int i ) { return i % 2; }
// Specify IteratorFilter (IF) information:
typedef IteratorFilter<It,FilterFctn> Phil;
// Prepare the container:
A a(NA);
It end( a.end() );
// Initialize container:
// ...
// Prepare IteratorFilter:
Phil p = Phil(end, &odd);
// Filter the container's contents:
for ( It i( a.begin() ); p(i); ++i )
std::cout << *i << ' ';
std::cout << std::endl;
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.
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.