Completed
Push — master ( 8863d6...f6a034 )
by Siro Díaz
01:40
created

DoublyLinkedList::indexOf()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
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 ArrayAccessTrait;
26
27
    private $head;
28
    private $tail;
29
    private $size;
30
    private $position;
31
    private $current;
32
33 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...
34
        $this->head = null;
35
        $this->tail = &$this->head;
36
        $this->size = 0;
37
        $this->position = 0;
38
        $this->current = &$this->head;
39
    }
40
41
    /**
42
     * Removes all nodes of the list. It removes from the beginning.
43
     */
44
    public function clear() {
45
        while($this->head !== null) {
46
            $this->shift();
47
        }
48
    }
49
    
50
    /**
51
     * Gets the data stored in the position especified.
52
     *
53
     * @param integer $index Index that must be greater than 0
54
     *  and lower than the list size.
55
     * @return mixed The data stored in the given index
56
     * @throws OutOfBoundsException if index is out bounds.
57
     */
58 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...
59
        $node = $this->search($index);
60
        if($node === null) {
61
            return null;
62
        }
63
64
        return $node->data;
65
    }
66
67
    /**
68
     * Gets the node stored in the position especified.
69
     * If index is 0 or (size - 1) the method is O(1) else O(n).
70
     *
71
     * @param integer $index the position.
72
     * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1)
73
     * @return DataStructures\Lists\Nodes\DoublyLinkedListNode|null the node stored in $index.
74
     */
75
    protected function search($index) {
76
        if($index < 0 || $index > $this->size - 1) {
77
            throw new OutOfBoundsException();
78
        }
79
80
        if($index === 0) {
81
            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 81 which is incompatible with the return type documented by DataStructures\Lists\DoublyLinkedList::search of type DataStructures\Lists\Dat...ublyLinkedListNode|null.
Loading history...
82
        }
83
84
        if($index === $this->size - 1) {
85
            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 85 which is incompatible with the return type documented by DataStructures\Lists\DoublyLinkedList::search of type DataStructures\Lists\Dat...ublyLinkedListNode|null.
Loading history...
86
        }
87
88
        $current = &$this->head;
89
        if($index < (int) ceil($this->size / 2)) {
90
            $i = 0;
91
            while($i < $index) {
92
                $current = &$current->next;
93
                $i++;
94
            }    
95
        } else {
96
            $i = $this->size;
97
            while($i > $index) {
98
                $current = &$current->prev;
99
                $i--;
100
            }
101
        }
102
103
        return $current;
104
    }
105
106
    /**
107
     * Returns the data in the last node with O(1).
108
     *
109
     * @return mixed null if the list is empty.
110
     */
111
    public function getLast() {
112
        if($this->head === null) {
113
            return null;
114
        }
115
        return $this->tail->data;
116
    }
117
118
    /**
119
     * Returns the last node with O(1).
120
     *
121
     * @return DataStructures\Lists\Nodes\DoublyLinkedListNode|null if the list is empty.
122
     */
123
    public function searchLast() {
124
        if($this->head === null) {
125
            return null;
126
        }
127
        return $this->tail;
128
    }
129
130
    /**
131
     *
132
     */
133
    public function contains($data) : bool {
134
        if($this->empty()) {
135
            return false;
136
        }
137
138
        $current = $this->head->next;
139
        $prev = $this->head;
140
        while($current !== $this->head) {
141
            if($prev->data === $data) {
142
                return true;
143
            }
144
            $prev = $current;
145
            $current = $current->next;
146
        }
147
148
        return false;
149
    }
150
151
    /**
152
     *
153
     */
154 View Code Duplication
    public function indexOf($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...
155
        if($this->head === null) {
156
            return false;
157
        }
158
        
159
        $current = $this->head;
160
        $i = 0;
161
        
162
        while($i < $this->size) {
163
            if($current->data == $data) {
164
                return $i;
165
            }
166
167
            $current = $current->next;
168
            $i++;
169
        }
170
171
        return false;
172
    }
173
    
