Completed
Push — master ( 71e8d2...303a8a )
by Siro Díaz
01:32
created

DoublyLinkedList::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * DataStructures for PHP
4
 *
5
 * @link      https://github.com/SiroDiaz/DataStructures
6
 * @copyright Copyright (c) 2017 Siro Díaz Palazón
7
 * @license   https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License)
8
 */
9
namespace DataStructures\Lists;
10
11
use DataStructures\Lists\Traits\{CountTrait, ArrayAccessTrait};
12
use DataStructures\Lists\Nodes\DoublyLinkedListNode as Node;
13
use DataStructures\Lists\Interfaces\ListInterface;
14
use OutOfBoundsException;
15
16
/**
17
 * DoublyLinkedList
18
 *
19
 * DoublyLinkedList is a double linked list that has
20
 * a pointer to the next and the previous node.
21
 *
22
 * @author Siro Diaz Palazon <[email protected]>
23
 */
24
class DoublyLinkedList implements ListInterface {
25
    use CountTrait;
26
    use ArrayAccessTrait;
27
28
    private $head;
29
    private $tail;
30
    private $size;
31
    private $position;
32
    private $current;
33
34 View Code Duplication
    public function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
35
        $this->head = null;
36
        $this->tail = &$this->head;
37
        $this->size = 0;
38
        $this->position = 0;
39
        $this->current = &$this->head;
40
    }
41
42
    /**
43
     * Removes all nodes of the list. It removes from the beginning.
44
     */
45
    public function clear() {
46
        while($this->head !== null) {
47
            $this->shift();
48
        }
49
    }
50
    
51
    /**
52
     * Gets the data stored in the position especified.
53
     *
54
     * @param integer $index Index that must be greater than 0
55
     *  and lower than the list size.
56
     * @return mixed The data stored in the given index
57
     * @throws OutOfBoundsException if index is out bounds.
58
     */
59 View Code Duplication
    public function get($index) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
60
        $node = $this->search($index);
61
        if($node === null) {
62
            return null;
63
        }
64
65
        return $node->data;
66
    }
67
68
    /**
69
     * Gets the node stored in the position especified.
70
     * If index is 0 or (size - 1) the method is O(1) else O(n).
71
     *
72
     * @param integer $index the position.
73
     * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1)
74
     * @return DataStructures\Lists\Nodes\DoublyLinkedListNode|null the node stored in $index.
75
     */
76
    protected function search($index) {
77
        if($index < 0 || $index > $this->size - 1) {
78
            throw new OutOfBoundsException();
79
        }
80
81
        if($index === 0) {
82
            return $this->head;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->head; of type null|DataStructures\List...es\DoublyLinkedListNode adds the type DataStructures\Lists\Nodes\DoublyLinkedListNode to the return on line 82 which is incompatible with the return type documented by DataStructures\Lists\DoublyLinkedList::search of type DataStructures\Lists\Dat...ublyLinkedListNode|null.
Loading history...
83
        }
84
85
        if($index === $this->size - 1) {
86
            return $this->tail;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->tail; of type null|DataStructures\List...es\DoublyLinkedListNode adds the type DataStructures\Lists\Nodes\DoublyLinkedListNode to the return on line 86 which is incompatible with the return type documented by DataStructures\Lists\DoublyLinkedList::search of type DataStructures\Lists\Dat...ublyLinkedListNode|null.
Loading history...
87
        }
88
89
        $current = &$this->head;
90
        if($index < (int) ceil($this->size / 2)) {
91
            $i = 0;
92
            while($i < $index) {
93
                $current = &$current->next;
94
                $i++;
95
            }    
96
        } else {
97
            $i = $this->size;
98
            while($i > $index) {
99
                $current = &$current->prev;
100
                $i--;
101
            }
102
        }
103
104
        return $current;
105
    }
106
107
    /**
108
     * Returns the data in the last node with O(1).
109
     *
110
     * @return mixed null if the list is empty.
111
     */
112
    public function getLast() {
113
        if($this->head === null) {
114
            return null;
115
        }
116
        return $this->tail->data;
117
    }
