PHP Classes

File: examples/Stack.php

Recommend this page to a friend!
  Classes of zinsou A.A.E.Moïse   PHP Dictionary to Array class   examples/Stack.php   Download  
File: examples/Stack.php
Role: Example script
Content type: text/plain
Description: example script
Class: PHP Dictionary to Array class
Manipulate value collections as arrays or objects
Author: By
Last change:
Date: 5 years ago
Size: 4,702 bytes
 

Contents

Class file image Download
This is a naive but functional Stack data structure implemented with only xDict:<br>
<?php
highlight_string
('
<?php
require_once(\'./../xdict.class.php\');
class Stack implements Iterator,Countable ,JsonSerializable{

    private $xdict=null;

    public function __construct($mixed=[]){
        if(is_array($mixed)){
            $this->xdict=xdict(0);
            $this->xdict->fill_with(array_reverse($mixed));
        }elseif(is_object($mixed)&&key_exists(\'Traversable\',class_implements($mixed))){
            $this->xdict=xdict(0);
            $this->xdict->fill_with(array_reverse(iterator_to_array($mixed)));
        }else{
            throw new LogicException(\'Argument $mixed must be of type array or must implements the Traversable Interface\');
        }
    }

    function rewind() {
        return $this->xdict->rewind();
    }

    function current() {
         return $this->xdict->current();
    }

    function key() {
        return $this->xdict->key();
    }

    function next() {
       $x=$this->xdict->next();
        $this->xdict->shift();
        return $x;
    }

    function valid() {
         return $this->xdict->valid();
    }
    public function clear (){
        return $this->xdict->clear();
    }
    public function count(){
            return $this->xdict->count();
    }
   
    public function copy (){
        return clone($this);
    }
    public function isEmpty (){
        return $this->xdict->isEmpty();
    }
    public function peek (){
        return $this->xdict->first();
    }
    public function pop (){
        if($this->isEmpty()) throw new UnderflowException(\'The queue is already empty\');
        return $this->xdict->shift(true);
    }
    public function push (...$values ){
        return $this->xdict->unshift(...$values);
       
    }
    public function toArray (){
        return $this->xdict->toArray();
    }

    public function __debugInfo(){
        return $this->xdict->container;
    }
   
    public function jsonSerialize() {
        $anonymous=function(&$v){
            if(is_object($v)) $v=serialize($v);
            if(is_resource($v)) $v=get_resource_type($v).\'_#\'.@intval($v);
        };
        $x=$this->xdict->container;
        array_walk_recursive($x,$anonymous);
        return $x;
    }
    public function __clone() {
        $this->xdict = clone ($this->xdict);
     }
}
   
echo \'<pre>\';
$stack = new Stack([1,2,3]);

$stack->push("a");
$stack->push("b");
$stack->push("c");

var_dump($stack->peek());
   

    foreach($stack as $v){
        echo $v.\'<br>\';

    }
   
    var_dump($stack);
?>'
);
   
   

require_once(
'./../xdict.class.php');

class
Stack implements Iterator,Countable ,JsonSerializable{

    private
$xdict=null;

    public function
__construct($mixed=[]){
        if(
is_array($mixed)){
           
$this->xdict=xdict(0);
           
$this->xdict->fill_with(array_reverse($mixed));
        }elseif(
is_object($mixed)&&key_exists('Traversable',class_implements($mixed))){
           
$this->xdict=xdict(0);
           
$this->xdict->fill_with(array_reverse(iterator_to_array($mixed)));
        }else{
            throw new
LogicException('Argument $mixed must be of type array or must implements the Traversable Interface');
        }
    }

    function
rewind() {
        return
$this->xdict->rewind();
    }

    function
current() {
         return
$this->xdict->current();
    }

    function
key() {
        return
$this->xdict->key();
    }

    function
next() {
      
$x=$this->xdict->next();
       
$this->xdict->shift();
        return
$x;
    }

    function
valid() {
         return
$this->xdict->valid();
    }
    public function
clear (){
        return
$this->xdict->clear();
    }
    public function
count(){
            return
$this->xdict->count();
    }
   
    public function
copy (){
        return clone(
$this);
    }
    public function
isEmpty (){
        return
$this->xdict->isEmpty();
    }
    public function
peek (){
        return
$this->xdict->first();
    }
    public function
pop (){
        if(
$this->isEmpty()) throw new UnderflowException('The queue is already empty');
        return
$this->xdict->shift(true);
    }
    public function
push (...$values ){
        return
$this->xdict->unshift(...$values);
       
    }
    public function
toArray (){
        return
$this->xdict->toArray();
    }

    public function
__debugInfo(){
        return
$this->xdict->container;
    }
   
    public function
jsonSerialize() {
       
$anonymous=function(&$v){
            if(
is_object($v)) $v=serialize($v);
            if(
is_resource($v)) $v=get_resource_type($v).'_#'.@intval($v);
        };
       
$x=$this->xdict->container;
       
array_walk_recursive($x,$anonymous);
        return
$x;
    }
    public function
__clone() {
       
$this->xdict = clone ($this->xdict);
     }
}
   
echo
'<pre>';
$stack = new Stack([1,2,3]);

$stack->push("a");
$stack->push("b");
$stack->push("c");

var_dump($stack->peek());
   

foreach(
$stack as $v){
    echo
$v.'<br>';
}
   
var_dump($stack);
   
?>