Completed
Push — master ( 5df011...747e00 )
by Changwan
03:50
created

HashSet   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 65.56%

Importance

Changes 0
Metric Value
dl 0
loc 147
ccs 40
cts 61
cp 0.6556
rs 9.8
c 0
b 0
f 0
wmc 31
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 4 1
A intersection() 0 6 2
A union() 0 6 2
A difference() 0 6 2
A has() 0 10 3
A __construct() 0 6 2
C equal() 0 33 12
A add() 0 12 3
A remove() 0 9 3
A getIterator() 0 7 1
1
<?php
2
namespace Wandu\Math\Foundation\Set;
3
4
use InvalidArgumentException;
5
use Wandu\Math\Foundation\SetInterface;
6
use ArrayIterator;
7
8
class HashSet implements SetInterface
9
{
10
    /** @var array */
11
    protected $scalars = [];
12
13
    /** @var array */
14
    protected $objects = [];
15
16
    /**
17
     * @param array $items
18
     */
19 5
	public function __construct(array $items)
20
	{
21 5
        foreach ($items as $item) {
22 4
            $this->add($item);
23
        }
24 5
	}
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function count()
30
    {
31 1
        return count($this->scalars) + count($this->objects);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function equal(SetInterface $other)
38
    {
39 2
        if (!$other instanceof HashSet) {
40
            throw new InvalidArgumentException('unsupported type of Set.');
41
        }
42 2
        if (count($other->scalars) !== count($this->scalars)) {
43
            return false;
44
        }
45 2
        if (count($other->objects) !== count($this->objects)) {
46
            return false;
47
        }
48 2
        foreach ($this->scalars as $key => $item) {
49 2
            if (!array_key_exists($key, $other->scalars)) {
50 2
                return false;
51
            }
52
        }
53 2
        foreach ($other->scalars as $key => $item) {
54 2
            if (!array_key_exists($key, $this->scalars)) {
55 2
                return false;
56
            }
57
        }
58 2
        foreach ($this->objects as $key => $item) {
59 1
            if (!array_key_exists($key, $other->objects)) {
60 1
                return false;
61
            }
62
        }
63 2
        foreach ($other->objects as $key => $item) {
64 1
            if (!array_key_exists($key, $this->objects)) {
65 1
                return false;
66
            }
67
        }
68 2
        return true;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function intersection(SetInterface $other)
75
    {
76
        if (!$other instanceof HashSet) {
77
            throw new InvalidArgumentException('unsupported type of Set.');
78
        }
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function union(SetInterface $other)
85
    {
86
        if (!$other instanceof HashSet) {
87
            throw new InvalidArgumentException('unsupported type of Set.');
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function difference(SetInterface $other)
95
    {
96
        if (!$other instanceof HashSet) {
97
            throw new InvalidArgumentException('unsupported type of Set.');
98
        }
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    public function has($item)
105
    {
106 1
        if (is_scalar($item)) {
107 1
            return array_key_exists(gettype($item) . $item, $this->scalars);
108
        }
109
        if (is_object($item)) {
110
            return array_key_exists(spl_object_hash($item), $this->objects);
111
        }
112
        return false;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 4
    public function add($item)
119
    {
120 4
        if (is_scalar($item)) {
121 4
            return $this->scalars[gettype($item) . $item] = $item;
122
        }
123 2
        if (is_object($item)) {
124 2
            return $this->objects[spl_object_hash($item)] = $item;
125
        }
126
        throw new InvalidArgumentException(
127
            'unsupported value: ' . str_replace("\n", '', var_export($item, true))
128
        );
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 1
    public function remove($item)
135
    {
136 1
        if (is_scalar($item)) {
137 1
            unset($this->scalars[gettype($item) . $item]);
138
        }
139 1
        if (is_object($item)) {
140
            unset($this->objects[spl_object_hash($item)]);
141
        }
142 1
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 1
    public function getIterator()
148
    {
149 1
        return new ArrayIterator(array_merge(
150 1
            array_values($this->scalars),
151 1
            array_values($this->objects)
152
        ));
153
    }
154
}
155