4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Intl\ResourceBundle\Util
;
14 use Symfony\Component\Intl\Exception\OutOfBoundsException
;
17 * Implements a ring buffer.
19 * A ring buffer is an array-like structure with a fixed size. If the buffer
20 * is full, the next written element overwrites the first bucket in the buffer,
21 * then the second and so on.
23 * @author Bernhard Schussek <bschussek@gmail.com>
25 class RingBuffer
implements \ArrayAccess
27 private $values = array();
29 private $indices = array();
35 public function __construct($size)
43 public function offsetExists($key)
45 return isset($this->indices
[$key]);
51 public function offsetGet($key)
53 if (!isset($this->indices
[$key])) {
54 throw new OutOfBoundsException(sprintf(
55 'The index "%s" does not exist.',
60 return $this->values
[$this->indices
[$key]];
66 public function offsetSet($key, $value)
68 if (false !== ($keyToRemove = array_search($this->cursor
, $this->indices
))) {
69 unset($this->indices
[$keyToRemove]);
72 $this->values
[$this->cursor
] = $value;
73 $this->indices
[$key] = $this->cursor
;
75 $this->cursor
= ($this->cursor +
1) %
$this->size
;
81 public function offsetUnset($key)
83 if (isset($this->indices
[$key])) {
84 $this->values
[$this->indices
[$key]] = null;
85 unset($this->indices
[$key]);