Heap Sample: Conversion of enumeration code to use C# iterators

When I started, I thought that this might be a hairy part of the problem, but it turned out to be trivial.
My guess was based on the fact that my original IEnumerator implementation maintains a version stamp from the collection that it is enumerating. Any operation on the collection increments its version number and thus the enumerator no longer matches and refuses to operate. This is in strict adherence to the documented contract for the IEnumerator interface.

To get the same logic using iterators, the code is as follows:

 public virtual IEnumerator<T> GetEnumerator()
{
for (int i = 0, version = this.version; i < this.size; i++)
{
if (this.version == version)
{
yield return this.nodeArray[i];
}
else
{
throw new InvalidOperationException(Resources.Enumerator_HeapModified);
}
}
}

Iterators are basically an inline code generator that creates a small state-machine based enumerator implementation. I found that I needed to have a look at the generated code using Reflector to convince myself of the correctness of the positioning of the version = this.version initializer. Happily it is in just the right place.

BTW, Wesner Moise has a great article about pushing iterators further into a pipelined programming model.

Published 05 October 04 03:16 by GarethJ
Filed under:

Comments

No Comments
New Comments to this post are disabled

Search

Go

This Blog

Disclaimer
The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion.
All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Tags

Archives

Architects who Model

DSL Tools Team

Links

Syndication

Page view tracker