118
119
    /**
120
     * Returns the last node with O(1).
121
     *
122
     * @return DataStructures\Lists\Nodes\DoublyLinkedListNode|null if the list is empty.
123
     */
124
    public function searchLast() {
125
        if($this->head === null) {
126
            return null;
127
        }
128
        return $this->tail;
129
    }
130
    
131
    /**
132
     * Generator for retrieve all nodes stored.
133
     * 
134
     * @return null if the head is null (or list is empty)
135
     */
136 View Code Duplication
    public function getAll() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
137
        if($this->head === null) {
138
            return;
139
        }
140
141
        if($this->head === $this->tail) {
142
            yield $this->head->data;
143
        } else {
144
            $current = $this->head;
145
            $i = 0;
146
            while($i < $this->size) {
147
                yield $current->data;
148
                $current = $current->next;
149
                $i++;
150
            }
151
        }
152
    }
153
154
    /**
155
     * Inserts data in the specified position.
156
     *
157
     * @param integer $index the position.
158
     * @param mixed $data the data to store.
159
     */
160 View Code Duplication
    public function insert($index, $data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
161
        if($index < 0) {
162
            throw new OutOfBoundsException();
163
        }
164
165
        if($index === 0) {
166
            $this->insertBeginning($data);
167
        } else if($index >= $this->size) {
168
            $this->insertEnd($data);
169
        } else if($index > 0 && $index < $this->size) {
170
            $this->insertAt($index, $data);
171
        }
172
        
173
        $this->size++;
174
    }
175
176
    /**
177
     * Inserts at the beginning of the list.
178
     *
179
     * @param mixed $data
180
     */
181
    private function insertBeginning($data) {
182
        $newNode = new Node($data);
183
        if($this->head === null) {
184
            $newNode->next = &$this->head;
185
            $newNode->prev = &$this->head;
186
            $this->head = &$newNode;
187
            $this->tail = &$newNode;
188
        } else {
189
            $this->tail->next = &$newNode;
190
            $newNode->next = &$this->head;
191
            $newNode->prev = &$this->tail;
192
            $this->head = &$newNode;
193
        }
194
    }
195
196
    /**
197
     * Add a new node in the specified index.
198
     *
199
     * @param mixed $data the data to be stored.
200
     */
201
    private function insertEnd($data) {
202
        $newNode = new Node($data);
203
        $this->tail->next = &$newNode;
204
        $newNode->next = &$this->head;
205
        $newNode->prev = &$this->tail;
206
        $this->tail = &$newNode;
207
        $this->head->prev = &$newNode;
208
    }
209
210
    /**
211
     * Add a new node in the specified index.
212
     *
213
     * @param integer $index the position.
214
     * @param mixed $data the data to be stored.
215
     */
216
    private function insertAt($index, $data) {
217
        $newNode = new Node($data);
218
        $current = $this->head;
219
        $prev = null;
220
        $i = 0;
221
        while($i < $index) {
222
            $prev = $current;
223
            $current = $current->next;
224
            $i++;
225
        }
226
        
227
        $prev->next = &$newNode;
228
        $newNode->prev = &$prev;
229
        $newNode->next = &$current;
230
        $current->prev = &$newNode;
231
    }
232
233
    /**
234
     * Adds at the end of the list new node containing
235
     * the data to be stored.
236
     *
237
     * @param mixed $data The data
238
     */
239
    public function push($data) {
240
        $this->insert($this->size, $data);
241
    }
242
243
    /**
244
     * Adds at the beginning a node in the list.
245
     *
246
     * @param mixed $data
247
     * @return mixed the data stored.
248
     */
249
    public function unshift($data) {
250
        $this->insert(0, $data);
251
    }
252
    
253
    /**
254
     * Delete a node in the given position and returns it back.
255
     *
256
     * @param integer $index the position.
257
     * @throws OutOfBoundsException if index is negative
258
     *  or is greater than the size of the list.
259
     */
260 View Code Duplication
    public function delete($index) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
261
        if($index < 0 || ($index > 0 && $index > $this->size - 1)) {
262
            throw new OutOfBoundsException();
263
        }
264
265
        // if the list is empty
