|
1
|
|
|
<?php namespace GenericCollections\Internal; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This is a double linked list that contains elements by position |
|
5
|
|
|
* |
|
6
|
|
|
* It extends \SplDoublyLinkedList and add this methods: |
|
7
|
|
|
* - contains: return if the value is found in the list |
|
8
|
|
|
* - search: search for a value in the list, return the index |
|
9
|
|
|
* - clear: clear the list |
|
10
|
|
|
* |
|
11
|
|
|
* The search depends on the flag strictComparison. |
|
12
|
|
|
* If is TRUE then the comparison is identical (===), this is the default behavior |
|
13
|
|
|
* If is FALSE then the comparison is equal (==) |
|
14
|
|
|
* |
|
15
|
|
|
* @package GenericCollections\Internal |
|
16
|
|
|
*/ |
|
17
|
|
|
class DoubleLinkedList extends \SplDoublyLinkedList |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Set if the search inside the storage must be identical (true) or equal (false) |
|
21
|
|
|
* |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
private $strict = true; |
|
25
|
|
|
|
|
26
|
3 |
|
public function strictComparisonOn() |
|
27
|
|
|
{ |
|
28
|
3 |
|
$this->strict = true; |
|
29
|
3 |
|
} |
|
30
|
|
|
|
|
31
|
15 |
|
public function strictComparisonOff() |
|
32
|
|
|
{ |
|
33
|
15 |
|
$this->strict = false; |
|
34
|
15 |
|
} |
|
35
|
|
|
|
|
36
|
6 |
|
public function getStrictComparison() |
|
37
|
|
|
{ |
|
38
|
6 |
|
return $this->strict; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns TRUE if the element exist in the storage |
|
44
|
|
|
* |
|
45
|
|
|
* @param mixed $element |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
33 |
|
public function contains($element) |
|
49
|
|
|
{ |
|
50
|
33 |
|
return (-1 !== $this->search($element)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Perform a linear search inside the storage, |
|
55
|
|
|
* because the elements contained are not sorted. |
|
56
|
|
|
* |
|
57
|
|
|
* Return the index of the element found, -1 if not found |
|
58
|
|
|
* |
|
59
|
|
|
* @param $element |
|
60
|
|
|
* @return int |
|
61
|
|
|
*/ |
|
62
|
39 |
|
public function search($element) |
|
63
|
|
|
{ |
|
64
|
39 |
|
$position = -1; |
|
65
|
39 |
|
if (! $this->isEmpty()) { |
|
66
|
39 |
|
foreach ($this as $index => $current) { |
|
67
|
39 |
|
if (($this->strict) ? $current === $element : $current == $element) { |
|
68
|
36 |
|
$position = $index; |
|
69
|
36 |
|
break; |
|
70
|
|
|
} |
|
71
|
39 |
|
} |
|
72
|
39 |
|
} |
|
73
|
39 |
|
return $position; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Clear the contents of the container |
|
78
|
|
|
* |
|
79
|
|
|
* There is a bug & patch for SplDoublyLinkedList https://bugs.php.net/bug.php?id=60759 |
|
80
|
|
|
* that does the same operation (pop until count == 0) |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function clear() |
|
83
|
|
|
{ |
|
84
|
3 |
|
while ($this->count() > 0) { |
|
85
|
3 |
|
$this->pop(); |
|
86
|
3 |
|
} |
|
87
|
3 |
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|