00001 /* 00002 * This file is part of the ToolBox. 00003 * Copyright Thomas Jacob. 00004 * 00005 * READ README.TXT BEFORE USE!! 00006 */ 00007 00008 00009 #ifndef __TOOLBOX_GRAPH_H 00010 #define __TOOLBOX_GRAPH_H 00011 00012 00013 namespace toolbox 00014 { 00022 class Edge 00023 { 00024 friend Graph; 00025 friend Vertex; 00026 friend VertexArray; 00027 friend ArrayList<Vertex>; 00028 friend Edge; 00029 friend EdgeArray; 00030 friend ArrayList<Edge>; 00031 00032 private: 00033 00040 Edge(class Graph * Graph, Vertex * FromVertex, Vertex * ToVertex); 00041 00045 virtual ~Edge(); 00046 00050 void * Data; 00051 00055 Vertex * FromVertex; 00056 00060 class Graph * Graph; 00061 00062 #ifdef _TOOLBOX_TEST 00063 00066 static int InstanceCount; 00067 #endif 00068 00072 Vertex * ToVertex; 00073 00077 double Weight; 00078 00079 public: 00080 00085 inline void * GetData(); 00086 00091 inline Vertex * GetFromVertex(); 00092 00097 inline Vertex * GetToVertex(); 00098 00099 #ifdef _TOOLBOX_TEST 00100 00106 static void RunTestSuite(int * performedTests, int * failedTests); 00107 #endif 00108 00113 inline void SetData(void * data); 00114 }; 00115 00116 00121 class EdgeArray : private ArrayList<Edge> 00122 { 00123 friend Graph; 00124 friend Vertex; 00125 friend VertexArray; 00126 friend ArrayList<Vertex>; 00127 friend Edge; 00128 friend EdgeArray; 00129 friend ArrayList<Edge>; 00130 00131 private: 00132 00133 #ifdef _TOOLBOX_TEST 00134 00137 static int InstanceCount; 00138 #endif 00139 00143 EdgeArray(); 00144 00148 ~EdgeArray(); 00149 00150 public: 00151 00157 inline Edge * Get(long nr); 00158 00163 inline long GetCount(); 00164 00169 inline bool IsEmpty(); 00170 00171 #ifdef _TOOLBOX_TEST 00172 00178 static void RunTestSuite(int * performedTests, int * failedTests); 00179 #endif 00180 }; 00181 00182 00192 class Graph 00193 { 00194 friend Graph; 00195 friend Vertex; 00196 friend VertexArray; 00197 friend ArrayList<Vertex>; 00198 friend Edge; 00199 friend EdgeArray; 00200 friend ArrayList<Edge>; 00201 00202 // Todo: Tests for symmetry, ... 00203 00204 private: 00205 00209 EdgeArray * Edges; 00210 00211 #ifdef _TOOLBOX_TEST 00212 00215 static int InstanceCount; 00216 #endif 00217 00221 VertexArray * Vertices; 00222 00223 public: 00224 00228 Graph(); 00229 00233 virtual ~Graph(); 00234 00242 Vertex * AddVertex(); 00243 00248 inline EdgeArray * GetEdges(); 00249 00254 inline VertexArray * GetVertices(); 00255 00264 inline bool DeleteEdge(class Edge * Edge); 00265 00272 inline void DeleteEdges(); 00273 00281 inline bool DeleteVertex(class Vertex * Vertex); 00282 00288 inline void DeleteVertices(); 00289 00290 #ifdef _TOOLBOX_TEST 00291 00297 static void RunTestSuite(int * performedTests, int * failedTests); 00298 #endif 00299 }; 00300 00301 00308 class Vertex 00309 { 00310 friend Graph; 00311 friend Vertex; 00312 friend VertexArray; 00313 friend ArrayList<Vertex>; 00314 friend Edge; 00315 friend EdgeArray; 00316 friend ArrayList<Edge>; 00317 00318 private: 00319 00324 Vertex(class Graph * Graph); 00325 00329 virtual ~Vertex(); 00330 00334 void * Data; 00335 00339 EdgeArray * Edges; 00340 00344 class Graph * Graph; 00345 00346 #ifdef _TOOLBOX_TEST 00347 00350 static int InstanceCount; 00351 #endif 00352 00353 public: 00354 00363 Edge * AddEdge(Vertex * ToVertex); 00364 00373 inline bool DeleteEdge(class Edge * Edge); 00374 00383 bool DeleteEdge(Vertex * ToVertex); 00384 00391 inline void DeleteEdges(); 00392 00398 inline EdgeArray * GetEdges(); 00399 00404 inline void * GetData(); 00405 00406 #ifdef _TOOLBOX_TEST 00407 00413 static void RunTestSuite(int * performedTests, int * failedTests); 00414 #endif 00415 00420 inline void SetData(void * data); 00421 }; 00422 00423 00428 class VertexArray : private ArrayList<Vertex> 00429 { 00430 friend Graph; 00431 friend Vertex; 00432 friend VertexArray; 00433 friend ArrayList<Vertex>; 00434 friend Edge; 00435 friend EdgeArray; 00436 friend ArrayList<Edge>; 00437 00438 private: 00439 00440 #ifdef _TOOLBOX_TEST 00441 00444 static int InstanceCount; 00445 #endif 00446 00450 VertexArray(); 00451 00455 ~VertexArray(); 00456 00457 public: 00458 00464 inline Vertex * Get(long nr); 00465 00470 inline long GetCount(); 00471 00476 inline bool IsEmpty(); 00477 00478 #ifdef _TOOLBOX_TEST 00479 00485 static void RunTestSuite(int * performedTests, int * failedTests); 00486 #endif 00487 }; 00488 } 00489 00490 00491 #endif