PHP Classes

File: Falcraft/examples/Data/Types/PriorityQueue.php

Recommend this page to a friend!
  Classes of Asher Wolfstein   Abstract Data Types   Falcraft/examples/Data/Types/PriorityQueue.php   Download  
File: Falcraft/examples/Data/Types/PriorityQueue.php
Role: Example script
Content type: text/plain
Description: Queue Example
Class: Abstract Data Types
Set of abstract data types as pure PHP classes
Author: By
Last change:
Date: 8 years ago
Size: 4,671 bytes
 

Contents

Class file image Download
<?php

require_once('../../../Data/Types/Priority.php');
require_once(
'../../../Data/Types/PriorityQueue.php');
require_once(
'../../../Data/Types/Stack.php');
require_once(
'../../../Data/Types/Restrictions.php');
require_once(
'../../../Data/Types/Type.php');

use
Falcraft\Data\Types;
use
Falcraft\Data\Types\Type;

echo
"Falcraft\\Data\\Types\\PriorityQueue.php Test\n";
echo
"------------------------------------------\n\n";

echo
"Basic Instantiation -> ";

$success = true;

$testPriorityQueue = null;

//try {
   
$testPriorityQueue = new Types\PriorityQueue();
//} catch (\Exception $e) {
// $success = false;
//}

if ($success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Array Instantiation -> ";

$success = true;

$testPriority0 = $testPriority1 = $testPriority2 = $testPriority3 = $testPriorityQueue = null;

try {
   
$testPriority0 = new Types\Priority('coffee', 10);
   
$testPriority1 = new Types\Priority('water', 5);
   
$testPriority2 = new Types\Priority('air', 11);
   
$testPriroity3 = new Types\Priority(42, 2);
   
$testPriorityQueue = new Types\PriorityQueue(array(
       
$testPriority0,
       
$testPriority1,
       
$testPriority2,
       
$testPriroity3,
    ),
   
null,
    array(
'strict' => true,));
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"getList() Internals -- \n\n";
   
var_dump($testPriorityQueue->getList());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Pass Non-Priority Object to Constructor -> ";

$fail = true;

$testPriorityQueuePass = null;

try {
   
$testPriorityQueuePass = new Types\PriorityQueue(array($testPriority0, 'huh?',));
   
$fail = false;
} catch (\
Exception $e) {
   
}

if (
$fail) {
    echo
"Failure!\n";
} else {
    echo
"Success...\n";
}

echo
"Instantiate and Pass (Data) with Custom Restrictions -> ";

$success = true;

$testPriorityQueueRestrictions = $testRestrictions = $testStack = null;

try {
   
$testStack = new Types\Stack();
   
$testRestrictions = new Types\Restrictions(array(Type::TYPED_OBJECT,),
        array(
'Falcraft\\Data\\Types\\Stack'));
   
$testPriorityQueueRestrictions = new Types\PriorityQueue(
        array(),
       
$testRestrictions,
        array(
'strict' => true));
   
$testPriorityQueueRestrictions->push($testStack);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Priority Queue Operations -- \n\n";

echo
" Population -> ";

$success = true;

try {
   
$testPriorityQueue->push(
        new
Types\Priority('towel', 7),
        new
Types\Priority('gas mask', 50),
        new
Types\Priority('cooler', 2));
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\nQueue Internals -- \n\n";
   
var_dump($testPriorityQueue->getList());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
" Top -> ";

try {
    echo
$testPriorityQueue->top() . "\n";
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT!\n";
}

echo
" Pop -> ";

$val = null;

try {
   
$val = $testPriorityQueue->pop();
    echo
"$val\n";
} catch(\
Exception $e) {
    echo
"EXCEPTION CAUGHT!\n";
}

echo
"\nQueue Internals --\n\n";
var_dump($testPriorityQueue->getList());
echo
"\n";

echo
" Bottom -> ";

try {
    echo
$testPriorityQueue->bottom() . "\n";
} catch(\
Exception $e) {
    echo
"EXCEPTION CAUGHT!";
}

echo
" Pull -> ";

$val = null;

try {
   
$val = $testPriorityQueue->pull();
    echo
"$val\n";
} catch(\
Exception $e) {
    echo
"EXCEPTION CAUGHT!\n";
}

echo
"\nQueue Internals --\n\n";
var_dump($testPriorityQueue->getList());
echo
"\n";

echo
" Delete ('air') -> ";

$success = true;

try {
   
$testPriorityQueue->delete('air');
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"\nQueue Internals --\n\n";
var_dump($testPriorityQueue->getList());
echo
"\n";

echo
" Delete Priority (5 'water') -> ";

$success = true;

try {
   
$testPriorityQueue->deletePriority(5);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"\nQueue Internals --\n\n";
var_dump($testPriorityQueue->getList());
echo
"\n";

echo
"Index Retrieval -- \n";

echo
" Priorities >= 7 -> \n\n";

var_dump($testPriorityQueue->index(7, Types\PriorityQueue::HIGHER));
echo
"\n";

echo
" Priorities <= 7 -> \n\n";

var_dump($testPriorityQueue->index(7, Types\PriorityQueue::LOWER));
echo
"\n";

echo
" Priorities == 2 -> \n\n";

var_dump($testPriorityQueue->index(2, Types\PriorityQueue::EQUAL));