473,513 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterators and versioning

kh
I notice that using iterators it is possible to modify a collection whilst
enumerating over it's elements. Is this by design or just a side-effect of
their implementation? Viewing the disassembly for the generated classes I
note that the compiler-generated enumerator has no version checking. Is there
an in-depth analysis of this on the www anywhere?

Thanks

kh

Apr 19 '06 #1
16 1335
VJ
I don't understand this question... are you saying while iterating through a
collection, you have a method to modify it?.. if so, the method to modify is
given as part of the collection object, you should not use it while
iterating through the list

VJ

"kh" <kh@newsgroups.nospam> wrote in message
news:F3**********************************@microsof t.com...
I notice that using iterators it is possible to modify a collection whilst
enumerating over it's elements. Is this by design or just a side-effect of
their implementation? Viewing the disassembly for the generated classes I
note that the compiler-generated enumerator has no version checking. Is
there
an in-depth analysis of this on the www anywhere?

Thanks

kh

Apr 19 '06 #2
it s part of my question ?????????

Apr 19 '06 #3
VJ
i don't understand what this question is either

VJ

<mu************@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
it s part of my question ?????????

Apr 19 '06 #4
kh
To put it another way, in 1.1 it was standard practice to implement
enumerators such that modifications to the collection during iterations would
cause MoveNext() to throw. In 2.0 the enumerators generated by the compiler
for iterator blocks do no such thing. Why is this?

kh

Apr 20 '06 #5

"kh" <kh@newsgroups.nospam> wrote in message
news:86**********************************@microsof t.com...
To put it another way, in 1.1 it was standard practice to implement
enumerators such that modifications to the collection during iterations
would
cause MoveNext() to throw. In 2.0 the enumerators generated by the
compiler
for iterator blocks do no such thing. Why is this?


Because it is, in general, needlessly inefficient.
Either the enumerated class would need to track all enumerators it creates
or else it would have to keep a counter that it incremented on every
modification for the enumerators to check. Either way there is both a space
and time overhead.
Even then it wouldn't handle multi-threads.
Apr 20 '06 #6
I just checked the documentation and it explicitly says

" the enumerator is irrecoverably invalidated and the next call to MoveNext
or Reset throws an InvalidOperationException."

SO it would seem that you have to do this to implement IEnumerator properly.

Obviously the compiler cannot implement "yield" in such a way as to support
this behaviour so this must be seen as a documentation bug and the
documentation should be altered to from "throws" to "may throw".

Also the documentation for "Reset" needs to document the possibility of
"NotSupportedException" since "yield" methods don't support it.

"Nick Hounsome" <Ne**@NickHounsome.Me.Uk> wrote in message
news:OY*********************@fe3.news.blueyonder.c o.uk...

"kh" <kh@newsgroups.nospam> wrote in message
news:86**********************************@microsof t.com...
To put it another way, in 1.1 it was standard practice to implement
enumerators such that modifications to the collection during iterations
would
cause MoveNext() to throw. In 2.0 the enumerators generated by the
compiler
for iterator blocks do no such thing. Why is this?


Because it is, in general, needlessly inefficient.
Either the enumerated class would need to track all enumerators it creates
or else it would have to keep a counter that it incremented on every
modification for the enumerators to check. Either way there is both a
space and time overhead.
Even then it wouldn't handle multi-threads.

Apr 20 '06 #7
"Nick Hounsome" <Ne**@NickHounsome.Me.Uk> wrote:
Obviously the compiler cannot implement "yield" in such a way as to support
this behaviour so this must be seen as a documentation bug and the
documentation should be altered to from "throws" to "may throw".


Yeah. I reckon it's actually USEFUL to have enumerators that allow
in-place modification. I mean, you're iterating through a linked list,
and the entire point of a linked list is that changes in one place
don't affect a reader in another.

--
Lucian
Apr 20 '06 #8

