Presentation
This page presents some code snippets related to the use of Iterators.
Additional snippets are available in directory: gatb-core/gatb-core/examples/tools.
Iterate a list
This snippet shows how to iterate a STL list with our iterator design.
Code is from example iterators1.cpp:
#include <list>
#include <iostream>
int main (int argc, char* argv[])
{
size_t nbItems = 0;
int values[] = {1,2,3,5,8,13,21,34};
std::list<int> l (values, values + sizeof(values)/sizeof(values[0]) );
ListIterator<int> it (l);
for (it.first(); !it.isDone(); it.next())
{
std::cout << *it << std::endl;
}
}
Iterate the Cartesian product of two lists
This snippet shows how to iterate the Cartesian product of two lists:
1) Declare two iterators
2) Declare one Cartesian iterator configured with the two iterators
3) Iterate the Cartesian iterator.
The current item of the iteration is a pair, so one should retrieve the couple of values with methods 'first' and 'second'.
Code is from example iterators2.cpp:
#include <list>
#include <iostream>
int main (int argc, char* argv[])
{
int values1[] = {1,2,3,5,8,13,21,34};
std::list<int> l1 (values1, values1 + sizeof(values1)/sizeof(values1[0]) );
float values2[] = {0.5, 3.1415, 2.71};
std::list<float> l2 (values2, values2 + sizeof(values2)/sizeof(values2[0]) );
ListIterator<int> it1 (l1);
ListIterator<float> it2 (l2);
ProductIterator<int,float> it (it1, it2);
for (it.first(); !it.isDone(); it.next())
{
std::cout << it->first << " -- " << it->second << std::endl;
}
}
[go back to top]
Iterate two lists by pairs
This snippet shows how to iterate two iterators at the same time, providing pairs of items at each iteration.
A usage of such an iterator is to iterate two paired ends banks.
Code is from example iterators7.cpp:
#include <list>
#include <iostream>
int main (int argc, char* argv[])
{
int values1[] = {13,5,34};
std::list<int> l1 (values1, values1 + sizeof(values1)/sizeof(values1[0]) );
float values2[] = {0.5, 3.1415, 2.71};
std::list<float> l2 (values2, values2 + sizeof(values2)/sizeof(values2[0]) );
PairedIterator<int,float> it (new ListIterator<int>(l1), new ListIterator<float>(l2));
for (it.first(); !it.isDone(); it.next())
{
std::cout << it->first << " -- " << it->second << std::endl;
}
}
[go back to top]
Truncating an iteration
This snippet shows how to truncate the iteration of some iterator.
Code is from example iterators3.cpp:
#include <list>
#include <iostream>
int main (int argc, char* argv[])
{
int values[] = {1,2,3,5,8,13,21,34};
int valuesLen = sizeof(values)/sizeof(values[0]);
std::list<int> l (values, values + valuesLen);
ListIterator<int> it (l);
TruncateIterator<int> itTrunc (it, valuesLen/2);
for (itTrunc.first(); !itTrunc.isDone(); itTrunc.next())
{
std::cout << *itTrunc << std::endl;
}
}
[go back to top]
Iterate a list with progress feedback
This snippet shows how to iterate a STL list and being notified as a listener about its progression.
The idea is to use a SubjectIterator instance that refers the actual iterator we want to iterate.
Then, it is possible to subscribe some callback function (here as a functor) to the SubjectIterator instance.
The listener will then receive at regular interval the number of currently iterated items.
Code is from example iterators4.cpp:
#include <list>
#include <iostream>
struct ProgressFunctor : public IteratorListener
{
void inc (u_int64_t current) { std::cout << "."; }
};
int main (int argc, char* argv[])
{
int values[] = {1,2,3,5,8,13,21,34};
int valuesLen = sizeof(values)/sizeof(values[0]);
std::list<int> l (values, values + valuesLen);
ListIterator<int>* itList = new ListIterator<int> (l);
SubjectIterator<int> itNotif (itList, 3);
itNotif.addObserver (new ProgressFunctor ());
for (itNotif.first(); !itNotif.isDone(); itNotif.next())
{
}
}
[go back to top]
Iterate a list with progress feedback (simple)
This snippet is the same as before but here we use a default console progress bar. In most case, it allows to avoid an explicit listener configuration.
Code is from example iterators5.cpp:
#include <list>
#include <iostream>
int main (int argc, char* argv[])
{
int values[] = {1,2,3,5,8,13,21,34};
int valuesLen = sizeof(values)/sizeof(values[0]);
std::list<int> l (values, values + valuesLen);
ProgressIterator<int> it (new ListIterator<int> (l), "Iteration running", valuesLen);
for (it.first(); !it.isDone(); it.next())
{
sleep (1);
}
}
[go back to top]
Iterate a list and filter out some items
This snippet shows how to iterate a STL list while filtering out some items that don't check some condition.
Code is from example iterators6.cpp:
#include <list>
#include <iostream>
struct FilterFunctor { bool operator () (int& val) { return val%2 == 0; } };
int main (int argc, char* argv[])
{
int values[] = {1,2,3,5,8,13,21,34};
std::list<int> l (values, values + sizeof(values)/sizeof(values[0]));
FilterFunctor filter;
FilterIterator<int,FilterFunctor> it (new ListIterator<int> (l), filter);
for (it.first(); !it.isDone(); it.next())
{
std::cout << *it << std::endl;
}
}
[go back to top]
Mixing iterators
This snippet shows how mix several iterators. Note again that the iteration loop is still the same.
Code is from example iterators8.cpp:
#include <list>
#include <iostream>
typedef std::pair<int,float> Type;
struct FilterFunctor { bool operator () (const Type& val) { return val.first > val.second; } };
int main (int argc, char* argv[])
{
int values1[] = {1,2,3,5,8,13,21,34};
std::list<int> l1 (values1, values1 + sizeof(values1)/sizeof(values1[0]) );
float values2[] = {0.5, 3.1415, 12.71, -1.51, 4.11, -11.3 };
std::list<float> l2 (values2, values2 + sizeof(values2)/sizeof(values2[0]));
FilterIterator<Type,FilterFunctor> it (
new PairedIterator<int,float> (
new ListIterator<int> (l1),
new ListIterator<float> (l2)
),
FilterFunctor()
);
for (it.first(); !it.isDone(); it.next())
{
std::cout << it->first << " -- " << it->second << std::endl;
}
}
[go back to top]