-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_graphs.h
189 lines (160 loc) ยท 8.94 KB
/
07_graphs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// encoding: UTF-8
#ifndef CRUUUBY_H6
#include "06_time_series_data.h"
#endif
#ifndef CRUUUBY_H7
#define CRUUUBY_H7
typedef struct Pseudo_Graph {
unsigned int num_nodes;
double * nodes;
} PseudoGraph, *ptr_pseudo_graph;
/* __
/\ \ __
___ ___ __ ___ ___ ___ _ __ __ __ \_\ \___ ____/\_\ ____ __
/' __` __`\ /'__`\/' __` __`\ / __`\/\`'__\/\ \/\ \ /\___ __\ /',__\/\ \/\_ ,`\ /'__`\
/\ \/\ \/\ \/\ __//\ \/\ \/\ \/\ \L\ \ \ \/ \ \ \_\ \ \/__/\ \_/ /\__, `\ \ \/_/ /_/\ __/
\ \_\ \_\ \_\ \____\ \_\ \_\ \_\ \____/\ \_\ \/`____ \ \ \_\ \/\____/\ \_\/\____\ \____\
\/_/\/_/\/_/\/____/\/_/\/_/\/_/\/___/ \/_/ `/___/> \ \/_/ \/___/ \/_/\/____/\/____/
/\___/
\/__/ */
static void _pseudo_graph_free(void * data);
static size_t _pseudo_graph_size(const void * data);
static VALUE pseudo_graph_m_free(VALUE self);
static VALUE pseudo_graph_alloc(VALUE self);
static VALUE pseudo_graph_m_initialize(VALUE self, const VALUE num_nodes);
static void ptr_pseudo_graph_free(ptr_pseudo_graph graph);
static void ptr_pseudo_graph_free(ptr_pseudo_graph graph) {
if (graph != NULL) {
if (graph->nodes != NULL && graph->num_nodes != 0) {
xfree(graph->nodes);
//graph->nodes = NULL;
graph->num_nodes = 0;
}
}
}
static void _pseudo_graph_free(void * data) {
if (data != NULL) {
ptr_pseudo_graph_free(data);
xfree(data);
}
}
static size_t _pseudo_graph_size(const void * data) {return sizeof(struct Pseudo_Graph);}
static const rb_data_type_t type_pseudo_graph = {
.data = NULL,
.flags = RUBY_TYPED_PROMOTED1,
.wrap_struct_name = "pseudo_graph",
.function = {
.dmark = NULL, // NULL as the struct contains no references to the c-data-type{VALUE}
.dfree = _pseudo_graph_free,
.dsize = _pseudo_graph_size,
},
};
static VALUE pseudo_graph_alloc(VALUE self){
ptr_pseudo_graph graph;
return TypedData_Make_Struct(self, PseudoGraph, & type_pseudo_graph, graph);
}
#define ๐parse_ptr_pseudo_graph(the_data, the_ptr) TypedData_Get_Struct(the_data, PseudoGraph, & type_pseudo_graph, the_ptr)
#define ๐self_to_ptr_pseudo_graph ptr_pseudo_graph graph; ๐parse_ptr_pseudo_graph(self, graph);
static VALUE pseudo_graph_m_free(VALUE self) {
๐self_to_ptr_pseudo_graph
ptr_pseudo_graph_free(graph);
return Qnil;
}
static VALUE pseudo_graph_m_initialize(VALUE self, const VALUE num_nodes) {
๐_is_fixnum("PseudoGraph", "new", "num_nodes", num_nodes);
๐self_to_ptr_pseudo_graph
graph->num_nodes = FIX2INT(num_nodes);
if (graph->num_nodes != 0) {
graph->nodes = ๐create_ptr_dbls(graph->num_nodes * graph->num_nodes);
unsigned int len = graph->num_nodes;
unsigned int r, c;
for (r = 0; r < len; r++) {
for (c = 0; c < len; c++) {
graph->nodes[r * len + c] = 0.0;
}
}
} else {
graph->nodes = NULL;
}
re_me
}
// ---------------------------
static void ptr_pseudo_graph_print_debugging(ptr_pseudo_graph graph);
static void ptr_pseudo_graph_set_edge(ptr_pseudo_graph graph, const unsigned int node_from, const unsigned int node_to, const double edge_weight);
static void ptr_pseudo_graph_set_edge_inverse(ptr_pseudo_graph graph, const unsigned int node_from, const unsigned int node_to, const double edge_weight);
static double ptr_pseudo_graph_get_edge(ptr_pseudo_graph graph, const unsigned int node_from, const unsigned int node_to);
static void ptr_pseudo_graph_print_debugging(ptr_pseudo_graph graph) {
unsigned int len = graph->num_nodes;
unsigned int r, c;
for (r = 0; r < len; r++) {
for (c = 0; c < len; c++) {
printf("[%u, %u]: {%f}\n", r, c, graph->nodes[r * len + c]);
}
}
}
static void ptr_pseudo_graph_set_edge(ptr_pseudo_graph graph, const unsigned int node_from, const unsigned int node_to, const double edge_weight) {
graph->nodes[node_from * graph->num_nodes + node_to] = edge_weight;
}
static void ptr_pseudo_graph_set_edge_inverse(ptr_pseudo_graph graph, const unsigned int node_from, const unsigned int node_to, const double edge_weight) {
graph->nodes[node_from * graph->num_nodes + node_to] = edge_weight;
graph->nodes[node_to * graph->num_nodes + node_from] = 1.0 / edge_weight;
}
static double ptr_pseudo_graph_get_edge(ptr_pseudo_graph graph, const unsigned int node_from, const unsigned int node_to) {
return graph->nodes[node_from * graph->num_nodes + node_to];
}
static VALUE pseudo_graph_m_print_debugging(const VALUE self);
static VALUE pseudo_graph_m_set_edge(const VALUE self, const VALUE node_from, const VALUE node_to, const VALUE edge_weight);
static VALUE pseudo_graph_m_set_edge_inverse(const VALUE self, const VALUE node_from, const VALUE node_to, const VALUE edge_weight);
static VALUE pseudo_graph_m_get_edge(const VALUE self, const VALUE node_from, const VALUE node_to);
static VALUE pseudo_graph_m_print_debugging(const VALUE self) {
๐self_to_ptr_pseudo_graph
ptr_pseudo_graph_print_debugging(graph);
re_me
}
static VALUE pseudo_graph_m_set_edge(const VALUE self, const VALUE node_from, const VALUE node_to, const VALUE edge_weight) {
๐self_to_ptr_pseudo_graph
๐_is_fixnum("PseudoGraph", "set_edge", "node_from", node_from);
๐_is_fixnum("PseudoGraph", "set_edge", "node_to", node_to);
const int val_node_from = FIX2INT(node_from);
const int val_node_to = FIX2INT(node_to);
if (val_node_from < 0 || val_node_from >= (int) (graph->num_nodes)) {
rb_raise(R_ERR_ARG, "| c{PseudoGraph}-> m{set_edge} got arg-node-from w/ val{%"PRIsVALUE"} when range is {0}-{%u} |", node_from, graph->num_nodes);
}
if (val_node_from < 0 || val_node_to >= (int) (graph->num_nodes)) {
rb_raise(R_ERR_ARG, "| c{PseudoGraph}-> m{set_edge} got arg-node-to w/ val{%"PRIsVALUE"} when range is {0}-{%u} |", node_to, graph->num_nodes);
}
๐_is_flt("PseudoGraph", "set_edge", "edge_weight", edge_weight);
ptr_pseudo_graph_set_edge(graph, (unsigned int) val_node_from, (unsigned int) val_node_to, NUM2DBL(edge_weight));
re_me
}
static VALUE pseudo_graph_m_set_edge_inverse(const VALUE self, const VALUE node_from, const VALUE node_to, const VALUE edge_weight) {
๐self_to_ptr_pseudo_graph
๐_is_fixnum("PseudoGraph", "set_edge_inverse", "node_from", node_from);
๐_is_fixnum("PseudoGraph", "set_edge_inverse", "node_to", node_to);
๐_is_flt("PseudoGraph", "set_edge_inverse", "edge_weight", edge_weight);
ptr_pseudo_graph_set_edge_inverse(graph, FIX2UINT(node_from), FIX2UINT(node_to), NUM2DBL(edge_weight));
re_me
}
static VALUE pseudo_graph_m_get_edge(const VALUE self, const VALUE node_from, const VALUE node_to) {
๐self_to_ptr_pseudo_graph
๐_is_fixnum("PseudoGraph", "[]", "node_from", node_from);
๐_is_fixnum("PseudoGraph", "[]", "node_to", node_to);
return DBL2NUM(ptr_pseudo_graph_get_edge(graph, FIX2UINT(node_from), FIX2UINT(node_to)));
}
/* __ __ ___ ___ __
/\ \ /\ \ /'___\ __ /\_ \ /\ \
___ __ ___\ \ \___ __ \_\ \ /\ \__//\_\ __\//\ \ \_\ \ ____
/'___\ /'__`\ /'___\ \ _ `\ /'__`\ /'_` \ \ \ ,__\/\ \ /'__`\\ \ \ /'_` \ /',__\
/\ \__//\ \L\.\_/\ \__/\ \ \ \ \/\ __//\ \L\ \ \ \ \_/\ \ \/\ __/ \_\ \_/\ \L\ \/\__, `\
\ \____\ \__/.\_\ \____\\ \_\ \_\ \____\ \___,_\ \ \_\ \ \_\ \____\/\____\ \___,_\/\____/
\/____/\/__/\/_/\/____/ \/_/\/_/\/____/\/__,_ / \/_/ \/_/\/____/\/____/\/__,_ /\/___/ */
#define โก๐_ptr_psuedo_graph(func_name, expr) static VALUE func_name(const VALUE self){๐self_to_ptr_pseudo_graph; expr}
#define โก๐_ptr_psuedo_graph_getter(func_name, expr) static VALUE func_name(const VALUE self){๐self_to_ptr_pseudo_graph; return expr;}
#define โก๐_ptr_psuedo_graph_getter_bool(func_name, expr) static VALUE func_name(const VALUE self){๐self_to_ptr_pseudo_graph; re_as_bool(expr)}
#define โก๐_ptr_psuedo_graph_getter_dbl(func_name, expr) static VALUE func_name(const VALUE self){๐self_to_ptr_pseudo_graph; return DBL2NUM(expr);}
#define โก๐_ptr_psuedo_graph_getter_int(func_name, expr) static VALUE func_name(const VALUE self){๐self_to_ptr_pseudo_graph; return UINT2NUM(expr);}
static VALUE pseudo_graph_m_get_num_nodes(const VALUE self);
static VALUE pseudo_graph_m_get_is_empty(const VALUE self);
โก๐_ptr_psuedo_graph_getter_int(pseudo_graph_m_get_num_nodes, graph->num_nodes)
โก๐_ptr_psuedo_graph_getter_bool(pseudo_graph_m_get_is_empty, graph->num_nodes == 0)
#endif