small bugfixes and memory optimizations
[swftools.git] / lib / gfxpoly / xrow.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <memory.h>
4 #include "../mem.h"
5 #include "xrow.h"
6
7 xrow_t* xrow_new()
8 {
9     xrow_t*r = rfx_calloc(sizeof(xrow_t));
10     r->size = 16;
11     r->x = rfx_alloc(sizeof(r->x[0])*r->size);
12     return r;
13 }
14
15 void xrow_add(xrow_t*r, int32_t x)
16 {
17     if(r->num && r->lastx==x)
18         return;
19     r->lastx = x;
20     if(r->num >= r->size) {
21         r->size *= 2;
22         r->x = rfx_realloc(r->x, sizeof(r->x[0])*r->size);
23     }
24     r->x[r->num++]=x;
25 }
26
27 int compare_int32(const void*_i1,const void*_i2)
28 {
29     int32_t*i1 = (int32_t*)_i1;
30     int32_t*i2 = (int32_t*)_i2;
31     return *i1-*i2;
32 }
33
34 void xrow_sort(xrow_t*r)
35 {
36     if(!r->num)
37         return;
38     qsort(r->x, r->num, sizeof(r->x[0]), compare_int32);
39     int t;
40     int pos = 1;
41     int32_t lastx=r->x[0];
42     for(t=1;t<r->num;t++) {
43         if(r->x[t]!=lastx) {
44             r->x[pos++] = lastx = r->x[t];
45         }
46     }
47     r->num = pos;
48 }
49
50 void xrow_reset(xrow_t*r)
51 {
52     r->num = 0;
53 }
54
55 void xrow_dump(xrow_t*xrow)
56 {
57     fprintf(stderr, "x: ");
58     int t;
59     for(t=0;t<xrow->num;t++) {
60         if(t)
61             fprintf(stderr, ", ");
62         fprintf(stderr, "%d", xrow->x[t]);
63     }
64     fprintf(stderr, "\n");
65 }
66
67 void xrow_destroy(xrow_t*r)
68 {
69     if(r->x) {
70         free(r->x);r->x = 0;
71     }
72     free(r);
73 }