Check out the new USENIX Web site. next up previous contents
Next: Future Implementations Up: Future-based Algorithms Previous: Future-based Algorithms

Example

The STL find algorithm looks for the first element in a list with value = value. It is written as follows:

 template <class InputIterator, class T>

InputIterator find(InputIterator first,

InputIterator last,

const T& value)

{

while (first != last && *first != value)

{

++first;

}

return first;

}

The following code fragment builds a list of future computations and executes find() over this list to find the first element in the list with a zero value.
 int foo(const int i);

typedef Future<int> future_type;

typedef list<future_type> list_type;

list_type xlist;

for(int i = 0; i < N; i++)

xlist.push_back(future_type(foo, i)); // add to list

list_type::iterator pos

= find(xlist.begin(), xlist.end(), 0);

Here find computes only to the point where it finds an element with a value = value. The remaining futures need not even be computed or resolved.



Sundaresan Neelakantan
Thu May 15 16:11:49 PDT 1997