You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
342 lines
10 KiB
342 lines
10 KiB
14 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
14 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\base;
|
||
|
|
||
|
/**
|
||
|
* Vector implements an integer-indexed collection class.
|
||
|
*
|
||
|
* You can access, append, insert, remove an item from the vector
|
||
13 years ago
|
* by calling methods such as [[itemAt()]], [[add()]], [[insertAt()]],
|
||
|
* [[remove()]] and [[removeAt()]].
|
||
14 years ago
|
*
|
||
13 years ago
|
* To get the number of the items in the vector, use [[getCount()]].
|
||
14 years ago
|
*
|
||
|
* Because Vector implements a set of SPL interfaces, it can be used
|
||
|
* like a regular PHP array as follows,
|
||
|
*
|
||
14 years ago
|
* ~~~
|
||
13 years ago
|
* $vector[] = $item; // append new item at the end
|
||
|
* $vector[$index] = $item; // set new item at $index
|
||
|
* unset($vector[$index]); // remove the item at $index
|
||
|
* if (isset($vector[$index])) // if the vector has an item at $index
|
||
14 years ago
|
* foreach ($vector as $index=>$item) // traverse each item in the vector
|
||
13 years ago
|
* $n = count($vector); // count the number of items
|
||
14 years ago
|
* ~~~
|
||
|
*
|
||
|
* Note that if you plan to extend Vector by performing additional operations
|
||
|
* with each addition or removal of an item (e.g. performing type check),
|
||
13 years ago
|
* please make sure you override [[insertAt()]] and [[removeAt()]].
|
||
|
*
|
||
|
* @property integer $count the number of items in the vector
|
||
14 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
13 years ago
|
class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Countable
|
||
14 years ago
|
{
|
||
|
/**
|
||
|
* @var array internal data storage
|
||
|
*/
|
||
|
private $_d = array();
|
||
|
/**
|
||
|
* @var integer number of items
|
||
|
*/
|
||
|
private $_c = 0;
|
||
|
|
||
|
/**
|
||
|
* Constructor.
|
||
|
* Initializes the vector with an array or an iterable object.
|
||
|
* @param mixed $data the initial data to be populated into the vector.
|
||
14 years ago
|
* This can be an array or an iterable object.
|
||
13 years ago
|
* @param array $config name-value pairs that will be used to initialize the object properties
|
||
14 years ago
|
* @throws Exception if data is not well formed (neither an array nor an iterable object)
|
||
|
*/
|
||
13 years ago
|
public function __construct($data = array(), $config = array())
|
||
14 years ago
|
{
|
||
12 years ago
|
if (!empty($data)) {
|
||
14 years ago
|
$this->copyFrom($data);
|
||
|
}
|
||
13 years ago
|
parent::__construct($config);
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns an iterator for traversing the items in the vector.
|
||
|
* This method is required by the SPL interface `IteratorAggregate`.
|
||
|
* It will be implicitly called when you use `foreach` to traverse the vector.
|
||
13 years ago
|
* @return VectorIterator an iterator for traversing the items in the vector.
|
||
14 years ago
|
*/
|
||
|
public function getIterator()
|
||
|
{
|
||
|
return new VectorIterator($this->_d);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the number of items in the vector.
|
||
|
* This method is required by the SPL `Countable` interface.
|
||
|
* It will be implicitly called when you use `count($vector)`.
|
||
|
* @return integer number of items in the vector.
|
||
|
*/
|
||
|
public function count()
|
||
|
{
|
||
|
return $this->getCount();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the number of items in the vector.
|
||
|
* @return integer the number of items in the vector
|
||
|
*/
|
||
|
public function getCount()
|
||
|
{
|
||
|
return $this->_c;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the item at the specified index.
|
||
|
* @param integer $index the index of the item
|
||
|
* @return mixed the item at the index
|
||
12 years ago
|
* @throws InvalidParamException if the index is out of range
|
||
14 years ago
|
*/
|
||
|
public function itemAt($index)
|
||
|
{
|
||
|
if (isset($this->_d[$index])) {
|
||
|
return $this->_d[$index];
|
||
13 years ago
|
} elseif ($index >= 0 && $index < $this->_c) { // in case the value is null
|
||
14 years ago
|
return $this->_d[$index];
|
||
13 years ago
|
} else {
|
||
12 years ago
|
throw new InvalidParamException('Index out of range: ' . $index);
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Appends an item at the end of the vector.
|
||
|
* @param mixed $item new item
|
||
|
* @return integer the zero-based index at which the item is added
|
||
|
* @throws Exception if the vector is read-only.
|
||
|
*/
|
||
|
public function add($item)
|
||
|
{
|
||
|
$this->insertAt($this->_c, $item);
|
||
13 years ago
|
return $this->_c - 1;
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Inserts an item at the specified position.
|
||
|
* Original item at the position and the following items will be moved
|
||
|
* one step towards the end.
|
||
|
* @param integer $index the specified position.
|
||
|
* @param mixed $item new item to be inserted into the vector
|
||
12 years ago
|
* @throws InvalidParamException if the index specified is out of range, or the vector is read-only.
|
||
14 years ago
|
*/
|
||
|
public function insertAt($index, $item)
|
||
|
{
|
||
14 years ago
|
if ($index === $this->_c) {
|
||
|
$this->_d[$this->_c++] = $item;
|
||
13 years ago
|
} elseif ($index >= 0 && $index < $this->_c) {
|
||
14 years ago
|
array_splice($this->_d, $index, 0, array($item));
|
||
|
$this->_c++;
|
||
13 years ago
|
} else {
|
||
12 years ago
|
throw new InvalidParamException('Index out of range: ' . $index);
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Removes an item from the vector.
|
||
|
* The vector will search for the item, and the first item found
|
||
|
* will be removed from the vector.
|
||
|
* @param mixed $item the item to be removed.
|
||
|
* @return mixed the index at which the item is being removed, or false
|
||
|
* if the item cannot be found in the vector.
|
||
|
* @throws Exception if the vector is read only.
|
||
|
*/
|
||
|
public function remove($item)
|
||
|
{
|
||
13 years ago
|
if (($index = $this->indexOf($item)) >= 0) {
|
||
14 years ago
|
$this->removeAt($index);
|
||
|
return $index;
|
||
13 years ago
|
} else {
|
||
14 years ago
|
return false;
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Removes an item at the specified position.
|
||
|
* @param integer $index the index of the item to be removed.
|
||
|
* @return mixed the removed item.
|
||
12 years ago
|
* @throws InvalidParamException if the index is out of range, or the vector is read only.
|
||
14 years ago
|
*/
|
||
|
public function removeAt($index)
|
||
|
{
|
||
14 years ago
|
if ($index >= 0 && $index < $this->_c) {
|
||
|
$this->_c--;
|
||
|
if ($index === $this->_c) {
|
||
|
return array_pop($this->_d);
|
||
13 years ago
|
} else {
|
||
14 years ago
|
$item = $this->_d[$index];
|
||
|
array_splice($this->_d, $index, 1);
|
||
|
return $item;
|
||
14 years ago
|
}
|
||
13 years ago
|
} else {
|
||
12 years ago
|
throw new InvalidParamException('Index out of range: ' . $index);
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Removes all items from the vector.
|
||
13 years ago
|
* @param boolean $safeClear whether to clear every item by calling [[removeAt]].
|
||
|
* Defaults to false, meaning all items in the vector will be cleared directly
|
||
|
* without calling [[removeAt]].
|
||
14 years ago
|
*/
|
||
12 years ago
|
public function removeAll($safeClear = false)
|
||
14 years ago
|
{
|
||
13 years ago
|
if ($safeClear) {
|
||
13 years ago
|
for ($i = $this->_c - 1; $i >= 0; --$i) {
|
||
13 years ago
|
$this->removeAt($i);
|
||
|
}
|
||
13 years ago
|
} else {
|
||
13 years ago
|
$this->_d = array();
|
||
|
$this->_c = 0;
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns a value indicating whether the vector contains the specified item.
|
||
|
* Note that the search is based on strict PHP comparison.
|
||
|
* @param mixed $item the item
|
||
|
* @return boolean whether the vector contains the item
|
||
|
*/
|
||
12 years ago
|
public function has($item)
|
||
14 years ago
|
{
|
||
|
return $this->indexOf($item) >= 0;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the index of the specified item in the vector.
|
||
|
* The index is zero-based. If the item is not found in the vector, -1 will be returned.
|
||
|
* Note that the search is based on strict PHP comparison.
|
||
|
* @param mixed $item the item
|
||
|
* @return integer the index of the item in the vector (0 based), -1 if not found.
|
||
|
*/
|
||
|
public function indexOf($item)
|
||
|
{
|
||
|
$index = array_search($item, $this->_d, true);
|
||
|
return $index === false ? -1 : $index;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the vector as a PHP array.
|
||
|
* @return array the items in the vector.
|
||
|
*/
|
||
|
public function toArray()
|
||
|
{
|
||
|
return $this->_d;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Copies iterable data into the vector.
|
||
|
* Note, existing data in the vector will be cleared first.
|
||
|
* @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable`
|
||
12 years ago
|
* @throws InvalidParamException if data is neither an array nor an object implementing `Traversable`.
|
||
14 years ago
|
*/
|
||
13 years ago
|
public function copyFrom($data)
|
||
14 years ago
|
{
|
||
14 years ago
|
if (is_array($data) || $data instanceof \Traversable) {
|
||
14 years ago
|
if ($this->_c > 0) {
|
||
12 years ago
|
$this->removeAll();
|
||
14 years ago
|
}
|
||
|
if ($data instanceof self) {
|
||
|
$data = $data->_d;
|
||
|
}
|
||
|
foreach ($data as $item) {
|
||
|
$this->add($item);
|
||
|
}
|
||
13 years ago
|
} else {
|
||
12 years ago
|
throw new InvalidParamException('Data must be either an array or an object implementing Traversable.');
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Merges iterable data into the vector.
|
||
|
* New items will be appended to the end of the existing items.
|
||
13 years ago
|
* @param array|\Traversable $data the data to be merged with. It must be an array or object implementing Traversable
|
||
12 years ago
|
* @throws InvalidParamException if data is neither an array nor an object implementing `Traversable`.
|
||
14 years ago
|
*/
|
||
|
public function mergeWith($data)
|
||
|
{
|
||
14 years ago
|
if (is_array($data) || ($data instanceof \Traversable)) {
|
||
14 years ago
|
if ($data instanceof Vector) {
|
||
|
$data = $data->_d;
|
||
|
}
|
||
|
foreach ($data as $item) {
|
||
|
$this->add($item);
|
||
|
}
|
||
13 years ago
|
} else {
|
||
12 years ago
|
throw new InvalidParamException('The data to be merged with must be an array or an object implementing Traversable.');
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns a value indicating whether there is an item at the specified offset.
|
||
|
* This method is required by the SPL interface `ArrayAccess`.
|
||
14 years ago
|
* It is implicitly called when you use something like `isset($vector[$offset])`.
|
||
14 years ago
|
* @param integer $offset the offset to be checked
|
||
|
* @return boolean whether there is an item at the specified offset.
|
||
|
*/
|
||
|
public function offsetExists($offset)
|
||
|
{
|
||
|
return $offset >= 0 && $offset < $this->_c;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the item at the specified offset.
|
||
|
* This method is required by the SPL interface `ArrayAccess`.
|
||
14 years ago
|
* It is implicitly called when you use something like `$value = $vector[$offset];`.
|
||
14 years ago
|
* This is equivalent to [[itemAt]].
|
||
14 years ago
|
* @param integer $offset the offset to retrieve item.
|
||
|
* @return mixed the item at the offset
|
||
|
* @throws Exception if the offset is out of range
|
||
|
*/
|
||
|
public function offsetGet($offset)
|
||
|
{
|
||
|
return $this->itemAt($offset);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets the item at the specified offset.
|
||
|
* This method is required by the SPL interface `ArrayAccess`.
|
||
14 years ago
|
* It is implicitly called when you use something like `$vector[$offset] = $item;`.
|
||
14 years ago
|
* If the offset is null or equal to the number of the existing items,
|
||
|
* the new item will be appended to the vector.
|
||
|
* Otherwise, the existing item at the offset will be replaced with the new item.
|
||
|
* @param integer $offset the offset to set item
|
||
|
* @param mixed $item the item value
|
||
|
* @throws Exception if the offset is out of range, or the vector is read only.
|
||
|
*/
|
||
|
public function offsetSet($offset, $item)
|
||
|
{
|
||
|
if ($offset === null || $offset === $this->_c) {
|
||
|
$this->insertAt($this->_c, $item);
|
||
13 years ago
|
} else {
|
||
14 years ago
|
$this->removeAt($offset);
|
||
|
$this->insertAt($offset, $item);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Unsets the item at the specified offset.
|
||
|
* This method is required by the SPL interface `ArrayAccess`.
|
||
14 years ago
|
* It is implicitly called when you use something like `unset($vector[$offset])`.
|
||
14 years ago
|
* This is equivalent to [[removeAt]].
|
||
|
* @param integer $offset the offset to unset item
|
||
|
* @throws Exception if the offset is out of range, or the vector is read only.
|
||
|
*/
|
||
|
public function offsetUnset($offset)
|
||
|
{
|
||
|
$this->removeAt($offset);
|
||
|
}
|
||
|
}
|