"Lucian Wischik" <lu***@wischik.com> wrote in message
news:8h********************************@4ax.com...
"Nick Hounsome" <Ne**@NickHounsome.Me.Uk> wrote:
Obviously the compiler cannot implement "yield" in such a way as to
support
this behaviour so this must be seen as a documentation bug and the
documentation should be altered to from "throws" to "may throw".


Yeah. I reckon it's actually USEFUL to have enumerators that allow
in-place modification. I mean, you're iterating through a linked list,
and the entire point of a linked list is that changes in one place
don't affect a reader in another.


That would be something else entirely.
I was saying that it is an error but that the enumerator might not throw an
exception. You are saying that, in some circumstances, it wouldn't be an
error at all.

In my experience, the only thing that you often want to do to a list whilst
iterating is remove the current element (which is better handled by having a
filter method on the collection that takes a predicate).
Apr 20 '06 #9
On Wed, 19 Apr 2006 09:54:02 -0700, kh <kh@newsgroups.nospam> wrote:
I notice that using iterators it is possible to modify a collection whilst
enumerating over it's elements.
C# 2.0 iterators provide a code transformation, via a generated state
machine, that turns yield return statements into reentry points for
subsequent calls to MoveNext() on the generated IEnumerator
implementation. This code transformation is useful for things which
aren't 'collections' per se: for example, an infinite stream of
positive integers, or an infinite stream of random numbers.
Is this by design or just a side-effect of
their implementation? Viewing the disassembly for the generated classes I
note that the compiler-generated enumerator has no version checking. Is there
an in-depth analysis of this on the www anywhere?


Iterators are useful for more than simply implementing collection
classes, or otherwise they wouldn't be a useful addition to the
language.

Who spends all their time implementing collection classes? If enough
people did this to warrant the feature, then somebody in the BCL
development team isn't doing their job.

Thus, it's a language feature with a far wider range of applicability.

Personally, whenever I need a collection, I descend from an existing
collection class like Collection<T>. However, I've implemented many
iterators, none of which were over an actual collection, but rather
aggregated data from other places, or synthesised data from some
algorithm.

