int main(){ struct codetype cd,code[MAXSYMBS]; struct nodetype node[MAXNODES]; int i,k,p,p1,p2,root; char symb,alph[MAXSYMBS]; for(i=0;i<MAXSYMBS;i++) alph[i]=' '; //scanf("%d",&n); input(); // cria o heap binario for(i=0;i<256;i++){ //flushall(); // scanf("%c %d",&symb,&node[i].freq); symb = a[i].alph; node[i].freq = a[i].freq; hinsert(i,node[i].freq); alph[i]=symb; } // monta a arvore com o codigo de huffman for(p=0;p<(2*256-1);p++){ p1 =hmin(); p2 =hmin(); node[p1].father = p; node[p1].isleft = 1; node[p2].father = p; node[p2].isleft = 0; node[p].freq =node[p1].freq+node[p2].freq; hinsert(p,node[p].freq); } root = hmin(); for(i=0;i<256;i++){ cd.startpos = MAXBITS; p=i; while(p!=root){ --cd.startpos ; if(node[p].isleft) cd.bits[cd.startpos] =0; else cd.bits[cd.startpos] =1; p =node[p].father; } for(k=cd.startpos;k<MAXBITS;k++) code[i].bits[k]=cd.bits[k]; code[i].startpos =cd.startpos; } for(i=0;i<256;i++){ printf("\n%c %d",alph[i],node[i].freq); for(k=code[i].startpos;k<MAXBITS;k++) printf(" %d",code[i].bits[k]); printf("\n"); } return(0); }
dbuf_t *dtry_reasm_timed(void *pile, uint32_t xid, char *data, uint16_t len, uint16_t offs, int more, time_t now) { reasm_pile_struct_t *rp = (void *)(((dbuf_t *)pile)->buf); reasm_chunk_t *chk; if (now > 0) { check_timeouts(rp, now); } if(offs + len > rp->mtu) { debug(DBG_REASM, 10, "Offset + length (%d + %d) of fragment > MTU (%d), discard", offs, len, rp->mtu); return NULL; } if ((offs > 0) && (offs < HOLE_MIN_LENGTH)) { debug(DBG_REASM, 10, "Offset %d less than min hole length %d\n", offs, HOLE_MIN_LENGTH); return NULL; } chk = hfind(rp->chs, &xid, sizeof(xid)); if (!chk) { debug(DBG_REASM, 10, "Reasm chunk %lu not found, creating", xid); chk = malloc(sizeof(reasm_chunk_t)); chk->xid = xid; chk->maxfrags = rp->maxfrags; chk->hole = 0; chk->esize = rp->ftu; chk->d = dalloc(chk->esize); chk->deadline = now + rp->reasm_timeout; memset(chk->d->buf, 0xaa, chk->d->size); hole_set(chk, 0, chk->esize, 0); hinsert(rp->chs, &xid, sizeof(xid), chk, chk_destructor, NULL, NULL, NULL); TAILQ_INSERT_TAIL(&rp->lru, chk, entries); } else { debug(DBG_REASM, 10, "Reasm chunk %lu found", xid); } debug(DBG_REASM, 100, "Chunk data (hole: %d, esize: %d):", chk->hole, chk->esize); debug_dump(DBG_REASM, 100, chk->d->buf, chk->d->size); if(offs + len > chk->d->size) { debug(DBG_REASM, 10, "Reasm chunk %lu overflow - %d + %d > %d", xid, offs, len, chk->d->size); /* We already checked the MTU overflow above, so we can safely reallocate here */ int oldsize = chk->d->size; dgrow(chk->d, rp->mtu - chk->d->size); hole_grow(chk, oldsize-1, chk->d->size); chk->esize = chk->d->size; debug(DBG_REASM, 100, "Fresh chunk data after growth to MTU:"); debug_dump(DBG_REASM, 100, chk->d->buf, chk->d->size); } chk->atime = now; return dperform_reasm(rp, chk, xid, data, len, offs, more); }
void shuffle1(const char *s, int l, int k) { int i, j, n_lets; s_ = s; l_ = l; k_ = k; if (k_ >= l_ || k_ <= 1) /* two special cases */ return; /* use hashtable to find distinct vertices */ n_lets = l_ - k_ + 2; /* number of (k-1)-lets */ n_vertices = 0; hinit(n_lets); for (i = 0; i < n_lets; i++) hinsert(i); root = entries[n_lets - 1].i_vertices; /* the last let */ if (vertices) free(vertices); vertices = malloc0(n_vertices * sizeof(vertex)); /* set i_sequence and n_indices for each vertex */ for (i = 0; i < n_lets; i++) { /* for each let */ hentry *ev = &entries[i]; vertex *v = &vertices[ev->i_vertices]; v->i_sequence = ev->i_sequence; if (i < n_lets - 1) /* not the last let */ v->n_indices++; } /* distribute indices for each vertex */ if (indices) free(indices); indices = malloc0((n_lets - 1) * sizeof(int)); j = 0; for (i = 0; i < n_vertices; i++) { /* for each vertex */ vertex *v = &vertices[i]; v->indices = indices + j; j += v->n_indices; } /* populate indices for each vertex */ for (i = 0; i < n_lets - 1; i++) { /* for each edge */ hentry *eu = &entries[i]; hentry *ev = &entries[i + 1]; vertex *u = &vertices[eu->i_vertices]; u->indices[u->i_indices++] = ev->i_vertices; } hcleanup(); }
void * hinserts(htable_t *ht, char *key, void *data, hcallback_func_t *destructor, hcallback_func_t *can_replace, hcallback_func_t *do_delete, int *did_delete) { return hinsert(ht, key, strlen(key)+1, data, destructor, can_replace, do_delete, did_delete); }