[Python-checkins] r68854 - python/branches/py3k/Lib/collections.py

raymond.hettinger python-checkins at python.org
Thu Jan 22 10:09:55 CET 2009


Author: raymond.hettinger
Date: Thu Jan 22 10:09:55 2009
New Revision: 68854

Log:
Update comments and add an optimized path for Counter.update().

Modified:
   python/branches/py3k/Lib/collections.py

Modified: python/branches/py3k/Lib/collections.py
==============================================================================
--- python/branches/py3k/Lib/collections.py	(original)
+++ python/branches/py3k/Lib/collections.py	Thu Jan 22 10:09:55 2009
@@ -255,8 +255,11 @@
 
         if iterable is not None:
             if isinstance(iterable, Mapping):
-                for elem, count in iterable.items():
-                    self[elem] += count
+                if self:
+                    for elem, count in iterable.items():
+                        self[elem] += count
+                else:
+                    dict.update(self, iterable) # fast path when counter is empty
             else:
                 for elem in iterable:
                     self[elem] += 1
@@ -282,7 +285,6 @@
     #       Knuth TAOCP Volume II section 4.6.3 exercise 19
     #       and at http://en.wikipedia.org/wiki/Multiset
     #
-    # Results are undefined when inputs contain negative counts.
     # Outputs guaranteed to only include positive counts.
     #
     # To strip negative and zero counts, add-in an empty counter:


More information about the Python-checkins mailing list