summaryrefslogtreecommitdiff
path: root/framework/Collections/TQueue.php
diff options
context:
space:
mode:
authorctrlaltca <>2012-07-12 11:21:01 +0000
committerctrlaltca <>2012-07-12 11:21:01 +0000
commit903ae8a581fac1e6917fc3e31d2ad8fb91df80c3 (patch)
treee08bf04f0823650a231227ac3499121270172a23 /framework/Collections/TQueue.php
parent3e4e6e66aeb3f8fea4e1eb4237498ef9d2358f63 (diff)
standardize the use of unix eol; use svn properties to enforce native eol
Diffstat (limited to 'framework/Collections/TQueue.php')
-rw-r--r--framework/Collections/TQueue.php526
1 files changed, 263 insertions, 263 deletions
diff --git a/framework/Collections/TQueue.php b/framework/Collections/TQueue.php
index 840ec478..d02a7aad 100644
--- a/framework/Collections/TQueue.php
+++ b/framework/Collections/TQueue.php
@@ -1,263 +1,263 @@
-<?php
-/**
- * TQueue, TQueueIterator classes
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright &copy; 2005-2012 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Collections
- */
-
-/**
- * TQueue class
- *
- * TQueue implements a queue.
- *
- * The typical queue operations are implemented, which include
- * {@link enqueue()}, {@link dequeue()} and {@link peek()}. In addition,
- * {@link contains()} can be used to check if an item is contained
- * in the queue. To obtain the number of the items in the queue,
- * check the {@link getCount Count} property.
- *
- * Items in the queue may be traversed using foreach as follows,
- * <code>
- * foreach($queue as $item) ...
- * </code>
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @author Knut Urdalen <knut.urdalen@gmail.com>
- * @version $Id$
- * @package System.Collections
- * @since 3.1
- */
-class TQueue extends TComponent implements IteratorAggregate,Countable
-{
- /**
- * internal data storage
- * @var array
- */
- private $_d=array();
- /**
- * number of items
- * @var integer
- */
- private $_c=0;
-
- /**
- * Constructor.
- * Initializes the queue with an array or an iterable object.
- * @param array|Iterator the intial data. Default is null, meaning no initialization.
- * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator.
- */
- public function __construct($data=null)
- {
- if($data!==null)
- $this->copyFrom($data);
- }
-
- /**
- * @return array the list of items in queue
- */
- public function toArray()
- {
- return $this->_d;
- }
-
- /**
- * Copies iterable data into the queue.
- * Note, existing data in the list will be cleared first.
- * @param mixed the data to be copied from, must be an array or object implementing Traversable
- * @throws TInvalidDataTypeException If data is neither an array nor a Traversable.
- */
- public function copyFrom($data)
- {
- if(is_array($data) || ($data instanceof Traversable))
- {
- $this->clear();
- foreach($data as $item)
- {
- $this->_d[]=$item;
- ++$this->_c;
- }
- }
- else if($data!==null)
- throw new TInvalidDataTypeException('queue_data_not_iterable');
- }
-
- /**
- * Removes all items in the queue.
- */
- public function clear()
- {
- $this->_c=0;
- $this->_d=array();
- }
-
- /**
- * @param mixed the item
- * @return boolean whether the queue contains the item
- */
- public function contains($item)
- {
- return array_search($item,$this->_d,true)!==false;
- }
-
- /**
- * Returns the first item at the front of the queue.
- * Unlike {@link dequeue()}, this method does not remove the item from the queue.
- * @return mixed item at the top of the queue
- * @throws TInvalidOperationException if the queue is empty
- */
- public function peek()
- {
- if($this->_c===0)
- throw new TInvalidOperationException('queue_empty');
- else
- return $this->_d[0];
- }
-
- /**
- * Removes and returns the object at the beginning of the queue.
- * @return mixed the item at the beginning of the queue
- * @throws TInvalidOperationException if the queue is empty
- */
- public function dequeue()
- {
- if($this->_c===0)
- throw new TInvalidOperationException('queue_empty');
- else
- {
- --$this->_c;
- return array_shift($this->_d);
- }
- }
-
- /**
- * Adds an object to the end of the queue.
- * @param mixed the item to be appended into the queue
- */
- public function enqueue($item)
- {
- ++$this->_c;
- $this->_d[] = $item;
- }
-
- /**
- * Returns an iterator for traversing the items in the queue.
- * This method is required by the interface IteratorAggregate.
- * @return Iterator an iterator for traversing the items in the queue.
- */
- public function getIterator()
- {
- return new ArrayIterator( $this->_d );
- }
-
- /**
- * @return integer the number of items in the queue
- */
- public function getCount()
- {
- return $this->_c;
- }
-
- /**
- * Returns the number of items in the queue.
- * This method is required by Countable interface.
- * @return integer number of items in the queue.
- */
- public function count()
- {
- return $this->getCount();
- }
-}
-
-/**
- * TQueueIterator class
- *
- * TQueueIterator implements Iterator interface.
- *
- * TQueueIterator is used by TQueue. It allows TQueue to return a new iterator
- * for traversing the items in the queue.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @version $Id$
- * @package System.Collections
- * @since 3.1
- */
-class TQueueIterator implements Iterator
-{
- /**
- * @var array the data to be iterated through
- */
- private $_d;
- /**
- * @var integer index of the current item
- */
- private $_i;
- /**
- * @var integer count of the data items
- */
- private $_c;
-
- /**
- * Constructor.
- * @param array the data to be iterated through
- */
- public function __construct(&$data)
- {
- $this->_d=&$data;
- $this->_i=0;
- $this->_c=count($this->_d);
- }
-
- /**
- * Rewinds internal array pointer.
- * This method is required by the interface Iterator.
- */
- public function rewind()
- {
- $this->_i=0;
- }
-
- /**
- * Returns the key of the current array item.
- * This method is required by the interface Iterator.
- * @return integer the key of the current array item
- */
- public function key()
- {
- return $this->_i;
- }
-
- /**
- * Returns the current array item.
- * This method is required by the interface Iterator.
- * @return mixed the current array item
- */
- public function current()
- {
- return $this->_d[$this->_i];
- }
-
- /**
- * Moves the internal pointer to the next array item.
- * This method is required by the interface Iterator.
- */
- public function next()
- {
- $this->_i++;
- }
-
- /**
- * Returns whether there is an item at current position.
- * This method is required by the interface Iterator.
- * @return boolean
- */
- public function valid()
- {
- return $this->_i<$this->_c;
- }
-}
-
+<?php
+/**
+ * TQueue, TQueueIterator classes
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005-2012 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Collections
+ */
+
+/**
+ * TQueue class
+ *
+ * TQueue implements a queue.
+ *
+ * The typical queue operations are implemented, which include
+ * {@link enqueue()}, {@link dequeue()} and {@link peek()}. In addition,
+ * {@link contains()} can be used to check if an item is contained
+ * in the queue. To obtain the number of the items in the queue,
+ * check the {@link getCount Count} property.
+ *
+ * Items in the queue may be traversed using foreach as follows,
+ * <code>
+ * foreach($queue as $item) ...
+ * </code>
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @author Knut Urdalen <knut.urdalen@gmail.com>
+ * @version $Id$
+ * @package System.Collections
+ * @since 3.1
+ */
+class TQueue extends TComponent implements IteratorAggregate,Countable
+{
+ /**
+ * internal data storage
+ * @var array
+ */
+ private $_d=array();
+ /**
+ * number of items
+ * @var integer
+ */
+ private $_c=0;
+
+ /**
+ * Constructor.
+ * Initializes the queue with an array or an iterable object.
+ * @param array|Iterator the intial data. Default is null, meaning no initialization.
+ * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator.
+ */
+ public function __construct($data=null)
+ {
+ if($data!==null)
+ $this->copyFrom($data);
+ }
+
+ /**
+ * @return array the list of items in queue
+ */
+ public function toArray()
+ {
+ return $this->_d;
+ }
+
+ /**
+ * Copies iterable data into the queue.
+ * Note, existing data in the list will be cleared first.
+ * @param mixed the data to be copied from, must be an array or object implementing Traversable
+ * @throws TInvalidDataTypeException If data is neither an array nor a Traversable.
+ */
+ public function copyFrom($data)
+ {
+ if(is_array($data) || ($data instanceof Traversable))
+ {
+ $this->clear();
+ foreach($data as $item)
+ {
+ $this->_d[]=$item;
+ ++$this->_c;
+ }
+ }
+ else if($data!==null)
+ throw new TInvalidDataTypeException('queue_data_not_iterable');
+ }
+
+ /**
+ * Removes all items in the queue.
+ */
+ public function clear()
+ {
+ $this->_c=0;
+ $this->_d=array();
+ }
+
+ /**
+ * @param mixed the item
+ * @return boolean whether the queue contains the item
+ */
+ public function contains($item)
+ {
+ return array_search($item,$this->_d,true)!==false;
+ }
+
+ /**
+ * Returns the first item at the front of the queue.
+ * Unlike {@link dequeue()}, this method does not remove the item from the queue.
+ * @return mixed item at the top of the queue
+ * @throws TInvalidOperationException if the queue is empty
+ */
+ public function peek()
+ {
+ if($this->_c===0)
+ throw new TInvalidOperationException('queue_empty');
+ else
+ return $this->_d[0];
+ }
+
+ /**
+ * Removes and returns the object at the beginning of the queue.
+ * @return mixed the item at the beginning of the queue
+ * @throws TInvalidOperationException if the queue is empty
+ */
+ public function dequeue()
+ {
+ if($this->_c===0)
+ throw new TInvalidOperationException('queue_empty');
+ else
+ {
+ --$this->_c;
+ return array_shift($this->_d);
+ }
+ }
+
+ /**
+ * Adds an object to the end of the queue.
+ * @param mixed the item to be appended into the queue
+ */
+ public function enqueue($item)
+ {
+ ++$this->_c;
+ $this->_d[] = $item;
+ }
+
+ /**
+ * Returns an iterator for traversing the items in the queue.
+ * This method is required by the interface IteratorAggregate.
+ * @return Iterator an iterator for traversing the items in the queue.
+ */
+ public function getIterator()
+ {
+ return new ArrayIterator( $this->_d );
+ }
+
+ /**
+ * @return integer the number of items in the queue
+ */
+ public function getCount()
+ {
+ return $this->_c;
+ }
+
+ /**
+ * Returns the number of items in the queue.
+ * This method is required by Countable interface.
+ * @return integer number of items in the queue.
+ */
+ public function count()
+ {
+ return $this->getCount();
+ }
+}
+
+/**
+ * TQueueIterator class
+ *
+ * TQueueIterator implements Iterator interface.
+ *
+ * TQueueIterator is used by TQueue. It allows TQueue to return a new iterator
+ * for traversing the items in the queue.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id$
+ * @package System.Collections
+ * @since 3.1
+ */
+class TQueueIterator implements Iterator
+{
+ /**
+ * @var array the data to be iterated through
+ */
+ private $_d;
+ /**
+ * @var integer index of the current item
+ */
+ private $_i;
+ /**
+ * @var integer count of the data items
+ */
+ private $_c;
+
+ /**
+ * Constructor.
+ * @param array the data to be iterated through
+ */
+ public function __construct(&$data)
+ {
+ $this->_d=&$data;
+ $this->_i=0;
+ $this->_c=count($this->_d);
+ }
+
+ /**
+ * Rewinds internal array pointer.
+ * This method is required by the interface Iterator.
+ */
+ public function rewind()
+ {
+ $this->_i=0;
+ }
+
+ /**
+ * Returns the key of the current array item.
+ * This method is required by the interface Iterator.
+ * @return integer the key of the current array item
+ */
+ public function key()
+ {
+ return $this->_i;
+ }
+
+ /**
+ * Returns the current array item.
+ * This method is required by the interface Iterator.
+ * @return mixed the current array item
+ */
+ public function current()
+ {
+ return $this->_d[$this->_i];
+ }
+
+ /**
+ * Moves the internal pointer to the next array item.
+ * This method is required by the interface Iterator.
+ */
+ public function next()
+ {
+ $this->_i++;
+ }
+
+ /**
+ * Returns whether there is an item at current position.
+ * This method is required by the interface Iterator.
+ * @return boolean
+ */
+ public function valid()
+ {
+ return $this->_i<$this->_c;
+ }
+}
+