1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
<?php
/**
* Queue class - under PHP 5
*
* @description This is an implementation of FIFO (First In First Out) queue.
*
* @copyright (c) 2003 Michal 'Seth' Golebiowski <sethmail at poczta dot fm>
* Released under the GNU General Public License
* license is attached to package in license.txt file
*
* @updated 10.08.2003
*
* @example example.php5 Simple example of puting ang geting datas from queue
*
* @requirement PHP 5
*
*
*
*
* @greetings goes to all developers from Poland especially from php.pl :-)
*/
/**
* Default size of queue
*/
define( 'QUEUE_DEFAULT_SIZE', 15 );
/**
* Abstract class AQueue of FIFO queue
* @version 1.5
*/
abstract class AQueue
{
/**
* Queue constructor
* @param int $intQueue - size of queue
*/
abstract public function __construct( $intSize = QUEUE_DEFAULT_SIZE );
/**
* Queue destructor
* Destroy queue items array
*/
abstract public function __destruct();
/**
* Add item to queue
* @param obj &$objQueueItem - queue item object
* @return true if added to queue or false if queue is full and item could not be added
*/
abstract public function Put( &$objQueueItem );
/**
* Get item from queue
* @return object (queue iteme) or false if there is now items in queue
*/
abstract public function Get();
/**
* Check if queue is empty
* @return true if it is empty or false if not
*/
abstract public function IsEmpty();
/**
* Clear queue
*/
abstract public function Clear();
}
/**
* Implementation of abstract class AQueue
* @version 1.9
*/
class Queue extends AQueue
{
private
$arrQueue, // Array of queue items
$intBegin, // Begin of queue - head
$intEnd, // End of queue - tail
$intArraySize, // Size of array
$intCurrentSize; // Current size of array
public function __construct( $intSize = QUEUE_DEFAULT_SIZE )
{
$this->arrQueue = Array();
$this->intArraySize = $intSize;
$this->Clear();
}
public function __destruct()
{
unset( $this->arrQueue );
}
public function Put( &$objQueueItem )
{
if ( $this->intCurrentSize >= $this->intArraySize )
{
return false;
}
if ( $this->intEnd == $this->intArraySize - 1 )
{
$this->intEnd = 0;
}
else
{
$this->intEnd++;
}
$this->arrQueue[ $this->intEnd ] = $objQueueItem;
$this->intCurrentSize++;
return true;
}
public function Get()
{
if ( $this->IsEmpty() ){
return false;
}
$objItem = $this->arrQueue[$this->intBegin];
if ( $this->intBegin == $this->intArraySize - 1 )
{
$this->intBegin = 0;
}
else
{
$this->intBegin++;
}
$this->intCurrentSize--;
return $objItem;
}
public function IsEmpty()
{
return ( $this->intCurrentSize == 0 ? true : false );
}
public function Clear()
{
$this->arrCurrentSize = 0;
$this->intBegin = 0;
$this->intEnd = $this->intArraySize - 1;
}
}
?> |
Partager