数组
class Arr
{
public $data = [];
public $len = 0;
public function push($value, $index = -1)
{
if ($index != -1 && $index > $this->len) {
throw new \Exception('index out of range');
}
if ($index == -1 || $index == $this->len) {
$this->data[$this->len] = $value;
$this->len++;
return true;
} else {
for ($i = $this->len - 1; $i >= $index; $i--) {
$this->data[$i + 1] = $this->data[$i];
}
$this->data[$index] = $value;
$this->len++;
return true;
}
}
public function pop($index = -1)
{
if ($this->len <= 0) {
throw new \Exception('array is empty');
}
if ($index != -1 && $index >= $this->len) {
throw new \Exception('index out of range');
}
if ($index == -1 || $index == $this->len - 1) {
$this->len--;
$value = $this->data[$this->len];
unset($this->data[$this->len]);
return $value;
} else {
$this->len--;
$value = $this->data[$index];
for ($i = $index; $i < $this->len; $i++) {
$this->data[$i] = $this->data[$i + 1];
}
unset($this->data[$this->len]);
return $value;
}
}
public function set($index, $value)
{
if ($index < 0 || $index >= $this->len) {
throw new \Exception('index out of range');
}
$this->data[$index] = $value;
}
public function get($index)
{
if ($index < 0 || $index >= $this->len) {
throw new \Exception('index out of range');
}
return $this->data[$index];
}
public function lists()
{
return $this->data;
}
}