266
        if($this->head === null) {
267
            return null;
268
        }
269
        
270
        // if only there is an element
271
        if($this->head->next === $this->head) {
272
            $temp = $this->head;
273
            $this->head = null;
274
            $this->size--;
275
276
            return $temp->data;
277
        }
278
279
        if($index === 0) {
280
            return $this->deleteBeginning();
281
        } else if($index === $this->size - 1) {
282
            return $this->deleteEnd();
283
        } else {
284
            return $this->deleteAt($index);
285
        }
286
    }
287
288
    /**
289
     * Deletes at the beginnig of the list and returns the data stored.
290
     *
291
     * @return mixed the data stored in the node.
292
     */
293 View Code Duplication
    private function deleteBeginning() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
294
        $temp = $this->head;
295
        $this->head = &$this->head->next;
296
        $this->tail->next = &$this->head;
297
        $this->size--;
298
299
        return $temp->data;
300
    }
301
302
    /**
303
     * Deletes at the specified position and returns the data stored.
304
     *
305
     * @param integer $index the position.
306
     * @return mixed the data stored in the node.
307
     */
308 View Code Duplication
    private function deleteAt($index) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
309
        $i = 0;
310
        $prev = $this->head;
311
        $current = $this->head;
312
        
313
        while($i < $index) {
314
            $prev = $current;
315
            $current = $current->next;
316
            $i++;
317
        }
318
319
        $temp = $current;
320
        $prev->next = &$current->next;
321
        $current->next->prev = &$pre;
0 ignored issues
show
Bug introduced by
The variable $pre does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
322
        $current = null;
0 ignored issues
show
Unused Code introduced by
$current is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
323
        $this->size--;
324
325
        return $temp->data;
326
    }
327
328
    /**
329
     * Deletes at the end of the list and returns the data stored.
330
     *
331
     * @return mixed the data stored in the node.
332
     */
333 View Code Duplication
    private function deleteEnd() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
334
        $prev = $this->head;
335
        $current = $this->head;
336
        
337
        while($current !== $this->tail) {
338
            $prev = $current;
339
            $current = $current->next;
340
        }
341
        
342
        $temp = $current;
343
        $prev->next = &$this->head;
344
        $this->head->prev = &$prev;
345
        $this->tail = &$prev;
346
        $current = null;
0 ignored issues
show
Unused Code introduced by
$current is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
347
348
        $this->size--;
349
350
        return $temp->data;
351
    }
352
353
    /**
354
     * Deletes the first node of the list and returns it.
355
     *
356
     * @return mixed the data.
357
     */
358
    public function shift() {
359
        return $this->delete(0);
360
    }
361
362
    /**
363
     * Removes and returns the last node in the list.
364
     *
365
     * @return mixed data in node.
366
     */
367
    public function pop() {
368
        return $this->delete($this->size - 1);
369
    }
370
    
371
    /**
372
     * Converts/exports the list content into array type.
373
     *
374
     * @return array data stored in all nodes.
375
     */
376
    public function toArray() : array {
377
        $arr = [];
378
        foreach($this->getAll() as $node) {
379
            $arr[] = $node;
380
        }
381
382
        return $arr;
383
    }
384
385
    /**
386
     * Reset the cursor position.
387
     */
388
    public function rewind() {
389
        $this->position = 0;
390
        $this->current = &$this->head;
391
    }
392
393
    /**
394
     * Returns the current node data.
395
     *
396
     * @return mixed
397
     */
398
    public function current() {
399
        return $this->current->data;
400
    }
401
402
    /**
403
     * Key or index that indicates the cursor position.
404
     *
405
     * @return integer The current position.
406
     */
407
    public function key() {
408
        return $this->position;
409
    }
410
411
    /**
412
     * Move the cursor to the next node and increments the
413
     * position counter.
414
     */
415
    public function next() {
416
        ++$this->position;
417
        $this->current = $this->current->next;
418
    }
419
420
    /**
421
     * Returns if the current pointer position is valid.
422
     *
423
     * @return boolean true if pointer is not last, else false.
424
     */
425
    public function valid() {
426
        return $this->position < $this->size;
427
    }
428
}