PHP Classes
elePHPant
Icontem

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

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

 

Contents

Class file image Download
<?php

require_once('../../../Data/Types/Set.php');

use
Falcraft\Data\Types;

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

echo
"Instantiation -> ";

$success = true;

try {
   
$testSet = new Types\Set(
        array(
1, 2),
        array(
'strict' => true, 'unique' => true,)
    );
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Build -> ";

$success = true;

$testBuild = null;

try {
   
$testBuild = Types\Set::build(array(1, 2, 3, 4, 5,));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"\nSet Internals -- \n";
var_dump($testSet->getArray());

echo
"\nAdd Operation (3) -> ";

$success = true;

try {
   
$testSet->add(3);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"\nSet Internals -- \n";
var_dump($testSet->getArray());

echo
"\nAdd Operation (2) -> ";

$fail = true;

try {
   
$testSet->add(2);
   
$fail = false;
} catch (\
Exception $e) {
   
}

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

echo
"\nSet Internals -- \n";
var_dump($testSet->getArray());

echo
"\nRemove Operation (2) -> ";

$success = true;

try {
   
$testSet->remove(2);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"\nSet Internals -- \n";
var_dump($testSet->getArray());

echo
"\nIs 1 In Set? ";

try {
    if (
$testSet->in(1)) {
        echo
"Yes\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Is Set 1 Empty? ";

try {
    if (
$testSet->isEmpty()) {
        echo
"Yes\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Is Set 2 Empty? ";

try {
   
$testSet2 = new Types\Set();
    if (
$testSet2->isEmpty()) {
        echo
"Yes\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Size of Set 1 -> ";

$success = true;

$count = null;

try {
   
$count = $testSet->size();
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Iteration -- \n";

try {
    foreach (
$testSet->iterate() as $setValue) {
        echo
" \$setValue - $setValue\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Hashing -> ";

$success = true;

$hash = $hash2 = null;

try {
   
$hash = $testSet->hash();
   
$testSet->add(4);
   
$hash2 = $testSet->hash();
} catch (\
Exception $e) {
   
$success = false;
}

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

class
testClass {
    public
$publicProp;
}

$obj = new testClass();

$identity = null;

echo
"Add Reference -> \n";

try {
   
$obj->publicProp = 2;
   
$identity = $testSet->addReference($obj);
   
$obj->publicProp = 3;
   
    echo
"\nSet Internals --\n\n";
   
var_dump($testSet->getArray());
    echo
"\n";
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Retrieve Reference -> ";

unset(
$obj);

try {
   
$obj = $testSet->retrieveReference($identity);
   
$obj->publicProp = 5;
   
    echo
"\n\nSet Internals -- \n\n";
   
var_dump($testSet->getArray());
    echo
"\n";
} catch (\
Exception $e) {
    echo
"EXCEPTION CUAGHT\n";
}

echo
"Remove By Identity -> ";

try {
   
$testSet->removeByIdentifier($identity);
   
    echo
"\n\nSet Internals -- \n\n";
   
var_dump($testSet->getArray());
    echo
"\n";
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"\n\nSet Operations -- \n";

echo
"Set Equality -> ";

$success = true;

$comparisonSet = null;

try {
   
$comparisonSet = new Types\Set(array(3,1,4,));
    if (!
Types\Set::equal($testSet, $comparisonSet)) {
       
$success = false;
    }
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Set Folding -> ";

$success = true;

$folder = $result = null;

try {
   
$folder = function($result, $elem) {$result += $elem; return $result;};
   
$result = Types\Set::fold($folder, $testSet);
} catch (\
Excpetion $e) {
   
$success = false;
}

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

echo
"Set Filtering -> ";

$success = true;

$filter = $result = null;

try {
   
$filter = function($test) {return ($test & 1);};
   
$result = Types\Set::filter($filter, $testSet);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Set Map -> ";

$success = true;

$map = $result = null;

try {
   
$map = function($test) {return $test + 1;};
   
$result = Types\Set::map($map, $testSet);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Set Walk -> ";

$success = true;

$walk = null;

try {
   
$walk = function(&$elem, $key, $prefix) {$elem ="$prefix: $elem";};
   
Types\Set::walk($testSet, $walk, "Walked: ");
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success:\n\n";
   
var_dump($testSet->getArray());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Set Union -> ";

$success = true;

$result = null;

try {
   
$u1 = array(4, 2, 1,);
   
$u2 = array('baz', 'foo', 'bar');
   
$result = Types\Set::union($testSet, $u1, $u2);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Set Intersection -> ";

$success = true;

$testSet = $otherTestSet = $testArray = $result = null;

try {
   
$testSet = Types\Set::build(array(1, 2, 3, 4, 5,));
   
$otherTestSet = Types\Set::build(array(3, 4, 5,));
   
$testArray = array(3, 4,);
   
$result = Types\Set::intersection(
       
$testSet,
       
$otherTestSet,
       
$testArray
   
);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Set Difference -> ";

$success = true;

$testSet = $otherTestSet = $testArrray = $result = null;

try {
   
$testSet = Types\Set::build(array(1, 2, 3, 4, 5, 6,));
   
$otherTestSet = Types\Set::build(array(2, 3, 4, 5,));
   
$testArray = array(1);
   
$result = Types\Set::difference($testSet, $otherTestSet, $testArray);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Set Subset: \n";

$testSet = $otherTestSet = $anotherTestSet = null;

try {
   
$testSet = Types\Set::build(array(1, 2, 3, 4, 5,));
   
$otherTestSet = Types\Set::build(array(2, 3, 4,));
   
$anotherTestSet = Types\Set::build(array(4, 5, 6,));
    if (
Types\Set::subset($testSet, $otherTestSet)) {
        echo
" otherTestSet Is a Subset of testSet\n";
    } else {
        echo
" otherTestSet Is NOT a Subset of testSet\n";
    }
   
    if (
Types\Set::subset($testSet, $anotherTestSet)) {
        echo
" anotherTestSet Is a Subset of testSet\n";
    } else {
        echo
" anotherTestSet Is NOT a Subset of testSet\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}