1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.collection.Deque;
13 
14 
15 import hunt.collection.Queue;
16 
17 /**
18  * A linear collection that supports element insertion and removal at
19  * both ends.  The name <i>deque</i> is short for "double ended queue"
20  * and is usually pronounced "deck".  Most {@code Deque}
21  * implementations place no fixed limits on the number of elements
22  * they may contain, but this interface supports capacity-restricted
23  * deques as well as those with no fixed size limit.
24  *
25  * <p>This interface defines methods to access the elements at both
26  * ends of the deque.  Methods are provided to insert, remove, and
27  * examine the element.  Each of these methods exists in two forms:
28  * one throws an exception if the operation fails, the other returns a
29  * special value (either {@code null} or {@code false}, depending on
30  * the operation).  The latter form of the insert operation is
31  * designed specifically for use with capacity-restricted
32  * {@code Deque} implementations; in most implementations, insert
33  * operations cannot fail.
34  *
35  * <p>The twelve methods described above are summarized in the
36  * following table:
37  *
38  * <table BORDER CELLPADDING=3 CELLSPACING=1>
39  * <caption>Summary of Deque methods</caption>
40  *  <tr>
41  *    <td></td>
42  *    <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td>
43  *    <td ALIGN=CENTER COLSPAN = 2> <b>Last Element (Tail)</b></td>
44  *  </tr>
45  *  <tr>
46  *    <td></td>
47  *    <td ALIGN=CENTER><em>Throws exception</em></td>
48  *    <td ALIGN=CENTER><em>Special value</em></td>
49  *    <td ALIGN=CENTER><em>Throws exception</em></td>
50  *    <td ALIGN=CENTER><em>Special value</em></td>
51  *  </tr>
52  *  <tr>
53  *    <td><b>Insert</b></td>
54  *    <td>{@link Deque#addFirst addFirst(e)}</td>
55  *    <td>{@link Deque#offerFirst offerFirst(e)}</td>
56  *    <td>{@link Deque#addLast addLast(e)}</td>
57  *    <td>{@link Deque#offerLast offerLast(e)}</td>
58  *  </tr>
59  *  <tr>
60  *    <td><b>Remove</b></td>
61  *    <td>{@link Deque#removeFirst removeFirst()}</td>
62  *    <td>{@link Deque#pollFirst pollFirst()}</td>
63  *    <td>{@link Deque#removeLast removeLast()}</td>
64  *    <td>{@link Deque#pollLast pollLast()}</td>
65  *  </tr>
66  *  <tr>
67  *    <td><b>Examine</b></td>
68  *    <td>{@link Deque#getFirst getFirst()}</td>
69  *    <td>{@link Deque#peekFirst peekFirst()}</td>
70  *    <td>{@link Deque#getLast getLast()}</td>
71  *    <td>{@link Deque#peekLast peekLast()}</td>
72  *  </tr>
73  * </table>
74  *
75  * <p>This interface extends the {@link Queue} interface.  When a deque is
76  * used as a queue, FIFO (First-In-First-Out) behavior results.  Elements are
77  * added at the end of the deque and removed from the beginning.  The methods
78  * inherited from the {@code Queue} interface are precisely equivalent to
79  * {@code Deque} methods as indicated in the following table:
80  *
81  * <table BORDER CELLPADDING=3 CELLSPACING=1>
82  * <caption>Comparison of Queue and Deque methods</caption>
83  *  <tr>
84  *    <td ALIGN=CENTER> <b>{@code Queue} Method</b></td>
85  *    <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td>
86  *  </tr>
87  *  <tr>
88  *    <td>{@link java.util.Queue#add add(e)}</td>
89  *    <td>{@link #addLast addLast(e)}</td>
90  *  </tr>
91  *  <tr>
92  *    <td>{@link java.util.Queue#offer offer(e)}</td>
93  *    <td>{@link #offerLast offerLast(e)}</td>
94  *  </tr>
95  *  <tr>
96  *    <td>{@link java.util.Queue#remove remove()}</td>
97  *    <td>{@link #removeFirst removeFirst()}</td>
98  *  </tr>
99  *  <tr>
100  *    <td>{@link java.util.Queue#poll poll()}</td>
101  *    <td>{@link #pollFirst pollFirst()}</td>
102  *  </tr>
103  *  <tr>
104  *    <td>{@link java.util.Queue#element element()}</td>
105  *    <td>{@link #getFirst getFirst()}</td>
106  *  </tr>
107  *  <tr>
108  *    <td>{@link java.util.Queue#peek peek()}</td>
109  *    <td>{@link #peek peekFirst()}</td>
110  *  </tr>
111  * </table>
112  *
113  * <p>Deques can also be used as LIFO (Last-In-First-Out) stacks.  This
114  * interface should be used in preference to the legacy {@link Stack} class.
115  * When a deque is used as a stack, elements are pushed and popped from the
116  * beginning of the deque.  Stack methods are precisely equivalent to
117  * {@code Deque} methods as indicated in the table below:
118  *
119  * <table BORDER CELLPADDING=3 CELLSPACING=1>
120  * <caption>Comparison of Stack and Deque methods</caption>
121  *  <tr>
122  *    <td ALIGN=CENTER> <b>Stack Method</b></td>
123  *    <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td>
124  *  </tr>
125  *  <tr>
126  *    <td>{@link #push push(e)}</td>
127  *    <td>{@link #addFirst addFirst(e)}</td>
128  *  </tr>
129  *  <tr>
130  *    <td>{@link #pop pop()}</td>
131  *    <td>{@link #removeFirst removeFirst()}</td>
132  *  </tr>
133  *  <tr>
134  *    <td>{@link #peek peek()}</td>
135  *    <td>{@link #peekFirst peekFirst()}</td>
136  *  </tr>
137  * </table>
138  *
139  * <p>Note that the {@link #peek peek} method works equally well when
140  * a deque is used as a queue or a stack; in either case, elements are
141  * drawn from the beginning of the deque.
142  *
143  * <p>This interface provides two methods to remove interior
144  * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
145  * {@link #removeLastOccurrence removeLastOccurrence}.
146  *
147  * <p>Unlike the {@link List} interface, this interface does not
148  * provide support for indexed access to elements.
149  *
150  * <p>While {@code Deque} implementations are not strictly required
151  * to prohibit the insertion of null elements, they are strongly
152  * encouraged to do so.  Users of any {@code Deque} implementations
153  * that do allow null elements are strongly encouraged <i>not</i> to
154  * take advantage of the ability to insert nulls.  This is so because
155  * {@code null} is used as a special return value by various methods
156  * to indicated that the deque is empty.
157  *
158  * <p>{@code Deque} implementations generally do not define
159  * element-based versions of the {@code equals} and {@code hashCode}
160  * methods, but instead inherit the identity-based versions from class
161  * {@code Object}.
162  *
163  * <p>This interface is a member of the <a
164  * href="{@docRoot}/../technotes/guides/collections/index.html"> Java Collections
165  * Framework</a>.
166  *
167  * @author Doug Lea
168  * @author Josh Bloch
169  * @since  1.6
170  * @param !E the type of elements held in this collection
171  */
172 interface Deque(E) : Queue!E {
173     /**
174      * Inserts the specified element at the front of this deque if it is
175      * possible to do so immediately without violating capacity restrictions,
176      * throwing an {@code IllegalStateException} if no space is currently
177      * available.  When using a capacity-restricted deque, it is generally
178      * preferable to use method {@link #offerFirst}.
179      *
180      * @param e the element to add
181      * @throws IllegalStateException if the element cannot be added at this
182      *         time due to capacity restrictions
183      * @throws ClassCastException if the class of the specified element
184      *         prevents it from being added to this deque
185      * @throws NullPointerException if the specified element is null and this
186      *         deque does not permit null elements
187      * @throws IllegalArgumentException if some property of the specified
188      *         element prevents it from being added to this deque
189      */
190     void addFirst(E e);
191 
192     /**
193      * Inserts the specified element at the end of this deque if it is
194      * possible to do so immediately without violating capacity restrictions,
195      * throwing an {@code IllegalStateException} if no space is currently
196      * available.  When using a capacity-restricted deque, it is generally
197      * preferable to use method {@link #offerLast}.
198      *
199      * <p>This method is equivalent to {@link #add}.
200      *
201      * @param e the element to add
202      * @throws IllegalStateException if the element cannot be added at this
203      *         time due to capacity restrictions
204      * @throws ClassCastException if the class of the specified element
205      *         prevents it from being added to this deque
206      * @throws NullPointerException if the specified element is null and this
207      *         deque does not permit null elements
208      * @throws IllegalArgumentException if some property of the specified
209      *         element prevents it from being added to this deque
210      */
211     void addLast(E e);
212 
213     /**
214      * Inserts the specified element at the front of this deque unless it would
215      * violate capacity restrictions.  When using a capacity-restricted deque,
216      * this method is generally preferable to the {@link #addFirst} method,
217      * which can fail to insert an element only by throwing an exception.
218      *
219      * @param e the element to add
220      * @return {@code true} if the element was added to this deque, else
221      *         {@code false}
222      * @throws ClassCastException if the class of the specified element
223      *         prevents it from being added to this deque
224      * @throws NullPointerException if the specified element is null and this
225      *         deque does not permit null elements
226      * @throws IllegalArgumentException if some property of the specified
227      *         element prevents it from being added to this deque
228      */
229     bool offerFirst(E e);
230 
231     /**
232      * Inserts the specified element at the end of this deque unless it would
233      * violate capacity restrictions.  When using a capacity-restricted deque,
234      * this method is generally preferable to the {@link #addLast} method,
235      * which can fail to insert an element only by throwing an exception.
236      *
237      * @param e the element to add
238      * @return {@code true} if the element was added to this deque, else
239      *         {@code false}
240      * @throws ClassCastException if the class of the specified element
241      *         prevents it from being added to this deque
242      * @throws NullPointerException if the specified element is null and this
243      *         deque does not permit null elements
244      * @throws IllegalArgumentException if some property of the specified
245      *         element prevents it from being added to this deque
246      */
247     bool offerLast(E e);
248 
249     /**
250      * Retrieves and removes the first element of this deque.  This method
251      * differs from {@link #pollFirst pollFirst} only in that it throws an
252      * exception if this deque is empty.
253      *
254      * @return the head of this deque
255      * @throws NoSuchElementException if this deque is empty
256      */
257     E removeFirst();
258 
259     /**
260      * Retrieves and removes the last element of this deque.  This method
261      * differs from {@link #pollLast pollLast} only in that it throws an
262      * exception if this deque is empty.
263      *
264      * @return the tail of this deque
265      * @throws NoSuchElementException if this deque is empty
266      */
267     E removeLast();
268 
269     /**
270      * Retrieves and removes the first element of this deque,
271      * or returns {@code null} if this deque is empty.
272      *
273      * @return the head of this deque, or {@code null} if this deque is empty
274      */
275     E pollFirst();
276 
277     /**
278      * Retrieves and removes the last element of this deque,
279      * or returns {@code null} if this deque is empty.
280      *
281      * @return the tail of this deque, or {@code null} if this deque is empty
282      */
283     E pollLast();
284 
285     /**
286      * Retrieves, but does not remove, the first element of this deque.
287      *
288      * This method differs from {@link #peekFirst peekFirst} only in that it
289      * throws an exception if this deque is empty.
290      *
291      * @return the head of this deque
292      * @throws NoSuchElementException if this deque is empty
293      */
294     E getFirst();
295 
296     /**
297      * Retrieves, but does not remove, the last element of this deque.
298      * This method differs from {@link #peekLast peekLast} only in that it
299      * throws an exception if this deque is empty.
300      *
301      * @return the tail of this deque
302      * @throws NoSuchElementException if this deque is empty
303      */
304     E getLast();
305 
306     /**
307      * Retrieves, but does not remove, the first element of this deque,
308      * or returns {@code null} if this deque is empty.
309      *
310      * @return the head of this deque, or {@code null} if this deque is empty
311      */
312     E peekFirst();
313 
314     /**
315      * Retrieves, but does not remove, the last element of this deque,
316      * or returns {@code null} if this deque is empty.
317      *
318      * @return the tail of this deque, or {@code null} if this deque is empty
319      */
320     E peekLast();
321 
322     /**
323      * Removes the first occurrence of the specified element from this deque.
324      * If the deque does not contain the element, it is unchanged.
325      * More formally, removes the first element {@code e} such that
326      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>
327      * (if such an element exists).
328      * Returns {@code true} if this deque contained the specified element
329      * (or equivalently, if this deque changed as a result of the call).
330      *
331      * @param o element to be removed from this deque, if present
332      * @return {@code true} if an element was removed as a result of this call
333      * @throws ClassCastException if the class of the specified element
334      *         is incompatible with this deque
335      * (<a href="Collection.html#optional-restrictions">optional</a>)
336      * @throws NullPointerException if the specified element is null and this
337      *         deque does not permit null elements
338      * (<a href="Collection.html#optional-restrictions">optional</a>)
339      */
340     bool removeFirstOccurrence(E o);
341 
342     /**
343      * Removes the last occurrence of the specified element from this deque.
344      * If the deque does not contain the element, it is unchanged.
345      * More formally, removes the last element {@code e} such that
346      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>
347      * (if such an element exists).
348      * Returns {@code true} if this deque contained the specified element
349      * (or equivalently, if this deque changed as a result of the call).
350      *
351      * @param o element to be removed from this deque, if present
352      * @return {@code true} if an element was removed as a result of this call
353      * @throws ClassCastException if the class of the specified element
354      *         is incompatible with this deque
355      * (<a href="Collection.html#optional-restrictions">optional</a>)
356      * @throws NullPointerException if the specified element is null and this
357      *         deque does not permit null elements
358      * (<a href="Collection.html#optional-restrictions">optional</a>)
359      */
360     // bool removeLastOccurrence(E o);
361 
362     // *** Queue methods ***
363 
364     /**
365      * Inserts the specified element into the queue represented by this deque
366      * (in other words, at the tail of this deque) if it is possible to do so
367      * immediately without violating capacity restrictions, returning
368      * {@code true} upon success and throwing an
369      * {@code IllegalStateException} if no space is currently available.
370      * When using a capacity-restricted deque, it is generally preferable to
371      * use {@link #offer(Object) offer}.
372      *
373      * <p>This method is equivalent to {@link #addLast}.
374      *
375      * @param e the element to add
376      * @return {@code true} (as specified by {@link Collection#add})
377      * @throws IllegalStateException if the element cannot be added at this
378      *         time due to capacity restrictions
379      * @throws ClassCastException if the class of the specified element
380      *         prevents it from being added to this deque
381      * @throws NullPointerException if the specified element is null and this
382      *         deque does not permit null elements
383      * @throws IllegalArgumentException if some property of the specified
384      *         element prevents it from being added to this deque
385      */
386     bool add(E e);
387 
388     /**
389      * Inserts the specified element into the queue represented by this deque
390      * (in other words, at the tail of this deque) if it is possible to do so
391      * immediately without violating capacity restrictions, returning
392      * {@code true} upon success and {@code false} if no space is currently
393      * available.  When using a capacity-restricted deque, this method is
394      * generally preferable to the {@link #add} method, which can fail to
395      * insert an element only by throwing an exception.
396      *
397      * <p>This method is equivalent to {@link #offerLast}.
398      *
399      * @param e the element to add
400      * @return {@code true} if the element was added to this deque, else
401      *         {@code false}
402      * @throws ClassCastException if the class of the specified element
403      *         prevents it from being added to this deque
404      * @throws NullPointerException if the specified element is null and this
405      *         deque does not permit null elements
406      * @throws IllegalArgumentException if some property of the specified
407      *         element prevents it from being added to this deque
408      */
409     bool offer(E e);
410 
411     /**
412      * Retrieves and removes the head of the queue represented by this deque
413      * (in other words, the first element of this deque).
414      * This method differs from {@link #poll poll} only in that it throws an
415      * exception if this deque is empty.
416      *
417      * <p>This method is equivalent to {@link #removeFirst()}.
418      *
419      * @return the head of the queue represented by this deque
420      * @throws NoSuchElementException if this deque is empty
421      */
422     E remove();
423 
424     /**
425      * Retrieves and removes the head of the queue represented by this deque
426      * (in other words, the first element of this deque), or returns
427      * {@code null} if this deque is empty.
428      *
429      * <p>This method is equivalent to {@link #pollFirst()}.
430      *
431      * @return the first element of this deque, or {@code null} if
432      *         this deque is empty
433      */
434     E poll();
435 
436     /**
437      * Retrieves, but does not remove, the head of the queue represented by
438      * this deque (in other words, the first element of this deque).
439      * This method differs from {@link #peek peek} only in that it throws an
440      * exception if this deque is empty.
441      *
442      * <p>This method is equivalent to {@link #getFirst()}.
443      *
444      * @return the head of the queue represented by this deque
445      * @throws NoSuchElementException if this deque is empty
446      */
447     E element();
448 
449     /**
450      * Retrieves, but does not remove, the head of the queue represented by
451      * this deque (in other words, the first element of this deque), or
452      * returns {@code null} if this deque is empty.
453      *
454      * <p>This method is equivalent to {@link #peekFirst()}.
455      *
456      * @return the head of the queue represented by this deque, or
457      *         {@code null} if this deque is empty
458      */
459     E peek();
460 
461 
462     // *** Stack methods ***
463 
464     /**
465      * Pushes an element onto the stack represented by this deque (in other
466      * words, at the head of this deque) if it is possible to do so
467      * immediately without violating capacity restrictions, throwing an
468      * {@code IllegalStateException} if no space is currently available.
469      *
470      * <p>This method is equivalent to {@link #addFirst}.
471      *
472      * @param e the element to push
473      * @throws IllegalStateException if the element cannot be added at this
474      *         time due to capacity restrictions
475      * @throws ClassCastException if the class of the specified element
476      *         prevents it from being added to this deque
477      * @throws NullPointerException if the specified element is null and this
478      *         deque does not permit null elements
479      * @throws IllegalArgumentException if some property of the specified
480      *         element prevents it from being added to this deque
481      */
482     void push(E e);
483 
484     /**
485      * Pops an element from the stack represented by this deque.  In other
486      * words, removes and returns the first element of this deque.
487      *
488      * <p>This method is equivalent to {@link #removeFirst()}.
489      *
490      * @return the element at the front of this deque (which is the top
491      *         of the stack represented by this deque)
492      * @throws NoSuchElementException if this deque is empty
493      */
494     E pop();
495 
496 
497     // *** Collection methods ***
498 
499     /**
500      * Removes the first occurrence of the specified element from this deque.
501      * If the deque does not contain the element, it is unchanged.
502      * More formally, removes the first element {@code e} such that
503      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>
504      * (if such an element exists).
505      * Returns {@code true} if this deque contained the specified element
506      * (or equivalently, if this deque changed as a result of the call).
507      *
508      * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
509      *
510      * @param o element to be removed from this deque, if present
511      * @return {@code true} if an element was removed as a result of this call
512      * @throws ClassCastException if the class of the specified element
513      *         is incompatible with this deque
514      * (<a href="Collection.html#optional-restrictions">optional</a>)
515      * @throws NullPointerException if the specified element is null and this
516      *         deque does not permit null elements
517      * (<a href="Collection.html#optional-restrictions">optional</a>)
518      */
519     bool remove(E o);
520 
521     /**
522      * Returns {@code true} if this deque contains the specified element.
523      * More formally, returns {@code true} if and only if this deque contains
524      * at least one element {@code e} such that
525      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
526      *
527      * @param o element whose presence in this deque is to be tested
528      * @return {@code true} if this deque contains the specified element
529      * @throws ClassCastException if the type of the specified element
530      *         is incompatible with this deque
531      * (<a href="Collection.html#optional-restrictions">optional</a>)
532      * @throws NullPointerException if the specified element is null and this
533      *         deque does not permit null elements
534      * (<a href="Collection.html#optional-restrictions">optional</a>)
535      */
536     bool contains(E o);
537 
538     /**
539      * Returns the number of elements in this deque.
540      *
541      * @return the number of elements in this deque
542      */
543     int size();
544 
545     /**
546      * Returns an iterator over the elements in this deque in proper sequence.
547      * The elements will be returned in order from first (head) to last (tail).
548      *
549      * @return an iterator over the elements in this deque in proper sequence
550      */
551     // Iterator!E iterator();
552 
553     /**
554      * Returns an iterator over the elements in this deque in reverse
555      * sequential order.  The elements will be returned in order from
556      * last (tail) to first (head).
557      *
558      * @return an iterator over the elements in this deque in reverse
559      * sequence
560      */
561     // Iterator!E descendingIterator();
562 
563 }