bool buffer_isEmpty(bool virtual_f) { FHeap* heap = virtual_f ? virtual_flows : flows; if (heap->count > 0) return (flow_next(heap_front(heap)) == NULL); else return TRUE; }
// pop next flow, dequeue next packet, push flow back to heap Packet* removePacketFromBuffer(bool virtual_f) { FHeap* heap = virtual_f ? virtual_flows : flows; Flow* flow = heap_front(heap); heap_pop(heap, flow); Packet* pkt = flow_dequeue(flow); heap_push(heap, flow); return pkt; }
//get the next flow and the next packet without deleting them from heap //return the next packet to transmit from virtual heap Packet* showNextPacketToTransmit(bool virtual_f) { Packet* p = NULL; Flow* flow; FHeap* heap = virtual_f ? virtual_flows : flows; if (heap->count == 0) return NULL; flow = heap_front(heap); if (flow == NULL) return NULL; p = (Packet*)queue_front(flow->packets); return p; }
static void merge_opt_Tdt(int k1, int k2, merge_alpha_t *M) { int d; struct heap_s up; struct heap_s down; /* * sorting on moves, * stores doc index for */ uint32_t *Tdt_up; uint32_t *Tdt_down; /* * change from incr/decr this docs Tdt */ float *score_up; float *score_down; Tdt_up = u32vec(ddN.DT); Tdt_down = u32vec(ddN.DT); score_up = fvec(ddN.DT); score_down = fvec(ddN.DT); if ( !score_down || !score_up || !Tdt_up || !Tdt_down ) yap_quit("Out of memory in likemerge()\n"); /* * initialise sort */ for (d=0; d<ddN.DT; d++) { assert(M->Tdt[d]<=M->Ndt[d]); /* don't change for some docs */ Tdt_up[d] = d; Tdt_down[d] = d; if ( M->Tdt[d]<M->Ndt[d] ) score_up[d] = (ddP.bpar + ddP.apar*M->TdT[d]) * S_V(ddC.SX,M->Ndt[d],M->Tdt[d]+1); else score_up[d] = 0; if ( M->Tdt[d]>1 ) score_down[d] = 1.0 / S_V(ddC.SX,M->Ndt[d],M->Tdt[d]) /(ddP.bpar + ddP.apar*(M->TdT[d]-1)); else score_down[d] = 0; assert((M->Tdt[d]>1)||score_down[d]==0); assert((M->Tdt[d]<M->Ndt[d])||score_up[d]==0); assert(M->Tdt[d]<=M->Ndt[d]); } assert(M->TDt>0); /* * use a heap, so only top of heap is least */ heap_init(&up, Tdt_up, ddN.DT, fveccmp, (void *)score_up); heap_init(&down, Tdt_down, ddN.DT, fveccmp, (void *)score_down); while ( 1 ) { float upv; float downv; upv = merge_alphabasetopicprob(M->TDTm+M->TDt, M->TDt, k1) *score_up[heap_front(&up)]; if ( M->TDt>1 ) downv = score_down[heap_front(&down)] / merge_alphabasetopicprob(M->TDTm+M->TDt-1, M->TDt-1, k1); else downv = 0.0; if ( downv>upv && downv>1.0 ){ // decrement this d = heap_front(&down); M->TdT[d]--; M->Tdt[d]--; assert(M->Tdt[d]>0); M->TDt--; heap_pop(&down); heap_remove(&up,d); } else if ( downv<upv && upv>1.0 ){ // increment this d = heap_front(&up); M->TdT[d]++; M->Tdt[d]++; assert(M->Tdt[d]<=M->Ndt[d]); M->TDt++; heap_pop(&up); heap_remove(&down,d); } else { // none are better so quit break; } if ( M->Tdt[d]<M->Ndt[d] ) score_up[d] = (ddP.bpar + ddP.apar*M->TdT[d]) * S_V(ddC.SX,M->Ndt[d],M->Tdt[d]+1); else score_up[d] = 0; if ( M->Tdt[d]>1 ) score_down[d] = 1.0 / S_V(ddC.SX,M->Ndt[d],M->Tdt[d]) /(ddP.bpar + ddP.apar*(M->TdT[d]-1)); else score_down[d] = 0; assert(M->Tdt[d]>1||score_down[d]==0); assert(M->Tdt[d]<M->Ndt[d] ||score_up[d]==0); assert(M->Tdt[d]<=M->Ndt[d]); /* * now adjust the two heaps for new vals for [d] */ heap_push(&down,d); heap_push(&up,d); } free(score_up); free(score_down); heap_free(&up); heap_free(&down); }