<?php
namespace Cassius\Collection;
use Cassius;
use Cassius\Collection\Collection;
use Cassius\Exceptions\DataOutOfRangeException;
/**
* TODO search
*/
class Queue extends Collection
{
/**
* Data
* @var type array
*/
private $queueArray = array();
/**
* Ukazatel na začátek fronty
* @var type int
*/
private $start = 0;
/**
* Ukazatel na konec fronty
* @var type int
*/
private $end = -1;
/**
* Maximální velikost fronty
* Pokud není definována, velikost je nekonečná
* @var type int
*/
private $Size = null;
/**
* Aktuální velikost fronty
* @var type int
*/
private $currentSize;
/**
* Do fronty se může přidávat při vytváření instance
* Pokud má mít fronta velikost, nelze zadávat data do konstruktoru
*/
public function __construct()
{
$this->ClearAll();
$data = array();
$data = func_get_args();
foreach($data as $key => $value)
{
$this->Enqueue($value);
//$this->queueArray[$this->end] = $value;
$this->end++;
$this->currentSize++;
}
}
public function setSize($size)
{
$this->Size = $size;
return $this;
}
public function getCurrentSize()
{
return $this->currentSize;
}
/**
* Kontroluje jestli je zásobník prázdný, vrací true pokud ano
* @return type boolen
*/
public function IsEmpty()
{
return ($this->currentSize == 0 ? true : false);
}
/**
* Umožňuje přidávat do fronty. Pokud je přidáno více prvků než je hodnota Size, vyvolá se výjimka DataOutOfRangeException
* Když bude ukazatel konce fronty roven Size - 1, vynuluje se.
* Možno přidávat i pokud není nastavena vlastnost Size, fronta bude nekonečná
* @param type $item přidávaná položka do fronty
* @throws DataOutOfRangeException
*/
public function Enqueue($item)
{
if($this->Size != null){
if($this->currentSize >= $this->Size)
throw new DataOutOfRangeException("Queue if full");
if($this->end == $this->Size) //? Size - 1 ?
$this->end = 0;
else $this->end++;
$this->queueArray[$this->end] = $item;
$this->currentSize++;
}else{
$this->queueArray[$this->end] = $item;
$this->currentSize++;
}
}
/**
* Odebere z fronty první prvek
* @return item
*/
public function Dequeue()
{
if($this->IsEmpty())
return false;
$item = $this->queueArray[$this->start];
if($this->start == $this->Size - 1)
$this->start = 0;
else $this->start++;
$this->currentSize--;
return $item;
}
public function getFirst()
{
if($this->IsEmpty())
return false;
return $this->queueArray[0];
}
public function ClearAll()
{
$this->queueArray = array();
$this->start = 0;
$this->end = 0;
$this->currentSize = 0;
}
}
?>