|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Gerard van Helden <[email protected]> |
|
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\FrameworkExtraBundle\Util; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A sorted set implementation: |
|
11
|
|
|
* |
|
12
|
|
|
* All values are ensured unique. |
|
13
|
|
|
* All values are ensured sorted. |
|
14
|
|
|
* |
|
15
|
|
|
* @deprecated Use sorting and unique functionality from `zicht/itertools` instead |
|
16
|
|
|
*/ |
|
17
|
|
|
class SortedSet implements \Countable |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* The set values |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $values; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Construct the set with initial values |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $values |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $values = array()) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->setValues($values); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Remove all current values and replace them with the values. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $values |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setValues($values) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->values = array(); |
|
47
|
|
|
$this->addValues($values); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns the values as an array |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
public function toArray() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->values; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Add all values in the given array. |
|
64
|
|
|
* Values already in the set are ignored, and the set is sorted after adding. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $values |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function addValues($values) |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($values as $value) { |
|
72
|
|
|
$this->add($value); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Checks if the set contains the value |
|
79
|
|
|
* |
|
80
|
|
|
* @param int|float|string|bool $value |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function contains($value) |
|
84
|
|
|
{ |
|
85
|
|
|
return in_array($value, $this->values); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Adds a value to the set. |
|
91
|
|
|
* If the value is already present, it is ignored |
|
92
|
|
|
* |
|
93
|
|
|
* @param int|float|string|bool $value |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public function add($value) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->values[] = $value; |
|
99
|
|
|
$this->stateChanged(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Removes a value from the set, if present |
|
105
|
|
|
* |
|
106
|
|
|
* @param int|float|string|bool $value |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
public function remove($value) |
|
110
|
|
|
{ |
|
111
|
|
|
foreach ($this->values as $i => $v) { |
|
112
|
|
|
if ($value == $v) { |
|
113
|
|
|
unset($this->values[$i]); |
|
114
|
|
|
break; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
$this->stateChanged(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Returns the number of items in the set |
|
123
|
|
|
* |
|
124
|
|
|
* @return int |
|
125
|
|
|
*/ |
|
126
|
|
|
public function count() |
|
127
|
|
|
{ |
|
128
|
|
|
return count($this->values); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Ensured uniqueness and sorted values |
|
134
|
|
|
* |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
|
|
private function stateChanged() |
|
138
|
|
|
{ |
|
139
|
|
|
if (count($this->values)) { |
|
140
|
|
|
$this->values = array_unique(array_values($this->values)); |
|
141
|
|
|
sort($this->values); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|