Location: PHPKode > scripts > ps stack > ps-stack/stack.class5.php
<?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. */

require_once( 'list.abstr.php' );

class Stack extends List_abstr implements Countable, ArrayAccess
{
	private $lifo;

	function __construct( $data = null )
	{
		if ( is_array( $data ) ) {
			$this->lifo = $data;
		} else if ( $data instanceof Stack ) {
			$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 );
			}
		}
	}
	
	public function top() {
		if ( !empty( $this->lifo ) ) {
			return array_slice( $this->lifo, -1 );
		} else {
			return null;
		}
	}
	
	public function &top_ref() {
		if ( !empty( $this->lifo ) ) {
			return $this->lifo[ $this->count() - 1 ];
		} else {
			return null;
		}
	}

	public 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 function push_ref( &$a ) {
		$this->lifo[] = $a;
		return;
	}

	public function pop() {
		return array_pop( $this->lifo );
	}
	
	public function &pop_ref() {
		$e =& $this->top_ref();
		array_pop( $this->lifo );
		return $e;
	}
	
	public function index( $i ) {
		if ( $i > ( $l = count( $this->lifo ) - 1 ) || $i < 0 ) {
			return null;
		} else {
			return $this->lifo[ $l - $i ];
		}
	}
	
	public function &index_ref( $i ) {
		if ( $i > ( $l = count( $this->lifo ) - 1 ) || $i < 0 ) {
			return null;
		} else {
			return $this->lifo[ $l - $i ];
		}
	}
	
	public function count() {
		return count( $this->lifo );
	}
	
	public function is_empty() {
		return empty( $this->lifo );
	}

	public function clear() {
		$this->lifo = array();
		return;
	}

	public function get_stack() {
		return array_reverse( $this->lifo );
	}
	
	public function get_list() {
		return get_stack();
	}
	
	public function exch() {
		$m1 =& $this->pop_ref();
		$m2 =& $this->pop_ref();
		$this->lifo[] =& $m1;
		$this->lifo[] =& $m2;
		return;
	}
	
	public function dup() {
		$this->lifo[] = $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->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;
	}
}

?>
Return current item: ps stack