Completed
Push — master ( 7c623d...b84c7d )
by Siro Díaz
01:29
created

CircularLinkedList::empty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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\SimpleLinkedListNode;
13
use DataStructures\Lists\Interfaces\ListInterface;
14
use DataStructures\Lists\ListAbstract;
15
use OutOfBoundsException;
16
17
/**
18
 * CircularLinkedList
19
 *
20
 * CircularLinkedList is a single and circular linked list that has
21
 * a pointer to the next node and also and last node points to head.
22
 *
23
 * @author Siro Diaz Palazon <[email protected]>
24
 */
25
class CircularLinkedList extends ListAbstract {
26
    use ArrayAccessTrait;
27
    private $head;
28
    private $tail;
29
    private $current;
30
    private $position;
31
32 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...
33
        $this->head = null;
34
        $this->tail = &$this->head;
35
        $this->size = 0;
36
        $this->position = 0;
37
        $this->current = &$this->head;
38
    }
39
40
    /**
41
     * Inserts data in the specified position.
42
     *
43
     * @param integer $index the position.
44
     * @param mixed $data the data to store.
45
     */
46 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...
47
        if($index < 0) {
48
            throw new OutOfBoundsException();
49
        }
50
51
        if($index === 0) {
52
            $this->insertBeginning($data);
53
        } else if($index >= $this->size) {
54
            $this->insertEnd($data);
55
        } else if($index > 0 && $index < $this->size) {
56
            $this->insertAt($index, $data);
57
        }
58
        $this->current = &$this->head;
59
        $this->size++;
60
    }
61
62
    /**
63
     * Inserts at the beginning of the list.
64
     *
65
     * @param mixed $data
66
     */
67
    private function insertBeginning($data) {
68
        $newNode = new SimpleLinkedListNode($data);
69
        if($this->head === null) {
70
            $newNode->next = &$this->head;
71
            $this->head = &$newNode;
72
            $this->tail = &$newNode;
73
        } else {
74
            $this->tail->next = &$newNode;
75
            $newNode->next = $this->head;
76
            $this->head = &$newNode;
77
        }
78
    }
79
80
    /**
81
     * Add a new node in the specified index.
82
     *
83
     * @param integer $index the position.
0 ignored issues
show
Bug introduced by
There is no parameter named $index. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

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