<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 10 May 2026 05:32:24 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Dave MacLachlan - 2025-09-28 01:42:48</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>And how about one more edit...
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;state-&amp;gt;mutationsPtr = &amp;amp;state-&amp;gt;state;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;</description><guid isPermaLink="true">4baf15d6f23957a415a74221f9d94cfc</guid><pubDate>Sun, 28 Sep 2025 01:42:48 GMT</pubDate></item><item><title>Dave MacLachlan - 2025-09-27 21:20:17</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>Actually my comment above is bad (should have tested before I did anything). Can't take the address of an rvalue. Sigh...and &lt;code&gt;mutationsPtr&lt;/code&gt; can't be &lt;code&gt;nil&lt;/code&gt;. So how about:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;state-&amp;gt;mutationsPtr = (unsigned long *)&amp;amp;enumerationState;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;That should be constant across calls.</description><guid isPermaLink="true">d7836e9136013e7b54fe55bd423ef9bc</guid><pubDate>Sat, 27 Sep 2025 21:20:17 GMT</pubDate></item><item><title>Dave MacLachlan - 2025-09-27 21:08:36</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>So with refcounts being stored in the isa pointer, I don't think that:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;state-&amp;gt;mutationsPtr = (unsigned long *)self;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;works anymore. It would seem that an added ref could make it appear that we had mutated things. Given your description of how the system works, either we want it set to something like
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;state-&amp;gt;mutationsPtr = (unsigned long *)&amp;amp;self;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;or maybe not set it to anything at all unless we have a real use for it?</description><guid isPermaLink="true">7e224431bad897107246107e7e212e8d</guid><pubDate>Sat, 27 Sep 2025 21:08:36 GMT</pubDate></item><item><title>Chris Chandler - 2013-07-05 21:36:07</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>Mike, do you (or any of your readers) know what happens when the body in
&lt;br /&gt;&lt;code&gt;    for(id obj in collection)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// body
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;contains a &lt;code&gt;break&lt;/code&gt; statement? What does your suggested implementation look like with &lt;code&gt;break&lt;/code&gt; support?
&lt;br /&gt;
&lt;br /&gt;Specifically, does &lt;code&gt; [NSFastEnumeration countByEnumeratingWithState: objects: count:]&lt;/code&gt; get called with a special &lt;code&gt;state&lt;/code&gt; value to indicate the end of the enumeration?
&lt;br /&gt;
&lt;br /&gt;Regarding your point about temporary objects, that problem could be solved if the function was always called one more time with a special &lt;code&gt;state&lt;/code&gt; value at the end of the enumeration, to give the implementation a chance to clean itself up, after retaining its objects in the first place. One wonders whether Apple has thought of that, and if not, whether a bug should be filed!
&lt;br /&gt;</description><guid isPermaLink="true">b379087d0df9fa9b86a7b2bf56bd5be5</guid><pubDate>Fri, 05 Jul 2013 21:36:07 GMT</pubDate></item><item><title>Denis Halabuzar - 2013-03-15 10:21:43</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>ah, sorry, didn't read the line "mutationsPtrValue != *state.mutationsPtr" carefully</description><guid isPermaLink="true">52906915b4f4c3a09b1aa65f55814ce4</guid><pubDate>Fri, 15 Mar 2013 10:21:43 GMT</pubDate></item><item><title>Denis Halabuzar - 2013-03-15 10:18:40</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>hm, by some unknown to me reason compiler tries to read memory pointed by mutationsPtr. (i tried to store element count there).</description><guid isPermaLink="true">e4bfd5d6a4da1248517cd2bc2dbb0fbf</guid><pubDate>Fri, 15 Mar 2013 10:18:40 GMT</pubDate></item><item><title>Heath Borders - 2011-06-14 04:50:31</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>For anyone interested, I've made a series of blocks-based categories that rely on NSFastEnumeration for their implementation.  I used this article as a reference.  I'd appreciate any comments anyone has.
&lt;br /&gt;
&lt;br /&gt;&lt;a href="https://github.com/hborders/HBCollections"&gt;https://github.com/hborders/HBCollections&lt;/a&gt;</description><guid isPermaLink="true">c9e25a9bff9cfc49aa96695f0e155538</guid><pubDate>Tue, 14 Jun 2011 04:50:31 GMT</pubDate></item><item><title>mikeash - 2010-04-25 16:05:28</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>Yes, &lt;code&gt;return 0;&lt;/code&gt; is what I meant to write there. &lt;code&gt;NULL&lt;/code&gt; will work, but is ugly.
&lt;br /&gt;
&lt;br /&gt;As for the documentation question, I agree, it looks wrong. &lt;code&gt;stackbuf&lt;/code&gt; is just a convenient storage, and the sender won't iterate over it unless you tell it to.</description><guid isPermaLink="true">23d69d92d1561d812b5b51f7551e785e</guid><pubDate>Sun, 25 Apr 2010 16:05:28 GMT</pubDate></item><item><title>Ling - 2010-04-19 04:02:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>One question about the Apple doc:
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;
&lt;br /&gt;stackbuf
&lt;br /&gt;A C array of objects over which the sender is to iterate.
&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;
&lt;br /&gt;itemsPtr
&lt;br /&gt;A C array of objects.
&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;I think the doc is very confusing, or right down wrong. It tells you that it is stackbuf that the sender is to iterate but in fact it it state -&amp;gt;itemsPtr. In fact, you can completely ignore stackbuf in your implementation and use an internal data structure to back up state -&amp;gt;itemsPtr.</description><guid isPermaLink="true">b4c40819bb340e8c7509ea375e03714d</guid><pubDate>Mon, 19 Apr 2010 04:02:26 GMT</pubDate></item><item><title>Ling - 2010-04-19 03:43:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>I think you mean "return 0" here:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(!currentNode)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return NULL
&lt;br /&gt;&lt;/code&gt;</description><guid isPermaLink="true">bcf635f2655647e0558b1b6f9b549880</guid><pubDate>Mon, 19 Apr 2010 03:43:21 GMT</pubDate></item><item><title>mikeash - 2010-04-19 01:01:40</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>Thanks for pointing that out. HTMLification gone wrong. I've fixed it now.</description><guid isPermaLink="true">c0bbd5af861c3a25013d405c20a97bcf</guid><pubDate>Mon, 19 Apr 2010 01:01:40 GMT</pubDate></item><item><title>Michael Babin - 2010-04-18 15:44:29</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>In the "Returning One Object At a Time" example:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// otherwise, point itemsPtr at the node's value
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;state-&amp;gt;itemsPtr = &amp;amp;currentNode-;&amp;gt;value
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Believe there is a superfluous semi-colon there.
&lt;br /&gt;
&lt;br /&gt;Thanks for the article and the entire series.</description><guid isPermaLink="true">45348a66b543aa0c19a8d94b9a8bf5f0</guid><pubDate>Sun, 18 Apr 2010 15:44:29 GMT</pubDate></item><item><title>Skirwan - 2010-04-18 03:22:35</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>That's what I get for running my mouth at a higher clock than my brain. Apologies. </description><guid isPermaLink="true">6ed365af1ba9546445a6c632498b444f</guid><pubDate>Sun, 18 Apr 2010 03:22:35 GMT</pubDate></item><item><title>mikeash - 2010-04-16 19:53:53</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>&lt;b&gt;Aaron:&lt;/b&gt; You are correct, fixed, and thanks.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Skirwan:&lt;/b&gt; It would be nice if people would check these claims before making them. Mac OS X uses LP64, which means that &lt;code&gt;long&lt;/code&gt; is 64 bits. That is the precise reason why these fields are of type &lt;code&gt;long&lt;/code&gt;.</description><guid isPermaLink="true">26cff2d27edef40902ac77c8f650ad36</guid><pubDate>Fri, 16 Apr 2010 19:53:53 GMT</pubDate></item><item><title>Charles Parnot - 2010-04-16 19:43:55</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>Skirwan: actually, I believe longs are 64-bit on Mac OS X (when compiling for 64 bits), see LP64 on this web page:
&lt;br /&gt;&lt;a href="http://developer.apple.com/macosx/64bit.html"&gt;http://developer.apple.com/macosx/64bit.html&lt;/a&gt;</description><guid isPermaLink="true">b7785c7647922aaba46e4fd73dc7d70e</guid><pubDate>Fri, 16 Apr 2010 19:43:55 GMT</pubDate></item><item><title>Skirwan - 2010-04-16 19:23:31</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>It's probably worth noting that many of the examples aren't 64-bit clean -- extra[0] isn't going to hold a 64-bit pointer without some backup from extra[1].  (Not to imply that I think you don't realize that, just that folks who find the page by Googling may need the reminder).</description><guid isPermaLink="true">8917858e7b5c632c613748d415c753d0</guid><pubDate>Fri, 16 Apr 2010 19:23:31 GMT</pubDate></item><item><title>Aaron - 2010-04-16 18:42:50</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments</link><description>I think you mean:
&lt;br /&gt;
&lt;br /&gt;firstLoop = NO;
&lt;br /&gt;
&lt;br /&gt;in the while loop example.</description><guid isPermaLink="true">77e0841f79f025c22f8ac2fc30815d28</guid><pubDate>Fri, 16 Apr 2010 18:42:50 GMT</pubDate></item></channel></rss>
