<?php
/* Basic Stack Class
* Written by Jon Lawrence ( http://www.phpclasses.org/browse/author/150618.html )
* Modified by Asher Holley (http://www.wolfoxinc.com/opensource/)
* Released under the GNU General Public License ( http://www.opensource.org/licenses/gpl-license.html )
* Copyright © 2006 Asher Holley
*
* This stack conforms to almost all of the PostScript stack manipulators
* except for mark and cleartomark. It is modified from its
* original design to eliminate the superfluous $index variable.
* It is designed for both PHP4 and PHP5 using Phpizer */
// require_once( 'list.abstr.php' ); %PHP5 >
class Stack
// extends List_abstr implements ArrayAccess %PHP5 >
// , Countable %PHP5-1 >
{
// var %PHP4 > 5
// private %PHP5
$lifo;
function
// __construct( $data = null ) %PHP5
// Stack( $data = null ) %PHP4
{
if ( is_array( $data ) ) {
$this->lifo = $data;
// } else if ( $data instanceof Stack ) { %PHP5 >
// } else if ( is_a( $data, 'Stack' ) ) { %PHP4 > 5
$this->lifo = $data->lifo;
} else if ( $data ) {
$this->lifo = array();
$this->lifo[] = $data;
} else {
$this->lifo = 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 > ( $l = count( $this->lifo ) - 1 ) || $offset < 0 ) {
return false;
} else {
unset( $this->lifo[ $l - $offset ] );
return true;
}
}
return false;
}
public function offsetExists( $offset ) {
if ( is_numeric( $key ) ) {
if ( $offset > ( $l = count( $this->lifo ) - 1 ) || $offset < 0 ) {
return false;
} else {
return array_key_exists( $l - $offset, $this->lifo );
}
}
} %PHP5 > */
// public %PHP5 >
function top() {
if ( !empty( $this->lifo ) ) {
return array_slice( $this->lifo, -1 );
} else {
return null;
}
}
// public %PHP5 >
function &top_ref() {
if ( !empty( $this->lifo ) ) {
return $this->lifo[ $this->count() - 1 ];
} else {
return null;
}
}
// public %PHP5 >
function push() {
$args = func_get_args();
$exec = 'array_push( $this->lifo';
for ( $a = 0; $a < count( $args ); $a++ ) {
$exec .= ', $args[' . $a . ']';
}
$exec .= ' );';
return eval( $exec );
}
// public %PHP5 >
function push_ref( &$a ) {
$this->lifo[] = $a;
return;
}
// public %PHP5 >
function pop() {
return array_pop( $this->lifo );
}
// public %PHP5 >
function &pop_ref() {
$e =& $this->top_ref();
array_pop( $this->lifo );
return $e;
}
// public %PHP5 >
function index( $i ) {
if ( $i > ( $l = count( $this->lifo ) - 1 ) || $i < 0 ) {
return null;
} else {
return $this->lifo[ $l - $i ];
}
}
// public %PHP5 >
function &index_ref( $i ) {
if ( $i > ( $l = count( $this->lifo ) - 1 ) || $i < 0 ) {
return null;
} else {
return $this->lifo[ $l - $i ];
}
}
// public %PHP5 >
function count() {
return count( $this->lifo );
}
// public %PHP5 >
function is_empty() {
return empty( $this->lifo );
}
function clear() {
$this->lifo = array();
return;
}
// public %PHP5 >
function get_stack() {
return array_reverse( $this->lifo );
}
// public %PHP5 >
function get_list() {
return get_stack();
}
// public %PHP5 >
function exch() {
$m1 =& $this->pop_ref();
$m2 =& $this->pop_ref();
$this->lifo[] =& $m1;
$this->lifo[] =& $m2;
return;
}
// public %PHP5 >
function dup() {
$this->lifo[] = $this->top();
return;
}
// public %PHP5 >
function roll( $c ) {
if ( $c ) {
if ( $c > ( $l = $this->count() ) ) {
$c %= $l;
}
if ( $c > 0 ) { // rotate right
$this->lifo = array_merge( array_slice( $this->lifo, $l - $c ),
array_slice( $this->lifo, 0, $l - $c ) );
} else if ( $c < 0 ) { // rotate left
$c = abs( $c );
$this->lifo = array_merge( array_slice( $this->lifo, $c ),
array_slice( $this->lifo, 0, $c ) );
}
}
return;
}
}
?>