Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit d7ab0e8

Browse files
committed
some modify for collection
1 parent db5406a commit d7ab0e8

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

src/FixedArray.php

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-04-26
6+
* Time: 9:18
7+
*/
8+
9+
namespace Toolkit\ArrUtil;
10+
11+
/**
12+
* Class FixedArray
13+
* fixed size array implements, and support string key.
14+
* `SplFixedArray` only allow int key.
15+
* @package Toolkit\ArrUtil
16+
*/
17+
class FixedArray implements \ArrayAccess, \IteratorAggregate
18+
{
19+
/**
20+
* @var array
21+
* [
22+
* 'string:key' => 'int:value index'
23+
* ]
24+
*/
25+
protected $keys;
26+
27+
/**
28+
* @var \SplFixedArray
29+
*/
30+
protected $values;
31+
32+
/**
33+
* FixedArray constructor.
34+
* @param int $size
35+
*/
36+
public function __construct(int $size = 0)
37+
{
38+
$this->keys = [];
39+
$this->values = new \SplFixedArray($size);
40+
}
41+
42+
/**
43+
* reset
44+
* @param int $size
45+
*/
46+
public function reset(int $size = 0)
47+
{
48+
$this->keys = [];
49+
$this->values = new \SplFixedArray($size);
50+
}
51+
52+
/**
53+
* @param string $key
54+
* @return bool
55+
*/
56+
public function __isset(string $key)
57+
{
58+
return $this->offsetExists($key);
59+
}
60+
61+
/**
62+
* @param string $key
63+
* @param mixed $value
64+
*/
65+
public function __set(string $key, $value)
66+
{
67+
$this->offsetSet($key, $value);
68+
}
69+
70+
/**
71+
* @param string $key
72+
* @return mixed
73+
*/
74+
public function __get(string $key)
75+
{
76+
return $this->offsetGet($key);
77+
}
78+
79+
/**
80+
* @return int
81+
*/
82+
public function getSize(): int
83+
{
84+
return $this->values->getSize();
85+
}
86+
87+
/**
88+
* @param $key
89+
* @return int
90+
*/
91+
public function getKeyIndex($key): int
92+
{
93+
return $this->keys[$key] ?? -1;
94+
}
95+
96+
/**
97+
* @return array
98+
*/
99+
public function getKeys(): array
100+
{
101+
return $this->keys;
102+
}
103+
104+
/**
105+
* @param array $keys
106+
*/
107+
public function setKeys(array $keys)
108+
{
109+
$this->keys = $keys;
110+
}
111+
112+
/**
113+
* @return \SplFixedArray
114+
*/
115+
public function getValues(): \SplFixedArray
116+
{
117+
return $this->values;
118+
}
119+
120+
/**
121+
* @param \SplFixedArray $values
122+
*/
123+
public function setValues(\SplFixedArray $values)
124+
{
125+
$this->values = $values;
126+
}
127+
128+
/**
129+
* Defined by IteratorAggregate interface
130+
* Returns an iterator for this object, for use with foreach
131+
* @return \SplFixedArray
132+
*/
133+
public function getIterator(): \SplFixedArray
134+
{
135+
return $this->values;
136+
}
137+
138+
/**
139+
* Checks whether an offset exists in the iterator.
140+
* @param mixed $offset The array offset.
141+
* @return boolean True if the offset exists, false otherwise.
142+
*/
143+
public function offsetExists($offset): bool
144+
{
145+
return isset($this->keys[$offset]);
146+
}
147+
148+
/**
149+
* Gets an offset in the iterator.
150+
* @param mixed $offset The array offset.
151+
* @return mixed The array value if it exists, null otherwise.
152+
*/
153+
public function offsetGet($offset)
154+
{
155+
$index = $this->getKeyIndex($offset);
156+
157+
if ($index >= 0) {
158+
return $this->values[$index];
159+
}
160+
161+
return null;
162+
}
163+
164+
/**
165+
* Sets an offset in the iterator.
166+
* @param mixed $offset The array offset.
167+
* @param mixed $value The array value.
168+
* @return void
169+
*/
170+
public function offsetSet($offset, $value)
171+
{
172+
$index = $this->getSize();
173+
174+
// change size.
175+
if ($index <= \count($this->keys)) {
176+
$this->values->setSize($index + 10);
177+
}
178+
179+
$this->values[$index] = $value;
180+
$this->keys[$offset] = $index;
181+
}
182+
183+
/**
184+
* Unset an offset in the iterator.
185+
* @param mixed $offset The array offset.
186+
* @return void
187+
*/
188+
public function offsetUnset($offset)
189+
{
190+
$index = $this->getKeyIndex($offset);
191+
192+
if ($index >= 0) {
193+
// change size.
194+
$this->values->setSize($index - 1);
195+
196+
unset($this->keys[$offset], $this->values[$index]);
197+
}
198+
}
199+
}

0 commit comments

Comments
 (0)