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
331722d4
Commit
331722d4
authored
Sep 02, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make OrderedDict.popitem() a bit smarter and faster
parent
19e5a6fb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
11 deletions
+23
-11
Lib/collections.py
Lib/collections.py
+23
-11
No files found.
Lib/collections.py
View file @
331722d4
...
...
@@ -108,25 +108,37 @@ class OrderedDict(dict, MutableMapping):
pass
dict
.
clear
(
self
)
setdefault
=
MutableMapping
.
setdefault
update
=
MutableMapping
.
update
pop
=
MutableMapping
.
pop
keys
=
MutableMapping
.
keys
values
=
MutableMapping
.
values
items
=
MutableMapping
.
items
__ne__
=
MutableMapping
.
__ne__
def
popitem
(
self
,
last
=
True
):
def
popitem
(
self
,
last
=
True
,
PREV
=
0
,
NEXT
=
1
,
KEY
=
2
,
dict_pop
=
dict
.
pop
):
'''od.popitem() -> (k, v), return and remove a (key, value) pair.
Pairs are returned in LIFO order if last is true or FIFO order if false.
'''
if
not
self
:
raise
KeyError
(
'dictionary is empty'
)
key
=
next
(
reversed
(
self
)
if
last
else
iter
(
self
))
value
=
self
.
pop
(
key
)
root
=
self
.
__root
if
last
:
# link_prev <--> link <--> root
link
=
root
[
PREV
]
link_prev
=
link
[
PREV
]
link_prev
[
NEXT
]
=
root
root
[
PREV
]
=
link_prev
else
:
# root <--> link <--> link_next
link
=
root
[
NEXT
]
link_next
=
link
[
NEXT
]
root
[
NEXT
]
=
link_next
link_next
[
PREV
]
=
root
key
=
link
[
KEY
]
del
self
.
__map
[
key
]
value
=
dict_pop
(
self
,
key
)
return
key
,
value
setdefault
=
MutableMapping
.
setdefault
update
=
MutableMapping
.
update
pop
=
MutableMapping
.
pop
keys
=
MutableMapping
.
keys
values
=
MutableMapping
.
values
items
=
MutableMapping
.
items
__ne__
=
MutableMapping
.
__ne__
def
__repr__
(
self
):
'od.__repr__() <==> repr(od)'
if
not
self
:
...
...
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