ordered_dict.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # Unmodified from http://code.activestate.com/recipes/576693/
  2. # other than to add MIT license header (as specified on page, but not in code).
  3. # Linked from Python documentation here:
  4. # http://docs.python.org/2/library/collections.html#collections.OrderedDict
  5. #
  6. # This should be deleted once Py2.7 is available on all bots, see
  7. # http://crbug.com/241769.
  8. #
  9. # Copyright (c) 2009 Raymond Hettinger.
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documentation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furnished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. # Backport of OrderedDict() class that runs on Python 2.4, 2.5, 2.6, 2.7 and pypy.
  29. # Passes Python2.7's test suite and incorporates all the latest updates.
  30. try:
  31. from thread import get_ident as _get_ident
  32. except ImportError:
  33. from dummy_thread import get_ident as _get_ident
  34. try:
  35. from _abcoll import KeysView, ValuesView, ItemsView
  36. except ImportError:
  37. pass
  38. class OrderedDict(dict):
  39. 'Dictionary that remembers insertion order'
  40. # An inherited dict maps keys to values.
  41. # The inherited dict provides __getitem__, __len__, __contains__, and get.
  42. # The remaining methods are order-aware.
  43. # Big-O running times for all methods are the same as for regular dictionaries.
  44. # The internal self.__map dictionary maps keys to links in a doubly linked list.
  45. # The circular doubly linked list starts and ends with a sentinel element.
  46. # The sentinel element never gets deleted (this simplifies the algorithm).
  47. # Each link is stored as a list of length three: [PREV, NEXT, KEY].
  48. def __init__(self, *args, **kwds):
  49. '''Initialize an ordered dictionary. Signature is the same as for
  50. regular dictionaries, but keyword arguments are not recommended
  51. because their insertion order is arbitrary.
  52. '''
  53. if len(args) > 1:
  54. raise TypeError('expected at most 1 arguments, got %d' % len(args))
  55. try:
  56. self.__root
  57. except AttributeError:
  58. self.__root = root = [] # sentinel node
  59. root[:] = [root, root, None]
  60. self.__map = {}
  61. self.__update(*args, **kwds)
  62. def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
  63. 'od.__setitem__(i, y) <==> od[i]=y'
  64. # Setting a new item creates a new link which goes at the end of the linked
  65. # list, and the inherited dictionary is updated with the new key/value pair.
  66. if key not in self:
  67. root = self.__root
  68. last = root[0]
  69. last[1] = root[0] = self.__map[key] = [last, root, key]
  70. dict_setitem(self, key, value)
  71. def __delitem__(self, key, dict_delitem=dict.__delitem__):
  72. 'od.__delitem__(y) <==> del od[y]'
  73. # Deleting an existing item uses self.__map to find the link which is
  74. # then removed by updating the links in the predecessor and successor nodes.
  75. dict_delitem(self, key)
  76. link_prev, link_next, key = self.__map.pop(key)
  77. link_prev[1] = link_next
  78. link_next[0] = link_prev
  79. def __iter__(self):
  80. 'od.__iter__() <==> iter(od)'
  81. root = self.__root
  82. curr = root[1]
  83. while curr is not root:
  84. yield curr[2]
  85. curr = curr[1]
  86. def __reversed__(self):
  87. 'od.__reversed__() <==> reversed(od)'
  88. root = self.__root
  89. curr = root[0]
  90. while curr is not root:
  91. yield curr[2]
  92. curr = curr[0]
  93. def clear(self):
  94. 'od.clear() -> None. Remove all items from od.'
  95. try:
  96. for node in self.__map.itervalues():
  97. del node[:]
  98. root = self.__root
  99. root[:] = [root, root, None]
  100. self.__map.clear()
  101. except AttributeError:
  102. pass
  103. dict.clear(self)
  104. def popitem(self, last=True):
  105. '''od.popitem() -> (k, v), return and remove a (key, value) pair.
  106. Pairs are returned in LIFO order if last is true or FIFO order if false.
  107. '''
  108. if not self:
  109. raise KeyError('dictionary is empty')
  110. root = self.__root
  111. if last:
  112. link = root[0]
  113. link_prev = link[0]
  114. link_prev[1] = root
  115. root[0] = link_prev
  116. else:
  117. link = root[1]
  118. link_next = link[1]
  119. root[1] = link_next
  120. link_next[0] = root
  121. key = link[2]
  122. del self.__map[key]
  123. value = dict.pop(self, key)
  124. return key, value
  125. # -- the following methods do not depend on the internal structure --
  126. def keys(self):
  127. 'od.keys() -> list of keys in od'
  128. return list(self)
  129. def values(self):
  130. 'od.values() -> list of values in od'
  131. return [self[key] for key in self]
  132. def items(self):
  133. 'od.items() -> list of (key, value) pairs in od'
  134. return [(key, self[key]) for key in self]
  135. def iterkeys(self):
  136. 'od.iterkeys() -> an iterator over the keys in od'
  137. return iter(self)
  138. def itervalues(self):
  139. 'od.itervalues -> an iterator over the values in od'
  140. for k in self:
  141. yield self[k]
  142. def iteritems(self):
  143. 'od.iteritems -> an iterator over the (key, value) items in od'
  144. for k in self:
  145. yield (k, self[k])
  146. # Suppress 'OrderedDict.update: Method has no argument':
  147. # pylint: disable=E0211
  148. def update(*args, **kwds):
  149. '''od.update(E, **F) -> None. Update od from dict/iterable E and F.
  150. If E is a dict instance, does: for k in E: od[k] = E[k]
  151. If E has a .keys() method, does: for k in E.keys(): od[k] = E[k]
  152. Or if E is an iterable of items, does: for k, v in E: od[k] = v
  153. In either case, this is followed by: for k, v in F.items(): od[k] = v
  154. '''
  155. if len(args) > 2:
  156. raise TypeError('update() takes at most 2 positional '
  157. 'arguments (%d given)' % (len(args),))
  158. elif not args:
  159. raise TypeError('update() takes at least 1 argument (0 given)')
  160. self = args[0]
  161. # Make progressively weaker assumptions about "other"
  162. other = ()
  163. if len(args) == 2:
  164. other = args[1]
  165. if isinstance(other, dict):
  166. for key in other:
  167. self[key] = other[key]
  168. elif hasattr(other, 'keys'):
  169. for key in other.keys():
  170. self[key] = other[key]
  171. else:
  172. for key, value in other:
  173. self[key] = value
  174. for key, value in kwds.items():
  175. self[key] = value
  176. __update = update # let subclasses override update without breaking __init__
  177. __marker = object()
  178. def pop(self, key, default=__marker):
  179. '''od.pop(k[,d]) -> v, remove specified key and return the corresponding value.
  180. If key is not found, d is returned if given, otherwise KeyError is raised.
  181. '''
  182. if key in self:
  183. result = self[key]
  184. del self[key]
  185. return result
  186. if default is self.__marker:
  187. raise KeyError(key)
  188. return default
  189. def setdefault(self, key, default=None):
  190. 'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
  191. if key in self:
  192. return self[key]
  193. self[key] = default
  194. return default
  195. def __repr__(self, _repr_running={}):
  196. 'od.__repr__() <==> repr(od)'
  197. call_key = id(self), _get_ident()
  198. if call_key in _repr_running:
  199. return '...'
  200. _repr_running[call_key] = 1
  201. try:
  202. if not self:
  203. return '%s()' % (self.__class__.__name__,)
  204. return '%s(%r)' % (self.__class__.__name__, self.items())
  205. finally:
  206. del _repr_running[call_key]
  207. def __reduce__(self):
  208. 'Return state information for pickling'
  209. items = [[k, self[k]] for k in self]
  210. inst_dict = vars(self).copy()
  211. for k in vars(OrderedDict()):
  212. inst_dict.pop(k, None)
  213. if inst_dict:
  214. return (self.__class__, (items,), inst_dict)
  215. return self.__class__, (items,)
  216. def copy(self):
  217. 'od.copy() -> a shallow copy of od'
  218. return self.__class__(self)
  219. @classmethod
  220. def fromkeys(cls, iterable, value=None):
  221. '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S
  222. and values equal to v (which defaults to None).
  223. '''
  224. d = cls()
  225. for key in iterable:
  226. d[key] = value
  227. return d
  228. def __eq__(self, other):
  229. '''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
  230. while comparison to a regular mapping is order-insensitive.
  231. '''
  232. if isinstance(other, OrderedDict):
  233. return len(self)==len(other) and self.items() == other.items()
  234. return dict.__eq__(self, other)
  235. def __ne__(self, other):
  236. return not self == other
  237. # -- the following methods are only used in Python 2.7 --
  238. def viewkeys(self):
  239. "od.viewkeys() -> a set-like object providing a view on od's keys"
  240. return KeysView(self)
  241. def viewvalues(self):
  242. "od.viewvalues() -> an object providing a view on od's values"
  243. return ValuesView(self)
  244. def viewitems(self):
  245. "od.viewitems() -> a set-like object providing a view on od's items"
  246. return ItemsView(self)