If the C# 2.0 iterator implementation kept track of some notion of
read-only-ness (and that's not possible anyway, since it doesn't know
where I'm getting the data), I wouldn't be using it at all.

-- Barry
Apr 20 '06 #10
On Thu, 20 Apr 2006 09:10:32 GMT, "Nick Hounsome"
<Ne**@NickHounsome.Me.Uk> wrote:
In my experience, the only thing that you often want to do to a list whilst
iterating is remove the current element (which is better handled by having a
filter method on the collection that takes a predicate).


Of course, not all enumerations are over a collection.

-- Barry
Apr 20 '06 #11
kh
hehe

Apr 20 '06 #12
kh
Thanks Barry and Nick. Very useful responses. I need to spend more time with
iterators to really appreciate their power and benefits.

kh

Apr 20 '06 #13
Nick Hounsome wrote:
I just checked the documentation and it explicitly says

" the enumerator is irrecoverably invalidated and the next call to MoveNext
or Reset throws an InvalidOperationException."

SO it would seem that you have to do this to implement IEnumerator properly.

Obviously the compiler cannot implement "yield" in such a way as to support
this behaviour so this must be seen as a documentation bug and the
documentation should be altered to from "throws" to "may throw".

Also the documentation for "Reset" needs to document the possibility of
"NotSupportedException" since "yield" methods don't support it.


I don't see in what way they don't support it. You can write an
iterator block which checks for modifications (with a version number)
easily enough, can't you? Sure, it's extra work - but it's doable.

Jon

Apr 20 '06 #14

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Nick Hounsome wrote:
I just checked the documentation and it explicitly says

" the enumerator is irrecoverably invalidated and the next call to
MoveNext
or Reset throws an InvalidOperationException."

SO it would seem that you have to do this to implement IEnumerator
properly.

Obviously the compiler cannot implement "yield" in such a way as to
support
this behaviour so this must be seen as a documentation bug and the
documentation should be altered to from "throws" to "may throw".

Also the documentation for "Reset" needs to document the possibility of
"NotSupportedException" since "yield" methods don't support it.


I don't see in what way they don't support it. You can write an
iterator block which checks for modifications (with a version number)
easily enough, can't you? Sure, it's extra work - but it's doable.


You don't get to write the Reset() method and the one that's created for you
throws NotSupportedException even though this is not mentioned in
IEnumerator.Reset (I'm not even sure where it is mentioned!) although
InvalidOperationException IS desecribed as being thrown if the collection is
modified [though I can't see why you shouldn't be able to reset an iterator
on a modified collection]

InvalidOperationException is what you should throw from your iterator block
if you want to do the checking but this is only used to implement MoveNext.

Apr 20 '06 #15
Nick Hounsome wrote:
I don't see in what way they don't support it. You can write an
iterator block which checks for modifications (with a version number)
easily enough, can't you? Sure, it's extra work - but it's doable.
You don't get to write the Reset() method and the one that's created for you
throws NotSupportedException even though this is not mentioned in
IEnumerator.Reset (I'm not even sure where it is mentioned!)


The C# spec states that it'll be thrown. Possibly a bad move - not
sure.
although InvalidOperationException IS desecribed as being thrown if the collection is
modified [though I can't see why you shouldn't be able to reset an iterator
on a modified collection]

InvalidOperationException is what you should throw from your iterator block
if you want to do the checking but this is only used to implement MoveNext.


I see - so you can implement *one* of the specified exceptions, just
not all of them.

Jon

Apr 20 '06 #16
kh
indeed you can, and the generated enumerator starts to resemble those we
wrote for v1.1. but the documentation is assuming that we all invariably
implement version checking in our iterator blocks..

public IEnumerator<T> GetEnumerator()
{
int version = this.version;
foreach (T item in items)
{
if (version != this.version)
throw new InvalidOperationException("collection has been
modified");
yield return item;
}
}
"Jon Skeet [C# MVP]" wrote:
Nick Hounsome wrote:
I just checked the documentation and it explicitly says

" the enumerator is irrecoverably invalidated and the next call to MoveNext
or Reset throws an InvalidOperationException."

SO it would seem that you have to do this to implement IEnumerator properly.

Obviously the compiler cannot implement "yield" in such a way as to support
this behaviour so this must be seen as a documentation bug and the
documentation should be altered to from "throws" to "may throw".

Also the documentation for "Reset" needs to document the possibility of
"NotSupportedException" since "yield" methods don't support it.


I don't see in what way they don't support it. You can write an
iterator block which checks for modifications (with a version number)
easily enough, can't you? Sure, it's extra work - but it's doable.

Jon

Apr 20 '06 #17

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

Similar topics

4
2340
by: Sandman | last post by:
Hello - I suppose this group could be suitable for my questions... I am interested in a versioning system that works good for web authoring. I don't know what differences web development might...
18
2275
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...
2
528
by: PatrickSA | last post by:
Hi, Am new to web services, so apologies for the basic nature of the question - and apologies in advance if this is the wrong newsgroup. We're building a new web service and I'm looking around...
24
3919
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 = ...
1
2104
by: MrNobody | last post by:
Is there any way to have .NET IDE to handle versioning for your application? If for example you want a certain build number to increment every time you build your project? Would need this version...
3
3738
by: Modica82 | last post by:
Hi all, Does anyone have any views/information on the best way to version web services. I am writing a proposal on how the company should handle versioning of its web services and would like...
2
2332
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
1
1638
by: zacks | last post by:
Am using VB.NET 2005. I have a solution that has four projects. Two projects are standard windows applications. One is a class library that is shared by the two windows application projects, it...
18
2093
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
7158
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
7380
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
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5085
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...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.