graph.cpp
Go to the documentation of this file.
00001 /* graph.cpp */
00002 
00003 
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include "graph.h"
00008 
00009 
00010 template <typename captype, typename tcaptype, typename flowtype>
00011         Graph<captype, tcaptype, flowtype>::Graph(int node_num_max, int edge_num_max, void (*err_function)(char *))
00012         : node_num(0),
00013           nodeptr_block(NULL),
00014           error_function(err_function)
00015 {
00016         if (node_num_max < 16) node_num_max = 16;
00017         if (edge_num_max < 16) edge_num_max = 16;
00018 
00019         nodes = (node*) malloc(node_num_max*sizeof(node));
00020         arcs = (arc*) malloc(2*edge_num_max*sizeof(arc));
00021         if (!nodes || !arcs) { if (error_function) (*error_function)("Not enough memory!"); exit(1); }
00022 
00023         node_last = nodes;
00024         node_max = nodes + node_num_max;
00025         arc_last = arcs;
00026         arc_max = arcs + 2*edge_num_max;
00027 
00028         maxflow_iteration = 0;
00029         flow = 0;
00030 }
00031 
00032 template <typename captype, typename tcaptype, typename flowtype>
00033         Graph<captype,tcaptype,flowtype>::~Graph()
00034 {
00035         if (nodeptr_block)
00036         {
00037                 delete nodeptr_block;
00038                 nodeptr_block = NULL;
00039         }
00040         free(nodes);
00041         free(arcs);
00042 }
00043 
00044 template <typename captype, typename tcaptype, typename flowtype>
00045         void Graph<captype,tcaptype,flowtype>::reset()
00046 {
00047         node_last = nodes;
00048         arc_last = arcs;
00049         node_num = 0;
00050 
00051         if (nodeptr_block)
00052         {
00053                 delete nodeptr_block;
00054                 nodeptr_block = NULL;
00055         }
00056 
00057         maxflow_iteration = 0;
00058         flow = 0;
00059 }
00060 
00061 template <typename captype, typename tcaptype, typename flowtype>
00062         void Graph<captype,tcaptype,flowtype>::reallocate_nodes(int num)
00063 {
00064         int node_num_max = (int)(node_max - nodes);
00065         node* nodes_old = nodes;
00066 
00067         node_num_max += node_num_max / 2;
00068         if (node_num_max < node_num + num) node_num_max = node_num + num;
00069         nodes = (node*) realloc(nodes_old, node_num_max*sizeof(node));
00070         if (!nodes) { if (error_function) (*error_function)("Not enough memory!"); exit(1); }
00071 
00072         node_last = nodes + node_num;
00073         node_max = nodes + node_num_max;
00074 
00075         if (nodes != nodes_old)
00076         {
00077                 arc* a;
00078                 for (a=arcs; a<arc_last; a++)
00079                 {
00080                         a->head = (node*) ((char*)a->head + (((char*) nodes) - ((char*) nodes_old)));
00081                 }
00082         }
00083 }
00084 
00085 template <typename captype, typename tcaptype, typename flowtype>
00086         void Graph<captype,tcaptype,flowtype>::reallocate_arcs()
00087 {
00088         int arc_num_max = (int)(arc_max - arcs);
00089         int arc_num = (int)(arc_last - arcs);
00090         arc* arcs_old = arcs;
00091 
00092         arc_num_max += arc_num_max / 2; if (arc_num_max & 1) arc_num_max ++;
00093         arcs = (arc*) realloc(arcs_old, arc_num_max*sizeof(arc));
00094         if (!arcs) { if (error_function) (*error_function)("Not enough memory!"); exit(1); }
00095 
00096         arc_last = arcs + arc_num;
00097         arc_max = arcs + arc_num_max;
00098 
00099         if (arcs != arcs_old)
00100         {
00101                 node* i;
00102                 arc* a;
00103                 for (i=nodes; i<node_last; i++)
00104                 {
00105                         if (i->first) i->first = (arc*) ((char*)i->first + (((char*) arcs) - ((char*) arcs_old)));
00106                 }
00107                 for (a=arcs; a<arc_last; a++)
00108                 {
00109                         if (a->next) a->next = (arc*) ((char*)a->next + (((char*) arcs) - ((char*) arcs_old)));
00110                         a->sister = (arc*) ((char*)a->sister + (((char*) arcs) - ((char*) arcs_old)));
00111                 }
00112         }
00113 }
00114 


tabletop_pushing
Author(s): Tucker Hermans
autogenerated on Wed Nov 27 2013 11:59:44