[highlight lang=php] public function offsetSet($key, $value) {
if ($key === NULL) return $this->push($value);
$this->throwEx(array(‘Do not directly set indices in a %s, use push() instead’, CLASS));
return false;
}
[/highlight] This allows the use of PHP’s ‘push’ syntax: $foo[] = ‘bar’;
Full class after the jump.
[highlight lang=php]<?php
class ringBuffer implements ArrayAccess {
private $data;
private $current = 0;
public $size;
public function __construct($sz=10) {
$this->size = $sz;
foreach(range(1, $sz) as $k=>$v) {
/* s: whether this node is set; d: whether this node is “dirty” (has been read since it was last set); v: the value */
$this->data[$k] = array(‘s’ => false, ‘d’ => false, ‘v’ =>
NULL);
}
}
public function offsetSet($key, $value) {
if ($key ===
NULL) return $this->push($value);
$this->throwEx(array(‘Do not directly set indices in a %s, use push() instead’,
CLASS));
return false;
}
public function offsetGet($key) {
if (! is_int($key)) $this->throwEx(array(“offset ‘%s’ must be an integer, not %s”, $key, gettype($key)));
if ($key > $this->size) $key %= $this->size;
if (!isset($this->data[$key])) return false;
if (!$this->data[$key][‘s’]) return false;
$this->data[$key][‘d’] = false;
return $this->data[$key][‘v’];
}
public function offsetExists($key) {
if (! is_int($key))
throw new Exception();
if ($key > $this->size)
return $this->data[$key][‘s’];
}
public function offsetUnset($key) {
$this->data[$key][‘s’] = false;
$this->data[$key][‘v’] =
NULL;
}
private function throwEx() {
$args = func_get_args();
if (is_array($args)) {
$msg = call_user_func_array(‘sprintf’, $args);
}
throw new Exception($msg);
}
public function push($value) {
if ($this->current >= $this->size) $this->current %= $this->size;
$this->data[$this->current] = array(‘s’=>true, ‘d’=>true, ‘v’ => $value);
$this->current++;
return $value;
}
}
$a = new ringBuffer(2);
$a->push(“foo”);
$a->push(“bar”);
$a->push(“baz”);
$a[] = ‘quux’;
var_dump($a);
[/highlight]