]>
Commit | Line | Data |
---|---|---|
02d1d628 AMH |
1 | #include "io.h" |
2 | #include "datatypes.h" | |
3 | #include <stdlib.h> | |
4 | #include <stdio.h> | |
5 | ||
6 | ||
7 | ||
8 | /* | |
9 | 2d bitmask with test and set operations | |
10 | */ | |
11 | ||
12 | struct i_bitmap* | |
13 | btm_new(int xsize,int ysize) { | |
14 | int i; | |
15 | struct i_bitmap *btm; | |
16 | btm=(struct i_bitmap*)mymalloc(sizeof(struct i_bitmap)); | |
17 | btm->data=(char*)mymalloc((xsize*ysize+8)/8); | |
18 | btm->xsize=xsize; | |
19 | btm->ysize=ysize; | |
20 | for(i=0;i<(xsize*ysize+8)/8;i++) btm->data[i]=0; /* Is this always needed */ | |
21 | return btm; | |
22 | } | |
23 | ||
24 | ||
25 | void | |
26 | btm_destroy(struct i_bitmap *btm) { | |
27 | myfree(btm->data); | |
28 | myfree(btm); | |
29 | } | |
30 | ||
31 | ||
32 | int | |
33 | btm_test(struct i_bitmap *btm,int x,int y) { | |
34 | int btno; | |
35 | if (x<0 || x>btm->xsize-1 || y<0 || y>btm->ysize-1) return 0; | |
36 | btno=btm->xsize*y+x; | |
37 | return (1<<(btno%8))&(btm->data[btno/8]); | |
38 | } | |
39 | ||
40 | void | |
41 | btm_set(struct i_bitmap *btm,int x,int y) { | |
42 | int btno; | |
43 | btno=btm->xsize*y+x; | |
44 | btm->data[btno/8]|=1<<(btno%8); | |
45 | } | |
46 | ||
47 | ||
48 | ||
49 | ||
50 | ||
51 | /* | |
a73aeb5f | 52 | Bucketed linked list - stack type |
02d1d628 AMH |
53 | */ |
54 | ||
55 | struct llink * | |
56 | llink_new(struct llink* p,int size) { | |
57 | struct llink *l; | |
a73aeb5f AMH |
58 | l = mymalloc(sizeof(struct llink)); |
59 | l->n = NULL; | |
60 | l->p = p; | |
61 | l->fill = 0; | |
62 | l->data = mymalloc(size); | |
02d1d628 AMH |
63 | return l; |
64 | } | |
65 | ||
66 | /* free's the data pointer, itself, and sets the previous' next pointer to null */ | |
67 | ||
68 | void | |
69 | llink_destroy(struct llink* l) { | |
70 | if (l->p != NULL) { l->p->n=NULL; } | |
71 | myfree(l->data); | |
72 | myfree(l); | |
73 | } | |
74 | ||
75 | ||
76 | /* if it returns true there wasn't room for the | |
77 | item on the link */ | |
78 | ||
79 | int | |
80 | llist_llink_push(struct llist *lst, struct llink *lnk,void *data) { | |
81 | int multip; | |
a73aeb5f | 82 | multip = lst->multip; |
02d1d628 AMH |
83 | |
84 | /* fprintf(stderr,"llist_llink_push: data=0x%08X -> 0x%08X\n",data,*(int*)data); | |
85 | fprintf(stderr,"ssize = %d, multip = %d, fill = %d\n",lst->ssize,lst->multip,lnk->fill); */ | |
86 | if (lnk->fill == lst->multip) return 1; | |
87 | /* memcpy((char*)(lnk->data)+lnk->fill*lst->ssize,data,lst->ssize); */ | |
88 | memcpy((char*)(lnk->data)+lnk->fill*lst->ssize,data,lst->ssize); | |
89 | ||
90 | /* printf("data=%X res=%X\n",*(int*)data,*(int*)(lnk->data));*/ | |
91 | lnk->fill++; | |
92 | lst->count++; | |
93 | return 0; | |
94 | } | |
95 | ||
96 | struct llist * | |
97 | llist_new(int multip, int ssize) { | |
98 | struct llist *l; | |
a73aeb5f AMH |
99 | l = mymalloc(sizeof(struct llist)); |
100 | l->h = NULL; | |
101 | l->t = NULL; | |
102 | l->multip = multip; | |
103 | l->ssize = ssize; | |
104 | l->count = 0; | |
02d1d628 AMH |
105 | return l; |
106 | } | |
107 | ||
108 | void | |
109 | llist_push(struct llist *l,void *data) { | |
a73aeb5f AMH |
110 | int ssize = l->ssize; |
111 | int multip = l->multip; | |
02d1d628 | 112 | |
a73aeb5f AMH |
113 | /* fprintf(stderr,"llist_push: data=0x%08X\n",data); |
114 | fprintf(stderr,"Chain size: %d\n", l->count); */ | |
115 | ||
116 | if (l->t == NULL) { | |
117 | l->t = l->h = llink_new(NULL,ssize*multip); /* Tail is empty - list is empty */ | |
118 | /* fprintf(stderr,"Chain empty - extended\n"); */ | |
119 | } | |
02d1d628 AMH |
120 | else { /* Check for overflow in current tail */ |
121 | if (l->t->fill >= l->multip) { | |
a73aeb5f | 122 | struct llink* nt = llink_new(l->t, ssize*multip); |
02d1d628 AMH |
123 | l->t->n=nt; |
124 | l->t=nt; | |
125 | /* fprintf(stderr,"Chain extended\n"); */ | |
126 | } | |
127 | } | |
128 | /* fprintf(stderr,"0x%08X\n",l->t); */ | |
a73aeb5f AMH |
129 | if (llist_llink_push(l,l->t,data)) { |
130 | m_fatal(3, "out of memory\n"); | |
131 | } | |
02d1d628 AMH |
132 | } |
133 | ||
134 | /* returns 0 if the list is empty */ | |
135 | ||
136 | int | |
137 | llist_pop(struct llist *l,void *data) { | |
138 | /* int ssize=l->ssize; | |
139 | int multip=l->multip;*/ | |
140 | if (l->t == NULL) return 0; | |
141 | l->t->fill--; | |
142 | l->count--; | |
02d1d628 AMH |
143 | memcpy(data,(char*)(l->t->data)+l->ssize*l->t->fill,l->ssize); |
144 | ||
145 | if (!l->t->fill) { /* This link empty */ | |
a73aeb5f AMH |
146 | if (l->t->p == NULL) { /* and it's the only link */ |
147 | llink_destroy(l->t); | |
148 | l->h = l->t = NULL; | |
149 | } | |
02d1d628 AMH |
150 | else { |
151 | l->t=l->t->p; | |
152 | llink_destroy(l->t->n); | |
153 | } | |
154 | } | |
155 | return 1; | |
156 | } | |
157 | ||
158 | void | |
159 | llist_dump(struct llist *l) { | |
160 | int k,j; | |
161 | int i=0; | |
162 | struct llink *lnk; | |
163 | lnk=l->h; | |
164 | while(lnk != NULL) { | |
165 | for(j=0;j<lnk->fill;j++) { | |
166 | /* memcpy(&k,(char*)(lnk->data)+l->ssize*j,sizeof(void*));*/ | |
167 | memcpy(&k,(char*)(lnk->data)+l->ssize*j,sizeof(void*)); | |
168 | printf("%d - %X\n",i,k); | |
169 | i++; | |
170 | } | |
171 | lnk=lnk->n; | |
172 | } | |
173 | } | |
174 | ||
175 | void | |
176 | llist_destroy(struct llist *l) { | |
177 | struct llink *t,*lnk = l->h; | |
178 | while( lnk != NULL ) { | |
179 | t=lnk; | |
180 | lnk=lnk->n; | |
181 | myfree(t); | |
182 | } | |
183 | myfree(l); | |
184 | } | |
185 | ||
186 | ||
187 | ||
188 | ||
189 | ||
190 | ||
191 | /* | |
192 | Oct-tree implementation | |
193 | */ | |
194 | ||
195 | struct octt * | |
196 | octt_new() { | |
197 | int i; | |
198 | struct octt *t; | |
199 | ||
200 | t=(struct octt*)mymalloc(sizeof(struct octt)); | |
201 | for(i=0;i<8;i++) t->t[i]=NULL; | |
202 | t->cnt=0; | |
203 | return t; | |
204 | } | |
205 | ||
206 | ||
207 | /* returns 1 if the colors wasn't in the octtree already */ | |
208 | ||
209 | ||
210 | int | |
211 | octt_add(struct octt *ct,unsigned char r,unsigned char g,unsigned char b) { | |
212 | struct octt *c; | |
213 | int i,cm; | |
214 | int ci,idx[8]; | |
215 | int rc; | |
216 | rc=0; | |
217 | c=ct; | |
218 | /* printf("[r,g,b]=[%d,%d,%d]\n",r,g,b); */ | |
219 | ct->cnt++; | |
220 | for(i=7;i>-1;i--) { | |
221 | cm=1<<i; | |
222 | ci=((!!(r&cm))<<2)+((!!(g&cm))<<1)+!!(b&cm); | |
223 | /* printf("idx[%d]=%d\n",i,ci); */ | |
224 | if (c->t[ci] == NULL) { c->t[ci]=octt_new(); rc=1; } | |
225 | c=c->t[ci]; | |
226 | c->cnt++; | |
227 | idx[i]=ci; | |
228 | } | |
229 | return rc; | |
230 | } | |
231 | ||
232 | ||
233 | void | |
234 | octt_delete(struct octt *ct) { | |
235 | int i; | |
236 | for(i=0;i<8;i++) if (ct->t[i] != NULL) octt_delete(ct->t[i]); /* do not free instance here because it will free itself */ | |
237 | myfree(ct); | |
238 | } | |
239 | ||
240 | ||
241 | void | |
242 | octt_dump(struct octt *ct) { | |
243 | int i; | |
244 | /* printf("node [0x%08X] -> (%d)\n",ct,ct->cnt); */ | |
245 | for(i=0;i<8;i++) if (ct->t[i] != NULL) printf("[ %d ] -> 0x%08X\n",i,(unsigned int)ct->t[i]); | |
246 | for(i=0;i<8;i++) if (ct->t[i] != NULL) octt_dump(ct->t[i]); | |
247 | } | |
248 | ||
249 | /* note that all calls of octt_count are operating on the same overflow | |
250 | variable so all calls will know at the same time if an overflow | |
251 | has occured and stops there. */ | |
252 | ||
253 | void | |
254 | octt_count(struct octt *ct,int *tot,int max,int *overflow) { | |
255 | int i,c; | |
256 | c=0; | |
257 | if (!(*overflow)) return; | |
258 | for(i=0;i<8;i++) if (ct->t[i]!=NULL) { | |
259 | octt_count(ct->t[i],tot,max,overflow); | |
260 | c++; | |
261 | } | |
262 | if (!c) (*tot)++; | |
263 | if ( (*tot) > (*overflow) ) *overflow=0; | |
264 | } |