174
    /**
175
     * Generator for retrieve all nodes stored.
176
     * 
177
     * @return null if the head is null (or list is empty)
178
     */
179 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...
180
        if($this->head === null) {
181
            return;
182
        }
183
184
        if($this->head === $this->tail) {
185
            yield $this->head->data;
186
        } else {
187
            $current = $this->head;
188
            $i = 0;
189
            while($i < $this->size) {
190
                yield $current->data;
191
                $current = $current->next;
192
                $i++;
193
            }
194
        }
195
    }
196
197
    /**
198
     * Inserts data in the specified position.
199
     *
200
     * @param integer $index the position.
201
     * @param mixed $data the data to store.
202
     */
203 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...
204
        if($index < 0) {
205
            throw new OutOfBoundsException();
206
        }
207
208
        if($index === 0) {
209
            $this->insertBeginning($data);
210
        } else if($index >= $this->size) {
211
            $this->insertEnd($data);
212
        } else if($index > 0 && $index < $this->size) {
213
            $this->insertAt($index, $data);
214
        }
215
        
216
        $this->size++;
217
    }
218
219
    /**
220
     * Inserts at the beginning of the list.
221
     *
222
     * @param mixed $data
223
     */
224
    private function insertBeginning($data) {
225
        $newNode = new Node($data);
226
        if($this->head === null) {
227
            $newNode->next = &$this->head;
228
            $newNode->prev = &$this->head;
229
            $this->head = &$newNode;
230
            $this->tail = &$newNode;
231
        } else {
232
            $this->tail->next = &$newNode;
233
            $newNode->next = &$this->head;
234
            $newNode->prev = &$this->tail;
235
            $this->head = &$newNode;
236
        }
237
    }
238
239
    /**
240
     * Add a new node in the specified index.
241
     *
242
     * @param mixed $data the data to be stored.
243
     */
244
    private function insertEnd($data) {
245
        $newNode = new Node($data);
246
        $this->tail->next = &$newNode;
247
        $newNode->next = &$this->head;
248
        $newNode->prev = &$this->tail;
249
        $this->tail = &$newNode;
250
        $this->head->prev = &$newNode;
251
    }
252
253
    /**
254
     * Add a new node in the specified index.
255
     *
256
     * @param integer $index the position.
257
     * @param mixed $data the data to be stored.
258
     */
259
    private function insertAt($index, $data) {
260
        $newNode = new Node($data);
261
        $current = $this->head;
262
        $prev = null;
263
        $i = 0;
264
        while($i < $index) {
265
            $prev = $current;
266
            $current = $current->next;
267
            $i++;
268
        }
269
        
270
        $prev->next = &$newNode;
271
        $newNode->prev = &$prev;
272
        $newNode->next = &$current;
273
        $current->prev = &$newNode;
274
    }
275
276
    /**
277
     * Adds at the end of the list new node containing
278
     * the data to be stored.
279
     *
280
     * @param mixed $data The data
281
     */
282
    public function push($data) {
283
        $this->insert($this->size, $data);
284
    }
285
286
    /**
287
     * Adds at the beginning a node in the list.
288
     *
289
     * @param mixed $data
290
     * @return mixed the data stored.
291
     */
292
    public function unshift($data) {
293
        $this->insert(0, $data);
294
    }
295
    
296
    /**
297
     * Delete a node in the given position and returns it back.
298
     *
299
     * @param integer $index the position.
300
     * @throws OutOfBoundsException if index is negative
301
     *  or is greater than the size of the list.
302
     */
303 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...
304
        if($index < 0 || ($index > 0 && $index > $this->size - 1)) {
305
            throw new OutOfBoundsException();
306
        }
307
308
        // if the list is empty
309
        if($this->head === null) {
310
            return null;
311
        }
312
        
313
        // if only there is an element
314
        if($this->head->next === $this->head) {
315
            $temp = $this->head;
316
            $this->head = null;
317
            $this->size--;
318
319
            return $temp->data;
320
        }
