_pdeque.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. from ._compat import Sequence, Hashable
  2. from itertools import islice, chain
  3. from numbers import Integral
  4. from pyrsistent._plist import plist
  5. class PDeque(object):
  6. """
  7. Persistent double ended queue (deque). Allows quick appends and pops in both ends. Implemented
  8. using two persistent lists.
  9. A maximum length can be specified to create a bounded queue.
  10. Fully supports the Sequence and Hashable protocols including indexing and slicing but
  11. if you need fast random access go for the PVector instead.
  12. Do not instantiate directly, instead use the factory functions :py:func:`dq` or :py:func:`pdeque` to
  13. create an instance.
  14. Some examples:
  15. >>> x = pdeque([1, 2, 3])
  16. >>> x.left
  17. 1
  18. >>> x.right
  19. 3
  20. >>> x[0] == x.left
  21. True
  22. >>> x[-1] == x.right
  23. True
  24. >>> x.pop()
  25. pdeque([1, 2])
  26. >>> x.pop() == x[:-1]
  27. True
  28. >>> x.popleft()
  29. pdeque([2, 3])
  30. >>> x.append(4)
  31. pdeque([1, 2, 3, 4])
  32. >>> x.appendleft(4)
  33. pdeque([4, 1, 2, 3])
  34. >>> y = pdeque([1, 2, 3], maxlen=3)
  35. >>> y.append(4)
  36. pdeque([2, 3, 4], maxlen=3)
  37. >>> y.appendleft(4)
  38. pdeque([4, 1, 2], maxlen=3)
  39. """
  40. __slots__ = ('_left_list', '_right_list', '_length', '_maxlen', '__weakref__')
  41. def __new__(cls, left_list, right_list, length, maxlen=None):
  42. instance = super(PDeque, cls).__new__(cls)
  43. instance._left_list = left_list
  44. instance._right_list = right_list
  45. instance._length = length
  46. if maxlen is not None:
  47. if not isinstance(maxlen, Integral):
  48. raise TypeError('An integer is required as maxlen')
  49. if maxlen < 0:
  50. raise ValueError("maxlen must be non-negative")
  51. instance._maxlen = maxlen
  52. return instance
  53. @property
  54. def right(self):
  55. """
  56. Rightmost element in dqueue.
  57. """
  58. return PDeque._tip_from_lists(self._right_list, self._left_list)
  59. @property
  60. def left(self):
  61. """
  62. Leftmost element in dqueue.
  63. """
  64. return PDeque._tip_from_lists(self._left_list, self._right_list)
  65. @staticmethod
  66. def _tip_from_lists(primary_list, secondary_list):
  67. if primary_list:
  68. return primary_list.first
  69. if secondary_list:
  70. return secondary_list[-1]
  71. raise IndexError('No elements in empty deque')
  72. def __iter__(self):
  73. return chain(self._left_list, self._right_list.reverse())
  74. def __repr__(self):
  75. return "pdeque({0}{1})".format(list(self),
  76. ', maxlen={0}'.format(self._maxlen) if self._maxlen is not None else '')
  77. __str__ = __repr__
  78. @property
  79. def maxlen(self):
  80. """
  81. Maximum length of the queue.
  82. """
  83. return self._maxlen
  84. def pop(self, count=1):
  85. """
  86. Return new deque with rightmost element removed. Popping the empty queue
  87. will return the empty queue. A optional count can be given to indicate the
  88. number of elements to pop. Popping with a negative index is the same as
  89. popleft. Executes in amortized O(k) where k is the number of elements to pop.
  90. >>> pdeque([1, 2]).pop()
  91. pdeque([1])
  92. >>> pdeque([1, 2]).pop(2)
  93. pdeque([])
  94. >>> pdeque([1, 2]).pop(-1)
  95. pdeque([2])
  96. """
  97. if count < 0:
  98. return self.popleft(-count)
  99. new_right_list, new_left_list = PDeque._pop_lists(self._right_list, self._left_list, count)
  100. return PDeque(new_left_list, new_right_list, max(self._length - count, 0), self._maxlen)
  101. def popleft(self, count=1):
  102. """
  103. Return new deque with leftmost element removed. Otherwise functionally
  104. equivalent to pop().
  105. >>> pdeque([1, 2]).popleft()
  106. pdeque([2])
  107. """
  108. if count < 0:
  109. return self.pop(-count)
  110. new_left_list, new_right_list = PDeque._pop_lists(self._left_list, self._right_list, count)
  111. return PDeque(new_left_list, new_right_list, max(self._length - count, 0), self._maxlen)
  112. @staticmethod
  113. def _pop_lists(primary_list, secondary_list, count):
  114. new_primary_list = primary_list
  115. new_secondary_list = secondary_list
  116. while count > 0 and (new_primary_list or new_secondary_list):
  117. count -= 1
  118. if new_primary_list.rest:
  119. new_primary_list = new_primary_list.rest
  120. elif new_primary_list:
  121. new_primary_list = new_secondary_list.reverse()
  122. new_secondary_list = plist()
  123. else:
  124. new_primary_list = new_secondary_list.reverse().rest
  125. new_secondary_list = plist()
  126. return new_primary_list, new_secondary_list
  127. def _is_empty(self):
  128. return not self._left_list and not self._right_list
  129. def __lt__(self, other):
  130. if not isinstance(other, PDeque):
  131. return NotImplemented
  132. return tuple(self) < tuple(other)
  133. def __eq__(self, other):
  134. if not isinstance(other, PDeque):
  135. return NotImplemented
  136. if tuple(self) == tuple(other):
  137. # Sanity check of the length value since it is redundant (there for performance)
  138. assert len(self) == len(other)
  139. return True
  140. return False
  141. def __hash__(self):
  142. return hash(tuple(self))
  143. def __len__(self):
  144. return self._length
  145. def append(self, elem):
  146. """
  147. Return new deque with elem as the rightmost element.
  148. >>> pdeque([1, 2]).append(3)
  149. pdeque([1, 2, 3])
  150. """
  151. new_left_list, new_right_list, new_length = self._append(self._left_list, self._right_list, elem)
  152. return PDeque(new_left_list, new_right_list, new_length, self._maxlen)
  153. def appendleft(self, elem):
  154. """
  155. Return new deque with elem as the leftmost element.
  156. >>> pdeque([1, 2]).appendleft(3)
  157. pdeque([3, 1, 2])
  158. """
  159. new_right_list, new_left_list, new_length = self._append(self._right_list, self._left_list, elem)
  160. return PDeque(new_left_list, new_right_list, new_length, self._maxlen)
  161. def _append(self, primary_list, secondary_list, elem):
  162. if self._maxlen is not None and self._length == self._maxlen:
  163. if self._maxlen == 0:
  164. return primary_list, secondary_list, 0
  165. new_primary_list, new_secondary_list = PDeque._pop_lists(primary_list, secondary_list, 1)
  166. return new_primary_list, new_secondary_list.cons(elem), self._length
  167. return primary_list, secondary_list.cons(elem), self._length + 1
  168. @staticmethod
  169. def _extend_list(the_list, iterable):
  170. count = 0
  171. for elem in iterable:
  172. the_list = the_list.cons(elem)
  173. count += 1
  174. return the_list, count
  175. def _extend(self, primary_list, secondary_list, iterable):
  176. new_primary_list, extend_count = PDeque._extend_list(primary_list, iterable)
  177. new_secondary_list = secondary_list
  178. current_len = self._length + extend_count
  179. if self._maxlen is not None and current_len > self._maxlen:
  180. pop_len = current_len - self._maxlen
  181. new_secondary_list, new_primary_list = PDeque._pop_lists(new_secondary_list, new_primary_list, pop_len)
  182. extend_count -= pop_len
  183. return new_primary_list, new_secondary_list, extend_count
  184. def extend(self, iterable):
  185. """
  186. Return new deque with all elements of iterable appended to the right.
  187. >>> pdeque([1, 2]).extend([3, 4])
  188. pdeque([1, 2, 3, 4])
  189. """
  190. new_right_list, new_left_list, extend_count = self._extend(self._right_list, self._left_list, iterable)
  191. return PDeque(new_left_list, new_right_list, self._length + extend_count, self._maxlen)
  192. def extendleft(self, iterable):
  193. """
  194. Return new deque with all elements of iterable appended to the left.
  195. NB! The elements will be inserted in reverse order compared to the order in the iterable.
  196. >>> pdeque([1, 2]).extendleft([3, 4])
  197. pdeque([4, 3, 1, 2])
  198. """
  199. new_left_list, new_right_list, extend_count = self._extend(self._left_list, self._right_list, iterable)
  200. return PDeque(new_left_list, new_right_list, self._length + extend_count, self._maxlen)
  201. def count(self, elem):
  202. """
  203. Return the number of elements equal to elem present in the queue
  204. >>> pdeque([1, 2, 1]).count(1)
  205. 2
  206. """
  207. return self._left_list.count(elem) + self._right_list.count(elem)
  208. def remove(self, elem):
  209. """
  210. Return new deque with first element from left equal to elem removed. If no such element is found
  211. a ValueError is raised.
  212. >>> pdeque([2, 1, 2]).remove(2)
  213. pdeque([1, 2])
  214. """
  215. try:
  216. return PDeque(self._left_list.remove(elem), self._right_list, self._length - 1)
  217. except ValueError:
  218. # Value not found in left list, try the right list
  219. try:
  220. # This is severely inefficient with a double reverse, should perhaps implement a remove_last()?
  221. return PDeque(self._left_list,
  222. self._right_list.reverse().remove(elem).reverse(), self._length - 1)
  223. except ValueError:
  224. raise ValueError('{0} not found in PDeque'.format(elem))
  225. def reverse(self):
  226. """
  227. Return reversed deque.
  228. >>> pdeque([1, 2, 3]).reverse()
  229. pdeque([3, 2, 1])
  230. Also supports the standard python reverse function.
  231. >>> reversed(pdeque([1, 2, 3]))
  232. pdeque([3, 2, 1])
  233. """
  234. return PDeque(self._right_list, self._left_list, self._length)
  235. __reversed__ = reverse
  236. def rotate(self, steps):
  237. """
  238. Return deque with elements rotated steps steps.
  239. >>> x = pdeque([1, 2, 3])
  240. >>> x.rotate(1)
  241. pdeque([3, 1, 2])
  242. >>> x.rotate(-2)
  243. pdeque([3, 1, 2])
  244. """
  245. popped_deque = self.pop(steps)
  246. if steps >= 0:
  247. return popped_deque.extendleft(islice(self.reverse(), steps))
  248. return popped_deque.extend(islice(self, -steps))
  249. def __reduce__(self):
  250. # Pickling support
  251. return pdeque, (list(self), self._maxlen)
  252. def __getitem__(self, index):
  253. if isinstance(index, slice):
  254. if index.step is not None and index.step != 1:
  255. # Too difficult, no structural sharing possible
  256. return pdeque(tuple(self)[index], maxlen=self._maxlen)
  257. result = self
  258. if index.start is not None:
  259. result = result.popleft(index.start % self._length)
  260. if index.stop is not None:
  261. result = result.pop(self._length - (index.stop % self._length))
  262. return result
  263. if not isinstance(index, Integral):
  264. raise TypeError("'%s' object cannot be interpreted as an index" % type(index).__name__)
  265. if index >= 0:
  266. return self.popleft(index).left
  267. shifted = len(self) + index
  268. if shifted < 0:
  269. raise IndexError(
  270. "pdeque index {0} out of range {1}".format(index, len(self)),
  271. )
  272. return self.popleft(shifted).left
  273. index = Sequence.index
  274. Sequence.register(PDeque)
  275. Hashable.register(PDeque)
  276. def pdeque(iterable=(), maxlen=None):
  277. """
  278. Return deque containing the elements of iterable. If maxlen is specified then
  279. len(iterable) - maxlen elements are discarded from the left to if len(iterable) > maxlen.
  280. >>> pdeque([1, 2, 3])
  281. pdeque([1, 2, 3])
  282. >>> pdeque([1, 2, 3, 4], maxlen=2)
  283. pdeque([3, 4], maxlen=2)
  284. """
  285. t = tuple(iterable)
  286. if maxlen is not None:
  287. t = t[-maxlen:]
  288. length = len(t)
  289. pivot = int(length / 2)
  290. left = plist(t[:pivot])
  291. right = plist(t[pivot:], reverse=True)
  292. return PDeque(left, right, length, maxlen)
  293. def dq(*elements):
  294. """
  295. Return deque containing all arguments.
  296. >>> dq(1, 2, 3)
  297. pdeque([1, 2, 3])
  298. """
  299. return pdeque(elements)