Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
2876a8c2
Commit
2876a8c2
authored
Apr 17, 2011
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rework multiset methods to use less memory and to make fewer calls to __hash__.
parent
340d2690
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
12 deletions
+19
-12
Lib/collections.py
Lib/collections.py
+19
-12
No files found.
Lib/collections.py
View file @
2876a8c2
...
@@ -495,10 +495,13 @@ class Counter(dict):
...
@@ -495,10 +495,13 @@ class Counter(dict):
if
not
isinstance
(
other
,
Counter
):
if
not
isinstance
(
other
,
Counter
):
return
NotImplemented
return
NotImplemented
result
=
Counter
()
result
=
Counter
()
for
elem
in
set
(
self
)
|
set
(
other
):
for
elem
,
count
in
self
.
items
(
):
newcount
=
self
[
elem
]
+
other
[
elem
]
newcount
=
count
+
other
[
elem
]
if
newcount
>
0
:
if
newcount
>
0
:
result
[
elem
]
=
newcount
result
[
elem
]
=
newcount
for
elem
,
count
in
other
.
items
():
if
elem
not
in
self
and
count
>
0
:
result
[
elem
]
=
count
return
result
return
result
def
__sub__
(
self
,
other
):
def
__sub__
(
self
,
other
):
...
@@ -511,10 +514,13 @@ class Counter(dict):
...
@@ -511,10 +514,13 @@ class Counter(dict):
if
not
isinstance
(
other
,
Counter
):
if
not
isinstance
(
other
,
Counter
):
return
NotImplemented
return
NotImplemented
result
=
Counter
()
result
=
Counter
()
for
elem
in
set
(
self
)
|
set
(
other
):
for
elem
,
count
in
self
.
items
(
):
newcount
=
self
[
elem
]
-
other
[
elem
]
newcount
=
count
-
other
[
elem
]
if
newcount
>
0
:
if
newcount
>
0
:
result
[
elem
]
=
newcount
result
[
elem
]
=
newcount
for
elem
,
count
in
other
.
items
():
if
elem
not
in
self
and
count
<
0
:
result
[
elem
]
=
0
-
count
return
result
return
result
def
__or__
(
self
,
other
):
def
__or__
(
self
,
other
):
...
@@ -527,11 +533,14 @@ class Counter(dict):
...
@@ -527,11 +533,14 @@ class Counter(dict):
if
not
isinstance
(
other
,
Counter
):
if
not
isinstance
(
other
,
Counter
):
return
NotImplemented
return
NotImplemented
result
=
Counter
()
result
=
Counter
()
for
elem
in
set
(
self
)
|
set
(
other
):
for
elem
,
count
in
self
.
items
(
):
p
,
q
=
self
[
elem
],
other
[
elem
]
other_count
=
other
[
elem
]
newcount
=
q
if
p
<
q
else
p
newcount
=
other_count
if
count
<
other_count
else
count
if
newcount
>
0
:
if
newcount
>
0
:
result
[
elem
]
=
newcount
result
[
elem
]
=
newcount
for
elem
,
count
in
other
.
items
():
if
elem
not
in
self
and
count
>
0
:
result
[
elem
]
=
count
return
result
return
result
def
__and__
(
self
,
other
):
def
__and__
(
self
,
other
):
...
@@ -544,11 +553,9 @@ class Counter(dict):
...
@@ -544,11 +553,9 @@ class Counter(dict):
if
not
isinstance
(
other
,
Counter
):
if
not
isinstance
(
other
,
Counter
):
return
NotImplemented
return
NotImplemented
result
=
Counter
()
result
=
Counter
()
if
len
(
self
)
<
len
(
other
):
for
elem
,
count
in
self
.
items
():
self
,
other
=
other
,
self
other_count
=
other
[
elem
]
for
elem
in
filter
(
self
.
__contains__
,
other
):
newcount
=
count
if
count
<
other_count
else
other_count
p
,
q
=
self
[
elem
],
other
[
elem
]
newcount
=
p
if
p
<
q
else
q
if
newcount
>
0
:
if
newcount
>
0
:
result
[
elem
]
=
newcount
result
[
elem
]
=
newcount
return
result
return
result
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment