|
|
@ -40,11 +40,6 @@ namespace yii\base; |
|
|
|
class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Countable |
|
|
|
class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Countable |
|
|
|
{ |
|
|
|
{ |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var boolean whether this vector is read-only or not. |
|
|
|
|
|
|
|
* If the vector is read-only, adding or moving items will throw an exception. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $readOnly; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var array internal data storage |
|
|
|
* @var array internal data storage |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private $_d = array(); |
|
|
|
private $_d = array(); |
|
|
@ -58,15 +53,13 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou |
|
|
|
* Initializes the vector with an array or an iterable object. |
|
|
|
* Initializes the vector with an array or an iterable object. |
|
|
|
* @param mixed $data the initial data to be populated into the vector. |
|
|
|
* @param mixed $data the initial data to be populated into the vector. |
|
|
|
* This can be an array or an iterable object. |
|
|
|
* This can be an array or an iterable object. |
|
|
|
* @param boolean $readOnly whether the vector should be marked as read-only. |
|
|
|
|
|
|
|
* @throws Exception if data is not well formed (neither an array nor an iterable object) |
|
|
|
* @throws Exception if data is not well formed (neither an array nor an iterable object) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function __construct($data = array(), $readOnly = false) |
|
|
|
public function __construct($data = array()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($data !== array()) { |
|
|
|
if ($data !== array()) { |
|
|
|
$this->copyFrom($data); |
|
|
|
$this->copyFrom($data); |
|
|
|
} |
|
|
|
} |
|
|
|
$this->readOnly = $readOnly; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -141,7 +134,6 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function insertAt($index, $item) |
|
|
|
public function insertAt($index, $item) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!$this->readOnly) { |
|
|
|
|
|
|
|
if ($index === $this->_c) { |
|
|
|
if ($index === $this->_c) { |
|
|
|
$this->_d[$this->_c++] = $item; |
|
|
|
$this->_d[$this->_c++] = $item; |
|
|
|
} |
|
|
|
} |
|
|
@ -153,10 +145,6 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou |
|
|
|
throw new Exception('Index out of range: ' . $index); |
|
|
|
throw new Exception('Index out of range: ' . $index); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
|
|
|
|
throw new Exception('Vector is read only.'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Removes an item from the vector. |
|
|
|
* Removes an item from the vector. |
|
|
@ -187,7 +175,6 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function removeAt($index) |
|
|
|
public function removeAt($index) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!$this->readOnly) { |
|
|
|
|
|
|
|
if ($index >= 0 && $index < $this->_c) { |
|
|
|
if ($index >= 0 && $index < $this->_c) { |
|
|
|
$this->_c--; |
|
|
|
$this->_c--; |
|
|
|
if ($index === $this->_c) { |
|
|
|
if ($index === $this->_c) { |
|
|
@ -203,10 +190,6 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou |
|
|
|
throw new Exception('Index out of range: ' . $index); |
|
|
|
throw new Exception('Index out of range: ' . $index); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
|
|
|
|
throw new Exception('Vector is read only.'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Removes all items from the vector. |
|
|
|
* Removes all items from the vector. |
|
|
|