 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Nicola Musatti Guest
|
Posted: Wed Aug 25, 2004 8:46 pm Post subject: Iterator pipes |
|
|
Hallo, groupmates.
Suppose I have a function that provides a sequence of elements by
taking an output iterator as argument:
class Element {};
void provider(SomeOutputIter<Element>);
I need to pass these elements to a function that takes an input
iterator range as argument:
void consumer(SomeInputIter<Element>,SomeInputIter<Element>);
Is there a more clever way to do it than to use a container as buffer?
Ideally it would be nice to have some sort of iterator pipe that made
it possible to write to an output iterator on one end and to read from
an iterator range from the other.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Raoul Gough Guest
|
Posted: Fri Aug 27, 2004 3:00 am Post subject: Re: Iterator pipes |
|
|
[email]Nicola.Musatti (AT) ObjectWay (DOT) it[/email] (Nicola Musatti) wrote in message news:<a327cf48.0408250036.24fe18c5 (AT) posting (DOT) google.com>...
| Quote: | Hallo, groupmates.
Suppose I have a function that provides a sequence of elements by
taking an output iterator as argument:
class Element {};
void provider(SomeOutputIter<Element>);
I need to pass these elements to a function that takes an input
iterator range as argument:
void consumer(SomeInputIter<Element>,SomeInputIter<Element>);
Is there a more clever way to do it than to use a container as buffer?
Ideally it would be nice to have some sort of iterator pipe that made
it possible to write to an output iterator on one end and to read from
an iterator range from the other.
|
I assume you mean interleaving the operations of the producer and the
consumer? i.e. the producer produces one Element, and the consumer
then processes this one element. Otherwise, I don't see any way to
work this other than by buffering the entire producer output and then
passing all this to the consumer.
The problem being of course, that the consumer function isn't called
once per element, but once for a whole range of elements. It would
have to wait somehow internally until each new element becomes
available, e.g. via cooperative multi-tasking. Modula-2 had neat
support for this (as "co-routines"), IIRC. It's also possible to
implement this on top of generic threading, but I don't see how you
would do it in standard C++.
Unix-like shells do it, of course, by starting processes in parallel
and connecting them via pipes, which would require some kind of
external representation of the Elements.
--
Raoul Gough.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel Salters Guest
|
Posted: Fri Aug 27, 2004 3:06 am Post subject: Re: Iterator pipes |
|
|
[email]Nicola.Musatti (AT) ObjectWay (DOT) it[/email] (Nicola Musatti) wrote in message news:<a327cf48.0408250036.24fe18c5 (AT) posting (DOT) google.com>...
| Quote: | Hallo, groupmates.
Suppose I have a function that provides a sequence of elements by
taking an output iterator as argument:
class Element {};
void provider(SomeOutputIter<Element>);
I need to pass these elements to a function that takes an input
iterator range as argument:
void consumer(SomeInputIter<Element>,SomeInputIter<Element>);
Is there a more clever way to do it than to use a container as buffer?
|
Yes, sort-of.
Write an output iterator and implement its operator* like this:
SomeOutputIter::operator* returns proxy<Element>,
proxy<Element>::operator=(Element e) calls consumer(&e,(&e)+1);
Of course, this will call consumer more than once, but that is
unavoidable anyway. provider may output many more elements than you
could fit in your buffer, or take several days. provider might not
even return!
Regards,
Michiel Salters.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Fri Aug 27, 2004 1:02 pm Post subject: Re: Iterator pipes |
|
|
[email]RaoulGough (AT) yahoo (DOT) co.uk[/email] (Raoul Gough) wrote in message news:<a3390f41.0408260521.51e2834b (AT) posting (DOT) google.com>...
| Quote: | Nicola.Musatti (AT) ObjectWay (DOT) it (Nicola Musatti) wrote in message
[...]
Is there a more clever way to do it than to use a container as buffer?
Ideally it would be nice to have some sort of iterator pipe that made
it possible to write to an output iterator on one end and to read from
an iterator range from the other.
I assume you mean interleaving the operations of the producer and the
consumer? i.e. the producer produces one Element, and the consumer
then processes this one element. Otherwise, I don't see any way to
work this other than by buffering the entire producer output and then
passing all this to the consumer.
|
Exactly.
| Quote: | The problem being of course, that the consumer function isn't called
once per element, but once for a whole range of elements. It would
have to wait somehow internally until each new element becomes
available, e.g. via cooperative multi-tasking. Modula-2 had neat
support for this (as "co-routines"), IIRC. It's also possible to
implement this on top of generic threading, but I don't see how you
would do it in standard C++.
|
To tell the truth I was aware of the problem. I asked just in case I
missed some detail that could be exploited to construct a clever
solution to my problem.
Without wishing to intrude the current heated discussion on C++ and
threads, I'd just like to note that my suggested iterator pipe would
make a convenient high level communication/synchronization mechanism.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jon hanson Guest
|
Posted: Sat Aug 28, 2004 2:43 am Post subject: Re: Iterator pipes |
|
|
[email]Nicola.Musatti (AT) ObjectWay (DOT) it[/email] (Nicola Musatti) wrote in message news:<a327cf48.0408250036.24fe18c5 (AT) posting (DOT) google.com>...
| Quote: | Hallo, groupmates.
Suppose I have a function that provides a sequence of elements by
taking an output iterator as argument:
class Element {};
void provider(SomeOutputIter<Element>);
I need to pass these elements to a function that takes an input
iterator range as argument:
void consumer(SomeInputIter<Element>,SomeInputIter<Element>);
Is there a more clever way to do it than to use a container as buffer?
Ideally it would be nice to have some sort of iterator pipe that made
it possible to write to an output iterator on one end and to read from
an iterator range from the other.
Cheers,
Nicola Musatti
|
If you allow multi-threading then the pipe could block the producer
from writing or the consumer from reading until the other is ready,
i.e. they would synchronise at the point of writing to and reading
from the pipe.
without threads, you'd either have to have the producer "pushing" the
items, or the consumer "pulling" them. in the first case the
OutputIterator would call the consumer as items were written to it,
and in the second case when an item was requested from the
InputIterator it would call the producer for another item.
jon
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
George van den Driessche Guest
|
Posted: Mon Sep 06, 2004 8:34 pm Post subject: Re: Iterator pipes |
|
|
"Nicola Musatti" <Nicola.Musatti (AT) ObjectWay (DOT) it> wrote
| Quote: | Hallo, groupmates.
Suppose I have a function that provides a sequence of elements by
taking an output iterator as argument:
class Element {};
void provider(SomeOutputIter<Element>);
I need to pass these elements to a function that takes an input
iterator range as argument:
void consumer(SomeInputIter<Element>,SomeInputIter<Element>);
Is there a more clever way to do it than to use a container as buffer?
Ideally it would be nice to have some sort of iterator pipe that made
it possible to write to an output iterator on one end and to read from
an iterator range from the other.
|
Some assumptions I made when reading your post:
- a single call to provider() writes through the
supplied output iterator repeatedly (once for
each element it wants to provide).
- a single call to consumer() reads repeatedly
until the two supplied input iterators become
equal.
- you're not going to put the two function calls
in different threads.
Since you can't have both function calls executing simultaneously, you're
either going to have to buffer the result or call one or other function
multiple times. You could have some sort of custom output iterator, maybe.
It would forward each output element to the consumer specified in its
constructor. But really that's no different from having a buffer of unit
size.
George
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|