cvs: pear /Structures_LinkedList CHANGELOG Double.php LICENSE Single.php index.html package.xml /Structures_LinkedList/examples
double_link_example.php single_link_example.php /Structures_LinkedList/tests LinkTester.php SingleLinkTester.php link_001.phpt link_002.phpt
link_003.phpt link_004.phpt link_005.phpt link_006.phpt link_007.phpt run-tests.log single_link_001.phpt single_link_002.phpt single_link_003.phpt
single_link_004.phpt single_link_005.phpt single_link_006.phpt single_link_007.phpt
| From: | Dan Scott | Date: | Wed, 11 Oct 2006 03:24:35 +0000 |
| Subject: | cvs: pear /Structures_LinkedList CHANGELOG Double.php LICENSE Single.php index.html package.xml /Structures_LinkedList/examples double_link_example.php single_link_example.php /Structures_LinkedList/tests LinkTester.php SingleLinkTester.php link_001.phpt link_002.phpt link_003.phpt link_004.phpt link_005.phpt link_006.phpt link_007.phpt run-tests.log single_link_001.phpt single_link_002.phpt single_link_003.phpt single_link_004.phpt single_link_005.phpt single_link_006.phpt single_link_007.phpt |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-42486@lists.php.net to get a copy of this message | ||
dbs Wed Oct 11 03:24:35 2006 UTC
Added files:
/pear/Structures_LinkedList CHANGELOG Double.php LICENSE Single.php
index.html package.xml
/pear/Structures_LinkedList/examples double_link_example.php
single_link_example.php
/pear/Structures_LinkedList/tests LinkTester.php
SingleLinkTester.php
link_001.phpt link_002.phpt
link_003.phpt link_004.phpt
link_005.phpt link_006.phpt
link_007.phpt run-tests.log
single_link_001.phpt
single_link_002.phpt
single_link_003.phpt
single_link_004.phpt
single_link_005.phpt
single_link_006.phpt
single_link_007.phpt
Log:
Import Structures_LinkedList into PEAR CVS
http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/Double.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/Double.php +++ pear/Structures_LinkedList/Double.php <?php /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker textwidth=80: */ /** * Linked list structure * * This package implements a doubly linked list structure. Each node * (Structures_LinkedList_DoubleNode object) in the list * (Structures_LinkedList_Double) knows the previous node and the next * node in the list. Unlike an array, you can insert or delete nodes at * arbitrary points in the list. * * If your application normally traverses the linked list in a forward-only * direction, use the singly-linked list implemented by * {@link Structures_LinkedList_Single}. If, however, your application * needs to traverse the list backwards, or insert nodes into the list before * other nodes in the list, use the double-linked list implemented by * {@link Structures_LinkedList_Double} to give your application better * performance at the cost of a slightly larger memory footprint. * * Structures_LinkedList_Double implements the Iterator interface so control * structures like foreach($list as $node) and while($list->next()) work * as expected. * * To use this package, derive a child class from * Structures_LinkedList_DoubleNode and add data to the object. Then use the * Structures_LinkedList_Double class to access the nodes. * * PHP version 5 * * LICENSE: Copyright 2006 Dan Scott * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @copyright 2006 Dan Scott * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @version CVS: $Id: Double.php,v 1.1 2006/10/11 03:24:34 dbs Exp $ * @link http://pear.php.net/package/Structures_LinkedList_Single * @example double_link_example.php * * @todo Add some actual error conditions **/ require_once 'PEAR/Exception.php'; require_once 'Single.php'; // {{{ class Structures_LinkedList_Double /** * The Structures_LinkedList_Double class represents a linked list structure * composed of {@link Structures_LinkedList_DoubleNode} objects. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @link http://pear.php.net/package/Structures_LinkedList_Single */ class Structures_LinkedList_Double extends Structures_LinkedList_Single implements Iterator { // {{{ properties /** * Tail node of the linked list * @var Structures_LinkedList_DoubleNode */ protected $tail_node; // }}} // {{{ Constructor: function __construct() /** * Structures_LinkedList_Double constructor * * @param Structures_LinkedList_DoubleNode $root root node for the * linked list */ function __construct(Structures_LinkedList_DoubleNode $root = null) { if ($root) { $this->tail_node = $root; } else { $this->tail_node = null; } parent::__construct($root); } // }}} // {{{ function end() /** * Sets the pointer for the linked list to its last node * * @return Structures_LinkedList_DoubleNode last node in the linked list */ public function end() { if ($this->tail_node) { $this->current = $this->tail_node; } else { $this->current = null; } return $this->current; } // }}} // {{{ function previous() /** * Sets the pointer for the linked list to the previous node and * returns that node * * @return Structures_LinkedList_DoubleNode previous node in the linked list */ public function previous() { if (!$this->current()->previous()) { return false; } $this->current = $this->current()->previous(); return $this->current(); } // }}} // {{{ function insertNode() /** * Inserts a {@link Structures_LinkedList_DoubleNode} object into the linked * list, based on a reference node that already exists in the list. * * @param Structures_LinkedList_DoubleNode $new_node New node to add to * the list * @param Structures_LinkedList_DoubleNode $existing_node Reference * position node * @param bool $before Insert new node before or after the existing node * @return bool Success or failure **/ public function insertNode(Structures_LinkedList_DoubleNode $new_node, Structures_LinkedList_DoubleNode $existing_node, $before = false) { if (!$this->root_node) { $this->__construct($new_node); } // Now add the node according to the requested mode switch ($before) { case true: $previous_node = $existing_node->previous(); if ($previous_node) { $previous_node->setNext($new_node); $new_node->setPrevious($previous_node); } else { // The existing node must be root node; make new node root $this->root_node = $new_node; $new_node->setPrevious(); } $new_node->setNext($existing_node); $existing_node->setPrevious($new_node); break; case false: $new_node->setPrevious($existing_node); $next_node = $existing_node->next(); if ($next_node) { $new_node->setNext($next_node); } else { // The existing node must have been the tail node $this->tail_node = $new_node; } $existing_node->setNext($new_node); break; } return true; } // }}} // {{{ private function _getTailNode() /** * Returns the tail node of the linked list. * * This is a cheap operation for a doubly-linked list. * * @param Structures_LinkedList_DoubleNode $new_node New node to append * @return bool Success or failure **/ private function _getTailNode() { return $this->tail_node; } // }}} // {{{ function deleteNode() /** * Deletes a {@link Structures_LinkedList_DoubleNode} from the list. * * @param Structures_LinkedList_DoubleNode $node Node to delete. */ public function deleteNode(Structures_LinkedList_DoubleNode $node) { /* If this is the root node, and there are more nodes in the list, * make the next node the new root node before deleting this node. */ if ($node === $this->root_node) { $this->root_node = $node->next(); } /* If this is the tail node, and there are more nodes in the list, * make the previous node the tail node before deleting this node */ if ($node === $this->tail_node) { $this->tail_node = $node->previous(); } /* If this is the current node, and there are other nodes in the list, * try making the previous node the current node so that next() works * as expected. * * If that fails, make the next node the current node. * * If that fails, null isn't such a bad place to be. */ if ($node === $this->current) { if ($node->previous()) { $this->current = $node->previous(); } elseif ($node->next()) { $this->current = $node->next(); } else { $this->current = null; } } $node->__destruct(); } // }}} } // }}} // {{{ class Structures_LinkedList_DoubleNode /** * The Structures_LinkedList_DoubleNode class represents a node in a * {@link Structures_LinkedList_Double} linked list structure. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @link http://pear.php.net/package/Structures_LinkedList_Single */ class Structures_LinkedList_DoubleNode extends Structures_LinkedList_SingleNode { // {{{ properties /** * Previous node in the linked list * @var Structures_LinkedList_DoubleNode */ protected $previous; // }}} // {{{ Constructor: function __construct() /** * Structures_LinkedList_DoubleNode constructor */ public function __construct() { $this->next = null; $this->previous = null; } // }}} // {{{ Destructor: function __destruct() /** * Removes node from the list, adjusting the related nodes accordingly. * * This is a problem if the node is the root node for the list. * At this point, however, we do not have access to the list itself. Hmm. */ public function __destruct() { $next = $this->next(); $previous = $this->previous(); if ($previous && $next) { $previous->setNext($next); $next->setPrevious($previous); } elseif ($previous) { $previous->setNext(); } elseif ($next) { $next->setPrevious(); } } // }}} // {{{ function previous() /** * Return the previous node in the linked list * * @return Structures_LinkedList_DoubleNode previous node in the linked list */ public function previous() { if ($this->previous) { return $this->previous; } else { return false; } } // }}} // {{{ function setPrevious() /** * Sets the pointer for the previous node in the linked list * to the specified node * * @param Structures_LinkedList_DoubleNode $node new previous node * in the linked list * @return Structures_LinkedList_DoubleNode new previous node in * the linked list */ public function setPrevious(Structures_LinkedList_DoubleNode $node = null) { $this->previous = $node; return $this->previous; } // }}} } // }}} ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/Single.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/Single.php +++ pear/Structures_LinkedList/Single.php <?php /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker textwidth=80: */ /** * Linked list structure * * This package implements a singly linked list structure. Each node * (Structures_LinkedList_SingleNode object) in the list * (Structures_LinkedList_Single) knows the the next node in the list. * Unlike an array, you can insert or delete nodes at arbitrary points * in the list. * * If your application normally traverses the linked list in a forward-only * direction, use the singly-linked list implemented by * {@link Structures_LinkedList_Single}. If, however, your application * needs to traverse the list backwards, or insert nodes into the list before * other nodes in the list, use the double-linked list implemented by * {@link Structures_LinkedList_Double} to give your application better * performance at the cost of a slightly larger memory footprint. * * Structures_LinkedList_Single implements the Iterator interface so control * structures like foreach($list as $node) and while($list->next()) work * as expected. * * To use this package, derive a child class from * Structures_LinkedList_SingleNode and add data to the object. Then use the * Structures_LinkedList_Single class to access the nodes. * * PHP version 5 * * LICENSE: Copyright 2006 Dan Scott * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @copyright 2006 Dan Scott * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @version CVS: $Id: Single.php,v 1.1 2006/10/11 03:24:34 dbs Exp $ * @link http://pear.php.net/package/Structures_LinkedList_Single * @example single_link_example.php * * @todo Add some actual error conditions **/ require_once 'PEAR/Exception.php'; // {{{ class Structures_LinkedList_Single /** * The Structures_LinkedList_Single class represents a linked list structure * composed of {@link Structures_LinkedList_SingleNode} objects. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @link http://pear.php.net/package/Structures_LinkedList_Single */ class Structures_LinkedList_Single implements Iterator { // {{{ properties /** * Current node in the linked list * @var Structures_LinkedList_SingleNode */ protected $current; /** * Root node of the linked list * @var Structures_LinkedList_SingleNode */ protected $root_node; /** * The linked list contains no nodes */ const ERROR_EMPTY = -1; public static $messages = array( self::ERROR_EMPTY => 'No nodes in this linked list' ); // }}} // {{{ Constructor: function __construct() /** * Structures_LinkedList_Single constructor * * @param Structures_LinkedList_SingleNode $root root node for the * linked list */ function __construct(Structures_LinkedList_SingleNode $root = null) { if ($root) { $this->root_node = $root; $this->current = $root; } else { $this->root_node = null; $this->current = null; } } // }}} // {{{ function current() /** * Returns the current node in the linked list * * @return Structures_LinkedList_SingleNode current node in the linked list */ public function current() { return $this->current; } // }}} // {{{ function rewind() /** * Sets the pointer for the linked list to the root node * * @return Structures_LinkedList_SingleNode root node in the linked list */ public function rewind() { if ($this->root_node) { $this->current = $this->root_node; } else { $this->current = null; } return $this->current; } // }}} // {{{ function end() /** * Sets the pointer for the linked list to the root node * * @return Structures_LinkedList_SingleNode root node in the linked list */ public function end() { $this->current = $this->_getTailNode(); return $this->current; } // }}} // {{{ function key() /** * Stub for Iterator interface that simply returns the current node * * @return Structures_LinkedList_SingleNode current node in the linked list */ public function key() { return $this->current; } // }}} // {{{ function valid() /** * Stub for Iterator interface that simply returns the current node * * @return Structures_LinkedList_SingleNode current node in the linked list */ public function valid() { return $this->current(); } // }}} // {{{ function next() /** * Sets the pointer for the linked list to the next node and * returns that node * * @return Structures_LinkedList_SingleNode next node in the linked list */ public function next() { if (!$this->current) { return false; } $this->current = $this->current()->next(); return $this->current; } // }}} // {{{ function previous() /** * Sets the pointer for the linked list to the previous node and * returns that node * * @return Structures_LinkedList_SingleNode previous node in the linked list */ public function previous() { if (!$this->current) { return false; } $this->current = $this->_getPreviousNode(); return $this->current; } // }}} // {{{ private function _getTailNode() /** * Returns the tail node of the linked list. * * This is an expensive operation! * * @param Structures_LinkedList_SingleNode $new_node New node to append * @return bool Success or failure **/ private function _getTailNode() { $tail_node = $this->root_node; while ($y = $tail_node->next()) { $tail_node = $y; } return $tail_node; } // }}} // {{{ private function _getPreviousNode() /** * Returns the node prior to the current node in the linked list. * * This is an expensive operation for a singly linked list! * * @return Structures_LinkedList_SingleNode Previous node **/ private function _getPreviousNode($node = null) { if (!$node) { $node = $this->current; } $prior_node = $this->root_node; while ($y = $prior_node->next()) { if ($y == $node) { return $prior_node; } $prior_node = $y; } } // }}} // {{{ function appendNode() /** * Adds a {@link Structures_LinkedList_SingleNode} object to the end of * the linked list. * * @param Structures_LinkedList_SingleNode $new_node New node to append * @return bool Success or failure **/ public function appendNode(Structures_LinkedList_SingleNode $new_node) { if (!$this->root_node) { $this->__construct($new_node); return true; } // This is just a special case of insertNode() $this->insertNode($new_node, $this->_getTailNode()); return true; } // }}} // {{{ function insertNode() /** * Inserts a {@link Structures_LinkedList_SingleNode} object into the linked * list, based on a reference node that already exists in the list. * * @param Structures_LinkedList_SingleNode $new_node New node to add to * the list * @param Structures_LinkedList_SingleNode $existing_node Reference * position node * @param bool $before Insert new node before or after the existing node * @return bool Success or failure **/ public function insertNode(Structures_LinkedList_SingleNode $new_node, Structures_LinkedList_SingleNode $existing_node, $before = false) { if (!$this->root_node) { $this->__construct($new_node); return true; } // Now add the node according to the requested mode switch ($before) { case true: if ($existing_node === $this->root_node) { $this->root_node = $new_node; } if ($previous_node = $this->_getPreviousNode($existing_node)) { $previous_node->setNext($new_node); } $new_node->setNext($existing_node); break; case false: $next_node = $existing_node->next(); if ($next_node) { $new_node->setNext($next_node); } $existing_node->setNext($new_node); break; } return true; } // }}} // {{{ function prependNode() /** * Adds a {@link Structures_LinkedList_SingleNode} object to the start * of the linked list. * * @param Structures_LinkedList_SingleNode $new_node Node to prepend * to the list * @return bool Success or failure **/ public function prependNode(Structures_LinkedList_SingleNode $new_node) { if (!$this->root_node) { $this->__construct($new_node); return true; } // This is just a special case of insertNode() $this->insertNode($new_node, $this->root_node, true); return true; } // }}} // {{{ function deleteNode() /** * Deletes a {@link Structures_LinkedList_SingleNode} from the list. * * @param Structures_LinkedList_SingleNode $node Node to delete. */ public function deleteNode(Structures_LinkedList_SingleNode $node) { /* If this is the root node, and there are more nodes in the list, * make the next node the new root node before deleting this node. */ if ($node === $this->root_node) { $this->root_node = $node->next(); } /* If this is the current node, make the next node the current node. * * If that fails, null isn't such a bad place to be. */ if ($node === $this->current) { if ($node->next()) { $this->current = $node->next(); } else { $this->current = null; } } $node->__destruct(); } // }}} } // }}} // {{{ class Structures_LinkedList_SingleNode /** * The Structures_LinkedList_SingleNode class represents a node in a * {@link Structures_LinkedList_Single} linked list structure. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @link http://pear.php.net/package/Structures_LinkedList_Single */ class Structures_LinkedList_SingleNode { // {{{ properties /** * Next node in the linked list * @var Structures_LinkedList_SingleNode */ protected $next; // }}} // {{{ Constructor: function __construct() /** * Structures_LinkedList_SingleNode constructor */ public function __construct() { $this->next = null; } // }}} // {{{ Destructor: function __destruct() /** * Removes node from the list, adjusting the related nodes accordingly. * * This is a problem if the node is the root node for the list. * At this point, however, we do not have access to the list itself. Hmm. */ public function __destruct() { } // }}} // {{{ function next() /** * Return the next node in the linked list * * @return Structures_LinkedList_SingleNode next node in the linked list */ public function next() { if ($this->next) { return $this->next; } else { return false; } } // }}} // {{{ function previous() /** * Return the previous node in the linked list * * Stub method for Structures_LinkedList_DoubleNode to override. * * @return Structures_LinkedList_SingleNode previous node in the linked list */ public function previous() { return false; } // }}} // {{{ function setNext() /** * Sets the pointer for the next node in the linked list to the * specified node * * @param Structures_LinkedList_SingleNode $node new next node in * the linked list * @return Structures_LinkedList_SingleNode new next node in the linked list */ public function setNext(Structures_LinkedList_SingleNode $node = null) { $this->next = $node; return $this->next; } // }}} // {{{ function setPrevious() /** * Sets the pointer for the next node in the linked list to the * specified node * * Stub method for Structures_LinkedList_DoubleNode to override. * * @param Structures_LinkedList_SingleNode $node new next node in * the linked list * @return Structures_LinkedList_SingleNode new next node in the linked list */ public function setPrevious(Structures_LinkedList_SingleNode $node = null) { return false; } // }}} } // }}} ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/index.html?view=markup&rev=1.1 Index: pear/Structures_LinkedList/index.html +++ pear/Structures_LinkedList/index.html <html> <head> <title>Structures_LinkedList package for PHP</title> </head> <body> <h1>Structures_LinkedList package for PHP</h1> <p>Hey, welcome to the Structures_LinkedList package for PHP. This package provides both a singly-linked list (Structures_LinkedList_Single) and a doubly-linked list (Structures_LinkedList_Double).</p> <h2>Requirements</h2> <p>This package requires PHP 5, and was primarily developed with PHP 5.1.4. I have also successfully tested 5.2.0RC1 through RC4.</p> <h2><a name="download">Download</a></h2> <p>Current release: <a href="current.tgz">current.tgz</a>.</p> <p>See <a href="#previous">Previous releases</a> for older versions <h2>Documentation</h2> <p>Read the <a href="docs/">Structures_LinkedList API documentation</a>.</p> <h3>Highlighted source files</h3> <ul> <li><a href="highlight_Single.php">Single.php (package source)</a></li> <li><a href="highlight_Double.php">Double.php (package source)</a></li> <li><a href="highlight_single_link_example.php">single_link_example.php (example)</a></li> <li><a href="highlight_double_link_example.php">double_link_example.php (example)</a></li> </ul> <h2>Installation</h2> <ol> <li>Download the <a href="#download">Structures_LinkedList</a> package.</li> <li>Forcefully install the package with the PEAR installer: <pre> # pear install --force Structures_LinkedList-0.0.#.tgz </pre></li> </ol> <h2>Comments</h2> <p>Comments are welcome by email at <a href="mailto:dan@coffeecode.net">dan@coffeecode.net</a>. <h2>Future</h2> <p>Add more unit tests, flesh out the documentation, and try to find any missing functionality that needs to be implemented before submitting this as a PEAR proposal. I'm (still) aiming for sometime in September.</p> <h2>Apologies</h2> <p>Sorry for the really raw HTML here... for me it's all about substance first, and style later.</p> <h2><a name="#previous">Previous releases</a></h2> <ul> <li><a href="Structures_LinkedList-0.0.6.tar.gz">Structures_LinkedList-0.0.6.tar.gz</a></li> <li><a href="Structures_LinkedList-0.0.5.tar.gz">Structures_LinkedList-0.0.5.tar.gz</a></li> <li><a href="Structures_LinkedList-0.0.4.tar.gz">Structures_LinkedList-0.0.4.tar.gz</a></li> <li><a href="Structures_Linked_List-0.0.3.tar.gz">Structures_Linked_List-0.0.3.tar.gz</a></li> <li><a href="Structures_Linked_List-0.0.2.tar.gz">Structures_Linked_List-0.0.2.tar.gz</a></li> </ul> </body> </html> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/package.xml?view=markup&rev=1.1 Index: pear/Structures_LinkedList/package.xml +++ pear/Structures_LinkedList/package.xml <?xml version="1.0" encoding="utf-8"?> <package version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>Structures_LinkedList</name> <channel>pear.php.net</channel> <summary>Implements singly and doubly-linked lists</summary> <description>A singly-linked list offers the ability to insert or delete nodes at any point within the list. A doubly-linked list also offers the ability to request previous nodes in the list.</description> <lead> <name>Dan Scott</name> <user>dbs</user> <email>dbs@php.net</email> <active>yes</active> </lead> <date>2006-10-10</date> <version> <release>0.0.8</release> <api>0.0.8</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <license uri="http://apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</license> <notes> * Convert to PEAR_Exception for error handling </notes> <contents> <dir name="/" baseinstalldir="Structures/LinkedList"> <file name="CHANGELOG" role="doc" /> <file name="Double.php" role="php" /> <file name="Single.php" role="php" /> <file name="LICENSE" role="doc" /> <dir name="examples"> <file name="double_link_example.php" role="doc" /> <file name="single_link_example.php" role="doc" /> </dir> <dir name="tests"> <file name="LinkTester.php" role="test" /> <file name="SingleLinkTester.php" role="test" /> <file name="link_001.phpt" role="test" /> <file name="link_002.phpt" role="test" /> <file name="link_003.phpt" role="test" /> <file name="link_004.phpt" role="test" /> <file name="link_005.phpt" role="test" /> <file name="link_006.phpt" role="test" /> <file name="single_link_001.phpt" role="test" /> </dir> </dir> </contents> <dependencies> <required> <php> <min>5.0</min> </php> <pearinstaller> <min>1.4.0</min> </pearinstaller> </required> </dependencies> <phprelease> </phprelease> </package> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/examples/double_link_example.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/examples/double_link_example.php +++ pear/Structures_LinkedList/examples/double_link_example.php <?php require 'Structures/LinkedList/Double.php'; /* To do anything useful with a linked list, you need to * extend the Node class to hold data associated with the * node. In this case, we're just going to hold a single * integer in the $_my_number property. */ class LinkTester extends Structures_LinkedList_DoubleNode { protected $_my_number; function __construct($num) { $this->_my_number = $num; } function getNumb() { return $this->_my_number; } function setNumb($numb) { $this->_my_number = $numb; } } /* Now we'll create some instances of the new class */ $node1 = new LinkTester(1); $node2 = new LinkTester(2); $node3 = new LinkTester(3); $node4 = new LinkTester(4); $node5 = new LinkTester(5); /* Start by instantiating a list object. * You can either pass the first node to the constructor, * or leave it null and add nodes later. */ $list = new Structures_LinkedList_Double($node1); // 1 /* appendNode() adds a node to the end of the list */ $list->appendNode($node2); // 1-2 /* prependNode() adds a node to the start of the list */ $list->prependNode($node3); // 3-1-2 /* insertNode($new_node, $reference_node, $before) adds a node * before the reference node if the third parameter is true, * or after the reference node if the third parameter is false */ $list->insertNode($node4, $node1); // 3-1-4-2 $list->insertNode($node5, $node1, true); // 3-5-1-4-2 /* current() returns the current pointer node in the list */ $link = $list->current(); print $link->getNumb(); // "1" /* rewind() resets the pointer to the root node of the list */ $link = $list->rewind(); print $link->getNumb(); // "3" // iterate through the list with do...while() do { print $link->getNumb(); } while ($link = $list->next()); // "35142" /* You can also iterate through a list with foreach(). * Override the key() method to enable $key=>$value iteration. */ foreach ($list as $bull) { print $bull->getNumb(); } // "35142" /* end() resets the pointer to the last node of the list */ $link = $list->end(); print $link->getNumb(); // "2" /* You can iterate backwards through a list with previous() */ do { print $link->getNumb(); } while ($link = $list->previous()); // "24153" ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/examples/single_link_example.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/examples/single_link_example.php +++ pear/Structures_LinkedList/examples/single_link_example.php <?php require 'Structures/LinkedList/Single.php'; /* To do anything useful with a linked list, you need to * extend the Node class to hold data associated with the * node. In this case, we're just going to hold a single * integer in the $_my_number property. */ class LinkTester extends Structures_LinkedList_SingleNode { protected $_my_number; function __construct($num) { $this->_my_number = $num; } function getNumb() { return $this->_my_number; } function setNumb($numb) { $this->_my_number = $numb; } } /* Now we'll create some instances of the new class */ $node1 = new LinkTester(1); $node2 = new LinkTester(2); $node3 = new LinkTester(3); $node4 = new LinkTester(4); $node5 = new LinkTester(5); /* Start by instantiating a list object. * You can either pass the first node to the constructor, * or leave it null and add nodes later. */ $list = new Structures_LinkedList_Single($node1); // 1 /* appendNode() adds a node to the end of the list */ $list->appendNode($node2); // 1-2 /* prependNode() adds a node to the start of the list */ $list->prependNode($node3); // 3-1-2 /* insertNode($new_node, $reference_node, $before) adds a node * before the reference node if the third parameter is true, * or after the reference node if the third parameter is false */ $list->insertNode($node4, $node2, true); // 3-1-4-2 /* current() returns the current pointer node in the list */ $link = $list->current(); // 1 /* You can iterate through a list with the next() method */ do { print $link->getNumb(); } while ($link = $list->next()); // 1-4-2 /* rewind() resets the pointer to the root node of the list */ $link = $list->rewind(); // 3 /* You can also iterate through a list with foreach(). * Override the key() method to enable $key=>$value iteration. */ foreach ($list as $bull) { print $bull->getNumb(); } // 3-1-4-2 /* end() resets the pointer to the last node of the list */ $link = $list->end(); // 2 /* You can iterate backwards through a list with previous() */ do { print $link->getNumb(); } while ($link = $list->previous()); // 2-4-1-3 ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/LinkTester.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/LinkTester.php +++ pear/Structures_LinkedList/tests/LinkTester.php <?php class LinkTester extends Structures_LinkedList_DoubleNode { protected $_my_number; function __construct($num) { $this->_my_number = $num; } function getNumb() { return $this->_my_number; } function setNumb($numb) { $this->_my_number = $numb; } } $tester1 = new LinkTester(1); $tester2 = new LinkTester(2); $tester3 = new LinkTester(3); $tester4 = new LinkTester(4); $tester5 = new LinkTester(5); ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/SingleLinkTester.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/SingleLinkTester.php +++ pear/Structures_LinkedList/tests/SingleLinkTester.php <?php class LinkTester extends Structures_LinkedList_SingleNode { protected $_my_number; function __construct($num) { $this->_my_number = $num; } function getNumb() { return $this->_my_number; } function setNumb($numb) { $this->_my_number = $numb; } } $tester1 = new LinkTester(1); $tester2 = new LinkTester(2); $tester3 = new LinkTester(3); $tester4 = new LinkTester(4); $tester5 = new LinkTester(5); ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_001.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_001.phpt +++ pear/Structures_LinkedList/tests/link_001.phpt --TEST-- link_001: Test linked list constructed with an initial link --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->appendNode($tester4); $link = $xyy->current(); print "Current: " . $link->getNumb() . "\n"; print "While: "; // test iteration with while() do { print $link->getNumb(); } while ($link = $xyy->next()); print "\nRewind: "; $link = $xyy->rewind(); print $link->getNumb(); print "\n"; print "Foreach: "; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } print "\nEnd: "; $link = $xyy->end(); print $link->getNumb(); print "\n"; print "While (in reverse): "; // test iteration with while() do { print $link->getNumb(); } while ($link = $xyy->previous()); ?> --EXPECT-- Current: 1 While: 1234 Rewind: 1 Foreach: 1234 End: 4 While (in reverse): 4321 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_002.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_002.phpt +++ pear/Structures_LinkedList/tests/link_002.phpt --TEST-- link_002: Append links to an initially empty linked list --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double(); $xyy->appendNode($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->insertNode($tester4, $tester2, true); $link = $xyy->current(); print $link->getNumb(); // test iteration with while() while ($link = $xyy->next()) { print $link->getNumb(); } $link = $xyy->rewind(); print "\n"; print $link->getNumb(); print "\n"; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- 1423 1 1423 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_003.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_003.phpt +++ pear/Structures_LinkedList/tests/link_003.phpt --TEST-- link_003: Append links in a specific order --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double(); // add initial link in the list $xyy->appendNode($tester1); print $tester1->getNumb() . "\n"; // add after initial link $xyy->appendNode($tester2); print $tester2->getNumb() . "\n"; // add after initial link, bumping #2 up $xyy->insertNode($tester3, $tester1); print $tester3->getNumb() . "\n"; // add after link #3, bumping #2 up again $xyy->insertNode($tester4, $tester3); print $tester4->getNumb() . "\n"; print "\n"; $link = $xyy->current(); print $link->getNumb(); print "\n"; // test iteration with while() while ($link = $xyy->next()) { print $link->getNumb(); print "\n"; } $link = $xyy->rewind(); print "\n"; print $link->getNumb(); print "\n"; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- 1 2 3 4 1 3 4 2 1 1342 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_004.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_004.phpt +++ pear/Structures_LinkedList/tests/link_004.phpt --TEST-- link_004: Delete every link in the list --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->appendNode($tester4); // Delete all nodes from the list while ($link = $xyy->rewind()) { print "Deleted " . $link->getNumb() . "\n"; $xyy->deleteNode($link); } $link = $xyy->rewind(); print "Done\n"; ?> --EXPECT-- Deleted 1 Deleted 2 Deleted 3 Deleted 4 Done http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_005.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_005.phpt +++ pear/Structures_LinkedList/tests/link_005.phpt --TEST-- link_005: Pass different class types to constructors --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; class TesterExtend extends Structures_LinkedList_DoubleNode { protected $_my_number; function __construct($tester, $num) { $this->_my_number = $num; parent::__construct($tester); } } class TesterFail { protected $summary; protected $fulltext; function __construct($summary, $fulltext) { $this->summary = $summary; $this->fulltext = $fulltext; } } // This should work: TesterExtend extends the Structure_LinkedList_DoubleNode class $tester_extend = new TesterExtend(null, 1); $xyy = new Structures_LinkedList_Double($tester_extend); print "Checking for errors in the expected success case:\n"; // This should fail print "Checking for errors in the expected failure case:\n"; $tester_fail = new TesterFail(null, 1); $xyy_fail = new Structures_LinkedList_Double($tester_fail); ?> --EXPECTF-- Checking for errors in the expected success case: Checking for errors in the expected failure case: %satal error: Argument 1 passed to Structures_LinkedList_Double::__construct() must be an instance of Structures_LinkedList_DoubleNode%s, called in %slink_005.php on line %d and defined in %sDouble.php on line %d http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_006.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_006.phpt +++ pear/Structures_LinkedList/tests/link_006.phpt --TEST-- link_006: Corner case: add a node before the root --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double(); $xyy->prependNode($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->insertNode($tester4, $tester2, true); // Ensure we can increment the current node without messing up the list print "\nCurrent: " . $xyy->current()->getNumb() . "\n"; $link = $xyy->next(); print "Current: " . $link->getNumb() . "\n"; $xyy->insertNode($tester5, $tester1, true); print "\n"; $link = $xyy->current(); print "Current: " . $link->getNumb(); print "\n\nWhile: "; // test iteration with while() $link = $xyy->rewind(); do { print $link->getNumb(); } while ($link = $xyy->next()); print "\n\nForeach: "; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- Current: 1 Current: 4 Current: 4 While: 51423 Foreach: 51423 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_007.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_007.phpt +++ pear/Structures_LinkedList/tests/link_007.phpt --TEST-- link_007: Test many corner cases --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double(); // prepend to an empty list corner case: 1 $xyy->prependNode($tester1); print "Current: {$xyy->current()->getNumb()}\n"; // prepend to a list with only one element: 21 $xyy->prependNode($tester2); print "Current: {$xyy->current()->getNumb()}\n"; // insert after tail node corner case: 213 $xyy->insertNode($tester3, $tester1); print "Current: {$xyy->current()->getNumb()}\n"; // insert before root node corner case: 4213 $xyy->insertNode($tester4, $tester2, true); print "Current: {$xyy->current()->getNumb()}\n"; // insert before tail node corner case: 42153 $xyy->insertNode($tester5, $tester3, true); print "Current: {$xyy->current()->getNumb()}\n"; print "Foreach: "; foreach ($xyy as $node) { print $node->getNumb(); } print "\nWhile (in reverse): "; // test reverse iteration with while() $link = $xyy->end(); do { print $link->getNumb(); } while ($link = $xyy->previous()); ?> --EXPECT-- Current: 1 Current: 1 Current: 1 Current: 1 Current: 1 Foreach: 42153 While (in reverse): 35124 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/run-tests.log?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/run-tests.log +++ pear/Structures_LinkedList/tests/run-tests.log TOTAL TIME: 00:03 11 PASSED TESTS 0 SKIPPED TESTS 3 FAILED TESTS: /home/dan/svn/Structures/LinkedList/tests/single_link_002.phpt /home/dan/svn/Structures/LinkedList/tests/single_link_006.phpt /home/dan/svn/Structures/LinkedList/tests/single_link_007.phpt http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_001.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_001.phpt +++ pear/Structures_LinkedList/tests/single_link_001.phpt --TEST-- single_link_001: Test linked list constructed with an initial link --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->appendNode($tester4); $link = $xyy->current(); print "Current: " . $link->getNumb() . "\n"; print "While: "; // test iteration with while() do { print $link->getNumb(); } while ($link = $xyy->next()); print "\nRewind: "; $link = $xyy->rewind(); print $link->getNumb(); print "\n"; print "Foreach: "; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } print "\nEnd: "; $link = $xyy->end(); print $link->getNumb(); print "\n"; print "While (in reverse): "; // test iteration with while() do { print $link->getNumb(); } while ($link = $xyy->previous()); ?> --EXPECT-- Current: 1 While: 1234 Rewind: 1 Foreach: 1234 End: 4 While (in reverse): 4321 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_002.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_002.phpt +++ pear/Structures_LinkedList/tests/single_link_002.phpt --TEST-- single_link_002: Append links to an initially empty linked list --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single(); $xyy->appendNode($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->insertNode($tester4, $tester2, true); $link = $xyy->current(); print $link->getNumb(); // test iteration with while() while ($link = $xyy->next()) { print $link->getNumb(); } $link = $xyy->rewind(); print "\n"; print $link->getNumb(); print "\n"; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- 1423 1 1423 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_003.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_003.phpt +++ pear/Structures_LinkedList/tests/single_link_003.phpt --TEST-- single_link_003: Append links in a specific order --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single(); // add initial link in the list $xyy->appendNode($tester1); print $tester1->getNumb() . "\n"; // add after initial link $xyy->appendNode($tester2); print $tester2->getNumb() . "\n"; // add after initial link, bumping #2 up $xyy->insertNode($tester3, $tester1); print $tester3->getNumb() . "\n"; // add after link #3, bumping #2 up again $xyy->insertNode($tester4, $tester3); print $tester4->getNumb() . "\n"; print "\n"; $link = $xyy->current(); print $link->getNumb(); print "\n"; // test iteration with while() while ($link = $xyy->next()) { print $link->getNumb(); print "\n"; } $link = $xyy->rewind(); print "\n"; print $link->getNumb(); print "\n"; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- 1 2 3 4 1 3 4 2 1 1342 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_004.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_004.phpt +++ pear/Structures_LinkedList/tests/single_link_004.phpt --TEST-- single_link_004: Delete every link in the list --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->appendNode($tester4); // Delete all nodes from the list while ($link = $xyy->rewind()) { print "Deleted " . $link->getNumb() . "\n"; $xyy->deleteNode($link); } $link = $xyy->rewind(); print "Done\n"; ?> --EXPECT-- Deleted 1 Deleted 2 Deleted 3 Deleted 4 Done http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_005.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_005.phpt +++ pear/Structures_LinkedList/tests/single_link_005.phpt --TEST-- single_link_005: Pass different class types to constructors --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; class TesterExtend extends Structures_LinkedList_SingleNode { protected $_my_number; function __construct($tester, $num) { $this->_my_number = $num; parent::__construct($tester); } } class TesterFail { protected $summary; protected $fulltext; function __construct($summary, $fulltext) { $this->summary = $summary; $this->fulltext = $fulltext; } } // This should work: TesterExtend extends the Structure_LinkedList_SingleNode class $tester_extend = new TesterExtend(null, 1); $xyy = new Structures_LinkedList_Single($tester_extend); print "Checking for errors in the expected success case:\n"; // This should fail print "Checking for errors in the expected failure case:\n"; $tester_fail = new TesterFail(null, 1); $xyy_fail = new Structures_LinkedList_Single($tester_fail); ?> --EXPECTF-- Checking for errors in the expected success case: Checking for errors in the expected failure case: %satal error: Argument 1 passed to Structures_LinkedList_Single::__construct() must be an instance of Structures_LinkedList_SingleNode%s, called in %slink_005.php on line %d and defined in %sSingle.php on line %d http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_006.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_006.phpt +++ pear/Structures_LinkedList/tests/single_link_006.phpt --TEST-- single_link_006: Corner case: add a node before the root --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single(); $xyy->prependNode($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->insertNode($tester4, $tester2, true); // Ensure we can increment the current node without messing up the list print "\nCurrent: " . $xyy->current()->getNumb() . "\n"; $link = $xyy->next(); print "Current: " . $link->getNumb() . "\n"; $xyy->insertNode($tester5, $tester1, true); print "\n"; $link = $xyy->current(); print "Current: " . $link->getNumb(); print "\n\nWhile: "; // test iteration with while() $link = $xyy->rewind(); do { print $link->getNumb(); } while ($link = $xyy->next()); print "\n\nForeach: "; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- Current: 1 Current: 4 Current: 4 While: 51423 Foreach: 51423 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_007.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_007.phpt +++ pear/Structures_LinkedList/tests/single_link_007.phpt --TEST-- single_link_007: Test many corner cases --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single(); // prepend to an empty list corner case: 1 $xyy->prependNode($tester1); print "Current: {$xyy->current()->getNumb()}\n"; // prepend to a list with only one element: 21 $xyy->prependNode($tester2); print "Current: {$xyy->current()->getNumb()}\n"; // insert after tail node corner case: 213 $xyy->insertNode($tester3, $tester1); print "Current: {$xyy->current()->getNumb()}\n"; // insert before root node corner case: 4213 $xyy->insertNode($tester4, $tester2, true); print "Current: {$xyy->current()->getNumb()}\n"; // insert before tail node corner case: 42153 $xyy->insertNode($tester5, $tester3, true); print "Current: {$xyy->current()->getNumb()}\n"; print "Foreach: "; foreach ($xyy as $node) { print $node->getNumb(); } print "\nWhile (in reverse): "; // test reverse iteration with while() $link = $xyy->end(); do { print $link->getNumb(); } while ($link = $xyy->previous()); ?> --EXPECT-- Current: 1 Current: 1 Current: 1 Current: 1 Current: 1 Foreach: 42153 While (in reverse): 35124
http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/Double.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/Double.php +++ pear/Structures_LinkedList/Double.php <?php /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker textwidth=80: */ /** * Linked list structure * * This package implements a doubly linked list structure. Each node * (Structures_LinkedList_DoubleNode object) in the list * (Structures_LinkedList_Double) knows the previous node and the next * node in the list. Unlike an array, you can insert or delete nodes at * arbitrary points in the list. * * If your application normally traverses the linked list in a forward-only * direction, use the singly-linked list implemented by * {@link Structures_LinkedList_Single}. If, however, your application * needs to traverse the list backwards, or insert nodes into the list before * other nodes in the list, use the double-linked list implemented by * {@link Structures_LinkedList_Double} to give your application better * performance at the cost of a slightly larger memory footprint. * * Structures_LinkedList_Double implements the Iterator interface so control * structures like foreach($list as $node) and while($list->next()) work * as expected. * * To use this package, derive a child class from * Structures_LinkedList_DoubleNode and add data to the object. Then use the * Structures_LinkedList_Double class to access the nodes. * * PHP version 5 * * LICENSE: Copyright 2006 Dan Scott * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @copyright 2006 Dan Scott * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @version CVS: $Id: Double.php,v 1.1 2006/10/11 03:24:34 dbs Exp $ * @link http://pear.php.net/package/Structures_LinkedList_Single * @example double_link_example.php * * @todo Add some actual error conditions **/ require_once 'PEAR/Exception.php'; require_once 'Single.php'; // {{{ class Structures_LinkedList_Double /** * The Structures_LinkedList_Double class represents a linked list structure * composed of {@link Structures_LinkedList_DoubleNode} objects. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @link http://pear.php.net/package/Structures_LinkedList_Single */ class Structures_LinkedList_Double extends Structures_LinkedList_Single implements Iterator { // {{{ properties /** * Tail node of the linked list * @var Structures_LinkedList_DoubleNode */ protected $tail_node; // }}} // {{{ Constructor: function __construct() /** * Structures_LinkedList_Double constructor * * @param Structures_LinkedList_DoubleNode $root root node for the * linked list */ function __construct(Structures_LinkedList_DoubleNode $root = null) { if ($root) { $this->tail_node = $root; } else { $this->tail_node = null; } parent::__construct($root); } // }}} // {{{ function end() /** * Sets the pointer for the linked list to its last node * * @return Structures_LinkedList_DoubleNode last node in the linked list */ public function end() { if ($this->tail_node) { $this->current = $this->tail_node; } else { $this->current = null; } return $this->current; } // }}} // {{{ function previous() /** * Sets the pointer for the linked list to the previous node and * returns that node * * @return Structures_LinkedList_DoubleNode previous node in the linked list */ public function previous() { if (!$this->current()->previous()) { return false; } $this->current = $this->current()->previous(); return $this->current(); } // }}} // {{{ function insertNode() /** * Inserts a {@link Structures_LinkedList_DoubleNode} object into the linked * list, based on a reference node that already exists in the list. * * @param Structures_LinkedList_DoubleNode $new_node New node to add to * the list * @param Structures_LinkedList_DoubleNode $existing_node Reference * position node * @param bool $before Insert new node before or after the existing node * @return bool Success or failure **/ public function insertNode(Structures_LinkedList_DoubleNode $new_node, Structures_LinkedList_DoubleNode $existing_node, $before = false) { if (!$this->root_node) { $this->__construct($new_node); } // Now add the node according to the requested mode switch ($before) { case true: $previous_node = $existing_node->previous(); if ($previous_node) { $previous_node->setNext($new_node); $new_node->setPrevious($previous_node); } else { // The existing node must be root node; make new node root $this->root_node = $new_node; $new_node->setPrevious(); } $new_node->setNext($existing_node); $existing_node->setPrevious($new_node); break; case false: $new_node->setPrevious($existing_node); $next_node = $existing_node->next(); if ($next_node) { $new_node->setNext($next_node); } else { // The existing node must have been the tail node $this->tail_node = $new_node; } $existing_node->setNext($new_node); break; } return true; } // }}} // {{{ private function _getTailNode() /** * Returns the tail node of the linked list. * * This is a cheap operation for a doubly-linked list. * * @param Structures_LinkedList_DoubleNode $new_node New node to append * @return bool Success or failure **/ private function _getTailNode() { return $this->tail_node; } // }}} // {{{ function deleteNode() /** * Deletes a {@link Structures_LinkedList_DoubleNode} from the list. * * @param Structures_LinkedList_DoubleNode $node Node to delete. */ public function deleteNode(Structures_LinkedList_DoubleNode $node) { /* If this is the root node, and there are more nodes in the list, * make the next node the new root node before deleting this node. */ if ($node === $this->root_node) { $this->root_node = $node->next(); } /* If this is the tail node, and there are more nodes in the list, * make the previous node the tail node before deleting this node */ if ($node === $this->tail_node) { $this->tail_node = $node->previous(); } /* If this is the current node, and there are other nodes in the list, * try making the previous node the current node so that next() works * as expected. * * If that fails, make the next node the current node. * * If that fails, null isn't such a bad place to be. */ if ($node === $this->current) { if ($node->previous()) { $this->current = $node->previous(); } elseif ($node->next()) { $this->current = $node->next(); } else { $this->current = null; } } $node->__destruct(); } // }}} } // }}} // {{{ class Structures_LinkedList_DoubleNode /** * The Structures_LinkedList_DoubleNode class represents a node in a * {@link Structures_LinkedList_Double} linked list structure. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @link http://pear.php.net/package/Structures_LinkedList_Single */ class Structures_LinkedList_DoubleNode extends Structures_LinkedList_SingleNode { // {{{ properties /** * Previous node in the linked list * @var Structures_LinkedList_DoubleNode */ protected $previous; // }}} // {{{ Constructor: function __construct() /** * Structures_LinkedList_DoubleNode constructor */ public function __construct() { $this->next = null; $this->previous = null; } // }}} // {{{ Destructor: function __destruct() /** * Removes node from the list, adjusting the related nodes accordingly. * * This is a problem if the node is the root node for the list. * At this point, however, we do not have access to the list itself. Hmm. */ public function __destruct() { $next = $this->next(); $previous = $this->previous(); if ($previous && $next) { $previous->setNext($next); $next->setPrevious($previous); } elseif ($previous) { $previous->setNext(); } elseif ($next) { $next->setPrevious(); } } // }}} // {{{ function previous() /** * Return the previous node in the linked list * * @return Structures_LinkedList_DoubleNode previous node in the linked list */ public function previous() { if ($this->previous) { return $this->previous; } else { return false; } } // }}} // {{{ function setPrevious() /** * Sets the pointer for the previous node in the linked list * to the specified node * * @param Structures_LinkedList_DoubleNode $node new previous node * in the linked list * @return Structures_LinkedList_DoubleNode new previous node in * the linked list */ public function setPrevious(Structures_LinkedList_DoubleNode $node = null) { $this->previous = $node; return $this->previous; } // }}} } // }}} ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/Single.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/Single.php +++ pear/Structures_LinkedList/Single.php <?php /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker textwidth=80: */ /** * Linked list structure * * This package implements a singly linked list structure. Each node * (Structures_LinkedList_SingleNode object) in the list * (Structures_LinkedList_Single) knows the the next node in the list. * Unlike an array, you can insert or delete nodes at arbitrary points * in the list. * * If your application normally traverses the linked list in a forward-only * direction, use the singly-linked list implemented by * {@link Structures_LinkedList_Single}. If, however, your application * needs to traverse the list backwards, or insert nodes into the list before * other nodes in the list, use the double-linked list implemented by * {@link Structures_LinkedList_Double} to give your application better * performance at the cost of a slightly larger memory footprint. * * Structures_LinkedList_Single implements the Iterator interface so control * structures like foreach($list as $node) and while($list->next()) work * as expected. * * To use this package, derive a child class from * Structures_LinkedList_SingleNode and add data to the object. Then use the * Structures_LinkedList_Single class to access the nodes. * * PHP version 5 * * LICENSE: Copyright 2006 Dan Scott * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @copyright 2006 Dan Scott * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @version CVS: $Id: Single.php,v 1.1 2006/10/11 03:24:34 dbs Exp $ * @link http://pear.php.net/package/Structures_LinkedList_Single * @example single_link_example.php * * @todo Add some actual error conditions **/ require_once 'PEAR/Exception.php'; // {{{ class Structures_LinkedList_Single /** * The Structures_LinkedList_Single class represents a linked list structure * composed of {@link Structures_LinkedList_SingleNode} objects. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @link http://pear.php.net/package/Structures_LinkedList_Single */ class Structures_LinkedList_Single implements Iterator { // {{{ properties /** * Current node in the linked list * @var Structures_LinkedList_SingleNode */ protected $current; /** * Root node of the linked list * @var Structures_LinkedList_SingleNode */ protected $root_node; /** * The linked list contains no nodes */ const ERROR_EMPTY = -1; public static $messages = array( self::ERROR_EMPTY => 'No nodes in this linked list' ); // }}} // {{{ Constructor: function __construct() /** * Structures_LinkedList_Single constructor * * @param Structures_LinkedList_SingleNode $root root node for the * linked list */ function __construct(Structures_LinkedList_SingleNode $root = null) { if ($root) { $this->root_node = $root; $this->current = $root; } else { $this->root_node = null; $this->current = null; } } // }}} // {{{ function current() /** * Returns the current node in the linked list * * @return Structures_LinkedList_SingleNode current node in the linked list */ public function current() { return $this->current; } // }}} // {{{ function rewind() /** * Sets the pointer for the linked list to the root node * * @return Structures_LinkedList_SingleNode root node in the linked list */ public function rewind() { if ($this->root_node) { $this->current = $this->root_node; } else { $this->current = null; } return $this->current; } // }}} // {{{ function end() /** * Sets the pointer for the linked list to the root node * * @return Structures_LinkedList_SingleNode root node in the linked list */ public function end() { $this->current = $this->_getTailNode(); return $this->current; } // }}} // {{{ function key() /** * Stub for Iterator interface that simply returns the current node * * @return Structures_LinkedList_SingleNode current node in the linked list */ public function key() { return $this->current; } // }}} // {{{ function valid() /** * Stub for Iterator interface that simply returns the current node * * @return Structures_LinkedList_SingleNode current node in the linked list */ public function valid() { return $this->current(); } // }}} // {{{ function next() /** * Sets the pointer for the linked list to the next node and * returns that node * * @return Structures_LinkedList_SingleNode next node in the linked list */ public function next() { if (!$this->current) { return false; } $this->current = $this->current()->next(); return $this->current; } // }}} // {{{ function previous() /** * Sets the pointer for the linked list to the previous node and * returns that node * * @return Structures_LinkedList_SingleNode previous node in the linked list */ public function previous() { if (!$this->current) { return false; } $this->current = $this->_getPreviousNode(); return $this->current; } // }}} // {{{ private function _getTailNode() /** * Returns the tail node of the linked list. * * This is an expensive operation! * * @param Structures_LinkedList_SingleNode $new_node New node to append * @return bool Success or failure **/ private function _getTailNode() { $tail_node = $this->root_node; while ($y = $tail_node->next()) { $tail_node = $y; } return $tail_node; } // }}} // {{{ private function _getPreviousNode() /** * Returns the node prior to the current node in the linked list. * * This is an expensive operation for a singly linked list! * * @return Structures_LinkedList_SingleNode Previous node **/ private function _getPreviousNode($node = null) { if (!$node) { $node = $this->current; } $prior_node = $this->root_node; while ($y = $prior_node->next()) { if ($y == $node) { return $prior_node; } $prior_node = $y; } } // }}} // {{{ function appendNode() /** * Adds a {@link Structures_LinkedList_SingleNode} object to the end of * the linked list. * * @param Structures_LinkedList_SingleNode $new_node New node to append * @return bool Success or failure **/ public function appendNode(Structures_LinkedList_SingleNode $new_node) { if (!$this->root_node) { $this->__construct($new_node); return true; } // This is just a special case of insertNode() $this->insertNode($new_node, $this->_getTailNode()); return true; } // }}} // {{{ function insertNode() /** * Inserts a {@link Structures_LinkedList_SingleNode} object into the linked * list, based on a reference node that already exists in the list. * * @param Structures_LinkedList_SingleNode $new_node New node to add to * the list * @param Structures_LinkedList_SingleNode $existing_node Reference * position node * @param bool $before Insert new node before or after the existing node * @return bool Success or failure **/ public function insertNode(Structures_LinkedList_SingleNode $new_node, Structures_LinkedList_SingleNode $existing_node, $before = false) { if (!$this->root_node) { $this->__construct($new_node); return true; } // Now add the node according to the requested mode switch ($before) { case true: if ($existing_node === $this->root_node) { $this->root_node = $new_node; } if ($previous_node = $this->_getPreviousNode($existing_node)) { $previous_node->setNext($new_node); } $new_node->setNext($existing_node); break; case false: $next_node = $existing_node->next(); if ($next_node) { $new_node->setNext($next_node); } $existing_node->setNext($new_node); break; } return true; } // }}} // {{{ function prependNode() /** * Adds a {@link Structures_LinkedList_SingleNode} object to the start * of the linked list. * * @param Structures_LinkedList_SingleNode $new_node Node to prepend * to the list * @return bool Success or failure **/ public function prependNode(Structures_LinkedList_SingleNode $new_node) { if (!$this->root_node) { $this->__construct($new_node); return true; } // This is just a special case of insertNode() $this->insertNode($new_node, $this->root_node, true); return true; } // }}} // {{{ function deleteNode() /** * Deletes a {@link Structures_LinkedList_SingleNode} from the list. * * @param Structures_LinkedList_SingleNode $node Node to delete. */ public function deleteNode(Structures_LinkedList_SingleNode $node) { /* If this is the root node, and there are more nodes in the list, * make the next node the new root node before deleting this node. */ if ($node === $this->root_node) { $this->root_node = $node->next(); } /* If this is the current node, make the next node the current node. * * If that fails, null isn't such a bad place to be. */ if ($node === $this->current) { if ($node->next()) { $this->current = $node->next(); } else { $this->current = null; } } $node->__destruct(); } // }}} } // }}} // {{{ class Structures_LinkedList_SingleNode /** * The Structures_LinkedList_SingleNode class represents a node in a * {@link Structures_LinkedList_Single} linked list structure. * * @category Structures * @package Structures_LinkedList_Single * @author Dan Scott <dscott@laurentian.ca> * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @link http://pear.php.net/package/Structures_LinkedList_Single */ class Structures_LinkedList_SingleNode { // {{{ properties /** * Next node in the linked list * @var Structures_LinkedList_SingleNode */ protected $next; // }}} // {{{ Constructor: function __construct() /** * Structures_LinkedList_SingleNode constructor */ public function __construct() { $this->next = null; } // }}} // {{{ Destructor: function __destruct() /** * Removes node from the list, adjusting the related nodes accordingly. * * This is a problem if the node is the root node for the list. * At this point, however, we do not have access to the list itself. Hmm. */ public function __destruct() { } // }}} // {{{ function next() /** * Return the next node in the linked list * * @return Structures_LinkedList_SingleNode next node in the linked list */ public function next() { if ($this->next) { return $this->next; } else { return false; } } // }}} // {{{ function previous() /** * Return the previous node in the linked list * * Stub method for Structures_LinkedList_DoubleNode to override. * * @return Structures_LinkedList_SingleNode previous node in the linked list */ public function previous() { return false; } // }}} // {{{ function setNext() /** * Sets the pointer for the next node in the linked list to the * specified node * * @param Structures_LinkedList_SingleNode $node new next node in * the linked list * @return Structures_LinkedList_SingleNode new next node in the linked list */ public function setNext(Structures_LinkedList_SingleNode $node = null) { $this->next = $node; return $this->next; } // }}} // {{{ function setPrevious() /** * Sets the pointer for the next node in the linked list to the * specified node * * Stub method for Structures_LinkedList_DoubleNode to override. * * @param Structures_LinkedList_SingleNode $node new next node in * the linked list * @return Structures_LinkedList_SingleNode new next node in the linked list */ public function setPrevious(Structures_LinkedList_SingleNode $node = null) { return false; } // }}} } // }}} ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/index.html?view=markup&rev=1.1 Index: pear/Structures_LinkedList/index.html +++ pear/Structures_LinkedList/index.html <html> <head> <title>Structures_LinkedList package for PHP</title> </head> <body> <h1>Structures_LinkedList package for PHP</h1> <p>Hey, welcome to the Structures_LinkedList package for PHP. This package provides both a singly-linked list (Structures_LinkedList_Single) and a doubly-linked list (Structures_LinkedList_Double).</p> <h2>Requirements</h2> <p>This package requires PHP 5, and was primarily developed with PHP 5.1.4. I have also successfully tested 5.2.0RC1 through RC4.</p> <h2><a name="download">Download</a></h2> <p>Current release: <a href="current.tgz">current.tgz</a>.</p> <p>See <a href="#previous">Previous releases</a> for older versions <h2>Documentation</h2> <p>Read the <a href="docs/">Structures_LinkedList API documentation</a>.</p> <h3>Highlighted source files</h3> <ul> <li><a href="highlight_Single.php">Single.php (package source)</a></li> <li><a href="highlight_Double.php">Double.php (package source)</a></li> <li><a href="highlight_single_link_example.php">single_link_example.php (example)</a></li> <li><a href="highlight_double_link_example.php">double_link_example.php (example)</a></li> </ul> <h2>Installation</h2> <ol> <li>Download the <a href="#download">Structures_LinkedList</a> package.</li> <li>Forcefully install the package with the PEAR installer: <pre> # pear install --force Structures_LinkedList-0.0.#.tgz </pre></li> </ol> <h2>Comments</h2> <p>Comments are welcome by email at <a href="mailto:dan@coffeecode.net">dan@coffeecode.net</a>. <h2>Future</h2> <p>Add more unit tests, flesh out the documentation, and try to find any missing functionality that needs to be implemented before submitting this as a PEAR proposal. I'm (still) aiming for sometime in September.</p> <h2>Apologies</h2> <p>Sorry for the really raw HTML here... for me it's all about substance first, and style later.</p> <h2><a name="#previous">Previous releases</a></h2> <ul> <li><a href="Structures_LinkedList-0.0.6.tar.gz">Structures_LinkedList-0.0.6.tar.gz</a></li> <li><a href="Structures_LinkedList-0.0.5.tar.gz">Structures_LinkedList-0.0.5.tar.gz</a></li> <li><a href="Structures_LinkedList-0.0.4.tar.gz">Structures_LinkedList-0.0.4.tar.gz</a></li> <li><a href="Structures_Linked_List-0.0.3.tar.gz">Structures_Linked_List-0.0.3.tar.gz</a></li> <li><a href="Structures_Linked_List-0.0.2.tar.gz">Structures_Linked_List-0.0.2.tar.gz</a></li> </ul> </body> </html> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/package.xml?view=markup&rev=1.1 Index: pear/Structures_LinkedList/package.xml +++ pear/Structures_LinkedList/package.xml <?xml version="1.0" encoding="utf-8"?> <package version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>Structures_LinkedList</name> <channel>pear.php.net</channel> <summary>Implements singly and doubly-linked lists</summary> <description>A singly-linked list offers the ability to insert or delete nodes at any point within the list. A doubly-linked list also offers the ability to request previous nodes in the list.</description> <lead> <name>Dan Scott</name> <user>dbs</user> <email>dbs@php.net</email> <active>yes</active> </lead> <date>2006-10-10</date> <version> <release>0.0.8</release> <api>0.0.8</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <license uri="http://apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</license> <notes> * Convert to PEAR_Exception for error handling </notes> <contents> <dir name="/" baseinstalldir="Structures/LinkedList"> <file name="CHANGELOG" role="doc" /> <file name="Double.php" role="php" /> <file name="Single.php" role="php" /> <file name="LICENSE" role="doc" /> <dir name="examples"> <file name="double_link_example.php" role="doc" /> <file name="single_link_example.php" role="doc" /> </dir> <dir name="tests"> <file name="LinkTester.php" role="test" /> <file name="SingleLinkTester.php" role="test" /> <file name="link_001.phpt" role="test" /> <file name="link_002.phpt" role="test" /> <file name="link_003.phpt" role="test" /> <file name="link_004.phpt" role="test" /> <file name="link_005.phpt" role="test" /> <file name="link_006.phpt" role="test" /> <file name="single_link_001.phpt" role="test" /> </dir> </dir> </contents> <dependencies> <required> <php> <min>5.0</min> </php> <pearinstaller> <min>1.4.0</min> </pearinstaller> </required> </dependencies> <phprelease> </phprelease> </package> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/examples/double_link_example.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/examples/double_link_example.php +++ pear/Structures_LinkedList/examples/double_link_example.php <?php require 'Structures/LinkedList/Double.php'; /* To do anything useful with a linked list, you need to * extend the Node class to hold data associated with the * node. In this case, we're just going to hold a single * integer in the $_my_number property. */ class LinkTester extends Structures_LinkedList_DoubleNode { protected $_my_number; function __construct($num) { $this->_my_number = $num; } function getNumb() { return $this->_my_number; } function setNumb($numb) { $this->_my_number = $numb; } } /* Now we'll create some instances of the new class */ $node1 = new LinkTester(1); $node2 = new LinkTester(2); $node3 = new LinkTester(3); $node4 = new LinkTester(4); $node5 = new LinkTester(5); /* Start by instantiating a list object. * You can either pass the first node to the constructor, * or leave it null and add nodes later. */ $list = new Structures_LinkedList_Double($node1); // 1 /* appendNode() adds a node to the end of the list */ $list->appendNode($node2); // 1-2 /* prependNode() adds a node to the start of the list */ $list->prependNode($node3); // 3-1-2 /* insertNode($new_node, $reference_node, $before) adds a node * before the reference node if the third parameter is true, * or after the reference node if the third parameter is false */ $list->insertNode($node4, $node1); // 3-1-4-2 $list->insertNode($node5, $node1, true); // 3-5-1-4-2 /* current() returns the current pointer node in the list */ $link = $list->current(); print $link->getNumb(); // "1" /* rewind() resets the pointer to the root node of the list */ $link = $list->rewind(); print $link->getNumb(); // "3" // iterate through the list with do...while() do { print $link->getNumb(); } while ($link = $list->next()); // "35142" /* You can also iterate through a list with foreach(). * Override the key() method to enable $key=>$value iteration. */ foreach ($list as $bull) { print $bull->getNumb(); } // "35142" /* end() resets the pointer to the last node of the list */ $link = $list->end(); print $link->getNumb(); // "2" /* You can iterate backwards through a list with previous() */ do { print $link->getNumb(); } while ($link = $list->previous()); // "24153" ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/examples/single_link_example.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/examples/single_link_example.php +++ pear/Structures_LinkedList/examples/single_link_example.php <?php require 'Structures/LinkedList/Single.php'; /* To do anything useful with a linked list, you need to * extend the Node class to hold data associated with the * node. In this case, we're just going to hold a single * integer in the $_my_number property. */ class LinkTester extends Structures_LinkedList_SingleNode { protected $_my_number; function __construct($num) { $this->_my_number = $num; } function getNumb() { return $this->_my_number; } function setNumb($numb) { $this->_my_number = $numb; } } /* Now we'll create some instances of the new class */ $node1 = new LinkTester(1); $node2 = new LinkTester(2); $node3 = new LinkTester(3); $node4 = new LinkTester(4); $node5 = new LinkTester(5); /* Start by instantiating a list object. * You can either pass the first node to the constructor, * or leave it null and add nodes later. */ $list = new Structures_LinkedList_Single($node1); // 1 /* appendNode() adds a node to the end of the list */ $list->appendNode($node2); // 1-2 /* prependNode() adds a node to the start of the list */ $list->prependNode($node3); // 3-1-2 /* insertNode($new_node, $reference_node, $before) adds a node * before the reference node if the third parameter is true, * or after the reference node if the third parameter is false */ $list->insertNode($node4, $node2, true); // 3-1-4-2 /* current() returns the current pointer node in the list */ $link = $list->current(); // 1 /* You can iterate through a list with the next() method */ do { print $link->getNumb(); } while ($link = $list->next()); // 1-4-2 /* rewind() resets the pointer to the root node of the list */ $link = $list->rewind(); // 3 /* You can also iterate through a list with foreach(). * Override the key() method to enable $key=>$value iteration. */ foreach ($list as $bull) { print $bull->getNumb(); } // 3-1-4-2 /* end() resets the pointer to the last node of the list */ $link = $list->end(); // 2 /* You can iterate backwards through a list with previous() */ do { print $link->getNumb(); } while ($link = $list->previous()); // 2-4-1-3 ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/LinkTester.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/LinkTester.php +++ pear/Structures_LinkedList/tests/LinkTester.php <?php class LinkTester extends Structures_LinkedList_DoubleNode { protected $_my_number; function __construct($num) { $this->_my_number = $num; } function getNumb() { return $this->_my_number; } function setNumb($numb) { $this->_my_number = $numb; } } $tester1 = new LinkTester(1); $tester2 = new LinkTester(2); $tester3 = new LinkTester(3); $tester4 = new LinkTester(4); $tester5 = new LinkTester(5); ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/SingleLinkTester.php?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/SingleLinkTester.php +++ pear/Structures_LinkedList/tests/SingleLinkTester.php <?php class LinkTester extends Structures_LinkedList_SingleNode { protected $_my_number; function __construct($num) { $this->_my_number = $num; } function getNumb() { return $this->_my_number; } function setNumb($numb) { $this->_my_number = $numb; } } $tester1 = new LinkTester(1); $tester2 = new LinkTester(2); $tester3 = new LinkTester(3); $tester4 = new LinkTester(4); $tester5 = new LinkTester(5); ?> http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_001.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_001.phpt +++ pear/Structures_LinkedList/tests/link_001.phpt --TEST-- link_001: Test linked list constructed with an initial link --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->appendNode($tester4); $link = $xyy->current(); print "Current: " . $link->getNumb() . "\n"; print "While: "; // test iteration with while() do { print $link->getNumb(); } while ($link = $xyy->next()); print "\nRewind: "; $link = $xyy->rewind(); print $link->getNumb(); print "\n"; print "Foreach: "; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } print "\nEnd: "; $link = $xyy->end(); print $link->getNumb(); print "\n"; print "While (in reverse): "; // test iteration with while() do { print $link->getNumb(); } while ($link = $xyy->previous()); ?> --EXPECT-- Current: 1 While: 1234 Rewind: 1 Foreach: 1234 End: 4 While (in reverse): 4321 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_002.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_002.phpt +++ pear/Structures_LinkedList/tests/link_002.phpt --TEST-- link_002: Append links to an initially empty linked list --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double(); $xyy->appendNode($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->insertNode($tester4, $tester2, true); $link = $xyy->current(); print $link->getNumb(); // test iteration with while() while ($link = $xyy->next()) { print $link->getNumb(); } $link = $xyy->rewind(); print "\n"; print $link->getNumb(); print "\n"; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- 1423 1 1423 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_003.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_003.phpt +++ pear/Structures_LinkedList/tests/link_003.phpt --TEST-- link_003: Append links in a specific order --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double(); // add initial link in the list $xyy->appendNode($tester1); print $tester1->getNumb() . "\n"; // add after initial link $xyy->appendNode($tester2); print $tester2->getNumb() . "\n"; // add after initial link, bumping #2 up $xyy->insertNode($tester3, $tester1); print $tester3->getNumb() . "\n"; // add after link #3, bumping #2 up again $xyy->insertNode($tester4, $tester3); print $tester4->getNumb() . "\n"; print "\n"; $link = $xyy->current(); print $link->getNumb(); print "\n"; // test iteration with while() while ($link = $xyy->next()) { print $link->getNumb(); print "\n"; } $link = $xyy->rewind(); print "\n"; print $link->getNumb(); print "\n"; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- 1 2 3 4 1 3 4 2 1 1342 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_004.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_004.phpt +++ pear/Structures_LinkedList/tests/link_004.phpt --TEST-- link_004: Delete every link in the list --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->appendNode($tester4); // Delete all nodes from the list while ($link = $xyy->rewind()) { print "Deleted " . $link->getNumb() . "\n"; $xyy->deleteNode($link); } $link = $xyy->rewind(); print "Done\n"; ?> --EXPECT-- Deleted 1 Deleted 2 Deleted 3 Deleted 4 Done http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_005.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_005.phpt +++ pear/Structures_LinkedList/tests/link_005.phpt --TEST-- link_005: Pass different class types to constructors --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; class TesterExtend extends Structures_LinkedList_DoubleNode { protected $_my_number; function __construct($tester, $num) { $this->_my_number = $num; parent::__construct($tester); } } class TesterFail { protected $summary; protected $fulltext; function __construct($summary, $fulltext) { $this->summary = $summary; $this->fulltext = $fulltext; } } // This should work: TesterExtend extends the Structure_LinkedList_DoubleNode class $tester_extend = new TesterExtend(null, 1); $xyy = new Structures_LinkedList_Double($tester_extend); print "Checking for errors in the expected success case:\n"; // This should fail print "Checking for errors in the expected failure case:\n"; $tester_fail = new TesterFail(null, 1); $xyy_fail = new Structures_LinkedList_Double($tester_fail); ?> --EXPECTF-- Checking for errors in the expected success case: Checking for errors in the expected failure case: %satal error: Argument 1 passed to Structures_LinkedList_Double::__construct() must be an instance of Structures_LinkedList_DoubleNode%s, called in %slink_005.php on line %d and defined in %sDouble.php on line %d http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_006.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_006.phpt +++ pear/Structures_LinkedList/tests/link_006.phpt --TEST-- link_006: Corner case: add a node before the root --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double(); $xyy->prependNode($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->insertNode($tester4, $tester2, true); // Ensure we can increment the current node without messing up the list print "\nCurrent: " . $xyy->current()->getNumb() . "\n"; $link = $xyy->next(); print "Current: " . $link->getNumb() . "\n"; $xyy->insertNode($tester5, $tester1, true); print "\n"; $link = $xyy->current(); print "Current: " . $link->getNumb(); print "\n\nWhile: "; // test iteration with while() $link = $xyy->rewind(); do { print $link->getNumb(); } while ($link = $xyy->next()); print "\n\nForeach: "; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- Current: 1 Current: 4 Current: 4 While: 51423 Foreach: 51423 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/link_007.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/link_007.phpt +++ pear/Structures_LinkedList/tests/link_007.phpt --TEST-- link_007: Test many corner cases --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Double.php'; require 'LinkTester.php'; $xyy = new Structures_LinkedList_Double(); // prepend to an empty list corner case: 1 $xyy->prependNode($tester1); print "Current: {$xyy->current()->getNumb()}\n"; // prepend to a list with only one element: 21 $xyy->prependNode($tester2); print "Current: {$xyy->current()->getNumb()}\n"; // insert after tail node corner case: 213 $xyy->insertNode($tester3, $tester1); print "Current: {$xyy->current()->getNumb()}\n"; // insert before root node corner case: 4213 $xyy->insertNode($tester4, $tester2, true); print "Current: {$xyy->current()->getNumb()}\n"; // insert before tail node corner case: 42153 $xyy->insertNode($tester5, $tester3, true); print "Current: {$xyy->current()->getNumb()}\n"; print "Foreach: "; foreach ($xyy as $node) { print $node->getNumb(); } print "\nWhile (in reverse): "; // test reverse iteration with while() $link = $xyy->end(); do { print $link->getNumb(); } while ($link = $xyy->previous()); ?> --EXPECT-- Current: 1 Current: 1 Current: 1 Current: 1 Current: 1 Foreach: 42153 While (in reverse): 35124 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/run-tests.log?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/run-tests.log +++ pear/Structures_LinkedList/tests/run-tests.log TOTAL TIME: 00:03 11 PASSED TESTS 0 SKIPPED TESTS 3 FAILED TESTS: /home/dan/svn/Structures/LinkedList/tests/single_link_002.phpt /home/dan/svn/Structures/LinkedList/tests/single_link_006.phpt /home/dan/svn/Structures/LinkedList/tests/single_link_007.phpt http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_001.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_001.phpt +++ pear/Structures_LinkedList/tests/single_link_001.phpt --TEST-- single_link_001: Test linked list constructed with an initial link --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->appendNode($tester4); $link = $xyy->current(); print "Current: " . $link->getNumb() . "\n"; print "While: "; // test iteration with while() do { print $link->getNumb(); } while ($link = $xyy->next()); print "\nRewind: "; $link = $xyy->rewind(); print $link->getNumb(); print "\n"; print "Foreach: "; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } print "\nEnd: "; $link = $xyy->end(); print $link->getNumb(); print "\n"; print "While (in reverse): "; // test iteration with while() do { print $link->getNumb(); } while ($link = $xyy->previous()); ?> --EXPECT-- Current: 1 While: 1234 Rewind: 1 Foreach: 1234 End: 4 While (in reverse): 4321 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_002.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_002.phpt +++ pear/Structures_LinkedList/tests/single_link_002.phpt --TEST-- single_link_002: Append links to an initially empty linked list --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single(); $xyy->appendNode($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->insertNode($tester4, $tester2, true); $link = $xyy->current(); print $link->getNumb(); // test iteration with while() while ($link = $xyy->next()) { print $link->getNumb(); } $link = $xyy->rewind(); print "\n"; print $link->getNumb(); print "\n"; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- 1423 1 1423 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_003.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_003.phpt +++ pear/Structures_LinkedList/tests/single_link_003.phpt --TEST-- single_link_003: Append links in a specific order --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single(); // add initial link in the list $xyy->appendNode($tester1); print $tester1->getNumb() . "\n"; // add after initial link $xyy->appendNode($tester2); print $tester2->getNumb() . "\n"; // add after initial link, bumping #2 up $xyy->insertNode($tester3, $tester1); print $tester3->getNumb() . "\n"; // add after link #3, bumping #2 up again $xyy->insertNode($tester4, $tester3); print $tester4->getNumb() . "\n"; print "\n"; $link = $xyy->current(); print $link->getNumb(); print "\n"; // test iteration with while() while ($link = $xyy->next()) { print $link->getNumb(); print "\n"; } $link = $xyy->rewind(); print "\n"; print $link->getNumb(); print "\n"; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- 1 2 3 4 1 3 4 2 1 1342 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_004.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_004.phpt +++ pear/Structures_LinkedList/tests/single_link_004.phpt --TEST-- single_link_004: Delete every link in the list --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->appendNode($tester4); // Delete all nodes from the list while ($link = $xyy->rewind()) { print "Deleted " . $link->getNumb() . "\n"; $xyy->deleteNode($link); } $link = $xyy->rewind(); print "Done\n"; ?> --EXPECT-- Deleted 1 Deleted 2 Deleted 3 Deleted 4 Done http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_005.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_005.phpt +++ pear/Structures_LinkedList/tests/single_link_005.phpt --TEST-- single_link_005: Pass different class types to constructors --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; class TesterExtend extends Structures_LinkedList_SingleNode { protected $_my_number; function __construct($tester, $num) { $this->_my_number = $num; parent::__construct($tester); } } class TesterFail { protected $summary; protected $fulltext; function __construct($summary, $fulltext) { $this->summary = $summary; $this->fulltext = $fulltext; } } // This should work: TesterExtend extends the Structure_LinkedList_SingleNode class $tester_extend = new TesterExtend(null, 1); $xyy = new Structures_LinkedList_Single($tester_extend); print "Checking for errors in the expected success case:\n"; // This should fail print "Checking for errors in the expected failure case:\n"; $tester_fail = new TesterFail(null, 1); $xyy_fail = new Structures_LinkedList_Single($tester_fail); ?> --EXPECTF-- Checking for errors in the expected success case: Checking for errors in the expected failure case: %satal error: Argument 1 passed to Structures_LinkedList_Single::__construct() must be an instance of Structures_LinkedList_SingleNode%s, called in %slink_005.php on line %d and defined in %sSingle.php on line %d http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_006.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_006.phpt +++ pear/Structures_LinkedList/tests/single_link_006.phpt --TEST-- single_link_006: Corner case: add a node before the root --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single(); $xyy->prependNode($tester1); $xyy->appendNode($tester2); $xyy->appendNode($tester3); $xyy->insertNode($tester4, $tester2, true); // Ensure we can increment the current node without messing up the list print "\nCurrent: " . $xyy->current()->getNumb() . "\n"; $link = $xyy->next(); print "Current: " . $link->getNumb() . "\n"; $xyy->insertNode($tester5, $tester1, true); print "\n"; $link = $xyy->current(); print "Current: " . $link->getNumb(); print "\n\nWhile: "; // test iteration with while() $link = $xyy->rewind(); do { print $link->getNumb(); } while ($link = $xyy->next()); print "\n\nForeach: "; // test foreach() iteration foreach ($xyy as $bull) { print $bull->getNumb(); } ?> --EXPECT-- Current: 1 Current: 4 Current: 4 While: 51423 Foreach: 51423 http://cvs.php.net/viewvc.cgi/pear/Structures_LinkedList/tests/single_link_007.phpt?view=markup&rev=1.1 Index: pear/Structures_LinkedList/tests/single_link_007.phpt +++ pear/Structures_LinkedList/tests/single_link_007.phpt --TEST-- single_link_007: Test many corner cases --FILE-- <?php $dir = dirname(__FILE__); require $dir . '/../' . 'Single.php'; require 'SingleLinkTester.php'; $xyy = new Structures_LinkedList_Single(); // prepend to an empty list corner case: 1 $xyy->prependNode($tester1); print "Current: {$xyy->current()->getNumb()}\n"; // prepend to a list with only one element: 21 $xyy->prependNode($tester2); print "Current: {$xyy->current()->getNumb()}\n"; // insert after tail node corner case: 213 $xyy->insertNode($tester3, $tester1); print "Current: {$xyy->current()->getNumb()}\n"; // insert before root node corner case: 4213 $xyy->insertNode($tester4, $tester2, true); print "Current: {$xyy->current()->getNumb()}\n"; // insert before tail node corner case: 42153 $xyy->insertNode($tester5, $tester3, true); print "Current: {$xyy->current()->getNumb()}\n"; print "Foreach: "; foreach ($xyy as $node) { print $node->getNumb(); } print "\nWhile (in reverse): "; // test reverse iteration with while() $link = $xyy->end(); do { print $link->getNumb(); } while ($link = $xyy->previous()); ?> --EXPECT-- Current: 1 Current: 1 Current: 1 Current: 1 Current: 1 Foreach: 42153 While (in reverse): 35124