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
dc879f03
Commit
dc879f03
authored
Mar 19, 2009
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Forward port r70470 and r70473 for OrderedDict to use a doubly linked list.
parent
6cf17aac
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
17 deletions
+44
-17
Doc/library/collections.rst
Doc/library/collections.rst
+10
-2
Lib/collections.py
Lib/collections.py
+27
-15
Lib/test/test_collections.py
Lib/test/test_collections.py
+7
-0
No files found.
Doc/library/collections.rst
View file @
dc879f03
...
...
@@ -836,8 +836,11 @@ the items are returned in the order their keys were first added.
.. versionadded:: 3.1
The :meth:`popitem` method for ordered dictionaries returns and removes the
last added entry. The key/value pairs are returned in LIFO order.
.. method:: OrderedDict.popitem(last=True)
The :meth:`popitem` method for ordered dictionaries returns and removes
a (key, value) pair. The pairs are returned in LIFO order if *last* is
true or FIFO order if false.
Equality tests between :class:`OrderedDict` objects are order-sensitive
and are implemented as ``list(od1.items())==list(od2.items())``.
...
...
@@ -846,6 +849,11 @@ Equality tests between :class:`OrderedDict` objects and other
This allows :class:`OrderedDict` objects to be substituted anywhere a
regular dictionary is used.
.. seealso::
`Equivalent OrderedDict recipe <http://code.activestate.com/recipes/576693/>`_
that runs on Python 2.4 or later.
:class:`UserDict` objects
-------------------------
...
...
Lib/collections.py
View file @
dc879f03
...
...
@@ -23,45 +23,57 @@ class OrderedDict(dict, MutableMapping):
if
len
(
args
)
>
1
:
raise
TypeError
(
'expected at most 1 arguments, got %d'
%
len
(
args
))
try
:
self
.
__
keys
self
.
__
end
except
AttributeError
:
# Note the underlying data structure for this class is likely to
# change in the future. Do not rely on it or access it directly.
self
.
__keys
=
deque
()
self
.
clear
()
self
.
update
(
*
args
,
**
kwds
)
def
clear
(
self
):
self
.
__keys
.
clear
()
self
.
__end
=
end
=
[]
end
+=
[
None
,
end
,
end
]
# sentinel node for doubly linked list
self
.
__map
=
{}
# key --> [key, prev, next]
dict
.
clear
(
self
)
def
__setitem__
(
self
,
key
,
value
):
if
key
not
in
self
:
self
.
__keys
.
append
(
key
)
end
=
self
.
__end
curr
=
end
[
1
]
curr
[
2
]
=
end
[
1
]
=
self
.
__map
[
key
]
=
[
key
,
curr
,
end
]
dict
.
__setitem__
(
self
,
key
,
value
)
def
__delitem__
(
self
,
key
):
dict
.
__delitem__
(
self
,
key
)
self
.
__keys
.
remove
(
key
)
key
,
prev
,
next
=
self
.
__map
.
pop
(
key
)
prev
[
2
]
=
next
next
[
1
]
=
prev
def
__iter__
(
self
):
return
iter
(
self
.
__keys
)
end
=
self
.
__end
curr
=
end
[
2
]
while
curr
is
not
end
:
yield
curr
[
0
]
curr
=
curr
[
2
]
def
__reversed__
(
self
):
return
reversed
(
self
.
__keys
)
end
=
self
.
__end
curr
=
end
[
1
]
while
curr
is
not
end
:
yield
curr
[
0
]
curr
=
curr
[
1
]
def
popitem
(
self
):
def
popitem
(
self
,
last
=
True
):
if
not
self
:
raise
KeyError
(
'dictionary is empty'
)
key
=
self
.
__keys
.
pop
(
)
value
=
dict
.
pop
(
self
,
key
)
key
=
next
(
reversed
(
self
))
if
last
else
next
(
iter
(
self
)
)
value
=
self
.
pop
(
key
)
return
key
,
value
def
__reduce__
(
self
):
items
=
[[
k
,
self
[
k
]]
for
k
in
self
]
tmp
=
self
.
__
keys
del
self
.
__
keys
tmp
=
self
.
__
map
,
self
.
__end
del
self
.
__
map
,
self
.
__end
inst_dict
=
vars
(
self
).
copy
()
self
.
__
keys
=
tmp
self
.
__
map
,
self
.
__end
=
tmp
if
inst_dict
:
return
(
self
.
__class__
,
(
items
,),
inst_dict
)
return
self
.
__class__
,
(
items
,)
...
...
Lib/test/test_collections.py
View file @
dc879f03
...
...
@@ -770,12 +770,19 @@ class TestOrderedDict(unittest.TestCase):
class
GeneralMappingTests
(
mapping_tests
.
BasicTestMappingProtocol
):
type2test
=
OrderedDict
def
test_popitem
(
self
):
d
=
self
.
_empty_mapping
()
self
.
assertRaises
(
KeyError
,
d
.
popitem
)
class
MyOrderedDict
(
OrderedDict
):
pass
class
SubclassMappingTests
(
mapping_tests
.
BasicTestMappingProtocol
):
type2test
=
MyOrderedDict
def
test_popitem
(
self
):
d
=
self
.
_empty_mapping
()
self
.
assertRaises
(
KeyError
,
d
.
popitem
)
import
doctest
,
collections
...
...
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