STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Author
Roman Dementiev (2006)

The semantics of the algorithm is equivalent to the STL std::for_each.

Prototype

template < typename ExtIterator, typename UnaryFunction >
UnaryFunction for_each ( ExtIterator first,
ExtIterator last,
UnaryFunction functor,
int_type nbuffers
)

Description

stxxl::for_each applies the function object functor to each element in the range [first, last); functor's return value, if any, is ignored. Applications are performed in forward order, i.e. from first to last. stxxl::for_each returns the function object after it has been applied to each element. To overlap I/O and computation nbuffers used (a value at least D is recommended). The size of the buffers is derived from the container that is pointed by the iterators.

Remarks
The implementation exploits STXXL buffered streams (computation and I/O overlapped).
Parameters
beginobject of model of ext_random_access_iterator concept
endobject of model of ext_random_access_iterator concept
functorfunction object of model of std::UnaryFunction concept
nbuffersnumber of buffers (blocks) for internal use (should be at least 2*D )
Returns
function object functor after it has been applied to the each element of the given range
Warning
nested stxxl::for_each are not supported

Requirements on types

  • ExtIterator is a model of External Random Access Iterator.
  • UnaryFunction is a model of STL Unary Function.
  • UnaryFunction does not apply any non-constant operations through its argument.
  • ExtIterator's value type is convertible to UnaryFunction's argument type.

Preconditions

[first, last) is a valid range.

Complexity

  • Internal work is linear.
  • External work: close to $ N/DB $ I/Os (read-only).

Example

template<class T>
struct print : public unary_function<T, void>
{
print(ostream& out) : os(out), count(0) {}
void operator() (T x) { os << x << ' '; ++count; }
ostream& os;
int count;
};
int main()
{
vector_type A(N);
// fill A with some values
// ...
print<int> P = stxxl::for_each(A.begin(), A.end(), print<int>(cout));
cout << endl << P.count << " objects printed." << endl;
}