Structures_Graph
[ class tree: Structures_Graph ] [ index: Structures_Graph ] [ all elements ]

Source for file Graph.php

Documentation is available at Graph.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
  3. // +-----------------------------------------------------------------------------+
  4. // | Copyright (c) 2003 Sérgio Gonçalves Carvalho                                |
  5. // +-----------------------------------------------------------------------------+
  6. // | This file is part of Structures_Graph.                                      |
  7. // |                                                                             |
  8. // | Structures_Graph is free software; you can redistribute it and/or modify    |
  9. // | it under the terms of the GNU Lesser General Public License as published by |
  10. // | the Free Software Foundation; either version 2.1 of the License, or         |
  11. // | (at your option) any later version.                                         |
  12. // |                                                                             |
  13. // | Structures_Graph is distributed in the hope that it will be useful,         |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
  16. // | GNU Lesser General Public License for more details.                         |
  17. // |                                                                             |
  18. // | You should have received a copy of the GNU Lesser General Public License    |
  19. // | along with Structures_Graph; if not, write to the Free Software             |
  20. // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                    |
  21. // | 02111-1307 USA                                                              |
  22. // +-----------------------------------------------------------------------------+
  23. // | Author: Sérgio Carvalho <sergio.carvalho@portugalmail.com>                  |
  24. // +-----------------------------------------------------------------------------+
  25. //
  26. /**
  27.  * The Graph.php file contains the definition of the Structures_Graph class
  28.  *
  29.  * @see Structures_Graph
  30.  * @package Structures_Graph
  31.  */
  32.  
  33. /* dependencies {{{ */
  34. /** PEAR base classes */
  35. require_once 'PEAR.php';
  36. /** Graph Node */
  37. require_once 'Structures/Graph/Node.php';
  38. /* }}} */
  39.  
  40. define('STRUCTURES_GRAPH_ERROR_GENERIC'100);
  41.  
  42. /* class Structures_Graph {{{ */
  43. /**
  44.  * The Structures_Graph class represents a graph data structure.
  45.  *
  46.  * A Graph is a data structure composed by a set of nodes, connected by arcs.
  47.  * Graphs may either be directed or undirected. In a directed graph, arcs are
  48.  * directional, and can be traveled only one way. In an undirected graph, arcs
  49.  * are bidirectional, and can be traveled both ways.
  50.  *
  51.  * @author        Sérgio Carvalho <sergio.carvalho@portugalmail.com>
  52.  * @copyright    (c) 2004 by Sérgio Carvalho
  53.  * @package Structures_Graph
  54.  */
  55. /* }}} */
  56.     /* fields {{{ */
  57.     /**
  58.      * @access private
  59.      */
  60.     var $_nodes = array();
  61.     /**
  62.      * @access private
  63.      */
  64.     var $_directed = false;
  65.     /* }}} */
  66.  
  67.     /* Constructor {{{ */
  68.     /**
  69.     *
  70.     * Constructor
  71.     *
  72.     * @param    boolean    Set to true if the graph is directed. Set to false if it is not directed. (Optional, defaults to true)
  73.     * @access    public
  74.     */
  75.     function Structures_Graph($directed = true{
  76.         $this->_directed $directed;
  77.     }
  78.     /* }}} */
  79.  
  80.     /* isDirected {{{ */
  81.     /**
  82.     *
  83.     * Return true if a graph is directed
  84.     *
  85.     * @return    boolean     true if the graph is directed
  86.     * @access    public
  87.     */
  88.     function isDirected({
  89.         return (boolean) $this->_directed;
  90.     }
  91.     /* }}} */
  92.  
  93.     /* addNode {{{ */
  94.     /**
  95.     *
  96.     * Add a Node to the Graph
  97.     *
  98.     * @param    Structures_Graph_Node   The node to be added.
  99.     * @access    public
  100.     */
  101.     function addNode(&$newNode{
  102.         // We only add nodes
  103.         if (!is_a($newNode'Structures_Graph_Node')) return Pear::raiseError('Structures_Graph::addNode received an object that is not a Structures_Graph_Node'STRUCTURES_GRAPH_ERROR_GENERIC);
  104.         // Graphs are node *sets*, so duplicates are forbidden. We allow nodes that are exactly equal, but disallow equal references.
  105.         foreach($this->_nodes as $key => $node{
  106.             /*
  107.              ZE1 equality operators choke on the recursive cycle introduced by the _graph field in the Node object.
  108.              So, we'll check references the hard way (change $this->_nodes[$key] and check if the change reflects in 
  109.              $node)
  110.             */
  111.             $savedData $this->_nodes[$key];
  112.             $referenceIsEqualFlag = false;
  113.             $this->_nodes[$key= true;
  114.             if ($node === true{
  115.                 $this->_nodes[$key= false;
  116.                 if ($node === false$referenceIsEqualFlag = true;
  117.             }
  118.             $this->_nodes[$key$savedData;
  119.             if ($referenceIsEqualFlagreturn Pear::raiseError('Structures_Graph::addNode received an object that is a duplicate for this dataset'STRUCTURES_GRAPH_ERROR_GENERIC);
  120.         }
  121.         $this->_nodes[=$newNode;
  122.         $newNode->setGraph($this);
  123.     }
  124.     /* }}} */
  125.  
  126.     /* removeNode (unimplemented) {{{ */
  127.     /**
  128.     *
  129.     * Remove a Node from the Graph
  130.     *
  131.     * @todo     This is unimplemented
  132.     * @param    Structures_Graph_Node   The node to be removed from the graph
  133.     * @access    public
  134.     */
  135.     function removeNode(&$node{
  136.     }
  137.     /* }}} */
  138.  
  139.     /* getNodes {{{ */
  140.     /**
  141.     *
  142.     * Return the node set, in no particular order. For ordered node sets, use a Graph Manipulator insted.
  143.     *
  144.     * @access   public
  145.     * @see      Structures_Graph_Manipulator_TopologicalSorter
  146.     * @return   array The set of nodes in this graph
  147.     */
  148.     function &getNodes({
  149.         return $this->_nodes;
  150.     }
  151.     /* }}} */
  152. }
  153. ?>

Documentation generated on Thu, 08 May 2008 18:00:05 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.