/[paths]/src/graph.c
ViewVC logotype

Annotation of /src/graph.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (hide annotations)
Sun Aug 12 10:21:50 2012 UTC (8 years, 1 month ago) by ben
File MIME type: text/plain
File size: 3783 byte(s)
fix mem leak
1 ben 6 #include <string.h>
2     #include <stdlib.h>
3 ben 7 #include <time.h>
4 ben 9 #include <assert.h>
5 ben 2 #include "graph.h"
6 ben 6
7 ben 2 void node_destroy(struct node* n)
8     {
9 ben 3 int i;
10     for(i = 0; i < n->count; i++) {
11 ben 6 node_unlink(n, n->neighbours[i].to);
12 ben 3 }
13 ben 6 free(n->neighbours);
14     n->neighbours = NULL;
15 ben 3 return;
16 ben 2 }
17    
18 ben 4 void node_randomize(struct node* n)
19     {
20     n->event.pitch = rand() % 128;
21     n->event.duration = rand();
22     n->event.attack = rand() % 128;
23     n->event.decay = rand() % 128;
24     n->event.sustain = rand() % 128;
25     n->event.release = rand() % 128;
26     n->event.instrument = rand() % 128;
27     n->event.modulation = rand() % 128;
28     n->event.detune = rand() % 128;
29     n->event.velocity = rand() % 128;
30     n->event.volume = rand() % 128;
31     }
32    
33 ben 2 void edge_randomize(struct edge* e)
34     {
35 ben 4 e->distance = rand();
36 ben 2 }
37    
38 ben 8 int node_is_linked(struct node* a, struct node* b)
39     {
40     int i;
41     for (i = 0; i < a->count; i++) {
42     if (a->neighbours[i].to == b)
43     return 1;
44     }
45     return 0;
46     }
47    
48 ben 3 int node_link(struct node* a, struct node* b, int distance)
49 ben 2 {
50 ben 3 struct edge *nba, *nbb;
51 ben 8
52 ben 9 assert (a != b);
53    
54 ben 3 a->count++;
55     nba = realloc(a->neighbours, a->count * sizeof(struct edge));
56     if (!nba) {
57     a->count--;
58     return -1;
59     }
60     nba[a->count - 1].distance = distance;
61     nba[a->count - 1].to = b;
62 ben 2
63 ben 3 b->count++;
64     nbb = realloc(b->neighbours, b->count * sizeof(struct edge));
65     if (!nbb) {
66     a->count--;
67     b->count--;
68     free(nba);
69     return -1;
70     }
71     nbb[b->count - 1].distance = distance;
72     nbb[b->count - 1].to = a;
73    
74     a->neighbours = nba;
75     b->neighbours = nbb;
76     return 0;
77 ben 2 }
78    
79 ben 6 int node_unlink(struct node* a, struct node* b)
80 ben 2 {
81 ben 3 struct edge *nba, *nbb;
82 ben 6 int i, j;
83 ben 3 a->count--;
84     nba = malloc(a->count * sizeof(struct edge));
85     if (!nba) {
86     a->count++;
87     return -1;
88     }
89 ben 6 for (i = 0, j = 0; i < a->count; i++, j++) {
90 ben 3 if (a->neighbours[i].to != b) {
91     nba[j].to = a->neighbours[i].to;
92     nba[j].distance = a->neighbours[i].distance;
93     } else {
94     j--;
95     }
96     }
97    
98     nbb = malloc(b->count * sizeof(struct edge));
99     if (!nbb) {
100     a->count++;
101     b->count++;
102     free(nba);
103     return - 1;
104     }
105 ben 6 for (i = 0, j = 0; i < b->count; i++, j++) {
106 ben 3 if (b->neighbours[i].to != a) {
107     nbb[j].to = b->neighbours[i].to;
108     nbb[j].distance = b->neighbours[i].distance;
109     } else {
110     j--;
111     }
112     }
113     free(a->neighbours);
114     free(b->neighbours);
115     a->neighbours = nba;
116     b->neighbours = nbb;
117     return 0;
118 ben 2 }
119    
120 ben 4 struct graph* graph_new(int n)
121 ben 2 {
122 ben 4 struct graph* g;
123    
124 ben 11 srand(time(NULL));
125    
126 ben 4 g = malloc(sizeof(struct graph));
127     if (!g) {
128     return NULL;
129     }
130    
131     g->nodes = malloc(n * sizeof(struct node));
132 ben 7 if (!(g->nodes)) {
133 ben 4 free(g);
134     return NULL;
135     }
136    
137 ben 7 memset(g->nodes, 0, n * sizeof(struct node));
138 ben 4 g->count = n;
139     return g;
140 ben 2 }
141    
142 ben 11 int graph_randomize1(struct graph* g, int l)
143 ben 2 {
144 ben 4 int m, n, i, j;
145 ben 2
146 ben 4 for (i = 0; i < g->count; i++) {
147     node_randomize(&(g->nodes[i]));
148 ben 11 }
149    
150     for (j = 0; j < l; j++) {
151     m = rand() % g->count;
152     n = rand() % g->count;
153     if ((m == n) || node_is_linked(&(g->nodes[m]), &(g->nodes[n]))) {
154     continue;
155     }
156     if (node_link(&(g->nodes[m]), &(g->nodes[n]), rand())) {
157     goto revert;
158     }
159     }
160    
161     return 0;
162     revert:
163     for (i = 0; i < g->count; i++) {
164     node_destroy(&(g->nodes[i]));
165     }
166     memset(g->nodes, 0, g->count * sizeof(struct node));
167     return -1;
168     }
169    
170     int graph_randomize2(struct graph* g, int l)
171     {
172     int i, j, n;
173     for (i = 0; i < g->count; i++) {
174     node_randomize(&(g->nodes[i]));
175    
176     for (j = 0; j < l; j++) {
177 ben 4 n = rand() % g->count;
178 ben 11 if ((i == n) || node_is_linked(&(g->nodes[i]), &(g->nodes[n]))) {
179 ben 8 continue;
180     }
181 ben 11 if (node_link(&(g->nodes[i]), &(g->nodes[n]), rand())) {
182 ben 4 goto revert;
183     }
184 ben 11 }
185 ben 4 }
186    
187     return 0;
188     revert:
189     for (i = 0; i < g->count; i++) {
190     node_destroy(&(g->nodes[i]));
191     }
192 ben 7 memset(g->nodes, 0, g->count * sizeof(struct node));
193 ben 4 return -1;
194 ben 2 }
195    
196 ben 10 void graph_destroy(struct graph* g)
197     {
198     int i;
199    
200     for (i = 0; i < g->count; i++) {
201     free(g->nodes[i].neighbours);
202     }
203 ben 14 free(g->nodes);
204 ben 10 free(g);
205     return;
206     }

  ViewVC Help
Powered by ViewVC 1.1.26