Commit dd01f8f3 authored by Raymond Hettinger's avatar Raymond Hettinger

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

parent 94adc8e4
...@@ -255,8 +255,11 @@ class Counter(dict): ...@@ -255,8 +255,11 @@ class Counter(dict):
if iterable is not None: if iterable is not None:
if isinstance(iterable, Mapping): if isinstance(iterable, Mapping):
if self:
for elem, count in iterable.items(): for elem, count in iterable.items():
self[elem] += count self[elem] += count
else:
dict.update(self, iterable) # fast path when counter is empty
else: else:
for elem in iterable: for elem in iterable:
self[elem] += 1 self[elem] += 1
...@@ -282,7 +285,6 @@ class Counter(dict): ...@@ -282,7 +285,6 @@ class Counter(dict):
# Knuth TAOCP Volume II section 4.6.3 exercise 19 # Knuth TAOCP Volume II section 4.6.3 exercise 19
# and at http://en.wikipedia.org/wiki/Multiset # and at http://en.wikipedia.org/wiki/Multiset
# #
# Results are undefined when inputs contain negative counts.
# Outputs guaranteed to only include positive counts. # Outputs guaranteed to only include positive counts.
# #
# To strip negative and zero counts, add-in an empty counter: # To strip negative and zero counts, add-in an empty counter:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment