summaryrefslogtreecommitdiff
path: root/ext/spl/internal/spldoublylinkedlist.inc
blob: f01b6306a729a803f96445b499a018e393cda643 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/** @file spldoublylinkedlist.inc
 * @ingroup SPL
 * @brief class SplDoublyLinkedList
 * @author  Etienne Kneuss
 * @date    2008 - 2009
 *
 * SPL - Standard PHP Library
 */

/** @ingroup SPL
 * @brief Doubly Linked List
 * @since PHP 5.3
 *
 * The SplDoublyLinkedList class provides the main functionalities of a
 * doubly linked list (DLL).
 * @note The following userland implementation of Iterator is a bit different
 *        from the internal one. Internally, iterators generated by nested
 *        foreachs are independent, while they share the same traverse pointer
 *        in userland.
 */
class SplDoublyLinkedList implements Iterator, ArrayAccess, Countable
{
	protected $_llist   = array();
	protected $_it_mode = 0;
	protected $_it_pos  = 0;

	/** Iterator mode
	 * @see setIteratorMode
	 */
	const IT_MODE_LIFO     = 0x00000002;

	/** Iterator mode
	 * @see setIteratorMode
	 */
	const IT_MODE_FIFO     = 0x00000000;

	/** Iterator mode
	 * @see setIteratorMode
	 */
	const IT_MODE_KEEP     = 0x00000000;

	/** Iterator mode
	 * @see setIteratorMode
	 */
	const IT_MODE_DELETE   = 0x00000001;

	/** @return the element popped from the end of the DLL.
	 * @throw RuntimeException If the datastructure is empty.
	 */
	public function pop()
	{
		if (count($this->_llist) == 0) {
			throw new RuntimeException("Can't pop from an empty datastructure");
		}
		return array_pop($this->_llist);
	}

	/** @return the element shifted from the beginning of the DLL.
	 * @throw RuntimeException If the datastructure is empty.
	 */
	public function shift()
	{
		if (count($this->_llist) == 0) {
			throw new RuntimeException("Can't shift from an empty datastructure");
		}
		return array_shift($this->_llist);
	}

	/** Pushes an element to the end of the DLL.
	 * @param $data variable to add to the DLL.
	 */
	public function push($data)
	{
		array_push($this->_llist, $data);
		return true;
	}

	/** Adds an element to the beginning of the DLL.
	 * @param $data variable to add to the DLL.
	 */
	public function unshift($data)
	{
		array_unshift($this->_llist, $data);
		return true;
	}

	/** @return the element at the beginning of the DLL.
	 */
	public function top()
	{
		return end($this->_llist);
	}

	/** @return the element at the end of the DLL.
	 */
	public function bottom()
	{
		return reset($this->_llist);
	}

	/** @return number elements in the DLL.
	 */
	public function count()
	{
		return count($this->_llist);
	}

	/** @return whether the DLL is empty.
	 */
	public function isEmpty()
	{
		return ($this->count() == 0);
	}

	/** Changes the iteration mode. There are two orthogonal sets of modes that
	 * can be set:
	 * - The direction of the iteration (either one or the other)
	 *  - SplDoublyLnkedList::IT_MODE_LIFO (Stack style)
	 *  - SplDoublyLnkedList::IT_MODE_FIFO (Queue style)
	 *
	 * - The behavior of the iterator (either one or the other)
	 *  - SplDoublyLnkedList::IT_MODE_DELETE (Elements are deleted by the iterator)
	 *  - SplDoublyLnkedList::IT_MODE_KEEP   (Elements are traversed by the iterator)
	 *
	 * The default mode is 0 : SplDoublyLnkedList::IT_MODE_FIFO | SplDoublyLnkedList::IT_MODE_KEEP
	 *
	 * @param $mode new mode of iteration
	 */
	public function setIteratorMode($mode)
	{
		$this->_it_mode = $mode;
	}

	/** @return the current iteration mode
	 * @see setIteratorMode
	 */
	public function getIteratorMode()
	{
		return $this->_it_mode;
	}

	/** Rewind to top iterator as set in constructor
	 */
	public function rewind()
	{
		if ($this->_it_mode & self::IT_MODE_LIFO) {
			$this->_it_pos = count($this->_llist)-1;
		} else {
			$this->_it_pos = 0;
		}
	}

	/** @return whether iterator is valid
	 */
	public function valid()
	{
		return array_key_exists($this->_it_pos, $this->_llist);
	}

	/** @return current key
	 */
	public function key()
	{
		return $this->_it_pos;
	}

	/** @return current object
	 */
	public function current()
	{
		return $this->_llist[$this->_it_pos];
	}

	/** Forward to next element
	 */
	public function next()
	{
		if ($this->_it_mode & self::IT_MODE_LIFO) {
			if ($this->_it_mode & self::IT_MODE_DELETE) {
				$this->pop();
			}
			$this->_it_pos--;
		} else {
			if ($this->_it_mode & self::IT_MODE_DELETE) {
				$this->shift();
			} else {
				$this->_it_pos++;
			}
		}
	}

	/** @return whether a certain offset exists in the DLL
	 *
	 * @param $offset             The offset
	 * @throw OutOfRangeException If the offset is either invalid or out of
	 *                            range.
	 */
	public function offsetExists($offset)
	{
		if (!is_numeric($offset)) {
			throw new OutOfRangeException("Offset invalid or out of range");
		} else {
			return array_key_exists($offset, $this->_llist);
		}
	}

	/** @return the data at a certain offset in the DLL
	 *
	 * @param $offset             The offset
	 * @throw OutOfRangeException If the offset is either invalid or out of
	 *                            range.
	 */
	public function offsetGet($offset)
	{
		if ($this->_it_mode & self::IT_MODE_LIFO) {
			$realOffset = count($this->_llist)-$offset;
		} else {
			$realOffset = $offset;
		}

		if (!is_numeric($offset) || !array_key_exists($realOffset, $this->_llist)) {
			throw new OutOfRangeException("Offset invalid or out of range");
		} else {
			return $this->_llist[$realOffset];
		}
	}

	/** Defines the data at a certain offset in the DLL
	 *
	 * @param $offset             The offset
	 * @param $value              New value
	 * @throw OutOfRangeException If the offset is either invalid or out of
	 *                            range.
	 */
	public function offsetSet($offset, $value)
	{
		if ($offset === null) {
			return $this->push($value);
		}

		if ($this->_it_mode & self::IT_MODE_LIFO) {
			$realOffset = count($this->_llist)-$offset;
		} else {
			$realOffset = $offset;
		}

		if (!is_numeric($offset) || !array_key_exists($realOffset, $this->_llist)) {
			throw new OutOfRangeException("Offset invalid or out of range");
		} else {
			$this->_llist[$realOffset] = $value;
		}
	}

	/** Unsets the element at a certain offset in the DLL
	 *
	 * @param $offset             The offset
	 * @throw OutOfRangeException If the offset is either invalid or out of
	 *                            range.
	 */
	public function offsetUnset($offset)
	{
		if ($this->_it_mode & self::IT_MODE_LIFO) {
			$realOffset = count($this->_llist)-$offset;
		} else {
			$realOffset = $offset;
		}

		if (!is_numeric($offset) || !array_key_exists($realOffset, $this->_llist)) {
			throw new OutOfRangeException("Offset invalid or out of range");
		} else {
			array_splice($this->_llist, $realOffset, 1);
		}
	}
}

?>