PHP Classes

File: examples/Queue.php

Recommend this page to a friend!
  Classes of zinsou A.A.E.Mo´se  >  PHP Dictionary to Array class  >  examples/Queue.php  >  Download  
File: examples/Queue.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: 3 years ago
Size: 4,732 bytes
 

Contents

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

    private $xdict=null;

    public function __construct($mixed=[]){
        if(is_array($mixed)){
            $this->xdict=xdict(0);
            $this->xdict->fill_with($mixed);
        }elseif(is_object($mixed)&&key_exists(\'Traversable\',class_implements($mixed))){
            $this->xdict=xdict(0);
            $this->xdict->fill_with(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->push(...$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>\';
$queue = new Queue();
var_dump($queue);


$queue = new Queue([1, 2, 3]);
var_dump($queue->isEmpty(),$queue->peek(),$queue->pop(),$queue);
    $queue->push(5,5,6,7);


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

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

class
Queue implements Iterator,Countable ,JsonSerializable{

    private
$xdict=null;

    public function
__construct($mixed=[]){
        if(
is_array($mixed)){
           
$this->xdict=xdict(0);
           
$this->xdict->fill_with($mixed);
        }elseif(
is_object($mixed)&&key_exists('Traversable',class_implements($mixed))){
           
$this->xdict=xdict(0);
           
$this->xdict->fill_with(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->push(...$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>';
$queue = new Queue();
var_dump($queue);


$queue = new Queue([1, 2, 3]);
var_dump($queue->isEmpty(),$queue->peek(),$queue->pop(),$queue);
   
$queue->push(5,5,6,7);
   
    foreach(
$queue as $v){
        echo
$v.'<br>';
    }
   
   
var_dump($queue);
   
?>

For more information send a message to info at phpclasses dot org.