Boost logo

Boost :

From: Daniel James (daniel_at_[hidden])
Date: 2005-01-13 09:47:22


Jonathan Turkanis wrote:
> It's not that I don't see the benefit of lower complexity; I don't really see
> the lower complexity. ;-)

Simon Tatham provides a nice motivation for this kind of thing at:

http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

Here's a rough (untested) translation of his run-length decoding example:

   void decompress(std::istream& in, std::ostream& out)
   {
     char c;
     while(in.get(c))
     {
       if(c == 0xFF) {
         int len = in.get();
         if(!in || !in.get(c)) // Return an error.
         while(len--) out.put(c);
       }
       else {
         out.put(c);
       }
     }
   }

And as a filter:

    struct toupper_filter : input_filter {
       int repeat_char;
       int repeat_length;

       toupper_filter() : repeat_char(0), repeat_length(0) {}

       template<typename Source>
       int get(Source& src)
       {
         if(repeat_length > 0) {
           repeat_length--;
           return repeat_char;
         }
         else {
           char c = boost::io::get(src);
           if(c == 0xFF) {
             repeat_length = boost::io::get(src);
             repeat_char = boost::io::get(src);
             repeat_length--;
             return repeat_char;
           }
           else {
             return c;
           }
         }
       }
    };

And that's a fairly simple example. (Sorry if you have a better way to
do this, I haven't really looked at the library).

> Your version requires that an entire stream
> of data be processed at once -- leading to poor memory use -- and doesn't work
> at all for streams which have no natural end.

Not necessarily, he could use threads or fibres with pipes, although
that's quite expensive. That's why I was playing around with using a
Duff's Device style switch statement for implementing coroutines.

Daniel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk