473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Iterators

Hi All,

Please could someone explain to me what an iterator is and
how it may be used in a real world situation.

I am new to C#, and I can't find a decent,plain english
explanation for iterators.

TIA
Kevin
Nov 15 '05 #1
6 1820
Hi Kevin,

In general, an iterator is a means for sequential access to a container. A
container, in turn, is a data structure holding one or more data items.
Arrays, collections, hashtables and even trees are all containers. Iterators
encapsulate all logic necessary for sequential access to the container
elements.

For example, an iterator for an array keeps an index of a current element
and increments the index by one each time next element is requested.

As far as I know, iterators are introduced in C# 2.0 which has not been
released yet. They are implemented as co-routines (that is, routines that
run on a different fiber within the thread and therefore have own stack and
CPU context). Thus, if such an iterator encapsulates quite a complex logic
(say, an iterator that recursively walks a binary tree), it won't be using
the caller's stack for recursion.

P.S. Only one fiber can be active within a thread, as far as I know,
therefore iterators do not run IN PARALLEL with the calling code. Gurus
please correct me if I am wrong here.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Kevin" <an*******@discussions.microsoft.com> wrote in message
news:21****************************@phx.gbl...
Hi All,

Please could someone explain to me what an iterator is and
how it may be used in a real world situation.

I am new to C#, and I can't find a decent,plain english
explanation for iterators.

TIA
Kevin


Nov 15 '05 #2
They are not implemented as fibers. It's just syntactical shorthand for the
enumerator pattern.
ILDASM is your friend ;)
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:Oi**************@TK2MSFTNGP12.phx.gbl...
Hi Kevin,

In general, an iterator is a means for sequential access to a container. A
container, in turn, is a data structure holding one or more data items.
Arrays, collections, hashtables and even trees are all containers. Iterators encapsulate all logic necessary for sequential access to the container
elements.

For example, an iterator for an array keeps an index of a current element
and increments the index by one each time next element is requested.

As far as I know, iterators are introduced in C# 2.0 which has not been
released yet. They are implemented as co-routines (that is, routines that
run on a different fiber within the thread and therefore have own stack and CPU context). Thus, if such an iterator encapsulates quite a complex logic
(say, an iterator that recursively walks a binary tree), it won't be using
the caller's stack for recursion.

P.S. Only one fiber can be active within a thread, as far as I know,
therefore iterators do not run IN PARALLEL with the calling code. Gurus
please correct me if I am wrong here.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Kevin" <an*******@discussions.microsoft.com> wrote in message
news:21****************************@phx.gbl...
Hi All,

Please could someone explain to me what an iterator is and
how it may be used in a real world situation.

I am new to C#, and I can't find a decent,plain english
explanation for iterators.

TIA
Kevin

Nov 15 '05 #3
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote in message news:<Oi**************@TK2MSFTNGP12.phx.gbl>...
Hi Kevin,

In general, an iterator is a means for sequential access to a container. A
container, in turn, is a data structure holding one or more data items.
Arrays, collections, hashtables and even trees are all containers. Iterators
encapsulate all logic necessary for sequential access to the container
elements.

For example, an iterator for an array keeps an index of a current element
and increments the index by one each time next element is requested.

As far as I know, iterators are introduced in C# 2.0 which has not been
released yet. They are implemented as co-routines (that is, routines that
run on a different fiber within the thread and therefore have own stack and
CPU context). Thus, if such an iterator encapsulates quite a complex logic
(say, an iterator that recursively walks a binary tree), it won't be using
the caller's stack for recursion.

P.S. Only one fiber can be active within a thread, as far as I know,
therefore iterators do not run IN PARALLEL with the calling code. Gurus
please correct me if I am wrong here.


so what is the point of parceling the work of the iterator out to the
fiber? Just to limit the activity on the stack? Doesnt that
complicate exception handling? that is, if the iterator code throws
an exception, will the exception be ignored by the code outside of the
fiber ( the code that owns the iterator )

-Steve
Nov 15 '05 #4
"Steve Richter" <sr******@autocoder.com> wrote in message
news:7b**************************@posting.google.c om...
so what is the point of parceling the work of the iterator out to the
fiber? Just to limit the activity on the stack? Doesnt that
complicate exception handling? that is, if the iterator code throws
an exception, will the exception be ignored by the code outside of the
fiber ( the code that owns the iterator )


They are not implemented in fibers. There is no need to implement a fiber.
Iterators are implemented with a simple enumerator pattern so that existing
languages can still interop against them. And even if the C# and CLR team
wanted to multi-thread this due sudden dementia, I'm sure they wouldn't use
fibers to do it. Fibers are only useful in a very narrow set of
circumstances that justify the micro perf gain and increased complexity.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com


Nov 15 '05 #5
Gents,

There is an article in MSDN magazine which shows how to do that with fibers
and which explains why it would be beneficial. The article is titled
"Implementing co-routines for .NET by Wrapping the Umnanaged Fiber API", the
author is Ajai Shankar. The article is published in September 2003 - Vol 18
No 9 issue of MSDN magazine, pp. 89-94

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
"Steve Richter" <sr******@autocoder.com> wrote in message
news:7b**************************@posting.google.c om...
so what is the point of parceling the work of the iterator out to the
fiber? Just to limit the activity on the stack? Doesnt that
complicate exception handling? that is, if the iterator code throws
an exception, will the exception be ignored by the code outside of the
fiber ( the code that owns the iterator )
They are not implemented in fibers. There is no need to implement a fiber.
Iterators are implemented with a simple enumerator pattern so that

existing languages can still interop against them. And even if the C# and CLR team
wanted to multi-thread this due sudden dementia, I'm sure they wouldn't use fibers to do it. Fibers are only useful in a very narrow set of
circumstances that justify the micro perf gain and increased complexity.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com


Nov 15 '05 #6
Okay, but the article doesn't have anything whatsoever to do with the
iterators implementation in C# - more to the point, I have ILDASM on my hard
drive and it says that fibers aren't being used. I also went to several
panel discussions at the PDC and my understanding of the future directions
in multi-threading support is different than the discussion in the article -
which is essentially just MC++ hackery that frankly can't be relied upon to
work (read the disclaimers at the end of the article), can't be made to work
with the current runtime (due to core architectural issues), and will
probably be completely broken in future versions of the runtime.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:uQ**************@TK2MSFTNGP10.phx.gbl...
Gents,

There is an article in MSDN magazine which shows how to do that with fibers and which explains why it would be beneficial. The article is titled
"Implementing co-routines for .NET by Wrapping the Umnanaged Fiber API", the author is Ajai Shankar. The article is published in September 2003 - Vol 18 No 9 issue of MSDN magazine, pp. 89-94

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
"Steve Richter" <sr******@autocoder.com> wrote in message
news:7b**************************@posting.google.c om...
so what is the point of parceling the work of the iterator out to the
fiber? Just to limit the activity on the stack? Doesnt that
complicate exception handling? that is, if the iterator code throws
an exception, will the exception be ignored by the code outside of the
fiber ( the code that owns the iterator )


They are not implemented in fibers. There is no need to implement a fiber. Iterators are implemented with a simple enumerator pattern so that

existing
languages can still interop against them. And even if the C# and CLR team wanted to multi-thread this due sudden dementia, I'm sure they wouldn't

use
fibers to do it. Fibers are only useful in a very narrow set of
circumstances that justify the micro perf gain and increased complexity.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Steven Bethard | last post by:
So, as I understand it, in Python 3000, zip will basically be replaced with izip, meaning that instead of returning a list, it will return an iterator. This is great for situations like: zip(*)...
18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
1
by: Marcin Kaliciñski | last post by:
template<class RanAccIt> void some_algorithm(RanAccIt begin, RanAccIt end) { // this algorithm involves calling std::lexicographical_compare // on range [begin, end), and on reverse of this range...
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
8
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
14
by: Jiri Kripac | last post by:
Languages such as Simula 67 contain a general concept of coroutines that allow the execution of a method to be suspended without rolling back the stack and then later resumed at the same place as...
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.