DoubleEndedQueueInterface.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * This file is part of the ramsey/collection library
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
  9. * @license http://opensource.org/licenses/MIT MIT
  10. */
  11. declare(strict_types=1);
  12. namespace Ramsey\Collection;
  13. use Ramsey\Collection\Exception\NoSuchElementException;
  14. use RuntimeException;
  15. /**
  16. * A linear collection that supports element insertion and removal at both ends.
  17. *
  18. * Most `DoubleEndedQueueInterface` implementations place no fixed limits on the
  19. * number of elements they may contain, but this interface supports
  20. * capacity-restricted double-ended queues as well as those with no fixed size
  21. * limit.
  22. *
  23. * This interface defines methods to access the elements at both ends of the
  24. * double-ended queue. Methods are provided to insert, remove, and examine the
  25. * element. Each of these methods exists in two forms: one throws an exception
  26. * if the operation fails, the other returns a special value (either `null` or
  27. * `false`, depending on the operation). The latter form of the insert operation
  28. * is designed specifically for use with capacity-restricted implementations; in
  29. * most implementations, insert operations cannot fail.
  30. *
  31. * The twelve methods described above are summarized in the following table:
  32. *
  33. * <table>
  34. * <caption>Summary of DoubleEndedQueueInterface methods</caption>
  35. * <thead>
  36. * <tr>
  37. * <th></th>
  38. * <th colspan=2>First Element (Head)</th>
  39. * <th colspan=2>Last Element (Tail)</th>
  40. * </tr>
  41. * <tr>
  42. * <td></td>
  43. * <td><em>Throws exception</em></td>
  44. * <td><em>Special value</em></td>
  45. * <td><em>Throws exception</em></td>
  46. * <td><em>Special value</em></td>
  47. * </tr>
  48. * </thead>
  49. * <tbody>
  50. * <tr>
  51. * <th>Insert</th>
  52. * <td><code>addFirst()</code></td>
  53. * <td><code>offerFirst()</code></td>
  54. * <td><code>addLast()</code></td>
  55. * <td><code>offerLast()</code></td>
  56. * </tr>
  57. * <tr>
  58. * <th>Remove</th>
  59. * <td><code>removeFirst()</code></td>
  60. * <td><code>pollFirst()</code></td>
  61. * <td><code>removeLast()</code></td>
  62. * <td><code>pollLast()</code></td>
  63. * </tr>
  64. * <tr>
  65. * <th>Examine</th>
  66. * <td><code>firstElement()</code></td>
  67. * <td><code>peekFirst()</code></td>
  68. * <td><code>lastElement()</code></td>
  69. * <td><code>peekLast()</code></td>
  70. * </tr>
  71. * </tbody>
  72. * </table>
  73. *
  74. * This interface extends the `QueueInterface`. When a double-ended queue is
  75. * used as a queue, FIFO (first-in-first-out) behavior results. Elements are
  76. * added at the end of the double-ended queue and removed from the beginning.
  77. * The methods inherited from the `QueueInterface` are precisely equivalent to
  78. * `DoubleEndedQueueInterface` methods as indicated in the following table:
  79. *
  80. * <table>
  81. * <caption>Comparison of QueueInterface and DoubleEndedQueueInterface methods</caption>
  82. * <thead>
  83. * <tr>
  84. * <th>QueueInterface Method</th>
  85. * <th>DoubleEndedQueueInterface Method</th>
  86. * </tr>
  87. * </thead>
  88. * <tbody>
  89. * <tr>
  90. * <td><code>add()</code></td>
  91. * <td><code>addLast()</code></td>
  92. * </tr>
  93. * <tr>
  94. * <td><code>offer()</code></td>
  95. * <td><code>offerLast()</code></td>
  96. * </tr>
  97. * <tr>
  98. * <td><code>remove()</code></td>
  99. * <td><code>removeFirst()</code></td>
  100. * </tr>
  101. * <tr>
  102. * <td><code>poll()</code></td>
  103. * <td><code>pollFirst()</code></td>
  104. * </tr>
  105. * <tr>
  106. * <td><code>element()</code></td>
  107. * <td><code>firstElement()</code></td>
  108. * </tr>
  109. * <tr>
  110. * <td><code>peek()</code></td>
  111. * <td><code>peekFirst()</code></td>
  112. * </tr>
  113. * </tbody>
  114. * </table>
  115. *
  116. * Double-ended queues can also be used as LIFO (last-in-first-out) stacks. When
  117. * a double-ended queue is used as a stack, elements are pushed and popped from
  118. * the beginning of the double-ended queue. Stack concepts are precisely
  119. * equivalent to `DoubleEndedQueueInterface` methods as indicated in the table
  120. * below:
  121. *
  122. * <table>
  123. * <caption>Comparison of stack concepts and DoubleEndedQueueInterface methods</caption>
  124. * <thead>
  125. * <tr>
  126. * <th>Stack concept</th>
  127. * <th>DoubleEndedQueueInterface Method</th>
  128. * </tr>
  129. * </thead>
  130. * <tbody>
  131. * <tr>
  132. * <td><em>push</em></td>
  133. * <td><code>addFirst()</code></td>
  134. * </tr>
  135. * <tr>
  136. * <td><em>pop</em></td>
  137. * <td><code>removeFirst()</code></td>
  138. * </tr>
  139. * <tr>
  140. * <td><em>peek</em></td>
  141. * <td><code>peekFirst()</code></td>
  142. * </tr>
  143. * </tbody>
  144. * </table>
  145. *
  146. * Note that the `peek()` method works equally well when a double-ended queue is
  147. * used as a queue or a stack; in either case, elements are drawn from the
  148. * beginning of the double-ended queue.
  149. *
  150. * While `DoubleEndedQueueInterface` implementations are not strictly required
  151. * to prohibit the insertion of `null` elements, they are strongly encouraged to
  152. * do so. Users of any `DoubleEndedQueueInterface` implementations that do allow
  153. * `null` elements are strongly encouraged *not* to take advantage of the
  154. * ability to insert nulls. This is so because `null` is used as a special
  155. * return value by various methods to indicated that the double-ended queue is
  156. * empty.
  157. *
  158. * @template T
  159. * @extends QueueInterface<T>
  160. */
  161. interface DoubleEndedQueueInterface extends QueueInterface
  162. {
  163. /**
  164. * Inserts the specified element at the front of this queue if it is
  165. * possible to do so immediately without violating capacity restrictions.
  166. *
  167. * When using a capacity-restricted double-ended queue, it is generally
  168. * preferable to use the `offerFirst()` method.
  169. *
  170. * @param T $element The element to add to the front of this queue.
  171. *
  172. * @return bool `true` if this queue changed as a result of the call.
  173. *
  174. * @throws RuntimeException if a queue refuses to add a particular element
  175. * for any reason other than that it already contains the element.
  176. * Implementations should use a more-specific exception that extends
  177. * `\RuntimeException`.
  178. */
  179. public function addFirst(mixed $element): bool;
  180. /**
  181. * Inserts the specified element at the end of this queue if it is possible
  182. * to do so immediately without violating capacity restrictions.
  183. *
  184. * When using a capacity-restricted double-ended queue, it is generally
  185. * preferable to use the `offerLast()` method.
  186. *
  187. * This method is equivalent to `add()`.
  188. *
  189. * @param T $element The element to add to the end of this queue.
  190. *
  191. * @return bool `true` if this queue changed as a result of the call.
  192. *
  193. * @throws RuntimeException if a queue refuses to add a particular element
  194. * for any reason other than that it already contains the element.
  195. * Implementations should use a more-specific exception that extends
  196. * `\RuntimeException`.
  197. */
  198. public function addLast(mixed $element): bool;
  199. /**
  200. * Inserts the specified element at the front of this queue if it is
  201. * possible to do so immediately without violating capacity restrictions.
  202. *
  203. * When using a capacity-restricted queue, this method is generally
  204. * preferable to `addFirst()`, which can fail to insert an element only by
  205. * throwing an exception.
  206. *
  207. * @param T $element The element to add to the front of this queue.
  208. *
  209. * @return bool `true` if the element was added to this queue, else `false`.
  210. */
  211. public function offerFirst(mixed $element): bool;
  212. /**
  213. * Inserts the specified element at the end of this queue if it is possible
  214. * to do so immediately without violating capacity restrictions.
  215. *
  216. * When using a capacity-restricted queue, this method is generally
  217. * preferable to `addLast()` which can fail to insert an element only by
  218. * throwing an exception.
  219. *
  220. * @param T $element The element to add to the end of this queue.
  221. *
  222. * @return bool `true` if the element was added to this queue, else `false`.
  223. */
  224. public function offerLast(mixed $element): bool;
  225. /**
  226. * Retrieves and removes the head of this queue.
  227. *
  228. * This method differs from `pollFirst()` only in that it throws an
  229. * exception if this queue is empty.
  230. *
  231. * @return T the first element in this queue.
  232. *
  233. * @throws NoSuchElementException if this queue is empty.
  234. */
  235. public function removeFirst(): mixed;
  236. /**
  237. * Retrieves and removes the tail of this queue.
  238. *
  239. * This method differs from `pollLast()` only in that it throws an exception
  240. * if this queue is empty.
  241. *
  242. * @return T the last element in this queue.
  243. *
  244. * @throws NoSuchElementException if this queue is empty.
  245. */
  246. public function removeLast(): mixed;
  247. /**
  248. * Retrieves and removes the head of this queue, or returns `null` if this
  249. * queue is empty.
  250. *
  251. * @return T | null the head of this queue, or `null` if this queue is empty.
  252. */
  253. public function pollFirst(): mixed;
  254. /**
  255. * Retrieves and removes the tail of this queue, or returns `null` if this
  256. * queue is empty.
  257. *
  258. * @return T | null the tail of this queue, or `null` if this queue is empty.
  259. */
  260. public function pollLast(): mixed;
  261. /**
  262. * Retrieves, but does not remove, the head of this queue.
  263. *
  264. * This method differs from `peekFirst()` only in that it throws an
  265. * exception if this queue is empty.
  266. *
  267. * @return T the head of this queue.
  268. *
  269. * @throws NoSuchElementException if this queue is empty.
  270. */
  271. public function firstElement(): mixed;
  272. /**
  273. * Retrieves, but does not remove, the tail of this queue.
  274. *
  275. * This method differs from `peekLast()` only in that it throws an exception
  276. * if this queue is empty.
  277. *
  278. * @return T the tail of this queue.
  279. *
  280. * @throws NoSuchElementException if this queue is empty.
  281. */
  282. public function lastElement(): mixed;
  283. /**
  284. * Retrieves, but does not remove, the head of this queue, or returns `null`
  285. * if this queue is empty.
  286. *
  287. * @return T | null the head of this queue, or `null` if this queue is empty.
  288. */
  289. public function peekFirst(): mixed;
  290. /**
  291. * Retrieves, but does not remove, the tail of this queue, or returns `null`
  292. * if this queue is empty.
  293. *
  294. * @return T | null the tail of this queue, or `null` if this queue is empty.
  295. */
  296. public function peekLast(): mixed;
  297. }