Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
graph_template.cxx
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 //-*-c++-*-
00037 
00059 #include "cxx_graph.h"
00060 #include "graph_template.h"
00061 
00062 #if 0
00063 
00064 template <class EDGE_TYPE, class VERTEX_TYPE>
00065 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::
00066 DIRECTED_GRAPH16( const VINDEX16 vsize, const EINDEX16 esize) {
00067 
00068   //Is_True( vsize >=0, ("Illegal graph size\n"));
00069 
00070   _vmpool = CXX_NEW(MEM_POOL,Malloc_Mem_Pool);
00071   MEM_POOL_Initialize(_vmpool,"vmpool",FALSE);
00072   MEM_POOL_Push(_vmpool);
00073   _v.Set_Mem_Pool(_vmpool);
00074   _v.Alloc_array(vsize+1);
00075   _v.Setidx( 0 );
00076   _vcnt = 0;
00077   _vfree = 0;
00078 
00079   //for (mINT16 i=1; i<=_vcnt; i++) {   // initialization
00080   //  _v[i].Set_Out_Edge(0);
00081   //  _v[i].Set_In_Edge(0);
00082   //}
00083 
00084   _empool = CXX_NEW(MEM_POOL,Malloc_Mem_Pool);
00085   MEM_POOL_Initialize(_empool,"empool",FALSE);
00086   MEM_POOL_Push(_empool);
00087   _e.Set_Mem_Pool(_empool);
00088   _e.Alloc_array(esize+1);
00089   _e.Setidx( 0 );
00090   _ecnt = 0;
00091   _efree = 0;
00092 
00093 }
00094 
00095 template <class EDGE_TYPE, class VERTEX_TYPE>
00096 VINDEX16
00097 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Add_Vertex() {
00098   VINDEX16 new_vertex;
00099 
00100   // Is_True(_vcnt < GRAPH16_CAPACITY, ("Too many vertices\n"));
00101   if (_vcnt == GRAPH16_CAPACITY) return 0;
00102 
00103   if (_vfree == 0) { // grow the _v[] to accept more vertices
00104     new_vertex = _v.Newidx();
00105   } else {
00106     new_vertex = _vfree;
00107     _vfree = _v[_vfree].Get_Next_Free_Vertex();
00108   }
00109 
00110   // reset the in/out edge-lists to NULL
00111   _v[new_vertex].Set_Out_Edge(0);
00112   _v[new_vertex].Set_In_Edge(0);
00113 
00114   _vcnt++;
00115 
00116   return new_vertex;
00117 
00118 }
00119 
00120 template <class EDGE_TYPE, class VERTEX_TYPE>
00121 EINDEX16
00122 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Add_Edge(VINDEX16 from, VINDEX16 to) {
00123   EINDEX16 new_edge;
00124 
00125   // Is_True(_ecnt < GRAPH16_CAPACITY, ("Too many edges\n"));
00126   if (_ecnt == GRAPH16_CAPACITY) return 0;
00127   if (_efree == 0) { // grow the _e[] to accept more edges
00128     new_edge = _e.Newidx();
00129   } else {
00130     new_edge = _efree;
00131     _efree = _e[_efree].Get_Next_Free_Edge();
00132   }
00133   
00134   _e[new_edge].Set_Source(from);
00135   _e[new_edge].Set_Sink(to);
00136 
00137   _ecnt++;
00138 
00139   // insert this edge into the out-edge list of the source vertex 'from'
00140   _e[new_edge].Set_Next_Out_Edge(_v[from].Get_Out_Edge());
00141   _v[from].Set_Out_Edge(new_edge);
00142 
00143   // insert this edge into the in-edge list of the sink vertex 'to'
00144   _e[new_edge].Set_Next_In_Edge(_v[to].Get_In_Edge());
00145   _v[to].Set_In_Edge(new_edge);
00146 
00147   return new_edge;
00148 
00149 }
00150 
00151 template <class EDGE_TYPE, class VERTEX_TYPE>
00152 EINDEX16
00153 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Add_Unique_Edge(VINDEX16 from, VINDEX16 to) {
00154   EINDEX16 new_edge;
00155 
00156   // see if an edge already exists. No multiple edges between the same
00157   // pair of source and sink vertices.
00158   if (new_edge=Get_Edge(from,to)) return new_edge;
00159 
00160   return Add_Edge(from,to);
00161 
00162 }
00163 
00164 template <class EDGE_TYPE, class VERTEX_TYPE>
00165 EINDEX16
00166 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Get_Edge(VINDEX16 from, VINDEX16 to) const {
00167   EINDEX16 e;
00168   e = _v[from].Get_Out_Edge();
00169   while (e != 0) {
00170     if (_e[e].Get_Sink() == to) return e;
00171     e = _e[e].Get_Next_Out_Edge();
00172   }
00173   return 0; // no corresponding edge
00174 }
00175 
00176 template <class EDGE_TYPE, class VERTEX_TYPE>
00177 void
00178 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Delete_Vertex(VINDEX16 v) {
00179 
00180   EINDEX16 e;
00181 
00182   // see if the vertex exists
00183   Is_True (Vertex_Is_In_Graph(v), ("Vertex not in graph\n"));
00184 
00185   while (e = _v[v].Get_In_Edge()) {
00186     Delete_Edge(e);
00187   };
00188   while (e = _v[v].Get_Out_Edge()) {
00189     Delete_Edge(e);
00190   };
00191 
00192   // insert the deleted vertex in free list
00193   _v[v].Set_Next_Free_Vertex(_vfree);
00194   _v[v].Set_To_Free();
00195   _vfree = v;
00196 
00197   _vcnt--;
00198   
00199 }
00200 
00201 template <class EDGE_TYPE, class VERTEX_TYPE>
00202 void
00203 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Delete_Edge(EINDEX16 e) {
00204 
00205   VINDEX16 from, to;
00206   EINDEX16 e1;
00207 
00208   // see if the edge exists
00209   Is_True (Edge_Is_In_Graph(e), ("Edge not in graph\n"));
00210 
00211   from = _e[e].Get_Source();
00212   to = _e[e].Get_Sink();
00213 
00214   if (_v[from].Get_Out_Edge() == e) {  // to delete the first edge in list
00215     _v[from].Set_Out_Edge(_e[e].Get_Next_Out_Edge());
00216   } else {
00217     e1 = _v[from].Get_Out_Edge();
00218     while (_e[e1].Get_Next_Out_Edge() != e) 
00219         e1 = _e[e1].Get_Next_Out_Edge();
00220     _e[e1].Set_Next_Out_Edge(_e[e].Get_Next_Out_Edge());
00221   }
00222 
00223   if (_v[to].Get_In_Edge() == e) {  // to delete the first edge in list
00224     _v[to].Set_In_Edge(_e[e].Get_Next_In_Edge());
00225   } else {
00226     e1 = _v[to].Get_In_Edge();
00227     while (_e[e1].Get_Next_In_Edge() != e) 
00228         e1 = _e[e1].Get_Next_In_Edge();
00229     _e[e1].Set_Next_In_Edge(_e[e].Get_Next_In_Edge());
00230   }
00231 
00232   _e[e].Set_Next_Free_Edge(_efree);  // insert the deleted edge in free list
00233   _e[e].Set_To_Free();
00234   _efree = e;
00235 
00236   _ecnt--;
00237   
00238 }
00239 
00240 template <class EDGE_TYPE, class VERTEX_TYPE>
00241 VINDEX16
00242 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Get_Vertex() const {
00243   VINDEX16 v;
00244 
00245   if (Is_Empty()) return 0;
00246 
00247   v = _v.Lastidx();
00248   while (v>0 &&_v[v].Is_Free() ) v--;   // skip over free vertices
00249   Is_True( v>0 , ("Fail to get vertex\n"));
00250 
00251   return v;
00252 }
00253 
00254 template <class EDGE_TYPE, class VERTEX_TYPE>
00255 VINDEX16
00256 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Get_Next_Vertex(VINDEX16 v) const {
00257 
00258   Is_True( Vertex_Is_In_Graph(v), ("Vertex does not exist in graph\n"));
00259 
00260   do {
00261     v--;
00262   } while (v>0 && _v[v].Is_Free() );    // skip over free vertices
00263 
00264   return v;
00265 }
00266 
00267 template <class EDGE_TYPE, class VERTEX_TYPE>
00268 EINDEX16
00269 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Get_Edge() const {
00270   EINDEX16 e;
00271 
00272   if (_ecnt == 0) return 0;
00273 
00274   e = _e.Lastidx();
00275   while (_e[e].Is_Free() && e>0) e--;   // skip over free edges
00276   Is_True( e>0 , ("Fail to get edge\n"));
00277 
00278   return e;
00279 }
00280 
00281 template <class EDGE_TYPE, class VERTEX_TYPE>
00282 EINDEX16
00283 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Get_Next_Edge(EINDEX16 e) const {
00284 
00285   Is_True( Edge_Is_In_Graph(e), ("Edge does not exist in graph\n"));
00286 
00287   do {
00288     e--;
00289   } while (e > 0 && _e[e].Is_Free() );  // skip over free edges
00290 
00291   return e;
00292 }
00293 
00294 template <class EDGE_TYPE, class VERTEX_TYPE>
00295 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>&
00296 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::
00297 operator=(const DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>& g) {
00298 
00299   // copy everything except mpool
00300 
00301   _vfree = g._vfree;
00302   _vcnt = g._vcnt;
00303   _efree = g._efree;
00304   _ecnt = g._ecnt;
00305 
00306   _v = g._v;
00307   _e = g._e;
00308 
00309   return *this;
00310 }
00311 
00312 template <class EDGE_TYPE, class VERTEX_TYPE>
00313 void
00314 DIRECTED_GRAPH16<EDGE_TYPE,VERTEX_TYPE>::Print(FILE *file) const {
00315   VINDEX16 i;
00316   EINDEX16 e;
00317 
00318   fprintf(file,"Print out graph edges and vertices ...\n");
00319   for (i=1; i<_e.Lastidx()+1; i++)
00320    if (!_e[i].Is_Free())
00321     fprintf(file, "%d: %d --> %d\n", i, _e[i]._from, _e[i]._to);
00322 
00323   for (i=1; i<_v.Lastidx()+1; i++)
00324    if (!_v[i].Is_Free()) {
00325     fprintf(file, "( ");
00326     e = _v[i].Get_In_Edge();
00327     while (e) {
00328       fprintf(file, "%d ", e);
00329       e = _e[e].Get_Next_In_Edge();
00330     }
00331     fprintf(file, ") %d ( ", i);
00332     e = _v[i].Get_Out_Edge();
00333     while (e) {
00334       fprintf(file, "%d ", e);
00335       e = _e[e].Get_Next_Out_Edge();
00336     }
00337     fprintf(file, ")\n");
00338   }
00339   
00340 }
00341 
00342 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines