00001 #include "util.h"
00002 #include "list.h"
00003 #include "array.h"
00004 #include "graph.h"
00005 #include "graph_static.h"
00006
00007
00008 static char *month[] = {"Jan", "Feb", "March", "april", "may", "June",
00009 "july", "Aug", "Sept", "Oct", "nov", "Dec"
00010 };
00011
00012 #define voidNULL (void (*)()) NULL
00013 #define gGenericNULL (gGeneric (*)()) NULL
00014
00015 static void
00016 strfree(thing)
00017 gGeneric thing;
00018 {
00019 FREE(thing);
00020 }
00021
00022 static int
00023 graph_test()
00024 {
00025 graph_t *g1, *g2;
00026 int i, j;
00027 vertex_t *v[10];
00028 lsGen gen, gen2;
00029 vertex_t *vert;
00030 edge_t *edge;
00031
00032 g1 = g_alloc();
00033
00034 for (i = 0; i < 10; i++) {
00035 v[i] = g_add_vertex(g1);
00036 v[i]->user_data = (gGeneric) i;
00037 }
00038 for (i = 0; i < 9; i++) {
00039 for (j = i + 1; j < 10; j++) {
00040 (void) g_add_edge(v[i],v[j]);
00041 }
00042 }
00043 (void) g_add_edge(v[5],v[5]);
00044
00045 (void) lsFirstItem(g_get_out_edges(v[4]),(lsGeneric *) &edge,LS_NH);
00046 g_delete_edge(edge,voidNULL);
00047
00048 g_delete_vertex(v[8],voidNULL,voidNULL);
00049
00050 g_add_vertex(g1)->user_data = (gGeneric) 10;
00051
00052 g2 = g_dup(g1,gGenericNULL,gGenericNULL,gGenericNULL);
00053 foreach_vertex (g2,gen,vert) {
00054 (void) fprintf(stdout,"\nCopy of %d\ngoes to: ",vert->user_data);
00055 foreach_out_edge (vert,gen2,edge) {
00056 (void) fprintf(stdout,"%d ",g_e_dest(edge)->user_data);
00057 }
00058 (void) fprintf(stdout,"\ncomes from: ");
00059 foreach_in_edge (vert,gen2,edge) {
00060 (void) fprintf(stdout,"%d ",g_e_source(edge)->user_data);
00061 }
00062 }
00063 (void) fputc('\n',stdout);
00064 g_check(g1);
00065 g_check(g2);
00066 g_free(g1,voidNULL,voidNULL,voidNULL);
00067 g_free(g2,voidNULL,voidNULL,voidNULL);
00068
00069 g1 = g_alloc();
00070 for (i = 0; i < 12; i++) {
00071 g_add_vertex(g1)->user_data = (gGeneric) month[i];
00072 }
00073 g2 = g_dup(g1,gGenericNULL,(gGeneric (*)()) util_strsav,gGenericNULL);
00074 foreach_vertex (g1,gen,vert) {
00075 ((char *) vert->user_data)[0] = '\0';
00076 }
00077 foreach_vertex (g2,gen,vert) {
00078 (void) fprintf(stdout, "%s\n", (char *) vert->user_data);
00079 }
00080 g_free(g1, voidNULL, voidNULL, voidNULL);
00081 g_free(g2, voidNULL, strfree, voidNULL);
00082 return(0);
00083 }
00084
00085 static void
00086 edge_free(thing)
00087 gGeneric thing;
00088 {
00089 FREE(((gGeneric *) thing)[2]);
00090 }
00091
00092 static gGeneric
00093 edge_copy(thing)
00094 gGeneric thing;
00095 {
00096 gGeneric *new = ALLOC(gGeneric,4);
00097 gGeneric *old = (gGeneric *) thing;
00098
00099 new[0] = old[0];
00100 new[1] = old[1];
00101 new[2] = (gGeneric) util_strsav((char *) old[2]);
00102 new[3] = old[3];
00103 return((gGeneric) new);
00104 }
00105
00106
00107 static int
00108 graph_static_test()
00109 {
00110 graph_t *g1, *g2;
00111 int i,j,x;
00112 vertex_t *v[10], *v1, *v2;
00113 edge_t *e, *edge;
00114 lsGen gen;
00115
00116 g1 = g_alloc_static(3,2,4);
00117
00118 for (i = 0; i < 10; i++) {
00119 v[i] = g_add_vertex_static(g1);
00120 g_set_v_slot_static(v[i],0,(gGeneric) i);
00121 g_set_v_slot_static(v[i],1,(gGeneric) (2 * i));
00122 }
00123 x = 0;
00124 for (i = 0; i < 9; i++) {
00125 for (j = i + 1; j < 10; j++) {
00126 e = g_add_edge_static(v[i],v[j]);
00127 g_set_e_slot_static(e,2,(gGeneric) util_strsav(month[i]));
00128 g_set_e_slot_static(e,1,(gGeneric) x++);
00129 }
00130 }
00131 g_delete_vertex_static(v[3],voidNULL,edge_free);
00132 (void) lsLastItem(g_get_out_edges(v[6]),(lsGeneric *) &edge,LS_NH);
00133 g_delete_edge_static(edge,edge_free);
00134
00135 g_set_g_slot_static(g1,1,(gGeneric) 'f');
00136 g2 = g_dup_static(g1,gGenericNULL,gGenericNULL,edge_copy);
00137
00138 v1 = g_add_vertex_static(g2);
00139 g_copy_v_slots_static(v[2],v1,gGenericNULL);
00140
00141 foreach_edge (g2,gen,edge) {
00142 v1 = g_e_source(edge);
00143 v2 = g_e_dest(edge);
00144 (void) fprintf(stdout,
00145 "%d (%s) connects %d & %d\n",g_get_e_slot_static(edge,1),
00146 g_get_e_slot_static(edge,2),g_get_v_slot_static(v1,0),
00147 g_get_v_slot_static(v2,0));
00148 }
00149 g_free_static(g1,voidNULL,voidNULL,edge_free);
00150 g_free_static(g2,voidNULL,voidNULL,edge_free);
00151 return(0);
00152 }
00153
00154 static int
00155 reverso(a,b)
00156 char *a,*b;
00157 {
00158 return((int) ((vertex_t *) b)->user_data - (int) ((vertex_t *) a)->user_data);
00159 }
00160
00161 static int
00162 graph_dfs_test()
00163 {
00164 int i;
00165 vertex_t *v[10];
00166 array_t *arr;
00167 graph_t *g;
00168 vertex_t *x;
00169
00170 g = g_alloc();
00171 for (i = 0; i < 10; i++) {
00172 v[i] = g_add_vertex(g);
00173 v[i]->user_data = (gGeneric) i;
00174 }
00175 (void) g_add_edge(v[3],v[4]);
00176 (void) g_add_edge(v[0],v[3]);
00177 (void) g_add_edge(v[0],v[6]);
00178 (void) g_add_edge(v[0],v[2]);
00179 (void) g_add_edge(v[1],v[3]);
00180 (void) g_add_edge(v[6],v[3]);
00181 (void) g_add_edge(v[2],v[5]);
00182 (void) g_add_edge(v[2],v[3]);
00183 (void) g_add_edge(v[3],v[5]);
00184 (void) g_add_edge(v[6],v[2]);
00185
00186 (void) g_add_edge(v[7],v[8]);
00187 (void) g_add_edge(v[9],v[7]);
00188 (void) g_add_edge(v[9],v[8]);
00189 arr = g_dfs(g);
00190 (void) fprintf(stdout,"Depth first sort\n");
00191 for (i = 0; i < 10; i++) {
00192 x = array_fetch(vertex_t *,arr,i);
00193 (void) fprintf(stdout,"%d\n",x->user_data);
00194 }
00195 array_free(arr);
00196 (void) fprintf(stdout,"\nReverse sort\n");
00197 arr = g_graph_sort(g,reverso);
00198 for (i = 0; i < 10; i++) {
00199 x = array_fetch(vertex_t *,arr,i);
00200 (void) fprintf(stdout,"%d\n",x->user_data);
00201 }
00202 array_free(arr);
00203 g_free(g,voidNULL,voidNULL,voidNULL);
00204 return(0);
00205 }
00206
00207 init_graph()
00208 {
00209 extern int g_unique_id;
00210
00211 g_unique_id = 0;
00212 com_add_command("_graph_test",graph_test,0);
00213 com_add_command("_graph_static_test",graph_static_test,0);
00214 com_add_command("_graph_dfs_test",graph_dfs_test,0);
00215 }
00216
00217 end_graph()
00218 {
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236