| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Jmw\Collection\Queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Jmw\Collection\CollectionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Jmw\Collection\Exception\NoSuchElementException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Jmw\Collection\Lists\ArrayList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Jmw\Collection\Lists\ReverseListIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Resizable-array implementation of the Deque interface.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Array deques have no capacity restrictions; they grow as necessary to support usage.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author john | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class ArrayDeque extends ArrayList implements DequeInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * Inserts the specified element at the front of this deque if it is possible | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * to do so immediately without violating capacity restrictions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @param multitype $element        	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	public function addFirst($element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$this->addAt(0, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * Inserts the specified element at the end of this deque if it is possible to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * do so immediately without violating capacity restrictions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @param multitype $element        	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	public function addLast($element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$this->addAt($this->size(), $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * Returns an iterator over the elements in this deque in reverse sequential order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * @return IteratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	public function descendingIterator() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		return new ReverseListIterator($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * Retrieves, but does not remove, the head of this queue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * @return multitype | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	public function element() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		if ($this->isEmpty()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			throw new NoSuchElementException("No head element, list is empty"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		return $this->get(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * Retrieves, but does not remove, the first element of this deque. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * This method differs from peekFirst only in that it throws an exception if this deque is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * @return multitype $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * @throws NoSuchElementException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	public function getFirst() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		return $this->element(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * Retrieves, but does not remove, the last element of this deque. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * This method differs from peekLast only in that it throws an exception if this deque is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * @return multitype $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  	 * @throws NoSuchElementException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	public function getLast() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		if($this->isEmpty()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			throw new NoSuchElementException("No tail element, list is empty"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		return $this->get($this->size() - 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 * Inserts the specified element into this queue if it is possible | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * to do so immediately without violating capacity restrictions.	  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * @param multitype $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @return true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	public function offer($element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		return $this->offerLast($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * Inserts the specified element at the front of this deque | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 * unless it would violate capacity restrictions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * @param multitype $element        	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	public function offerFirst($element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		$this->addFirst($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * Inserts the specified element at the end of this deque | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * unless it would violate capacity restrictions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * @param multitype $element        	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	public function offerLast($element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		$this->add($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * Retrieves, but does not remove, the head of this queue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * or returns null if this queue is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 * @return multitype | null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	public function peek() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		return $this->peekFirst(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 * Retrieves, but does not remove, the first element of this deque, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * or returns null if this deque is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 * @return multitype | null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	public function peekFirst() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		if ($this->isEmpty()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 			return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		return $this->get(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * Retrieves, but does not remove, the last element of this deque, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 * or returns null if this deque is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * @return multitype | null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	public function peekLast() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		if($this->isEmpty()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 			return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		return $this->get($this->size() - 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	 * Retrieves and removes the head of this queue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	 * or returns null if this queue is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	 * @return multitype | NULL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	public function poll() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 		return $this->pollFirst(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 	 * Retrieves and removes the first element of this deque, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	 * or returns null if this deque is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	 * @return multitype | null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	public function pollFirst() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 		if ($this->isEmpty()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 			return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 		return $this->removeFirst(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 	 * Retrieves and removes the last element of this deque, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 	 * or returns null if this deque is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 	 * @return multitype | null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 201 |  | View Code Duplication | 	public function pollLast() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 		if ($this->isEmpty()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 			return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 		$index = $this->size() - 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 		$element = $this->get($index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | 		$this->removeAt($index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 		return $element; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | 	 * Pops an element from the stack represented by this deque. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | 	 * @return multitype | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  | 	public function pop() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | 		return $this->removeFirst(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  | 	 * Pushes an element onto the stack represented by this deque | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  | 	 * (in other words, at the head of this deque) if it is possible | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  | 	 * to do so immediately without violating capacity restrictions, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 	 * returning true upon success and throwing an IllegalStateException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  | 	 * if no space is currently available. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 	 * @param unknown $element        	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  | 	 * @throws IllegalStateException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  | 	public function push($element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  | 		$this->addFirst($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  | 	 * Retrieves and removes the head of the queue represented | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 	 * by this deque (in other words, the first element of this deque). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  | 	 * @return multitype | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  | 	 * @throws NoSuchElementException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 245 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 246 |  |  | 	public function removeHead() | 
            
                                                                        
                            
            
                                    
            
            
                | 247 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 248 |  |  | 		if ($this->isEmpty()) | 
            
                                                                        
                            
            
                                    
            
            
                | 249 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 250 |  |  | 			throw new NoSuchElementException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 251 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 252 |  |  | 		$element = $this->get(0); | 
            
                                                                        
                            
            
                                    
            
            
                | 253 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 254 |  |  | 		$this->removeAt(0); | 
            
                                                                        
                            
            
                                    
            
            
                | 255 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 256 |  |  | 		return $element; | 
            
                                                                        
                            
            
                                    
            
            
                | 257 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 	 * Retrieves and removes the first element of this deque. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  | 	 * @return multitype $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  | 	 * @throws NoSuchElementException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  | 	public function removeFirst() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  | 		return $this->removeHead();		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | 	 * Removes the first occurrence of the specified element from this deque. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  | 	 * @param multitype $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  | 	 * @return boolean      	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 	public function removeFirstOccurrence($element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  | 		return $this->remove($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  | 	 * Retrieves and removes the last element of this deque. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  | 	 * @return multitype $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  | 	 * @throws NoSuchElementException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 286 |  | View Code Duplication | 	public function removeLast() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  | 		if($this->isEmpty()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  | 			throw new NoSuchElementException("List is empty, cannot remove tail element"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  | 		$index = $this->size() - 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  | 		$element = $this->get($index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  | 		$this->removeAt($index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  | 		return $element; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  | 	 * Removes the last occurrence of the specified element from this deque. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  | 	 * @param multitype $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  | 	 * @return boolean	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  | 	public function removeLastOccurrence($element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  | 		return $this->removeAt($this->lastIndexOf($element)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 308 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 309 |  |  | } | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.