Bearsampp 2026.9.23
Loading...
Searching...
No Matches
Node.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//
32
33/* dependencies {{{ */
35require_once 'PEAR.php';
37require_once 'Structures/Graph.php';
38/* }}} */
39
40/* class Structures_Graph_Node {{{ */
56/* }}} */
58 /* fields {{{ */
62 var $_data = null;
64 var $_metadata = array();
66 var $_arcs = array();
68 var $_graph = null;
69 /* }}} */
70
71 /* Constructor {{{ */
78 function __construct() {
79 }
80 /* }}} */
81
82 /* getGraph {{{ */
90 function &getGraph() {
91 return $this->_graph;
92 }
93 /* }}} */
94
95 /* setGraph {{{ */
104 function setGraph(&$graph) {
105 $this->_graph =& $graph;
106 }
107 /* }}} */
108
109 /* getData {{{ */
119 function &getData() {
120 return $this->_data;
121 }
122 /* }}} */
123
124 /* setData {{{ */
134 function setData(&$data) {
135 $this->_data =& $data;
136 }
137 /* }}} */
138
139 /* metadataKeyExists {{{ */
151 function metadataKeyExists($key) {
152 return array_key_exists($key, $this->_metadata);
153 }
154 /* }}} */
155
156 /* getMetadata {{{ */
171 function &getMetadata($key, $nullIfNonexistent = false) {
172 if (array_key_exists($key, $this->_metadata)) {
173 return $this->_metadata[$key];
174 } else {
175 if ($nullIfNonexistent) {
176 $a = null;
177 return $a;
178 } else {
179 $a = Pear::raiseError('Structures_Graph_Node::getMetadata: Requested key does not exist', STRUCTURES_GRAPH_ERROR_GENERIC);
180 return $a;
181 }
182 }
183 }
184 /* }}} */
185
186 /* unsetMetadata {{{ */
198 function unsetMetadata($key) {
199 if (array_key_exists($key, $this->_metadata)) unset($this->_metadata[$key]);
200 }
201 /* }}} */
202
203 /* setMetadata {{{ */
216 function setMetadata($key, &$data) {
217 $this->_metadata[$key] =& $data;
218 }
219 /* }}} */
220
221 /* _connectTo {{{ */
223 function _connectTo(&$destinationNode) {
224 $this->_arcs[] =& $destinationNode;
225 }
226 /* }}} */
227
228 /* connectTo {{{ */
238 function connectTo(&$destinationNode) {
239 // We only connect to nodes
240 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);
241 // Nodes must already be in graphs to be connected
242 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);
243 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);
244 // Connect here
245 $this->_connectTo($destinationNode);
246 // If graph is undirected, connect back
247 if (!$this->_graph->isDirected()) {
248 $destinationNode->_connectTo($this);
249 }
250 }
251 /* }}} */
252
253 /* getNeighbours {{{ */
261 function getNeighbours() {
262 return $this->_arcs;
263 }
264 /* }}} */
265
266 /* connectsTo {{{ */
274 function connectsTo(&$target) {
275 if (version_compare(PHP_VERSION, '5.0.0') >= 0) {
276 return in_array($target, $this->getNeighbours(), true);
277 }
278
279 $copy = $target;
280 $arcKeys = array_keys($this->_arcs);
281 foreach($arcKeys as $key) {
282 /* ZE1 chokes on this expression:
283 if ($target === $arc) return true;
284 so, we'll use more convoluted stuff
285 */
286 $arc =& $this->_arcs[$key];
287 $target = true;
288 if ($arc === true) {
289 $target = false;
290 if ($arc === false) {
291 $target = $copy;
292 return true;
293 }
294 }
295 }
296 $target = $copy;
297 return false;
298 }
299 /* }}} */
300
301 /* inDegree {{{ */
312 function inDegree() {
313 if ($this->_graph == null) return 0;
314 if (!$this->_graph->isDirected()) return $this->outDegree();
315 $result = 0;
316 $graphNodes =& $this->_graph->getNodes();
317 foreach (array_keys($graphNodes) as $key) {
318 if ($graphNodes[$key]->connectsTo($this)) $result++;
319 }
320 return $result;
321
322 }
323 /* }}} */
324
325 /* outDegree {{{ */
336 function outDegree() {
337 if ($this->_graph == null) return 0;
338 return sizeof($this->_arcs);
339 }
340 /* }}} */
341}
342?>
const STRUCTURES_GRAPH_ERROR_GENERIC
Definition Graph.php:37
$result
& getMetadata($key, $nullIfNonexistent=false)
Definition Node.php:171
_connectTo(&$destinationNode)
Definition Node.php:223
connectsTo(&$target)
Definition Node.php:274
metadataKeyExists($key)
Definition Node.php:151
setMetadata($key, &$data)
Definition Node.php:216
connectTo(&$destinationNode)
Definition Node.php:238
setGraph(&$graph)
Definition Node.php:104