321
322
        if($index === 0) {
323
            return $this->deleteBeginning();
324
        } else if($index === $this->size - 1) {
325
            return $this->deleteEnd();
326
        } else {
327
            return $this->deleteAt($index);
328
        }
329
    }
330
331
    /**
332
     * Deletes at the beginnig of the list and returns the data stored.
333
     *
334
     * @return mixed the data stored in the node.
335
     */
336 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...
337
        $temp = $this->head;
338
        $this->head = &$this->head->next;
339
        $this->tail->next = &$this->head;
340
        $this->size--;
341
342
        return $temp->data;
343
    }
344
345
    /**
346
     * Deletes at the specified position and returns the data stored.
347
     *
348
     * @param integer $index the position.
349
     * @return mixed the data stored in the node.
350
     */
351
    private function deleteAt($index) {
352
        $i = 0;
353
        $prev = $this->head;
354
        $current = $this->head;
355
        
356
        while($i < $index) {
357
            $prev = $current;
358
            $current = $current->next;
359
            $i++;
360
        }
361
362
        $temp = $current;
363
        $prev->next = &$current->next;
364
        $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...
365
        $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...
366
        $this->size--;
367
368
        return $temp->data;
369
    }
370
371
    /**
372
     * Deletes at the end of the list and returns the data stored.
373
     *
374
     * @return mixed the data stored in the node.
375
     */
376 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...
377
        $prev = $this->head;
378
        $current = $this->head;
379
        
380
        while($current !== $this->tail) {
381
            $prev = $current;
382
            $current = $current->next;
383
        }
384
        
385
        $temp = $current;
386
        $prev->next = &$this->head;
387
        $this->head->prev = &$prev;
388
        $this->tail = &$prev;
389
        $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...
390
391
        $this->size--;
392
393
        return $temp->data;
394
    }
395
396
    /**
397
     * Deletes the first node of the list and returns it.
398
     *
399
     * @return mixed the data.
400
     */
401
    public function shift() {
402
        return $this->delete(0);
403
    }
404
405
    /**
406
     * Removes and returns the last node in the list.
407
     *
408
     * @return mixed data in node.
409
     */
410
    public function pop() {
411
        return $this->delete($this->size - 1);
412
    }
413
    
414
    /**
415
     * Converts/exports the list content into array type.
416
     *
417
     * @return array data stored in all nodes.
418
     */
419
    public function toArray() : array {
420
        $arr = [];
421
        foreach($this->getAll() as $node) {
422
            $arr[] = $node;
423
        }
424
425
        return $arr;
426
    }
427
428
    /**
429
     * Reset the cursor position.
430
     */
431
    public function rewind() {
432
        $this->position = 0;
433
        $this->current = &$this->head;
434
    }
435
436
    /**
437
     * Returns the current node data.
438
     *
439
     * @return mixed
440
     */
441
    public function current() {
442
        return $this->current->data;
443
    }
444
445
    /**
446
     * Key or index that indicates the cursor position.
447
     *
448
     * @return integer The current position.
449
     */
450
    public function key() {
451
        return $this->position;
452
    }
453
454
    /**
455
     * Move the cursor to the next node and increments the
456
     * position counter.
457
     */
458
    public function next() {
459
        ++$this->position;
460
        $this->current = $this->current->next;
461
    }
462
463
    /**
464
     * Returns if the current pointer position is valid.
465
     *
466
     * @return boolean true if pointer is not last, else false.
467
     */
468
    public function valid() {
469
        return $this->position < $this->size;
470
    }
471
472
    /**
473
     * Binds to count() method. This is equal to make $this->tree->size().
474
     *
475
     * @return integer the tree size. 0 if it is empty.
476
     */
477
    public function count() {
478
        return $this->size;
479
    }
480
481
    /**
482
     * Returns the array size.
483
     *
484
     * @return int the length
485
     */
486
    public function size() : int {
487
        return $this->size;
488
    }
489
490
    /**
491
     * Checks if the list is empty.
492
     *
493
     * @return boolean true if is empty, else false.
494
     */
495
    public function empty() : bool {
496
        return $this->size === 0;
497
    }
498
}