]> granicus.if.org Git - python/commitdiff
Update comments and add an optimized path for Counter.update().
authorRaymond Hettinger <python@rcn.com>
Thu, 22 Jan 2009 09:05:43 +0000 (09:05 +0000)
committerRaymond Hettinger <python@rcn.com>
Thu, 22 Jan 2009 09:05:43 +0000 (09:05 +0000)
Lib/collections.py

index eb13f4d2fd860de4700f7f26f53f99c1a2f5d53b..effae7dd8db734888edd512be5418af08467b66f 100644 (file)
@@ -253,8 +253,11 @@ class Counter(dict):
 
         if iterable is not None:
             if isinstance(iterable, Mapping):
-                for elem, count in iterable.iteritems():
-                    self[elem] += count
+                if self:
+                    for elem, count in iterable.iteritems():
+                        self[elem] += count
+                else:
+                    dict.update(self, iterable) # fast path when counter is empty
             else:
                 for elem in iterable:
                     self[elem] += 1
@@ -280,7 +283,6 @@ class Counter(dict):
     #       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: