Bearsampp
2026.9.23
Toggle main menu visibility
Loading...
Searching...
No Matches
Graph.php
Go to the documentation of this file.
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
//
31
32
/* dependencies {{{ */
33
require_once
'PEAR.php'
;
34
require_once
'Structures/Graph/Node.php'
;
35
/* }}} */
36
37
define(
'STRUCTURES_GRAPH_ERROR_GENERIC'
, 100);
38
39
/* class Structures_Graph {{{ */
52
/* }}} */
53
class
Structures_Graph
54
{
59
var
$_nodes
= array();
60
65
var
$_directed
=
false
;
66
67
74
public
function
__construct
($directed =
true
)
75
{
76
$this->_directed = $directed;
77
}
78
87
public
function
Structures_Graph
($directed =
true
)
88
{
89
$this->
__construct
($directed);
90
}
91
97
public
function
isDirected
()
98
{
99
return
(
boolean
) $this->_directed;
100
}
101
109
public
function
addNode
(&$newNode)
110
{
111
// We only add nodes
112
if
(!is_a($newNode,
'Structures_Graph_Node'
)) {
113
return
Pear::raiseError(
114
'Structures_Graph::addNode received an object that is not'
115
.
' a Structures_Graph_Node'
,
116
STRUCTURES_GRAPH_ERROR_GENERIC
117
);
118
}
119
120
//Graphs are node *sets*, so duplicates are forbidden.
121
// We allow nodes that are exactly equal, but disallow equal references.
122
foreach
($this->_nodes as $key => $node) {
123
/*
124
ZE1 equality operators choke on the recursive cycle introduced
125
by the _graph field in the Node object.
126
So, we'll check references the hard way
127
(change $this->_nodes[$key] and check if the change reflects in
128
$node)
129
*/
130
$savedData = $this->_nodes[$key];
131
$referenceIsEqualFlag =
false
;
132
$this->_nodes[$key] =
true
;
133
if
($node ===
true
) {
134
$this->_nodes[$key] =
false
;
135
if
($node ===
false
) {
136
$referenceIsEqualFlag =
true
;
137
}
138
}
139
$this->_nodes[$key] = $savedData;
140
if
($referenceIsEqualFlag) {
141
return
Pear::raiseError(
142
'Structures_Graph::addNode received an object that is'
143
.
' a duplicate for this dataset'
,
144
STRUCTURES_GRAPH_ERROR_GENERIC
145
);
146
}
147
}
148
$this->_nodes[] =& $newNode;
149
$newNode->setGraph($this);
150
}
151
160
public
function
removeNode
(&$node)
161
{
162
}
163
171
public
function
&
getNodes
()
172
{
173
return
$this->_nodes;
174
}
175
}
176
?>
STRUCTURES_GRAPH_ERROR_GENERIC
const STRUCTURES_GRAPH_ERROR_GENERIC
Definition
Graph.php:37
Structures_Graph\addNode
addNode(&$newNode)
Definition
Graph.php:109
Structures_Graph\isDirected
isDirected()
Definition
Graph.php:97
Structures_Graph\$_nodes
$_nodes
Definition
Graph.php:59
Structures_Graph\removeNode
removeNode(&$node)
Definition
Graph.php:160
Structures_Graph\Structures_Graph
Structures_Graph($directed=true)
Definition
Graph.php:87
Structures_Graph\getNodes
& getNodes()
Definition
Graph.php:171
Structures_Graph\__construct
__construct($directed=true)
Definition
Graph.php:74
Structures_Graph\$_directed
$_directed
Definition
Graph.php:65
Structures_Graph
sandbox
core
libs
php
pear
pear
Structures
Graph.php
Generated by
1.17.0