9 2d bitmask with test and set operations
13 btm_new(int xsize,int ysize) {
16 btm=(struct i_bitmap*)mymalloc(sizeof(struct i_bitmap));
17 btm->data=(char*)mymalloc((xsize*ysize+8)/8);
20 for(i=0;i<(xsize*ysize+8)/8;i++) btm->data[i]=0; /* Is this always needed */
26 btm_destroy(struct i_bitmap *btm) {
33 btm_test(struct i_bitmap *btm,int x,int y) {
35 if (x<0 || x>btm->xsize-1 || y<0 || y>btm->ysize-1) return 0;
37 return (1<<(btno%8))&(btm->data[btno/8]);
41 btm_set(struct i_bitmap *btm,int x,int y) {
44 btm->data[btno/8]|=1<<(btno%8);
52 Linked list - stack type
56 llink_new(struct llink* p,int size) {
58 l=(struct llink*)mymalloc(sizeof(struct llink));
62 l->data=(void*)mymalloc(size);
66 /* free's the data pointer, itself, and sets the previous' next pointer to null */
69 llink_destroy(struct llink* l) {
70 if (l->p != NULL) { l->p->n=NULL; }
76 /* if it returns true there wasn't room for the
80 llist_llink_push(struct llist *lst, struct llink *lnk,void *data) {
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);
90 /* printf("data=%X res=%X\n",*(int*)data,*(int*)(lnk->data));*/
97 llist_new(int multip, int ssize) {
99 l=(struct llist*)mymalloc(sizeof(struct llist));
108 llist_push(struct llist *l,void *data) {
110 int multip=l->multip;
112 /* fprintf(stderr,"llist_push: data=0x%08X\n",data); */
114 if (l->t == NULL) l->t=l->h=llink_new(NULL,ssize*multip); /* Tail is empty - list is empty */
115 else { /* Check for overflow in current tail */
116 if (l->t->fill >= l->multip) {
117 struct llink* nt=llink_new(l->t,ssize*multip);
120 /* fprintf(stderr,"Chain extended\n"); */
123 /* fprintf(stderr,"0x%08X\n",l->t); */
124 if (llist_llink_push(l,l->t,data)) { fprintf(stderr,"DARN!\n"); }
127 /* returns 0 if the list is empty */
130 llist_pop(struct llist *l,void *data) {
131 /* int ssize=l->ssize;
132 int multip=l->multip;*/
133 if (l->t == NULL) return 0;
136 /* memcpy(data,(char*)(l->t->data)+l->ssize*l->t->fill,l->ssize); */
137 memcpy(data,(char*)(l->t->data)+l->ssize*l->t->fill,l->ssize);
139 if (!l->t->fill) { /* This link empty */
140 if (l->t->p == NULL) l->h=l->t=NULL; /* and it's the only link */
143 llink_destroy(l->t->n);
150 llist_dump(struct llist *l) {
156 for(j=0;j<lnk->fill;j++) {
157 /* memcpy(&k,(char*)(lnk->data)+l->ssize*j,sizeof(void*));*/
158 memcpy(&k,(char*)(lnk->data)+l->ssize*j,sizeof(void*));
159 printf("%d - %X\n",i,k);
167 llist_destroy(struct llist *l) {
168 struct llink *t,*lnk = l->h;
169 while( lnk != NULL ) {
183 Oct-tree implementation
191 t=(struct octt*)mymalloc(sizeof(struct octt));
192 for(i=0;i<8;i++) t->t[i]=NULL;
198 /* returns 1 if the colors wasn't in the octtree already */
202 octt_add(struct octt *ct,unsigned char r,unsigned char g,unsigned char b) {
209 /* printf("[r,g,b]=[%d,%d,%d]\n",r,g,b); */
213 ci=((!!(r&cm))<<2)+((!!(g&cm))<<1)+!!(b&cm);
214 /* printf("idx[%d]=%d\n",i,ci); */
215 if (c->t[ci] == NULL) { c->t[ci]=octt_new(); rc=1; }
225 octt_delete(struct octt *ct) {
227 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 */
233 octt_dump(struct octt *ct) {
235 /* printf("node [0x%08X] -> (%d)\n",ct,ct->cnt); */
236 for(i=0;i<8;i++) if (ct->t[i] != NULL) printf("[ %d ] -> 0x%08X\n",i,(unsigned int)ct->t[i]);
237 for(i=0;i<8;i++) if (ct->t[i] != NULL) octt_dump(ct->t[i]);
240 /* note that all calls of octt_count are operating on the same overflow
241 variable so all calls will know at the same time if an overflow
242 has occured and stops there. */
245 octt_count(struct octt *ct,int *tot,int max,int *overflow) {
248 if (!(*overflow)) return;
249 for(i=0;i<8;i++) if (ct->t[i]!=NULL) {
250 octt_count(ct->t[i],tot,max,overflow);
254 if ( (*tot) > (*overflow) ) *overflow=0;