topical media & game development

talk show tell print

lib-as-de-polygonal-ds-GraphNode.ax

lib-as-de-polygonal-ds-GraphNode.ax (swf ) [ flash ] flex


  
Copyright (c) Michael Baczynski 2007 http://lab.polygonal.de/ds/ This software is distributed under licence. Use of this software implies agreement with all terms and conditions of the accompanying software licence.

  
  package de.polygonal.ds
  {
          import de.polygonal.ds.GraphArc;
          
          
A graph node.

  
          public class @ax-lib-as-de-polygonal-ds-GraphNode
          {
                  
The data being referenced.

  
                  public var data:*;
                  
                  
An array of arcs connecting this node to other nodes.

  
                  public var arcs:Array;
                  
                  
A flag indicating whether the node is marked or not. Used for iterating over a graph structure.

  
                  public var marked:Boolean;
                  
                  private var _arcCount:int = 0;
                  
                  
Constructs a new graph node.
parameter: obj The data to store inside the node.

  
                  public function @ax-lib-as-de-polygonal-ds-GraphNode(obj:*)
                  {
                          this.data = obj;
                          arcs = [];
                          _arcCount = 0;
                          marked = false;
                  }
                  
                  
Adds an arc to the current graph node, pointing to a different graph node and with a given weight.
parameter: target The destination node the arc should point to.
parameter: weigth The arc's weigth.

  
                  public function addArc(target:@ax-lib-as-de-polygonal-ds-GraphNode, weight:Number):void
                  {
                          arcs.push(new GraphArc(target, weight));
                          _arcCount++;
                  }
  
                  
Removes the arc that points to the given node.
returns: True if removal was successful, otherwise false.

  
                  public function removeArc(target:@ax-lib-as-de-polygonal-ds-GraphNode):Boolean
                  {
                          for (var i:int = 0; i < _arcCount; i++)
                          {
                                  if (arcs[i].node == target)
                                  {
                                          arcs.splice(i, 1);
                                          _arcCount--;
                                          return true;
                                  }
                          }
                          return false;
                  }
                  
                  
Finds the arc that points to the given node.
parameter: target The destination node.
returns: A GraphArc object or null if the arc doesn't exist.

  
                  public function getArc(target:@ax-lib-as-de-polygonal-ds-GraphNode):GraphArc
                  {
                          for (var i:int = 0 ; i < _arcCount; i++)
                          {
                                  var arc:GraphArc = arcs[i];
                                  if (arc.node == target) return arc;
                          }
                          return null;
                  }
                  
                  
The number of arcs extending from this node.

  
                  public function get numArcs():int
                  {
                          return _arcCount;
                  }
          }
  }


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.