<?php
/* Basic Queue Class
* Asher Holley ( http://www.wolfoxinc.com/opensource/ )
* Based on the Stack class ( http://www.phpclasses.org/browse/package/3442.html )
* Released under the GNU General Public License ( http://www.opensource.org/licenses/gpl-license.html )
* Copyright © 2006 Asher Holley
*
* This queue is based on the "ps stack" class available at PHP Classes
* that I have authored. */
require_once( 'list.abstr.php' );
class Queue extends List_abstr implements Countable, ArrayAccess {
private $fifo;
function __construct( $data = null ) {
if ( is_array( $data ) ) {
$this->fifo = $data;
} else if ( is_a( $data, 'Queue' ) ) {
$this->fifo = $data->fifo;
} else if ( $data ) {
$this->fifo = array();
$this->fifo[] = $data;
} else {
$this->fifo = array();
}
return;
}
public function offsetSet( $key, $value ) {
if ( is_numeric( $key ) ) {
if ( $this->index( $key ) ) {
$r =& $this->index_ref( $key );
$r = $value;
return $value;
}
}
if ( $key = '' ) {
$this->push( $value );
}
return null;
}
public function offsetGet( $key ) {
if ( is_numeric( $key ) ) {
return $this->index( $key );
}
return null;
}
public function offsetUnset( $key ) {
if ( is_numeric( $key ) ) {
if ( $offset > count( $this->fifo ) - 1 || $offset < 0 ) {
return false;
} else {
unset( $this->fifo[ $offset ] );
return true;
}
}
return false;
}
public function offsetExists( $offset ) {
if ( is_numeric( $key ) ) {
if ( $offset > count( $this->fifo ) || $offset < 0 ) {
return false;
} else {
return array_key_exists( $offset, $this->fifo );
}
}
}
public function top() {
if ( !empty( $this->fifo ) ) {
return $this->fifo[0];
} else {
return null;
}
}
public function &top_ref() {
if ( !empty( $this->fifo ) ) {
return $this->fifo[0];
} else {
return null;
}
}
public function push() {
$args = func_get_args();
$exec = 'array_push( $this->fifo';
for ( $a = 0; $a < count( $args ); $a++ ) {
$exec .= ', $args[' . $a . ']';
}
$exec .= ' );';
return eval( $exec );
}
public function pop() {
return array_shift( $this->fifo );
}
public function &pop_ref() {
$e =& $this->top_ref();
array_shift( $this->fifo );
return $e;
}
public function index( $i ) {
if ( $i > count( $this->fifo ) - 1 || $i < 0 ) {
return null;
} else {
return $this->fifo[ $i ];
}
}
public function &index_ref( $i ) {
if ( $i > count( $this->fifo ) - 1 || $i < 0 ) {
return null;
} else {
return $this->fifo[ $i ];
}
}
public function count() {
return count( $this->fifo );
}
public function is_empty() {
return empty( $this->fifo );
}
public function clear() {
$this->fifo = array();
return;
}
public function get_queue() {
return $this->fifo;
}
public function get_list() {
return get_queue();
}
public function exch() {
$m0 = $this->pop_ref();
$m1 = $this->pop_ref();
$this->fifo[1] =& $m0;
$this->fifo[0] =& $m1;
return;
}
public function dup() {
array_unshift( $this->fifo, $this->top() );
return;
}
public function roll( $c ) {
if ( is_int( $c ) ) {
if ( abs( $c ) > ( $l = $this->count() ) ) {
if ( $c < 0 ) {
$c = ( abs( $c ) % $l ) * -1;
} else {
$c %= $l;
}
}
if ( $c > 0 ) { // rotate right
$this->fifo = array_merge( array_slice( $this->fifo, $c ),
array_slice( $this->fifo, 0, $c ) );
} else if ( $c < 0 ) { // rotate left
$c = abs( $c );
$this->fifo = array_merge( array_slice( $this->fifo, $l - $c ),
array_slice( $this->fifo, 0, $l - $c ) );
}
}
return;
}
}
?>