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 Bucketed linked list - stack type
56 llink_new(struct llink* p,int size) {
58 l = mymalloc(sizeof(struct llink));
62 l->data = 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 = mymalloc(sizeof(struct llist));
109 llist_push(struct llist *l,void *data) {
110 int ssize = l->ssize;
111 int multip = l->multip;
113 /* fprintf(stderr,"llist_push: data=0x%08X\n",data);
114 fprintf(stderr,"Chain size: %d\n", l->count); */
117 l->t = l->h = llink_new(NULL,ssize*multip); /* Tail is empty - list is empty */
118 /* fprintf(stderr,"Chain empty - extended\n"); */
120 else { /* Check for overflow in current tail */
121 if (l->t->fill >= l->multip) {
122 struct llink* nt = llink_new(l->t, ssize*multip);
125 /* fprintf(stderr,"Chain extended\n"); */
128 /* fprintf(stderr,"0x%08X\n",l->t); */
129 if (llist_llink_push(l,l->t,data)) {
130 m_fatal(3, "out of memory\n");
134 /* returns 0 if the list is empty */
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;
143 memcpy(data,(char*)(l->t->data)+l->ssize*l->t->fill,l->ssize);
145 if (!l->t->fill) { /* This link empty */
146 if (l->t->p == NULL) { /* and it's the only link */
152 llink_destroy(l->t->n);
159 llist_dump(struct llist *l) {
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);
176 llist_destroy(struct llist *l) {
177 struct llink *t,*lnk = l->h;
178 while( lnk != NULL ) {
192 Oct-tree implementation
200 t=(struct octt*)mymalloc(sizeof(struct octt));
201 for(i=0;i<8;i++) t->t[i]=NULL;
207 /* returns 1 if the colors wasn't in the octtree already */
211 octt_add(struct octt *ct,unsigned char r,unsigned char g,unsigned char b) {
218 /* printf("[r,g,b]=[%d,%d,%d]\n",r,g,b); */
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; }
234 octt_delete(struct octt *ct) {
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 */
242 octt_dump(struct octt *ct) {
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]);
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. */
254 octt_count(struct octt *ct,int *tot,int max,int *overflow) {
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);
263 if ( (*tot) > (*overflow) ) *overflow=0;