Passed
Push — 1.x ( 2d6b66...c4f678 )
by Ulises Jeremias
02:24
created

Set::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Mbh\Collection;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
12
use Traversable;
13
use ArrayAccess;
14
use IteratorAggregate;
15
use Error;
16
use OutOfBoundsException;
17
use OutOfRangeException;
18
19
/**
20
 * A sequence of unique values.
21
 *
22
 * @package structures
23
 * @author Ulises Jeremias Cornejo Fandos <[email protected]>
24
 */
25
class Set implements ArrayAccess, CollectionInterface, IteratorAggregate
26
{
27
    use Traits\Collection;
28
29
    const MIN_CAPACITY = Map::MIN_CAPACITY;
30
31
    /**
32
     * @var Map internal map to store the values.
33
     */
34
    private $table;
35
36
    /**
37
     * Creates a new set using the values of an array or Traversable object.
38
     * The keys of either will not be preserved.
39
     *
40
     * @param array|Traversable|null $values
41
     */
42
    public function __construct($values = null)
43
    {
44
        $this->table = new Map();
45
46
        if (func_num_args()) {
47
            $this->add(...$values);
48
        }
49
    }
50
51
    /**
52
     * Adds zero or more values to the set.
53
     *
54
     * @param mixed ...$values
55
     */
56
    public function add(...$values)
57
    {
58
        foreach ($values as $value) {
59
            $this->table->put($value, null);
60
        }
61
    }
62
63
    /**
64
     * Ensures that enough memory is allocated for a specified capacity. This
65
     * potentially reduces the number of reallocations as the size increases.
66
     *
67
     * @param int $capacity The number of values for which capacity should be
68
     *                      allocated. Capacity will stay the same if this value
69
     *                      is less than or equal to the current capacity.
70
     */
71
    public function allocate(int $capacity)
72
    {
73
        $this->table->allocate($capacity);
74
    }
75
76
    /**
77
     * Returns the current capacity of the set.
78
     *
79
     * @return int
80
     */
81
    public function capacity(): int
82
    {
83
        return $this->table->capacity();
84
    }
85
86
    /**
87
     * Clear all elements in the Set
88
     */
89
    public function clear()
90
    {
91
        $this->table->clear();
92
    }
93
94
    /**
95
     * Determines whether the set contains all of zero or more values.
96
     *
97
     * @param mixed ...$values
98
     *
99
     * @return bool true if at least one value was provided and the set
100
     *              contains all given values, false otherwise.
101
     */
102
    public function contains(...$values): bool
103
    {
104
        foreach ($values as $value) {
105
            if (! $this->table->hasKey($value)) {
106
                return false;
107
            }
108
        }
109
110
        return true;
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function copy()
117
    {
118
        return new self($this);
119
    }
120
121
    /**
122
     * Returns the number of elements in the Stack
123
     *
124
     * @return int
125
     */
126
    public function count(): int
127
    {
128
        return count($this->table);
129
    }
130
131
    /**
132
     * Returns the first value in the set.
133
     *
134
     * @return mixed the first value in the set.
135
     */
136
    public function first()
137
    {
138
        return $this->table->first()->key;
139
    }
140
141
    /**
142
     * Returns the value at a specified position in the set.
143
     *
144
     * @param int $position
145
     *
146
     * @return mixed|null
147
     *
148
     * @throws OutOfRangeException
149
     */
150
    public function get(int $position)
151
    {
152
        return $this->table->skip($position)->key;
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function isEmpty(): bool
159
    {
160
        return $this->table->isEmpty();
161
    }
162
163
    /**
164
     * Returns the last value in the set.
165
     *
166
     * @return mixed the last value in the set.
167
     */
168
    public function last()
169
    {
170
        return $this->table->last()->key;
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176
    public function toArray(): array
177
    {
178
        return iterator_to_array($this);
179
    }
180
181
    /**
182
     * Get iterator
183
     */
184
    public function getIterator()
185
    {
186
        foreach ($this->table as $key => $value) {
187
            yield $key;
188
        }
189
    }
190
191
    /**
192
     * @inheritdoc
193
     *
194
     * @throws OutOfBoundsException
195
     */
196
    public function offsetSet($offset, $value)
197
    {
198
        if ($offset === null) {
199
            $this->add($value);
200
            return;
201
        }
202
203
        throw new OutOfBoundsException();
204
    }
205
206
    /**
207
     * @inheritdoc
208
     */
209
    public function offsetGet($offset)
210
    {
211
        return $this->table->skip($offset)->key;
212
    }
213
214
    /**
215
     * @inheritdoc
216
     *
217
     * @throws Error
218
     */
219
    public function offsetExists($offset)
220
    {
221
        throw new Error();
222
    }
223
224
    /**
225
     * @inheritdoc
226
     *
227
     * @throws Error
228
     */
229
    public function offsetUnset($offset)
230
    {
231
        throw new Error();
232
    }
233
}
234