mikeash.com pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsmikeash.com Recent CommentsTue, 19 Mar 2024 05:43:44 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssChris Chandler - 2013-07-05 21:36:07http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsMike, do you (or any of your readers) know what happens when the body in <br /><code> for(id obj in collection) <br />&nbsp;&nbsp;&nbsp;&nbsp;{ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// body <br />&nbsp;&nbsp;&nbsp;&nbsp;} <br /></code> <br />contains a <code>break</code> statement? What does your suggested implementation look like with <code>break</code> support? <br /> <br />Specifically, does <code> [NSFastEnumeration countByEnumeratingWithState: objects: count:]</code> get called with a special <code>state</code> value to indicate the end of the enumeration? <br /> <br />Regarding your point about temporary objects, that problem could be solved if the function was always called one more time with a special <code>state</code> 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! <br />b379087d0df9fa9b86a7b2bf56bd5be5Fri, 05 Jul 2013 21:36:07 GMTDenis Halabuzar - 2013-03-15 10:21:43http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsah, sorry, didn't read the line "mutationsPtrValue != *state.mutationsPtr" carefully52906915b4f4c3a09b1aa65f55814ce4Fri, 15 Mar 2013 10:21:43 GMTDenis Halabuzar - 2013-03-15 10:18:40http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentshm, by some unknown to me reason compiler tries to read memory pointed by mutationsPtr. (i tried to store element count there).e4bfd5d6a4da1248517cd2bc2dbb0fbfFri, 15 Mar 2013 10:18:40 GMTHeath Borders - 2011-06-14 04:50:31http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsFor 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. <br /> <br /><a href="https://github.com/hborders/HBCollections">https://github.com/hborders/HBCollections</a>c9e25a9bff9cfc49aa96695f0e155538Tue, 14 Jun 2011 04:50:31 GMTmikeash - 2010-04-25 16:05:28http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsYes, <code>return 0;</code> is what I meant to write there. <code>NULL</code> will work, but is ugly. <br /> <br />As for the documentation question, I agree, it looks wrong. <code>stackbuf</code> is just a convenient storage, and the sender won't iterate over it unless you tell it to.23d69d92d1561d812b5b51f7551e785eSun, 25 Apr 2010 16:05:28 GMTLing - 2010-04-19 04:02:26http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsOne question about the Apple doc: <br /><div class="blogcommentquote"><div class="blogcommentquoteinner"> <br />stackbuf <br />A C array of objects over which the sender is to iterate. <br /></div></div> <br /> <br /><div class="blogcommentquote"><div class="blogcommentquoteinner"> <br />itemsPtr <br />A C array of objects. <br /></div></div> <br /> <br />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 -&gt;itemsPtr. In fact, you can completely ignore stackbuf in your implementation and use an internal data structure to back up state -&gt;itemsPtr.b4c40819bb340e8c7509ea375e03714dMon, 19 Apr 2010 04:02:26 GMTLing - 2010-04-19 03:43:21http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsI think you mean "return 0" here: <br /><code> <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!currentNode) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NULL <br /></code>bcf635f2655647e0558b1b6f9b549880Mon, 19 Apr 2010 03:43:21 GMTmikeash - 2010-04-19 01:01:40http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsThanks for pointing that out. HTMLification gone wrong. I've fixed it now.c0bbd5af861c3a25013d405c20a97bcfMon, 19 Apr 2010 01:01:40 GMTMichael Babin - 2010-04-18 15:44:29http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsIn the "Returning One Object At a Time" example: <br /> <br /><code> <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// otherwise, point itemsPtr at the node's value <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state-&gt;itemsPtr = &amp;currentNode-;&gt;value <br /></code> <br /> <br />Believe there is a superfluous semi-colon there. <br /> <br />Thanks for the article and the entire series.45348a66b543aa0c19a8d94b9a8bf5f0Sun, 18 Apr 2010 15:44:29 GMTSkirwan - 2010-04-18 03:22:35http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsThat's what I get for running my mouth at a higher clock than my brain. Apologies. 6ed365af1ba9546445a6c632498b444fSun, 18 Apr 2010 03:22:35 GMTmikeash - 2010-04-16 19:53:53http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#comments<b>Aaron:</b> You are correct, fixed, and thanks. <br /> <br /><b>Skirwan:</b> It would be nice if people would check these claims before making them. Mac OS X uses LP64, which means that <code>long</code> is 64 bits. That is the precise reason why these fields are of type <code>long</code>.26cff2d27edef40902ac77c8f650ad36Fri, 16 Apr 2010 19:53:53 GMTCharles Parnot - 2010-04-16 19:43:55http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsSkirwan: actually, I believe longs are 64-bit on Mac OS X (when compiling for 64 bits), see LP64 on this web page: <br /><a href="http://developer.apple.com/macosx/64bit.html">http://developer.apple.com/macosx/64bit.html</a>b7785c7647922aaba46e4fd73dc7d70eFri, 16 Apr 2010 19:43:55 GMTSkirwan - 2010-04-16 19:23:31http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsIt'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).8917858e7b5c632c613748d415c753d0Fri, 16 Apr 2010 19:23:31 GMTAaron - 2010-04-16 18:42:50http://www.mikeash.com/?page=pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html#commentsI think you mean: <br /> <br />firstLoop = NO; <br /> <br />in the while loop example.77e0841f79f025c22f8ac2fc30815d28Fri, 16 Apr 2010 18:42:50 GMT