Вы находитесь на странице: 1из 2

D:\PhD\Graph Cut\maxflow-v3.01\graph.

cpp 1
1 /* graph.cpp */
2
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "graph.h"
8
9
10 template <typename captype, typename tcaptype, typename flowtype>
11 Graph<captype, tcaptype, flowtype>::Graph(int node_num_max, int edge_num_max, void
(*err_function)(char *))
12 : node_num(0),
13 nodeptr_block(NULL),
14 error_function(err_function)
15 {
16 if (node_num_max < 16) node_num_max = 16;
17 if (edge_num_max < 16) edge_num_max = 16;
18
19 nodes = (node*) malloc(node_num_max*sizeof(node));
20 arcs = (arc*) malloc(2*edge_num_max*sizeof(arc));
21 if (!nodes || !arcs) { if (error_function) (*error_function)("Not enough memory!")
; exit(1); }
22
23 node_last = nodes;
24 node_max = nodes + node_num_max;
25 arc_last = arcs;
26 arc_max = arcs + 2*edge_num_max;
27
28 maxflow_iteration = 0;
29 flow = 0;
30 }
31
32 template <typename captype, typename tcaptype, typename flowtype>
33 Graph<captype,tcaptype,flowtype>::~Graph()
34 {
35 if (nodeptr_block)
36 {
37 delete nodeptr_block;
38 nodeptr_block = NULL;
39 }
40 free(nodes);
41 free(arcs);
42 }
43
44 template <typename captype, typename tcaptype, typename flowtype>
45 void Graph<captype,tcaptype,flowtype>::reset()
46 {
47 node_last = nodes;
48 arc_last = arcs;
49 node_num = 0;
50
51 if (nodeptr_block)
52 {
53 delete nodeptr_block;
54 nodeptr_block = NULL;
55 }
56
57 maxflow_iteration = 0;
58 flow = 0;
59 }
60
61 template <typename captype, typename tcaptype, typename flowtype>
62 void Graph<captype,tcaptype,flowtype>::reallocate_nodes(int num)
63 {
64 int node_num_max = (int)(node_max - nodes);
65 node* nodes_old = nodes;
66
67 node_num_max += node_num_max / 2;
68 if (node_num_max < node_num + num) node_num_max = node_num + num;
69 nodes = (node*) realloc(nodes_old, node_num_max*sizeof(node));
70 if (!nodes) { if (error_function) (*error_function)("Not enough memory!"); exit(1)
; }
71
D:\PhD\Graph Cut\maxflow-v3.01\graph.cpp 2
72 node_last = nodes + node_num;
73 node_max = nodes + node_num_max;
74
75 if (nodes != nodes_old)
76 {
77 arc* a;
78 for (a=arcs; a<arc_last; a++)
79 {
80 a->head = (node*) ((char*)a->head + (((char*) nodes) - ((char*)
nodes_old)));
81 }
82 }
83 }
84
85 template <typename captype, typename tcaptype, typename flowtype>
86 void Graph<captype,tcaptype,flowtype>::reallocate_arcs()
87 {
88 int arc_num_max = (int)(arc_max - arcs);
89 int arc_num = (int)(arc_last - arcs);
90 arc* arcs_old = arcs;
91
92 arc_num_max += arc_num_max / 2; if (arc_num_max & 1) arc_num_max ++;
93 arcs = (arc*) realloc(arcs_old, arc_num_max*sizeof(arc));
94 if (!arcs) { if (error_function) (*error_function)("Not enough memory!"); exit(1);
}
95
96 arc_last = arcs + arc_num;
97 arc_max = arcs + arc_num_max;
98
99 if (arcs != arcs_old)
100 {
101 node* i;
102 arc* a;
103 for (i=nodes; i<node_last; i++)
104 {
105 if (i->first) i->first = (arc*) ((char*)i->first + (((char*) arcs) - (
(char*) arcs_old)));
106 }
107 for (a=arcs; a<arc_last; a++)
108 {
109 if (a->next) a->next = (arc*) ((char*)a->next + (((char*) arcs) - ((char*)
arcs_old)));
110 a->sister = (arc*) ((char*)a->sister + (((char*) arcs) - ((char*)
arcs_old)));
111 }
112 }
113 }
114
115 #include "instances.inc"
116

Вам также может понравиться