Click here to Skip to main content
Licence BSD
First Posted 20 Sep 2008
Views 14,501
Bookmarked 10 times

Generators in C++

By | 20 Sep 2008 | Article
The way to add generators with yield to C++.

Introduction

As we know, iterators in C++ is a good but not a perfect abstraction. The concept of foreach() (D, Python, Ruby, etc.) appears as a more generic solution. At least, foreach() does not require an artificial iterator::end() to be defined for the collection.

The foreach() abstraction can be imagined as some function/object that returns the next value of collection/sequence each time it gets invoked. Such functions are known as generators.

The proposed implementation of the generator/yield feature is provided below in full.

Background

This version of generator() for C++ is based on the bright idea of Simon Tatham - "coroutines in C". In particular,on the idea of using switch/case for this implementation.

Declaring a generator

To declare a generator, you will use $generator, $yield, $emit, and $stop "keywords" that are macro definitions in fact.

And here is a typical implementation of a generator that emits numbers from 10 to 1 in descending order:

include "generator.h"

$generator(descent)
{
   // place for all variables used in the generator
   int i; // our counter

   // place the constructor of our generator, e.g. 
   // descent(int minv, int maxv) {...}
   
   // from $emit to $stop is a body of our generator:
    
   $emit(int) // will emit int values. Start of body of the generator.
      for (i = 10; i > 0; --i)
         $yield(i); // a.k.a. yield in Python,
                    // returns next number in [1..10], reversed.
   $stop; // stop, end of sequence. End of body of the generator.
};

Having such a descending generator declared, we will use it as:

int main(int argc, char* argv[])
{
  descent gen;
  for(int n; gen(n);) // "get next" generator invocation
    printf("next number is %d\n", n);
  return 0;
}

The gen(n) thing is in fact an invocation of the bool operator()(int& v) method defined "under the hood" of our generator object. It returns true if the parameter v was set, and false if our generator cannot provide more elements - was stopped.

As you may see, for(int n; gen(n);) looks close enough to the construction for(var n in gen) used in JavaScript for exactly the same purpose. Expressiveness is the beauty of the approach.

generator.h

And here is the source code of the generator implementation:

#ifndef __generator_h__
#define __generator_h__

// generator/continuation for C++
// author: Andrew Fedoniouk @ terrainformatica.com
// idea borrowed from: "coroutines in C" Simon Tatham,
//   http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

struct _generator
{
  int _line;
  _generator():_line(0) {}
};

#define $generator(NAME) struct NAME : public _generator

#define $emit(T) bool operator()(T& _rv) { \
                    switch(_line) { case 0:;

#define $stop  } _line = 0; return false; }

#define $yield(V)     \
        do {\
            _line=__LINE__;\
            _rv = (V); return true; case __LINE__:;\
        } while (0)
#endif

That is a bit cryptic, but if you would read the original article of Simon Tatham, then you will get an idea of what is going on here.

Limitations of the approach

One obvious limitation - $yield cannot be placed inside a switch as $emit() declares a switch by itself.

History

This approach of making generators in C++ was originally published in two articles in my blog:

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

c-smile

Founder
Terra Informatica Software
Canada Canada

Member

Andrew Fedoniouk.
 
MS in Physics and Applied Mathematics.
Designing software applications and systems since 1991.
 
W3C HTML5 Working Group, Invited Expert.
 
Terra Informatica Software, Inc.
http://terrainformatica.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhat's the use? PinmemberRoland Pibinger1:32 21 Sep '08  
AnswerRe: What's the use? Pinmemberc-smile7:47 21 Sep '08  
I cannot say better than author of this message:
Here's a brief case: generators let you factor a complex loop,
dividing the value-producing part from the value-consuming part.
Neither part has to be transformed in a non-obvious way. With
iterators alone, you do this by rewriting the value-producing part as
an Iterator class. This obfuscates the iterator implementation.

 
Below is a practical example from my TIScript implementation.
 
On each generator invocation it returns next property of the object. Property is key/value pair.
Object contains either flat table of key/value pairs if there are less than 8 such pairs. Otherwise
properties are organized as a hash table.
 
$generator(each_property)
{
   int    i,cnt;
   pvalue props;
   value  prop;
 
   each_property(VM *c, value obj):props(c) 
   { 
      props = CsObjectProperties( obj ); 
   }
 
   $emit2(value,value) // will emit key/value pair of the obj

     if (CsHashTableP(props)) 
     {
          cnt = CsHashTableSize(props);
          for (i = 0; i < cnt; ++i) 
          {
              prop = CsHashTableElement(props,i);
              for (; prop != VM::undefinedValue; prop = CsPropertyNext(prop))
              {
                $yield2 ( CsPropertyTag(prop),CsPropertyValue(prop) );
              }
          }
      }
      else
          for (; props != VM::undefinedValue; props = CsPropertyNext(props))
          {
             $yield2 ( CsPropertyTag(props),CsPropertyValue(props) );
          }
   $stop; // stop, end of sequence. End of body of the generator.
};
 
Enumeration/iteration of hash table is non-trivial process per se. Here it is a matter of writing loop with collision list walker.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120604.1 | Last Updated 21 Sep 2008
Article Copyright 2008 by c-smile
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid