Login   Register  
PHP Classes
elePHPant
Icontem

File: demo/queuedemo.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Ordland  >  PHP Collections Framework  >  demo/queuedemo.php  >  Download  
File: demo/queuedemo.php
Role: Example script
Content type: text/plain
Description: A demo using priorityqueue and arraydeque
Class: PHP Collections Framework
Manipulate collections of objects like Java and C#
Author: By
Last change:
Date: 2013-04-08 04:51
Size: 2,431 bytes
 

Contents

Class file image Download
<?php

require "autoloader.php";
use 
Resource\Native\Objective as Objective;
use 
Resource\Native\String as String;
use 
Resource\Collection\ArrayList as ArrayList;
use 
Resource\Utility\Comparative as Comparative;
use 
Resource\Collection\ArrayList as ArrayList;
use 
Resource\Collection\ArrayDeque as ArrayDeque;
use 
Resource\Collection\PriorityQueue as PriorityQueue;

class 
StringComparator implements Comparative{

    public function 
compare(Objective $object NULLObjective $object2 NULL){
        
$length = ($object == NULL)?0:$object->count();
        
$length2 = ($object2 == NULL)?0:$object2->count();
        
$diff $length $length2;
        return 
$diff;
    }

}

$mithos = new String("Mithos Yggdrasill");
$martel = new String("Martel Yggdrasill");
$yuan = new String("Yuan Ka-fei");
$kratos = new String("Kratos Aurion");
$remiel = new String("Remiel");
$zelos = new String("Zelos");
$colette = new String("Colette");
$botta = new String("Botta");

$arraylist = new ArrayList(3);
$arraylist->add($mithos);
$arraylist->add($martel);
$arraylist->add($yuan);
$arraylist->add($kratos); 
$arraylist->add($remiel);
$arraylist->add($zelos); 
$arraylist->add($colette);
$arraylist->add($botta);
$queue = new PriorityQueue($arraylist);
//$queue = new PriorityQueue(4, new StringComparator);
//$queue = new PriorityQueue;
//$queue->offer($mithos);
//$queue->offer($martel);
//$queue->offer($yuan);
//$queue->offer($kratos);
//var_dump($queue);
//var_dump($queue->getArray());

echo "Elements inside PriorityQueue are<br>";
while(
$queue->peek()){
    echo 
$queue->poll();
    echo 
"<br>";
}

$deque = new ArrayDeque;
$deque->offerLast($mithos);
$deque->addLast($martel);
$deque->addLast($yuan);
$deque->offerLast($kratos);
$deque->addLast($remiel);

/*
var_dump($deque);
$deque->clear();
var_dump($deque->contains($mithos));
echo $deque->getFirst();
echo $deque->peekLast();
*/

$deque->remove($yuan);
$deque->offerLast($yuan);
echo 
"<br>Elements inside Deque are: <br>";
$iterator $deque->iterator();
//$iterator = $deque->descendingIterator();
while($iterator->hasNext()){
    echo 
$iterator->next();
    echo 
"<br>";
}

/*
echo "<br>Now polling elements: <br>";
while(!$deque->isEmpty()){
    echo "Polling out {$deque->pollLast()}<br>";
    echo "Deque size shrinks to {$deque->size()}<br>";
}
echo "Deque is now empty!";
*/

?>