1 pnkfelix 1.1.2.1 // SparseNode.java, created Wed Jan 13 16:17:46 1999 by pnkfelix
  2 cananian 1.1.2.9 // Copyright (C) 1998 Felix S. Klock II <pnkfelix@mit.edu>
  3 cananian 1.1.2.7 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 pnkfelix 1.1.2.1 package harpoon.Analysis.GraphColoring;
  5 pnkfelix 1.1.2.1 
  6 cananian 1.3     import net.cscott.jutil.UniqueVector;
  7 pnkfelix 1.1.2.1 import java.util.Enumeration;
  8 pnkfelix 1.1.2.1 
  9 pnkfelix 1.1.2.1 /**
 10 pnkfelix 1.1.2.5  * <code>SparseNode</code> is an extension of a ColorableNode for
 11 pnkfelix 1.1.2.1  * use with the SparseGraph object.
 12 pnkfelix 1.1.2.1  * 
 13 cananian 1.1.2.9  * @author  Felix S. Klock II <pnkfelix@mit.edu>
 14 cananian 1.3      * @version $Id: SparseNode.java,v 1.3 2004/02/08 01:52:03 cananian Exp $ 
 15 pnkfelix 1.1.2.1  */
 16 pnkfelix 1.1.2.1 
 17 pnkfelix 1.1.2.5 public abstract class SparseNode extends ColorableNode {
 18 pnkfelix 1.1.2.1     
 19 pnkfelix 1.1.2.1     private UniqueVector toNodes;
 20 pnkfelix 1.1.2.1     private UniqueVector fromNodes;
 21 pnkfelix 1.1.2.1 
 22 pnkfelix 1.1.2.1     // unifiedNodes tracks the union of the TO and FROM set, to
 23 pnkfelix 1.1.2.1     // simplify the getDegree() method
 24 pnkfelix 1.1.2.8     UniqueVector unifiedNodes;
 25 pnkfelix 1.1.2.1 
 26 pnkfelix 1.1.2.1     /** Creates a <code>SparseNode</code>. */
 27 pnkfelix 1.1.2.1     public SparseNode() {
 28 pnkfelix 1.1.2.1         super();
 29 pnkfelix 1.1.2.1         toNodes = new UniqueVector();
 30 pnkfelix 1.1.2.1         fromNodes = new UniqueVector();
 31 pnkfelix 1.1.2.1         unifiedNodes = new UniqueVector();
 32 pnkfelix 1.1.2.1     }
 33 pnkfelix 1.1.2.1 
 34 pnkfelix 1.1.2.6     /** Adds an edge from <code>this</code> to <code>to</code>
 35 pnkfelix 1.1.2.8         <BR> <B>requires:</B> 
 36 pnkfelix 1.1.2.8              1. <code>this</code> is allowed to be modified. 
 37 pnkfelix 1.1.2.8              2. <code>this</code> does not have an edge to <code>to</code>
 38 pnkfelix 1.1.2.6         <BR> <B>modifies:</B> this
 39 pnkfelix 1.1.2.6         <BR> <B>effects:</B> Adds an edge from <code>this</code> to
 40 pnkfelix 1.1.2.6                              <code>to</code>  
 41 pnkfelix 1.1.2.1      */
 42 pnkfelix 1.1.2.6     void addEdgeTo( SparseNode to ) {
 43 pnkfelix 1.1.2.6         // modifies: this.toNodes, this.unifiedNodes
 44 pnkfelix 1.1.2.2         if (toNodes.contains( to )) {
 45 pnkfelix 1.1.2.2             throw new IllegalEdgeException
 46 pnkfelix 1.1.2.2                 ("SparseNode does not allow duplicate edges");
 47 pnkfelix 1.1.2.2         }
 48 pnkfelix 1.1.2.2 
 49 pnkfelix 1.1.2.1         if (this.isModifiable()) {
 50 pnkfelix 1.1.2.1             toNodes.addElement( to );
 51 pnkfelix 1.1.2.1             unifiedNodes.addElement( to );
 52 pnkfelix 1.1.2.3         } else {
 53 pnkfelix 1.1.2.3             throw new ObjectNotModifiableException
 54 pnkfelix 1.1.2.3                 (this + " is not allowed to be modified.");
 55 pnkfelix 1.1.2.1         }
 56 pnkfelix 1.1.2.1     }
 57 pnkfelix 1.1.2.1 
 58 pnkfelix 1.1.2.1     /** Adds an edge from <code>from</code> to <code>this</code>.
 59 pnkfelix 1.1.2.8         <BR> <B>requires:</B> 
 60 pnkfelix 1.1.2.8              1. <code>this</code> is allowed to be modified
 61 pnkfelix 1.1.2.8              2. <code>this</code> does not have an edge from <code>from</code>
 62 pnkfelix 1.1.2.6         <BR> <B>modifies:</B> <code>this</code>
 63 pnkfelix 1.1.2.6         <BR> <B>effects:</B> Adds an edge from <code>from</code> to <code>this</code>
 64 pnkfelix 1.1.2.1      */ 
 65 pnkfelix 1.1.2.8     void addEdgeFrom( SparseNode from ) throws IllegalEdgeException {
 66 pnkfelix 1.1.2.6         // modifies: this.fromNodes, this.unifiedNodes
 67 pnkfelix 1.1.2.2         if (fromNodes.contains( from )) {
 68 pnkfelix 1.1.2.2             throw new IllegalEdgeException
 69 pnkfelix 1.1.2.2                 ("SparseNode does not allow duplicate edges");
 70 pnkfelix 1.1.2.2         }
 71 pnkfelix 1.1.2.2 
 72 pnkfelix 1.1.2.1         if (this.isModifiable()) {
 73 pnkfelix 1.1.2.1             fromNodes.addElement( from );
 74 pnkfelix 1.1.2.1             unifiedNodes.addElement( from );
 75 pnkfelix 1.1.2.3         } else {
 76 pnkfelix 1.1.2.3             throw new ObjectNotModifiableException
 77 pnkfelix 1.1.2.3                 (this + " is not allowed to be modified.");
 78 pnkfelix 1.1.2.1         }
 79 pnkfelix 1.1.2.1     }
 80 pnkfelix 1.1.2.1 
 81 pnkfelix 1.1.2.1     /** Removes an edge from <code>from</code> to <code>this</code>.
 82 pnkfelix 1.1.2.6         <BR> <B>requires:</B> <code>this</code> is allowed to be
 83 pnkfelix 1.1.2.6                               modified and an edge exists from
 84 pnkfelix 1.1.2.6                               <code>from</code> to <code>this</code>
 85 pnkfelix 1.1.2.6         <BR> <B>modifies:</B> <code>this</code>
 86 pnkfelix 1.1.2.6         <BR> <B>effects:</B> Removes the edge from <code>from</code>
 87 pnkfelix 1.1.2.6                              to <code>this</code> 
 88 pnkfelix 1.1.2.1     */
 89 pnkfelix 1.1.2.1     void removeEdgeFrom( SparseNode from ) 
 90 pnkfelix 1.1.2.3         throws EdgeNotPresentException, ObjectNotModifiableException {
 91 pnkfelix 1.1.2.6         // modifies: this.fromNodes, this.unifiedNodes
 92 pnkfelix 1.1.2.1         if (this.isModifiable()) {
 93 pnkfelix 1.1.2.1             if (! fromNodes.removeElement( from )) {
 94 pnkfelix 1.1.2.1                 throw new EdgeNotPresentException
 95 pnkfelix 1.1.2.1                     ("No Edge from " + from +" to " + this);
 96 pnkfelix 1.1.2.1             } else {
 97 pnkfelix 1.1.2.1                 if ( ! toNodes.contains( from ) ) {
 98 pnkfelix 1.1.2.1                     unifiedNodes.removeElement( from );
 99 pnkfelix 1.1.2.1                 }
100 pnkfelix 1.1.2.1             }
101 pnkfelix 1.1.2.3         } else {
102 pnkfelix 1.1.2.3             throw new ObjectNotModifiableException
103 pnkfelix 1.1.2.3                 (this + " is not allowed to be modified.");
104 pnkfelix 1.1.2.1         }
105 pnkfelix 1.1.2.1     }
106 pnkfelix 1.1.2.1 
107 pnkfelix 1.1.2.1     /** Removes an edge from <code>this</code> to <code>to</code>.
108 pnkfelix 1.1.2.6         <BR> <B>requires:</B> <code>this</code> is allowed to be
109 pnkfelix 1.1.2.6                               modified, and an edge exists from
110 pnkfelix 1.1.2.6                               <code>this</code> to <code>to</code>
111 pnkfelix 1.1.2.6         <BR> <B>modifies:</B> <code>this</code>
112 pnkfelix 1.1.2.6         <BR> <B>effects:</B> Removes the edge from <code>this</code>
113 pnkfelix 1.1.2.6                              to <code>to</code>. 
114 pnkfelix 1.1.2.6     */
115 pnkfelix 1.1.2.1     void removeEdgeTo( SparseNode to ) 
116 pnkfelix 1.1.2.3         throws EdgeNotPresentException, ObjectNotModifiableException {
117 pnkfelix 1.1.2.6         // modifies: this.toNodes, this.unifiedNodes
118 pnkfelix 1.1.2.6 
119 pnkfelix 1.1.2.1         if (this.isModifiable()) {
120 pnkfelix 1.1.2.1             if (! toNodes.removeElement( to )) {
121 pnkfelix 1.1.2.1                 throw new EdgeNotPresentException
122 pnkfelix 1.1.2.1                     ("No Edge from " + to +" to " + this);
123 pnkfelix 1.1.2.1             } else {
124 pnkfelix 1.1.2.1                 if ( ! fromNodes.contains( to ) ) {
125 pnkfelix 1.1.2.1                     unifiedNodes.removeElement( to );
126 pnkfelix 1.1.2.1                 }
127 pnkfelix 1.1.2.1             }
128 pnkfelix 1.1.2.3         } else {
129 pnkfelix 1.1.2.3             throw new ObjectNotModifiableException
130 pnkfelix 1.1.2.3                 (this + " is not allowed to be modified.");
131 pnkfelix 1.1.2.1         }
132 pnkfelix 1.1.2.1     }
133 pnkfelix 1.1.2.1     
134 pnkfelix 1.1.2.6     /** Returns the degree of <code>this</code>.
135 pnkfelix 1.1.2.6         <BR> <B>effects:</B> Returns the number of other
136 pnkfelix 1.1.2.6                              <code>SparseNode</code>s that
137 pnkfelix 1.1.2.6                              <code>node</code> is connected to.  
138 pnkfelix 1.1.2.6     */
139 pnkfelix 1.1.2.1     int getDegree() {
140 pnkfelix 1.1.2.1         return unifiedNodes.size();
141 pnkfelix 1.1.2.1     }    
142 pnkfelix 1.1.2.1     
143 pnkfelix 1.1.2.1     /** Returns a to-node enumerator.
144 pnkfelix 1.1.2.6         <BR> <B>effects:</B> Returns an <code>Enumeration</code> of
145 pnkfelix 1.1.2.6                              nodes that <code>this</code> has edges
146 pnkfelix 1.1.2.6                              to.
147 pnkfelix 1.1.2.6         <BR> <B>requires:</B> <code>this</code> and the
148 pnkfelix 1.1.2.6                               <code>Node</code>s connected to it are
149 pnkfelix 1.1.2.6                               not modified while the
150 pnkfelix 1.1.2.6                               <code>Enumeration</code> returned is still in use.
151 pnkfelix 1.1.2.1     */
152 pnkfelix 1.1.2.1     Enumeration getToNodes() {
153 pnkfelix 1.1.2.1         return toNodes.elements();
154 pnkfelix 1.1.2.1     }
155 pnkfelix 1.1.2.1 
156 pnkfelix 1.1.2.1     /** Returns a from-node enumerator.
157 pnkfelix 1.1.2.6         <BR> <B>effects:</B> Returns an <code>Enumeration</code> of
158 pnkfelix 1.1.2.6                              nodes that <code>this</code> has edges
159 pnkfelix 1.1.2.6                              from.
160 pnkfelix 1.1.2.6         <BR> <B>requires:</B> <code>this</code> and the
161 pnkfelix 1.1.2.6                               <code>Node</code>s connected to it are
162 pnkfelix 1.1.2.6                               not modified while the
163 pnkfelix 1.1.2.6                               <code>Enumeration</code> returned is still in use.
164 pnkfelix 1.1.2.1     */
165 pnkfelix 1.1.2.1     Enumeration getFromNodes() {
166 pnkfelix 1.1.2.1         return fromNodes.elements();
167 pnkfelix 1.1.2.1     }
168 pnkfelix 1.1.2.1 
169 pnkfelix 1.1.2.1     /** Returns a neighboring-node enumerator.
170 pnkfelix 1.1.2.6         <BR> <B>effects:</B> Returns an <code>Enumeration</code> of
171 pnkfelix 1.1.2.6                              nodes that <code>this</code> has edges to
172 pnkfelix 1.1.2.6                              or from. 
173 pnkfelix 1.1.2.6         <BR> <B>requires:</B> <code>this</code> and the
174 pnkfelix 1.1.2.6                               <code>Node</code>s connected to it are
175 pnkfelix 1.1.2.6                               not modified while the
176 pnkfelix 1.1.2.6                               <code>Enumeration</code> returned is
177 pnkfelix 1.1.2.6                               still in use. 
178 pnkfelix 1.1.2.1     */
179 pnkfelix 1.1.2.1     Enumeration getNeighboringNodes() {
180 pnkfelix 1.1.2.1         return unifiedNodes.elements();
181 pnkfelix 1.1.2.1     }
182 pnkfelix 1.1.2.1 
183 pnkfelix 1.1.2.1 }
184 pnkfelix 1.1.2.1 
185 pnkfelix 1.1.2.1 
186 cananian 1.2