diff options
author | Raymond Hettinger <python@rcn.com> | 2011-04-18 02:49:29 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2011-04-18 02:49:29 (GMT) |
commit | 90375bc7c095f1bc7971f97f1082fc4851f62487 (patch) | |
tree | 9f24374cdb89e6a1baf2c1b5ac166406267c46de /Lib/collections/__init__.py | |
parent | 2476ee32b181a3270b7ae85b60f21e64be9df13b (diff) | |
download | cpython-90375bc7c095f1bc7971f97f1082fc4851f62487.zip cpython-90375bc7c095f1bc7971f97f1082fc4851f62487.tar.gz cpython-90375bc7c095f1bc7971f97f1082fc4851f62487.tar.bz2 |
Rework multiset methods to use less memory and to make fewer calls to __hash__.
Diffstat (limited to 'Lib/collections/__init__.py')
-rw-r--r-- | Lib/collections/__init__.py | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index bd19614..a6bcaea 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -601,10 +601,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): @@ -617,10 +620,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): @@ -633,11 +639,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): @@ -650,11 +659,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 |