summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-04-18 02:47:24 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-04-18 02:47:24 (GMT)
commitc15d9e759fbbaf54cc97ab41a067ac637c296781 (patch)
treead54968d0a253fbdefcb57c48a7297702c65acf2 /Lib
parent2409d770d9eefd17133b2446c1cb9719e3081726 (diff)
parent2876a8c272ec9fd7ddd9fec62676bc61ba947747 (diff)
downloadcpython-c15d9e759fbbaf54cc97ab41a067ac637c296781.zip
cpython-c15d9e759fbbaf54cc97ab41a067ac637c296781.tar.gz
cpython-c15d9e759fbbaf54cc97ab41a067ac637c296781.tar.bz2
Rework multiset methods to use less memory and to make fewer calls to __hash__.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/collections.py31
1 files changed, 19 insertions, 12 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index c77b089..cde734d 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -572,10 +572,13 @@ class Counter(dict):
if not isinstance(other, Counter):
return NotImplemented
result = Counter()
- for elem in set(self) | set(other):
- newcount = self[elem] + other[elem]
+ for elem, count in self.items():
+ newcount = count + other[elem]
if newcount > 0:
result[elem] = newcount
+ for elem, count in other.items():
+ if elem not in self and count > 0:
+ result[elem] = count
return result
def __sub__(self, other):
@@ -588,10 +591,13 @@ class Counter(dict):
if not isinstance(other, Counter):
return NotImplemented
result = Counter()
- for elem in set(self) | set(other):
- newcount = self[elem] - other[elem]
+ for elem, count in self.items():
+ newcount = count - other[elem]
if newcount > 0:
result[elem] = newcount
+ for elem, count in other.items():
+ if elem not in self and count < 0:
+ result[elem] = 0 - count
return result
def __or__(self, other):
@@ -604,11 +610,14 @@ class Counter(dict):
if not isinstance(other, Counter):
return NotImplemented
result = Counter()
- for elem in set(self) | set(other):
- p, q = self[elem], other[elem]
- newcount = q if p < q else p
+ for elem, count in self.items():
+ other_count = other[elem]
+ newcount = other_count if count < other_count else count
if newcount > 0:
result[elem] = newcount
+ for elem, count in other.items():
+ if elem not in self and count > 0:
+ result[elem] = count
return result
def __and__(self, other):
@@ -621,11 +630,9 @@ class Counter(dict):
if not isinstance(other, Counter):
return NotImplemented
result = Counter()
- if len(self) < len(other):
- self, other = other, self
- for elem in filter(self.__contains__, other):
- p, q = self[elem], other[elem]
- newcount = p if p < q else q
+ for elem, count in self.items():
+ other_count = other[elem]
+ newcount = count if count < other_count else other_count
if newcount > 0:
result[elem] = newcount
return result