#include "util.h"
#include "list.h"
#include "array.h"
#include "graph.h"
#include "graph_static.h"
Go to the source code of this file.
Defines | |
#define | voidNULL (void (*)()) NULL |
#define | gGenericNULL (gGeneric (*)()) NULL |
Functions | |
static void | strfree (gGeneric thing) |
static int | graph_test () |
static void | edge_free (gGeneric thing) |
static gGeneric | edge_copy (gGeneric thing) |
static int | graph_static_test () |
static int | reverso (char *a, char *b) |
static int | graph_dfs_test () |
init_graph () | |
end_graph () | |
Variables | |
static char * | month [] |
#define gGenericNULL (gGeneric (*)()) NULL |
Definition at line 13 of file com_graph.c.
#define voidNULL (void (*)()) NULL |
Definition at line 12 of file com_graph.c.
Definition at line 93 of file com_graph.c.
static void edge_free | ( | gGeneric | thing | ) | [static] |
Definition at line 86 of file com_graph.c.
end_graph | ( | ) |
Definition at line 217 of file com_graph.c.
static int graph_dfs_test | ( | ) | [static] |
Definition at line 162 of file com_graph.c.
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 }
static int graph_static_test | ( | ) | [static] |
Definition at line 108 of file com_graph.c.
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); /* kill v[3] */ 00132 (void) lsLastItem(g_get_out_edges(v[6]),(lsGeneric *) &edge,LS_NH); 00133 g_delete_edge_static(edge,edge_free); /* kill last edge of v[6] */ 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 }
static int graph_test | ( | ) | [static] |
Definition at line 23 of file com_graph.c.
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]); /* self loop */ 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; /* unconnected vertex */ 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) { /* strings copied by strsav */ 00078 (void) fprintf(stdout, "%s\n", (char *) vert->user_data); 00079 } 00080 g_free(g1, voidNULL, voidNULL, voidNULL); /* don't free static strings */ 00081 g_free(g2, voidNULL, strfree, voidNULL); /* free copies */ 00082 return(0); 00083 }
init_graph | ( | ) |
Definition at line 207 of file com_graph.c.
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 }
static int reverso | ( | char * | a, | |
char* | b | |||
) | [static] |
Definition at line 155 of file com_graph.c.
static void strfree | ( | gGeneric | thing | ) | [static] |
Definition at line 16 of file com_graph.c.
00018 { 00019 FREE(thing); 00020 }
char* month[] [static] |
{"Jan", "Feb", "March", "april", "may", "June", "july", "Aug", "Sept", "Oct", "nov", "Dec" }
Definition at line 8 of file com_graph.c.