1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Compiler\Math; |
3
|
|
|
|
4
|
|
|
use ArrayIterator; |
5
|
|
|
use Countable; |
6
|
|
|
use IteratorAggregate; |
7
|
|
|
|
8
|
|
|
class Set implements Countable, IteratorAggregate |
9
|
|
|
{ |
10
|
|
|
/** @var array */ |
11
|
|
|
protected $items; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Set constructor. |
15
|
|
|
* @param ...$items |
16
|
|
|
*/ |
17
|
|
|
public function __construct(...$items) |
18
|
|
|
{ |
19
|
|
|
$this->items = $this->arrayUnique($items); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function count() |
26
|
|
|
{ |
27
|
|
|
return count($this->items); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function getIterator() |
34
|
|
|
{ |
35
|
|
|
return new ArrayIterator($this->items); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param mixed $item |
40
|
|
|
*/ |
41
|
|
|
public function insert($item) |
42
|
|
|
{ |
43
|
|
|
if (!in_array($item, $this->items, true)) { |
44
|
|
|
$this->items[] = $item; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param mixed $item |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function has($item) |
53
|
|
|
{ |
54
|
|
|
return in_array($item, $this->items, true); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \Wandu\Compiler\Math\Set $other |
59
|
|
|
* @return \Wandu\Compiler\Math\Set |
60
|
|
|
*/ |
61
|
|
|
public function ringSum(Set $other) |
62
|
|
|
{ |
63
|
|
|
if (!in_array(null, $this->items, true)) { |
64
|
|
|
return new Set(...$this->items); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$unionSet = $this->arrayUnique(array_merge($this->items, $other->items)); |
68
|
|
|
if (in_array(null, $other->items, true)) { |
69
|
|
|
return new Set(...$unionSet); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new Set(...array_filter($unionSet)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function toArray() |
79
|
|
|
{ |
80
|
|
|
return $this->items; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param \Wandu\Compiler\Math\Set $other |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function equal(Set $other) |
88
|
|
|
{ |
89
|
|
|
foreach ($this->items as $item) { |
90
|
|
|
if (!in_array($item, $other->items, true)) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
foreach ($other->items as $item) { |
95
|
|
|
if (!in_array($item, $this->items, true)) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param \Wandu\Compiler\Math\Set $other |
104
|
|
|
*/ |
105
|
|
|
public function union(Set $other) |
106
|
|
|
{ |
107
|
|
|
$this->items = $this->arrayUnique(array_merge($this->items, $other->items)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function arrayUnique(array $array) |
111
|
|
|
{ |
112
|
|
|
$arrayToReturn = []; |
113
|
|
|
foreach ($array as $item) { |
114
|
|
|
if (!in_array($item, $arrayToReturn, true)) { |
115
|
|
|
$arrayToReturn[] = $item; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return $arrayToReturn; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|