|
1
|
|
|
<?php |
|
2
|
|
|
namespace Jmw\Collection\Set; |
|
3
|
|
|
|
|
4
|
|
|
use Jmw\Collection\Lists\Tuple; |
|
5
|
|
|
use Jmw\Collection\Lists\ArrayList; |
|
6
|
|
|
/** |
|
7
|
|
|
* An implementation of a Set using a php array |
|
8
|
|
|
* It is advisable to use a HashSet in most instances, |
|
9
|
|
|
* as it is significantly faster |
|
10
|
|
|
* @author john |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
class ArraySet extends SetAbstract |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $array; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param array $array |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct($array) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->clear(); |
|
26
|
|
|
foreach ($array as $element) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->add($element); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Ensures that this collection contains the specified element (optional operation). |
|
34
|
|
|
* @param multitype $element |
|
35
|
|
|
* @return boolean |
|
36
|
|
|
*/ |
|
37
|
|
|
public function add($element) |
|
38
|
|
|
{ |
|
39
|
|
|
$changed = false; |
|
40
|
|
|
if(!$this->contains($element)) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->array[] = $element; |
|
43
|
|
|
$changed = true; |
|
44
|
|
|
} |
|
45
|
|
|
return $changed; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Removes all of the elements from this collection (optional operation). |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public function clear() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->array = []; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns an iterator over the elements in this collection. |
|
59
|
|
|
* @return IteratorInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
public function iterator() |
|
62
|
|
|
{ |
|
63
|
|
|
return new SetIterator($this); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Removes a single instance of the specified element from this collection, if it is present (optional operation). |
|
68
|
|
|
* @param boolean $changed |
|
|
|
|
|
|
69
|
|
|
*/ |
|
70
|
|
|
public function remove($element) |
|
71
|
|
|
{ |
|
72
|
|
|
$changed = false; |
|
73
|
|
|
foreach ($this->array as $key=>$value) |
|
74
|
|
|
{ |
|
75
|
|
|
if($element === $value) |
|
76
|
|
|
{ |
|
77
|
|
|
$changed = true; |
|
78
|
|
|
unset($this->array[$key]); |
|
79
|
|
|
$this->array = array_values($this->array); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
return $changed; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns the number of elements in this collection. |
|
87
|
|
|
* @return int |
|
88
|
|
|
*/ |
|
89
|
|
|
public function size() |
|
90
|
|
|
{ |
|
91
|
|
|
return count($this->array); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns an array containing all of the elements in this collection. |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
public function toArray() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->array; |
|
101
|
|
|
} |
|
102
|
|
|
} |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.