gatb.core-API-0.0.0
Iterators snippets

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:

// We include what we need for the test
#include <list>
#include <iostream>
/********************************************************************************/
/* Iteration of a list with the gatb-core api */
/********************************************************************************/
int main (int argc, char* argv[])
{
size_t nbItems = 0;
// We declare a STL list with some values.
int values[] = {1,2,3,5,8,13,21,34};
std::list<int> l (values, values + sizeof(values)/sizeof(values[0]) );
// We create an iterator on this STL list
ListIterator<int> it (l);
// We iterate the list through the iterator.
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:

// We include what we need for the test
#include <list>
#include <iostream>
/********************************************************************************/
/* Iteration of a Cartesian product of two iterators */
/********************************************************************************/
int main (int argc, char* argv[])
{
// We declare a STL list with some values.
int values1[] = {1,2,3,5,8,13,21,34};
std::list<int> l1 (values1, values1 + sizeof(values1)/sizeof(values1[0]) );
// We declare a STL list with some values.
float values2[] = {0.5, 3.1415, 2.71};
std::list<float> l2 (values2, values2 + sizeof(values2)/sizeof(values2[0]) );
// We declare two iterators on the two lists.
ListIterator<int> it1 (l1);
ListIterator<float> it2 (l2);
// We declare a Cartesian iterator on the two iterators.
ProductIterator<int,float> it (it1, it2);
// We iterate the Cartesian product of the two lists
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:

// We include what we need for the test
#include <list>
#include <iostream>
/********************************************************************************/
/* Iteration of a two iterators by pairs of items */
/********************************************************************************/
int main (int argc, char* argv[])
{
// We declare a STL list with some values.
int values1[] = {13,5,34};
std::list<int> l1 (values1, values1 + sizeof(values1)/sizeof(values1[0]) );
// We declare a STL list with some values.
float values2[] = {0.5, 3.1415, 2.71};
std::list<float> l2 (values2, values2 + sizeof(values2)/sizeof(values2[0]) );
// We declare a paired iterator on the two iterators.
PairedIterator<int,float> it (new ListIterator<int>(l1), new ListIterator<float>(l2));
// We iterate the pairs of the two lists
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:

// We include what we need for the test
#include <list>
#include <iostream>
/********************************************************************************/
/* Iteration of a part of an iterator */
/********************************************************************************/
int main (int argc, char* argv[])
{
// We declare a STL list with some values.
int values[] = {1,2,3,5,8,13,21,34};
int valuesLen = sizeof(values)/sizeof(values[0]);
std::list<int> l (values, values + valuesLen);
// We declare one iterator on the list
ListIterator<int> it (l);
// We declare a truncated iterator for the list iterator.
TruncateIterator<int> itTrunc (it, valuesLen/2);
// We iterate the truncated list
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:

// We include what we need for the test
#include <list>
#include <iostream>
/********************************************************************************/
// We a define a functor that will be called during iteration
struct ProgressFunctor : public IteratorListener
{
// we receive 'current' amount of done iterations since last call
void inc (u_int64_t current) { std::cout << "."; }
};
/********************************************************************************/
/* Iteration with progress information */
/********************************************************************************/
int main (int argc, char* argv[])
{
// We declare a STL list with some values.
int values[] = {1,2,3,5,8,13,21,34};
int valuesLen = sizeof(values)/sizeof(values[0]);
std::list<int> l (values, values + valuesLen);
// We create an iterator on the list.
ListIterator<int>* itList = new ListIterator<int> (l);
// We declare an iterator that will send progress status every 3 iterations.
// Note that it refers a ListIterator instance given as constructor parameter.
SubjectIterator<int> itNotif (itList, 3);
// We create some listener to be notified about progress and attach it to the iterator.
itNotif.addObserver (new ProgressFunctor ());
// We iterate the truncated list
for (itNotif.first(); !itNotif.isDone(); itNotif.next())
{
// We can do something in the loop, but there is nothing here about progress management
}
}

[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:

// We include what we need for the test
#include <list>
#include <iostream>
/********************************************************************************/
/* Iteration with progress information */
/********************************************************************************/
int main (int argc, char* argv[])
{
// We declare a STL list with some values.
int values[] = {1,2,3,5,8,13,21,34};
int valuesLen = sizeof(values)/sizeof(values[0]);
std::list<int> l (values, values + valuesLen);
// We declare an iterator that will send default progress status.
// Note that we define the 'actual' iterator on the fly as first parameter of ProgressIterator
ProgressIterator<int> it (new ListIterator<int> (l), "Iteration running", valuesLen);
// We iterate the list
for (it.first(); !it.isDone(); it.next())
{
// We force a small wait
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:

// We include what we need for the test
#include <list>
#include <iostream>
/********************************************************************************/
// We a define a functor that will be called during iteration for filtering odd items.
struct FilterFunctor { bool operator () (int& val) { return val%2 == 0; } };
/********************************************************************************/
/* Iteration with filtering */
/********************************************************************************/
int main (int argc, char* argv[])
{
// We declare a STL list with some values.
int values[] = {1,2,3,5,8,13,21,34};
std::list<int> l (values, values + sizeof(values)/sizeof(values[0]));
// We declare a functor for filtering items.
FilterFunctor filter;
// We declare an iterator over list entries with filtering out odd values.
FilterIterator<int,FilterFunctor> it (new ListIterator<int> (l), filter);
// We iterate the truncated list
for (it.first(); !it.isDone(); it.next())
{
// We should have only even values here.
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:

// We include what we need for the test
#include <list>
#include <iostream>
// Shortcut
typedef std::pair<int,float> Type;
// We define our filtering functor
struct FilterFunctor { bool operator () (const Type& val) { return val.first > val.second; } };
/********************************************************************************/
/* Mixing iterators */
/********************************************************************************/
int main (int argc, char* argv[])
{
// We declare a STL list with some values.
int values1[] = {1,2,3,5,8,13,21,34};
std::list<int> l1 (values1, values1 + sizeof(values1)/sizeof(values1[0]) );
// We declare a STL list with some values.
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]));
// We declare our 'mix' iterator
FilterIterator<Type,FilterFunctor> it (
new PairedIterator<int,float> (
new ListIterator<int> (l1),
new ListIterator<float> (l2)
),
FilterFunctor()
);
// We iterate the pairs of the two lists
for (it.first(); !it.isDone(); it.next())
{
std::cout << it->first << " -- " << it->second << std::endl;
}
}

[go back to top]