Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28370

Re: set and dict iteration

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'suppose': 0.07; 'python': 0.09; 'agree,': 0.09; 'any.': 0.09; 'bool': 0.09; 'exception,': 0.09; 'longs': 0.09; 'sep': 0.09; 'subject:set': 0.09; 'throw': 0.09; 'bug': 0.10; 'cc:addr:python-list': 0.10; "wouldn't": 0.11; 'index': 0.13; 'cases': 0.15; 'modification': 0.15; 'bug,': 0.16; 'count.': 0.16; 'flag,': 0.16; 'increment': 0.16; 'iterating': 0.16; 'iterator': 0.16; 'iterator.': 0.16; 'iterators,': 0.16; 'justified': 0.16; 'overflow.': 0.16; 'set,': 0.16; 'wrote:': 0.17; 'byte': 0.17; 'detect': 0.17; 'instance': 0.17; 'pointer': 0.17; 'version.': 0.17; '>>>': 0.18; "i'd": 0.22; 'cc:2**0': 0.23; 'monday,': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'testing': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'prevent': 0.27; 'set.': 0.27; 'faster,': 0.29; 'long.': 0.29; 'overhead': 0.29; 'wrap': 0.29; 'reset': 0.29; 'figure': 0.30; 'point': 0.31; 'problem.': 0.32; 'structure': 0.32; 'could': 0.32; 'int': 0.33; 'another': 0.33; 'that,': 0.34; 'version': 0.34; 'done': 0.34; 'faster': 0.35; 'open': 0.35; 'doing': 0.35; 'pm,': 0.35; 'there': 0.35; 'next': 0.35; 'created': 0.36; 'but': 0.36; 'enough': 0.36; 'possible': 0.37; 'keeps': 0.37; 'two': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'received:192': 0.39; 'easily': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'most': 0.61; 'exceed': 0.65; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'counts': 0.81; 'flag.': 0.84; 'technique': 0.93
Date Mon, 03 Sep 2012 16:27:38 -0400
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version 1.0
To Aaron Brady <castironpi@gmail.com>
Subject Re: set and dict iteration
References <b8dd3aca-2a87-4124-ad6e-66a8720af99a@googlegroups.com> <mailman.3389.1345146609.4697.python-list@python.org> <7xy5le7cli.fsf@ruckus.brouhaha.com> <mailman.3404.1345158704.4697.python-list@python.org> <502dab6c$0$29978$c3e8da3$5496439d@news.astraweb.com> <fe95c29c-2289-4e9c-870e-e3c475f13459@googlegroups.com> <mailman.3435.1345240665.4697.python-list@python.org> <d4708687-2925-421a-b755-969d6dac731a@googlegroups.com> <mailman.3476.1345328046.4697.python-list@python.org> <mailman.3480.1345343315.4697.python-list@python.org> <c7452db1-5b78-4d54-81a1-1c8683631d6e@googlegroups.com> <mailman.3883.1346095064.4697.python-list@python.org> <1567e8c7-a2bb-41f4-9be8-18e9f4d063cb@googlegroups.com> <mailman.154.1346700607.27098.python-list@python.org> <ed8c3b7a-f9ff-45fe-9e2c-a225764da7ae@googlegroups.com>
In-Reply-To <ed8c3b7a-f9ff-45fe-9e2c-a225764da7ae@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:mfy8OtjOOuHtIUa1q6cRHlnd5b8SW5XJzO4zgmZBkPg EP3F8CbT6I77DvE3SNhUuax/Wbpo15tKRjSPQIvfWopCwIM5uq og9CnP+LKfYjYkXli5b1hlF091ba6XW5hKf1TzQ84zGFFSWY49 bO7YeJfGbfnoZjssFh0dtGPvWskVWBSQHph7FFndkUZYvDlunE 1BiwXnzibiORVSd5T7p+jGM655GJR1/emwO9Kh7+pMsgXx9W5I xhjm11Xbbgs3W1DrX724aOTrQ7RJVBaDJ6NFaTIx3YNxwG4kKt MkhEgGZXuX1eBTGzOfECghBq1bYkkeiftLS/T1Kl2BvEiKKTw= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.156.1346704107.27098.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346704107 news.xs4all.nl 6878 [2001:888:2000:d::a6]:53797
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28370

Show key headers only | View raw


On 09/03/2012 04:04 PM, Aaron Brady wrote:
> On Monday, September 3, 2012 2:30:24 PM UTC-5, Ian wrote:
>> On Sun, Sep 2, 2012 at 11:43 AM, Aaron Brady <castironpi@gmail.com> wrote:
>>
>>> We could use a Python long object for the version index to prevent overflow.  Combined with P. Rubin's idea to count the number of open iterators, most use cases still wouldn't exceed a single word comparison; we could reset the counter when there weren't any.
>>
>>
>> We could use a Python long; I just don't think the extra overhead is
>>
>> justified in a data structure that is already highly optimized for
>>
>> speed.  Incrementing and testing a C int is *much* faster than doing
>>
>> the same with a Python long.
> I think the technique would require two python longs and a bool in the set, and a python long in the iterator.
>
> One long counts the number of existing (open) iterators.  Another counts the version.  The bool keeps track of whether an iterator has been created since the last modification, in which case the next modification requires incrementing the version and resetting the flag.

I think you're over-engineering the problem.  it's a bug if an iterator
is used after some change is made to the set it's iterating over.  We
don't need to catch every possible instance of the bug, that's what
testing is for.  The point is to "probably" detect it, and for that, all
we need is a counter in the set and a counter in the open iterator. 
Whenever changing the set, increment its count.  And whenever iterating,
check the two counters.  if they don't agree, throw an exception, and
destroy the iterator.  i suppose that could be done with a flag, but it
could just as easily be done by zeroing the pointer to the set.

I'd figure a byte or two would be good enough for the counts, but a C
uint would be somewhat faster, and wouldn't wrap as quickly.

-- 

DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: set and dict iteration Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-27 13:17 -0600
  Re: set and dict iteration Aaron Brady <castironpi@gmail.com> - 2012-09-02 10:43 -0700
    Re: set and dict iteration Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-03 13:29 -0600
      Re: set and dict iteration Aaron Brady <castironpi@gmail.com> - 2012-09-03 13:04 -0700
        Re: set and dict iteration Dave Angel <d@davea.name> - 2012-09-03 16:27 -0400
          Re: set and dict iteration Aaron Brady <castironpi@gmail.com> - 2012-09-03 17:24 -0700
          Re: set and dict iteration Aaron Brady <castironpi@gmail.com> - 2012-09-03 17:24 -0700
      Re: set and dict iteration Aaron Brady <castironpi@gmail.com> - 2012-09-03 13:04 -0700
        Re: set and dict iteration Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-04 01:26 +0000
          Re: set and dict iteration Dave Angel <d@davea.name> - 2012-09-03 21:50 -0400
            Re: set and dict iteration Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-04 01:59 +0000
              Re: set and dict iteration Aaron Brady <castironpi@gmail.com> - 2012-09-08 08:42 -0700
  Re: set and dict iteration Aaron Brady <castironpi@gmail.com> - 2012-09-02 10:43 -0700

csiph-web