_orderedbidict.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Joshua Bronson. All Rights Reserved.
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. #==============================================================================
  8. # * Welcome to the bidict source code *
  9. #==============================================================================
  10. # Doing a code review? You'll find a "Code review nav" comment like the one
  11. # below at the top and bottom of the most important source files. This provides
  12. # a suggested initial path through the source when reviewing.
  13. #
  14. # Note: If you aren't reading this on https://github.com/jab/bidict, you may be
  15. # viewing an outdated version of the code. Please head to GitHub to review the
  16. # latest version, which contains important improvements over older versions.
  17. #
  18. # Thank you for reading and for any feedback you provide.
  19. # * Code review nav *
  20. #==============================================================================
  21. # ← Prev: _frozenordered.py Current: _orderedbidict.py <FIN>
  22. #==============================================================================
  23. """Provides :class:`OrderedBidict`."""
  24. from ._mut import _MutableBidict
  25. from ._orderedbase import OrderedBidictBase
  26. class OrderedBidict(OrderedBidictBase, _MutableBidict):
  27. """Mutable bidict type that maintains items in insertion order."""
  28. __slots__ = ()
  29. __hash__ = None # since this class is mutable; explicit > implicit.
  30. def clear(self):
  31. """Remove all items."""
  32. self._fwdm.clear()
  33. self._invm.clear()
  34. self._sntl.nxt = self._sntl.prv = self._sntl
  35. def popitem(self, last=True): # pylint: disable=arguments-differ
  36. u"""*x.popitem() → (k, v)*
  37. Remove and return the most recently added item as a (key, value) pair
  38. if *last* is True, else the least recently added item.
  39. :raises KeyError: if *x* is empty.
  40. """
  41. if not self:
  42. raise KeyError('mapping is empty')
  43. key = next((reversed if last else iter)(self))
  44. val = self._pop(key)
  45. return key, val
  46. def move_to_end(self, key, last=True):
  47. """Move an existing key to the beginning or end of this ordered bidict.
  48. The item is moved to the end if *last* is True, else to the beginning.
  49. :raises KeyError: if the key does not exist
  50. """
  51. node = self._fwdm[key]
  52. node.prv.nxt = node.nxt
  53. node.nxt.prv = node.prv
  54. sntl = self._sntl
  55. if last:
  56. last = sntl.prv
  57. node.prv = last
  58. node.nxt = sntl
  59. sntl.prv = last.nxt = node
  60. else:
  61. first = sntl.nxt
  62. node.prv = sntl
  63. node.nxt = first
  64. sntl.nxt = first.prv = node
  65. # * Code review nav *
  66. #==============================================================================
  67. # ← Prev: _frozenordered.py Current: _orderedbidict.py <FIN>
  68. #==============================================================================