Skrytí panelu Zobrazení panelu Nainstalujte si nejnovější Adobe FlashPlayer bublinaVyzkoušejte také Zkracovač URL adres (ShortURL)


Detail Bez názvu z PasteBIN archivu.

Popis kódu:
Nebyl zadán žádný popis kódu
Zdrojový kód:
  1. <?php
  2. namespace Cassius\Collection;
  3. use Cassius;
  4. use Cassius\Collection\Collection;
  5. use Cassius\Exceptions\DataOutOfRangeException;
  6. /**
  7.  * TODO search
  8.  */
  9. class Queue extends Collection
  10. {
  11. /**
  12.   * Data
  13.   * @var type array
  14.   */
  15. private $queueArray = array();
  16.  
  17.  
  18. /**
  19.   * Ukazatel na začátek fronty
  20.   * @var type int
  21.   */
  22. private $start = 0;
  23.  
  24.  
  25. /**
  26.   * Ukazatel na konec fronty
  27.   * @var type int
  28.   */
  29. private $end = -1;
  30.  
  31.  
  32. /**
  33.   * Maximální velikost fronty
  34.   * Pokud není definována, velikost je nekonečná
  35.   * @var type int
  36.   */
  37. private $Size = null;
  38.  
  39.  
  40. /**
  41.   * Aktuální velikost fronty
  42.   * @var type int
  43.   */
  44. private $currentSize;
  45.  
  46.  
  47. /**
  48.   * Do fronty se může přidávat při vytváření instance
  49.   * Pokud má mít fronta velikost, nelze zadávat data do konstruktoru
  50.   */
  51. public function __construct()
  52. {
  53. $this->ClearAll();
  54. $data = array();
  55. $data = func_get_args();
  56. foreach($data as $key => $value)
  57. {
  58. $this->Enqueue($value);
  59. //$this->queueArray[$this->end] = $value;
  60. $this->end++;
  61. $this->currentSize++;
  62. }
  63. }
  64.  
  65.  
  66. public function setSize($size)
  67. {
  68. $this->Size = $size;
  69. return $this;
  70. }
  71.  
  72. public function getCurrentSize()
  73. {
  74. return $this->currentSize;
  75. }
  76.  
  77. /**
  78.   * Kontroluje jestli je zásobník prázdný, vrací true pokud ano
  79.   * @return type boolen
  80.   */
  81. public function IsEmpty()
  82. {
  83. return ($this->currentSize == 0 ? true : false);
  84. }
  85.  
  86. /**
  87.   * Umožňuje přidávat do fronty. Pokud je přidáno více prvků než je hodnota Size, vyvolá se výjimka DataOutOfRangeException
  88.   * Když bude ukazatel konce fronty roven Size - 1, vynuluje se.
  89.   * Možno přidávat i pokud není nastavena vlastnost Size, fronta bude nekonečná
  90.   * @param type $item přidávaná položka do fronty
  91.   * @throws DataOutOfRangeException
  92.   */
  93. public function Enqueue($item)
  94. {
  95. if($this->Size != null){
  96. if($this->currentSize >= $this->Size)
  97. throw new DataOutOfRangeException("Queue if full");
  98. if($this->end == $this->Size) //? Size - 1 ?
  99. $this->end = 0;
  100. else $this->end++;
  101. $this->queueArray[$this->end] = $item;
  102. $this->currentSize++;
  103. }else{
  104. $this->queueArray[$this->end] = $item;
  105. $this->currentSize++;
  106. }
  107. }
  108.  
  109. /**
  110.   * Odebere z fronty první prvek
  111.   * @return item
  112.   */
  113. public function Dequeue()
  114. {
  115. if($this->IsEmpty())
  116. return false;
  117. $item = $this->queueArray[$this->start];
  118. if($this->start == $this->Size - 1)
  119. $this->start = 0;
  120. else $this->start++;
  121. $this->currentSize--;
  122. return $item;
  123. }
  124.  
  125. public function getFirst()
  126. {
  127. if($this->IsEmpty())
  128. return false;
  129. return $this->queueArray[0];
  130. }
  131.  
  132. public function ClearAll()
  133. {
  134. $this->queueArray = array();
  135. $this->start = 0;
  136. $this->end = 0;
  137. $this->currentSize = 0;
  138. }
  139.  
  140. }
  141.  
  142. ?>
  143.  
Vložil:
Anonymní, 2.7.2012, v 11:20