9 2d bitmask with test and set operations
13 btm_new(int xsize,int ysize) {
17 btm=(struct i_bitmap*)mymalloc(sizeof(struct i_bitmap)); /* checked 4jul05 tonyc */
18 bytes = (xsize*ysize+8)/8;
19 if (bytes * 8 / ysize < xsize-1) { /* this is kind of rough */
20 fprintf(stderr, "Integer overflow allocating bitmap %d x %d", xsize, ysize);
23 btm->data=(char*)mymalloc(bytes); /* checked 4jul05 tonyc */
26 for(i=0;i<(xsize*ysize+8)/8;i++) btm->data[i]=0; /* Is this always needed */
32 btm_destroy(struct i_bitmap *btm) {
39 btm_test(struct i_bitmap *btm,int x,int y) {
41 if (x<0 || x>btm->xsize-1 || y<0 || y>btm->ysize-1) return 0;
43 return (1<<(btno%8))&(btm->data[btno/8]);
47 btm_set(struct i_bitmap *btm,int x,int y) {
49 if (x<0 || x>btm->xsize-1 || y<0 || y>btm->ysize-1) abort();
51 btm->data[btno/8]|=1<<(btno%8);
59 Bucketed linked list - stack type
63 llink_new(struct llink* p,int size) {
65 l = mymalloc(sizeof(struct llink)); /* checked 4jul05 tonyc */
69 l->data = mymalloc(size); /* checked 4jul05 tonyc - depends on caller to llist_push */
73 /* free's the data pointer, itself, and sets the previous' next pointer to null */
76 llink_destroy(struct llink* l) {
77 if (l->p != NULL) { l->p->n=NULL; }
83 /* if it returns true there wasn't room for the
87 llist_llink_push(struct llist *lst, struct llink *lnk,void *data) {
91 /* fprintf(stderr,"llist_llink_push: data=0x%08X -> 0x%08X\n",data,*(int*)data);
92 fprintf(stderr,"ssize = %d, multip = %d, fill = %d\n",lst->ssize,lst->multip,lnk->fill); */
93 if (lnk->fill == lst->multip) return 1;
94 /* memcpy((char*)(lnk->data)+lnk->fill*lst->ssize,data,lst->ssize); */
95 memcpy((char*)(lnk->data)+lnk->fill*lst->ssize,data,lst->ssize);
97 /* printf("data=%X res=%X\n",*(int*)data,*(int*)(lnk->data));*/
104 llist_new(int multip, int ssize) {
106 l = mymalloc(sizeof(struct llist)); /* checked 4jul05 tonyc */
116 llist_push(struct llist *l,void *data) {
117 int ssize = l->ssize;
118 int multip = l->multip;
120 /* fprintf(stderr,"llist_push: data=0x%08X\n",data);
121 fprintf(stderr,"Chain size: %d\n", l->count); */
124 l->t = l->h = llink_new(NULL,ssize*multip); /* Tail is empty - list is empty */
125 /* fprintf(stderr,"Chain empty - extended\n"); */
127 else { /* Check for overflow in current tail */
128 if (l->t->fill >= l->multip) {
129 struct llink* nt = llink_new(l->t, ssize*multip);
132 /* fprintf(stderr,"Chain extended\n"); */
135 /* fprintf(stderr,"0x%08X\n",l->t); */
136 if (llist_llink_push(l,l->t,data)) {
137 m_fatal(3, "out of memory\n");
141 /* returns 0 if the list is empty */
144 llist_pop(struct llist *l,void *data) {
145 /* int ssize=l->ssize;
146 int multip=l->multip;*/
147 if (l->t == NULL) return 0;
150 memcpy(data,(char*)(l->t->data)+l->ssize*l->t->fill,l->ssize);
152 if (!l->t->fill) { /* This link empty */
153 if (l->t->p == NULL) { /* and it's the only link */
159 llink_destroy(l->t->n);
166 llist_dump(struct llist *l) {
172 for(j=0;j<lnk->fill;j++) {
173 /* memcpy(&k,(char*)(lnk->data)+l->ssize*j,sizeof(void*));*/
174 memcpy(&k,(char*)(lnk->data)+l->ssize*j,sizeof(void*));
175 printf("%d - %X\n",i,k);
183 llist_destroy(struct llist *l) {
184 struct llink *t,*lnk = l->h;
185 while( lnk != NULL ) {
199 Oct-tree implementation
207 t=(struct octt*)mymalloc(sizeof(struct octt)); /* checked 4jul05 tonyc */
208 for(i=0;i<8;i++) t->t[i]=NULL;
214 /* returns 1 if the colors wasn't in the octtree already */
218 octt_add(struct octt *ct,unsigned char r,unsigned char g,unsigned char b) {
225 /* printf("[r,g,b]=[%d,%d,%d]\n",r,g,b); */
229 ci=((!!(r&cm))<<2)+((!!(g&cm))<<1)+!!(b&cm);
230 /* printf("idx[%d]=%d\n",i,ci); */
231 if (c->t[ci] == NULL) { c->t[ci]=octt_new(); rc=1; }
241 octt_delete(struct octt *ct) {
243 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 */
249 octt_dump(struct octt *ct) {
251 /* printf("node [0x%08X] -> (%d)\n",ct,ct->cnt); */
252 for(i=0;i<8;i++) if (ct->t[i] != NULL) printf("[ %d ] -> 0x%08X\n",i,(unsigned int)ct->t[i]);
253 for(i=0;i<8;i++) if (ct->t[i] != NULL) octt_dump(ct->t[i]);
256 /* note that all calls of octt_count are operating on the same overflow
257 variable so all calls will know at the same time if an overflow
258 has occured and stops there. */
261 octt_count(struct octt *ct,int *tot,int max,int *overflow) {
264 if (!(*overflow)) return;
265 for(i=0;i<8;i++) if (ct->t[i]!=NULL) {
266 octt_count(ct->t[i],tot,max,overflow);
270 if ( (*tot) > (*overflow) ) *overflow=0;