abscondment-rubyvor 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,118 @@
1
+
2
+ /*** HEAP.C ***/
3
+
4
+
5
+ #include "vdefs.h"
6
+
7
+ static int PQmin, PQcount, PQhashsize ;
8
+ static Halfedge * PQhash ;
9
+
10
+ void
11
+ PQinsert(Halfedge * he, Site * v, float offset)
12
+ {
13
+ Halfedge * last, * next ;
14
+
15
+ he->vertex = v ;
16
+ ref(v) ;
17
+ he->ystar = v->coord.y + offset ;
18
+ last = &PQhash[ PQbucket(he)] ;
19
+ while ((next = last->PQnext) != (Halfedge *)NULL &&
20
+ (he->ystar > next->ystar ||
21
+ (he->ystar == next->ystar &&
22
+ v->coord.x > next->vertex->coord.x)))
23
+ {
24
+ last = next ;
25
+ }
26
+ he->PQnext = last->PQnext ;
27
+ last->PQnext = he ;
28
+ PQcount++ ;
29
+ }
30
+
31
+ void
32
+ PQdelete(Halfedge * he)
33
+ {
34
+ Halfedge * last;
35
+
36
+ if(he -> vertex != (Site *) NULL)
37
+ {
38
+ last = &PQhash[PQbucket(he)] ;
39
+ while (last -> PQnext != he)
40
+ {
41
+ last = last->PQnext ;
42
+ }
43
+ last->PQnext = he->PQnext;
44
+ PQcount-- ;
45
+ deref(he->vertex) ;
46
+ he->vertex = (Site *)NULL ;
47
+ }
48
+ }
49
+
50
+ int
51
+ PQbucket(Halfedge * he)
52
+ {
53
+ int bucket ;
54
+
55
+
56
+ if (he->ystar < rubyvorState.ymin) bucket = 0;
57
+ else if (he->ystar >= rubyvorState.ymax) bucket = PQhashsize-1;
58
+ else bucket = (he->ystar - rubyvorState.ymin)/rubyvorState.deltay * PQhashsize;
59
+ if (bucket < 0)
60
+ {
61
+ bucket = 0 ;
62
+ }
63
+ if (bucket >= PQhashsize)
64
+ {
65
+ bucket = PQhashsize-1 ;
66
+ }
67
+ if (bucket < PQmin)
68
+ {
69
+ PQmin = bucket ;
70
+ }
71
+ return (bucket);
72
+ }
73
+
74
+ int
75
+ PQempty(void)
76
+ {
77
+ return (PQcount == 0) ;
78
+ }
79
+
80
+
81
+ Point
82
+ PQ_min(void)
83
+ {
84
+ Point answer ;
85
+
86
+ while (PQhash[PQmin].PQnext == (Halfedge *)NULL)
87
+ {
88
+ ++PQmin ;
89
+ }
90
+ answer.x = PQhash[PQmin].PQnext->vertex->coord.x ;
91
+ answer.y = PQhash[PQmin].PQnext->ystar ;
92
+ return (answer) ;
93
+ }
94
+
95
+ Halfedge *
96
+ PQextractmin(void)
97
+ {
98
+ Halfedge * curr ;
99
+
100
+ curr = PQhash[PQmin].PQnext ;
101
+ PQhash[PQmin].PQnext = curr->PQnext ;
102
+ PQcount-- ;
103
+ return (curr) ;
104
+ }
105
+
106
+ void
107
+ PQinitialize(void)
108
+ {
109
+ int i ;
110
+
111
+ PQcount = PQmin = 0 ;
112
+ PQhashsize = 4 * rubyvorState.sqrt_nsites ;
113
+ PQhash = (Halfedge *)myalloc(PQhashsize * sizeof *PQhash) ;
114
+ for (i = 0 ; i < PQhashsize; i++)
115
+ {
116
+ PQhash[i].PQnext = (Halfedge *)NULL ;
117
+ }
118
+ }
@@ -0,0 +1,118 @@
1
+
2
+ /*** MEMORY.C ***/
3
+
4
+ #include <ruby.h>
5
+ #include <stdio.h>
6
+ #include <stdlib.h> /* malloc() */
7
+
8
+ #include "vdefs.h"
9
+
10
+ static char** memory_map;
11
+ static int nallocs = 0;
12
+
13
+ void
14
+ freeinit(Freelist * fl, int size)
15
+ {
16
+ fl->head = (Freenode *)NULL ;
17
+ fl->nodesize = size ;
18
+ }
19
+
20
+ char *
21
+ getfree(Freelist * fl)
22
+ {
23
+ int i ;
24
+ Freenode * t ;
25
+ if (fl->head == (Freenode *)NULL)
26
+ {
27
+ t = (Freenode *) myalloc(rubyvorState.sqrt_nsites * fl->nodesize) ;
28
+ for(i = 0 ; i < rubyvorState.sqrt_nsites ; i++)
29
+ {
30
+ makefree((Freenode *)((char *)t+i*fl->nodesize), fl) ;
31
+ }
32
+ }
33
+ t = fl->head ;
34
+ fl->head = (fl->head)->nextfree ;
35
+ return ((char *)t) ;
36
+ }
37
+
38
+ void
39
+ makefree(Freenode * curr, Freelist * fl)
40
+ {
41
+ curr->nextfree = fl->head ;
42
+ fl->head = curr ;
43
+ }
44
+
45
+ int total_alloc;
46
+
47
+ void
48
+ update_memory_map(char * newp)
49
+ {
50
+ if (nallocs % 1000 == 0)
51
+ {
52
+ if (nallocs == 0)
53
+ memory_map = (char **)malloc((nallocs+1000)*sizeof(char*));
54
+ else
55
+ memory_map = (char **)realloc(memory_map,(nallocs+1000)*sizeof(char*));
56
+ }
57
+ memory_map[nallocs++] = newp;
58
+ }
59
+
60
+ char *
61
+ myalloc(unsigned n)
62
+ {
63
+ char * t;
64
+
65
+ if ((t=(char*)malloc(n)) == (char *) 0)
66
+ rb_raise(rb_eNoMemError, "Insufficient memory processing site %d (%d bytes in use)\n", rubyvorState.siteidx, total_alloc);
67
+
68
+ total_alloc += n;
69
+
70
+ update_memory_map(t);
71
+ return (t);
72
+ }
73
+
74
+ char *
75
+ myrealloc(void * oldp, unsigned n, unsigned oldn)
76
+ {
77
+ char * newp;
78
+ int i;
79
+
80
+ if ((newp=(char*)realloc(oldp, n)) == (char *) 0)
81
+ rb_raise(rb_eNoMemError, "Insufficient memory processing site %d (%d bytes in use)\n", rubyvorState.siteidx, total_alloc);
82
+
83
+ total_alloc += (n - oldn);
84
+
85
+ update_memory_map(newp);
86
+
87
+ /*
88
+ * Mark oldp as freed, since free() was called by realloc.
89
+ *
90
+ * TODO: this seems naive; measure if this is a bottleneck & use a hash table or some other scheme if it is.
91
+ */
92
+ for (i=0; i<nallocs; i++)
93
+ {
94
+ if (memory_map[i] != (char*)0 && memory_map[i] == oldp)
95
+ {
96
+ memory_map[i] = (char*)0;
97
+ break;
98
+ }
99
+ }
100
+
101
+ return (newp);
102
+ }
103
+
104
+
105
+ void free_all(void)
106
+ {
107
+ int i;
108
+ for (i=0; i<nallocs; i++)
109
+ {
110
+ if (memory_map[i] != (char*)0)
111
+ {
112
+ free(memory_map[i]);
113
+ memory_map[i] = (char*)0;
114
+ }
115
+ }
116
+ free(memory_map);
117
+ nallocs = 0;
118
+ }
@@ -0,0 +1,251 @@
1
+
2
+ /*** OUTPUT.C ***/
3
+
4
+ #include <vdefs.h>
5
+ #include <stdio.h>
6
+ #include <unistd.h>
7
+
8
+ static float pxmin, pxmax, pymin, pymax, cradius;
9
+
10
+ void openpl(void) {}
11
+ void line(float ax, float ay, float bx, float by) {}
12
+ void circle(float ax, float ay, float radius) {}
13
+ void range(float pxmin, float pxmax, float pymin, float pymax) {}
14
+
15
+ void
16
+ out_bisector(Edge * e)
17
+ {
18
+ /* Save line to our ruby object */
19
+ (*rubyvorState.storeL)(e->a, e->b, e->c);
20
+
21
+
22
+ /* printf("l %f %f %f\n", e->a, e->b, e->c); */
23
+
24
+ if (rubyvorState.plot)
25
+ line(e->reg[0]->coord.x, e->reg[0]->coord.y, e->reg[1]->coord.x, e->reg[1]->coord.y);
26
+
27
+ if (rubyvorState.debug)
28
+ printf("line(%d) %gx+%gy=%g, bisecting %d %d\n", e->edgenbr, e->a, e->b, e->c, e->reg[le]->sitenbr, e->reg[re]->sitenbr);
29
+ }
30
+
31
+ void
32
+ out_ep(Edge * e)
33
+ {
34
+ /* Save endpoint to our ruby object */
35
+ (*rubyvorState.storeE)(e->edgenbr,
36
+ e->ep[le] != (Site *)NULL ? e->ep[le]->sitenbr : -1,
37
+ e->ep[re] != (Site *)NULL ? e->ep[re]->sitenbr : -1);
38
+
39
+ /*
40
+ printf("e %d", e->edgenbr);
41
+ printf(" %d ", e->ep[le] != (Site *)NULL ? e->ep[le]->sitenbr : -1) ;
42
+ printf("%d\n", e->ep[re] != (Site *)NULL ? e->ep[re]->sitenbr : -1) ;
43
+ */
44
+
45
+ if (rubyvorState.plot)
46
+ clip_line(e) ;
47
+ }
48
+
49
+ void
50
+ out_vertex(Site * v)
51
+ {
52
+ /* Save vertex to our ruby object */
53
+ (*rubyvorState.storeV)(v->coord.x, v->coord.y);
54
+
55
+ /* printf ("v %f %f\n", v->coord.x, v->coord.y) ; */
56
+
57
+ if (rubyvorState.debug)
58
+ printf("vertex(%d) at %f %f\n", v->sitenbr, v->coord.x, v->coord.y);
59
+ }
60
+
61
+ void
62
+ out_site(Site * s)
63
+ {
64
+ /* Save site to our ruby object */
65
+ (*rubyvorState.storeS)(s->coord.x, s->coord.y);
66
+
67
+ /* printf("s %f %f\n", s->coord.x, s->coord.y) ; */
68
+
69
+ if (rubyvorState.plot)
70
+ circle (s->coord.x, s->coord.y, cradius);
71
+
72
+ if (rubyvorState.debug)
73
+ printf("site (%d) at %f %f\n", s->sitenbr, s->coord.x, s->coord.y);
74
+ }
75
+
76
+ void
77
+ out_triple(Site * s1, Site * s2, Site * s3)
78
+ {
79
+ /* Save triplet to our ruby object */
80
+ (*rubyvorState.storeT)(s1->sitenbr, s2->sitenbr, s3->sitenbr);
81
+
82
+ if (rubyvorState.debug)
83
+ {
84
+ printf("%d %d %d\n", s1->sitenbr, s2->sitenbr, s3->sitenbr);
85
+ printf("circle through left=%d right=%d bottom=%d\n",
86
+ s1->sitenbr, s2->sitenbr, s3->sitenbr) ;
87
+ }
88
+ }
89
+
90
+ void
91
+ plotinit(void)
92
+ {
93
+ float dx, dy, d ;
94
+
95
+ dy = rubyvorState.ymax - rubyvorState.ymin ;
96
+ dx = rubyvorState.xmax - rubyvorState.xmin ;
97
+ d = ( dx > dy ? dx : dy) * 1.1 ;
98
+ pxmin = rubyvorState.xmin - (d-dx) / 2.0 ;
99
+ pxmax = rubyvorState.xmax + (d-dx) / 2.0 ;
100
+ pymin = rubyvorState.ymin - (d-dy) / 2.0 ;
101
+ pymax = rubyvorState.ymax + (d-dy) / 2.0 ;
102
+ cradius = (pxmax - pxmin) / 350.0 ;
103
+ openpl() ;
104
+ range(pxmin, pymin, pxmax, pymax) ;
105
+ }
106
+
107
+ void
108
+ clip_line(Edge * e)
109
+ {
110
+ Site * s1, * s2 ;
111
+ float x1, x2, y1, y2 ;
112
+
113
+ if (e->a == 1.0 && e->b >= 0.0)
114
+ {
115
+ s1 = e->ep[1] ;
116
+ s2 = e->ep[0] ;
117
+ }
118
+ else
119
+ {
120
+ s1 = e->ep[0] ;
121
+ s2 = e->ep[1] ;
122
+ }
123
+ if (e->a == 1.0)
124
+ {
125
+ y1 = pymin ;
126
+ if (s1 != (Site *)NULL && s1->coord.y > pymin)
127
+ {
128
+ y1 = s1->coord.y ;
129
+ }
130
+ if (y1 > pymax)
131
+ {
132
+ return ;
133
+ }
134
+ x1 = e->c - e->b * y1 ;
135
+ y2 = pymax ;
136
+ if (s2 != (Site *)NULL && s2->coord.y < pymax)
137
+ {
138
+ y2 = s2->coord.y ;
139
+ }
140
+ if (y2 < pymin)
141
+ {
142
+ return ;
143
+ }
144
+ x2 = e->c - e->b * y2 ;
145
+ if (((x1 > pxmax) && (x2 > pxmax)) || ((x1 < pxmin) && (x2 < pxmin)))
146
+ {
147
+ return ;
148
+ }
149
+ if (x1 > pxmax)
150
+ {
151
+ x1 = pxmax ;
152
+ y1 = (e->c - x1) / e->b ;
153
+ }
154
+ if (x1 < pxmin)
155
+ {
156
+ x1 = pxmin ;
157
+ y1 = (e->c - x1) / e->b ;
158
+ }
159
+ if (x2 > pxmax)
160
+ {
161
+ x2 = pxmax ;
162
+ y2 = (e->c - x2) / e->b ;
163
+ }
164
+ if (x2 < pxmin)
165
+ {
166
+ x2 = pxmin ;
167
+ y2 = (e->c - x2) / e->b ;
168
+ }
169
+ }
170
+ else
171
+ {
172
+ x1 = pxmin ;
173
+ if (s1 != (Site *)NULL && s1->coord.x > pxmin)
174
+ {
175
+ x1 = s1->coord.x ;
176
+ }
177
+ if (x1 > pxmax)
178
+ {
179
+ return ;
180
+ }
181
+ y1 = e->c - e->a * x1 ;
182
+ x2 = pxmax ;
183
+ if (s2 != (Site *)NULL && s2->coord.x < pxmax)
184
+ {
185
+ x2 = s2->coord.x ;
186
+ }
187
+ if (x2 < pxmin)
188
+ {
189
+ return ;
190
+ }
191
+ y2 = e->c - e->a * x2 ;
192
+ if (((y1 > pymax) && (y2 > pymax)) || ((y1 < pymin) && (y2 <pymin)))
193
+ {
194
+ return ;
195
+ }
196
+ if (y1> pymax)
197
+ {
198
+ y1 = pymax ;
199
+ x1 = (e->c - y1) / e->a ;
200
+ }
201
+ if (y1 < pymin)
202
+ {
203
+ y1 = pymin ;
204
+ x1 = (e->c - y1) / e->a ;
205
+ }
206
+ if (y2 > pymax)
207
+ {
208
+ y2 = pymax ;
209
+ x2 = (e->c - y2) / e->a ;
210
+ }
211
+ if (y2 < pymin)
212
+ {
213
+ y2 = pymin ;
214
+ x2 = (e->c - y2) / e->a ;
215
+ }
216
+ }
217
+ line(x1,y1,x2,y2);
218
+ }
219
+
220
+ /* Linux-specific. */
221
+ void
222
+ debug_memory(void)
223
+ {
224
+ char buf[30];
225
+ FILE* pf;
226
+ int tmp;
227
+ float totalSize;
228
+ unsigned size;/* total program size */
229
+
230
+ /*
231
+ unsigned resident;// resident set size
232
+ unsigned share;// shared pages
233
+ unsigned text;// text (code)
234
+ unsigned lib;// library
235
+ unsigned data;// data/stack
236
+ unsigned dt;// dirty pages (unused in Linux 2.6)
237
+ */
238
+
239
+ if (!rubyvorState.debug)
240
+ return;
241
+
242
+ snprintf(buf, 30, "/proc/%u/statm", (unsigned)getpid());
243
+ pf = fopen(buf, "r");
244
+ if (NULL != pf)
245
+ {
246
+ tmp = fscanf(pf, "%u", &size); /*, %u, %u ... etc &resident, &share, &text, &lib, &data); */
247
+ totalSize = (float)size / 1024.0;
248
+ fprintf(stderr, "%f ", totalSize);
249
+ }
250
+ fclose(pf);
251
+ }