Skip to content

Commit

Permalink
Fixes for bugs:
Browse files Browse the repository at this point in the history
 #2536 Bug in Structures_graph::addNode
 #2545 Changes in error reporting


git-svn-id: http://svn.php.net/repository/pear/packages/Structures_Graph/trunk@202473 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
Sergio Carvalho committed Dec 9, 2005
1 parent d6bb025 commit e82d3d9
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 21 deletions.
12 changes: 5 additions & 7 deletions Structures/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,23 @@ function isDirected() {
*/
function addNode(&$newNode) {
// We only add nodes
if (!is_a($newNode, 'Structures_Graph_Node'))
Pear::raiseError('Structures_Graph::addNode received an object that is not a Structures_Graph_Node', STRUCTURES_GRAPH_ERROR_GENERIC);
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);
// Graphs are node *sets*, so duplicates are forbidden. We allow nodes that are exactly equal, but disallow equal references.
foreach($this->_nodes as $key => $node) {
/*
ZE1 equality operators choke on the recursive cycle introduced by the _graph field in the Node object.
So, we'll check references the hard way
So, we'll check references the hard way (change $this->_nodes[$key] and check if the change reflects in
$node)
*/
$savedData = $this->_nodes[$key];
$referenceIsEqualFlag = false;
$this->_nodes[$key] = true;
if ($node === true) {
$this->_nodes[$key] = false;
if ($node === false)
$referenceIsEqualFlag = true;
if ($node === false) $referenceIsEqualFlag = true;
}
$this->_nodes[$key] = $savedData;
if ($referenceIsEqualFlag)
Pear::raiseError('Structures_Graph::addNode received an object that is a duplicate for this dataset', STRUCTURES_GRAPH_ERROR_GENERIC);
if ($referenceIsEqualFlag) return Pear::raiseError('Structures_Graph::addNode received an object that is a duplicate for this dataset', STRUCTURES_GRAPH_ERROR_GENERIC);
}
$this->_nodes[] =& $newNode;
$newNode->setGraph($this);
Expand Down
3 changes: 1 addition & 2 deletions Structures/Graph/Manipulator/AcyclicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ function _isAcyclic(&$graph) {
*/
function isAcyclic(&$graph) {
// We only test graphs
if (!is_a($graph, 'Structures_Graph'))
Pear::raiseError('Structures_Graph_Manipulator_AcyclicTest::isAcyclic received an object that is not a Structures_Graph', STRUCTURES_GRAPH_ERROR_GENERIC);
if (!is_a($graph, 'Structures_Graph')) return Pear::raiseError('Structures_Graph_Manipulator_AcyclicTest::isAcyclic received an object that is not a Structures_Graph', STRUCTURES_GRAPH_ERROR_GENERIC);
if (!$graph->isDirected()) return false; // Only directed graphs may be acyclic

return Structures_Graph_Manipulator_AcyclicTest::_isAcyclic($graph);
Expand Down
6 changes: 2 additions & 4 deletions Structures/Graph/Manipulator/TopologicalSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ function _sort(&$graph) {
*/
function sort(&$graph) {
// We only sort graphs
if (!is_a($graph, 'Structures_Graph'))
Pear::raiseError('Structures_Graph_Manipulator_TopologicalSorter::sort received an object that is not a Structures_Graph', STRUCTURES_GRAPH_ERROR_GENERIC);
if (!Structures_Graph_Manipulator_AcyclicTest::isAcyclic($graph))
Pear::raiseError('Structures_Graph_Manipulator_TopologicalSorter::sort received an graph that has cycles', STRUCTURES_GRAPH_ERROR_GENERIC);
if (!is_a($graph, 'Structures_Graph')) return Pear::raiseError('Structures_Graph_Manipulator_TopologicalSorter::sort received an object that is not a Structures_Graph', STRUCTURES_GRAPH_ERROR_GENERIC);
if (!Structures_Graph_Manipulator_AcyclicTest::isAcyclic($graph)) return Pear::raiseError('Structures_Graph_Manipulator_TopologicalSorter::sort received an graph that has cycles', STRUCTURES_GRAPH_ERROR_GENERIC);

Structures_Graph_Manipulator_TopologicalSorter::_sort($graph);
$result = array();
Expand Down
11 changes: 4 additions & 7 deletions Structures/Graph/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function &getMetadata($key, $nullIfNonexistent = false) {
if ($nullIfNonexistent) {
return null;
} else {
Pear::raiseError('Structures_Graph_Node::getMetadata: Requested key does not exist', STRUCTURES_GRAPH_ERROR_GENERIC);
return Pear::raiseError('Structures_Graph_Node::getMetadata: Requested key does not exist', STRUCTURES_GRAPH_ERROR_GENERIC);
}
}
}
Expand Down Expand Up @@ -235,13 +235,10 @@ function _connectTo(&$destinationNode) {
*/
function connectTo(&$destinationNode) {
// We only connect to nodes
if (!is_a($destinationNode, 'Structures_Graph_Node'))
Pear::raiseError('Structures_Graph_Node::connectTo received an object that is not a Structures_Graph_Node', STRUCTURES_GRAPH_ERROR_GENERIC);
if (!is_a($destinationNode, 'Structures_Graph_Node')) return Pear::raiseError('Structures_Graph_Node::connectTo received an object that is not a Structures_Graph_Node', STRUCTURES_GRAPH_ERROR_GENERIC);
// Nodes must already be in graphs to be connected
if ($this->_graph == null)
Pear::raiseError('Structures_Graph_Node::connectTo Tried to connect a node that is not in a graph', STRUCTURES_GRAPH_ERROR_GENERIC);
if ($destinationNode->getGraph() == null)
Pear::raiseError('Structures_Graph_Node::connectTo Tried to connect to a node that is not in a graph', STRUCTURES_GRAPH_ERROR_GENERIC);
if ($this->_graph == null) return Pear::raiseError('Structures_Graph_Node::connectTo Tried to connect a node that is not in a graph', STRUCTURES_GRAPH_ERROR_GENERIC);
if ($destinationNode->getGraph() == null) return Pear::raiseError('Structures_Graph_Node::connectTo Tried to connect to a node that is not in a graph', STRUCTURES_GRAPH_ERROR_GENERIC);
// Connect here
$this->_connectTo($destinationNode);
// If graph is undirected, connect back
Expand Down
2 changes: 1 addition & 1 deletion package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ FILESGOHERE
</filelist>
</release>
<deps>
<dep type="pkg" rel="ge" version="1.2">Pear</dep>
<dep type="pkg" rel="ge" version="1.2">PEAR</dep>
</deps>
</package>
EOF
Expand Down

0 comments on commit e82d3d9

Please sign in to comment.