void AddSorted( CDblCharNode * & sroot ) { if (!sroot) sroot = this; else if (count>sroot->count) AddSorted( sroot->sleft ); else AddSorted( sroot->sright ); }
/*********************************************************************************** ** ** TreeViewModel::OnItemAdded ** ***********************************************************************************/ void TreeViewModel::OnItemAdded(OpTreeModel* tree_model, INT32 index) { TreeViewModelItem* item = OP_NEW(TreeViewModelItem, (m_model->GetItemByPosition(index))); if (!item) return; if (GetSortListener()) { INT32 parent = GetIndexByModelIndex(m_model->GetItemParent(index)); if (parent == -1 && GetTreeModelGrouping() && GetTreeModelGrouping()->HasGrouping()) { parent = static_cast<TreeViewModelItem*>(GetTreeModelGrouping()->GetGroupHeader(GetTreeModelGrouping()->GetGroupForItem(item)))->GetIndex(); } AddSorted(item, parent); } else { INT32 parent = m_model->GetItemParent(index); if (index < GetCount() && GetParentIndex(index) == parent) { InsertBefore(item, index); } else { AddLast(item, parent); } } }
int main() { log(AddSorted(5, 'a')); log(AddSorted(4, 'b')); log(AddSorted(3, 'c')); log(AddSorted(2, 'd')); log(AddSorted(1, 'e')); std::cout << '\n'; log(AddSorted(0, 'a')); // delete 'a' log(AddSorted(3, 'b')); // change 'b's priority to 'c's log(AddSorted(4, 'd')); // 'd' now has highest priority log(AddSorted(4, 'e')); // 'e' now has 'd's priority log(AddSorted(0, 'b')); // delete 'b' }
void ZopfliBlockSplitLZ77(const ZopfliOptions* options, const ZopfliLZ77Store* lz77, size_t maxblocks, size_t** splitpoints, size_t* npoints) { size_t lstart, lend; size_t i; size_t llpos = 0; size_t numblocks = 1; unsigned char* done; double splitcost, origcost; if (lz77->size < 10) return; /* This code fails on tiny files. */ done = (unsigned char*)malloc(lz77->size); if (!done) exit(-1); /* Allocation failed. */ for (i = 0; i < lz77->size; i++) done[i] = 0; lstart = 0; lend = lz77->size; for (;;) { SplitCostContext c; if (maxblocks > 0 && numblocks >= maxblocks) { break; } c.lz77 = lz77; c.start = lstart; c.end = lend; assert(lstart < lend); llpos = FindMinimum(SplitCost, &c, lstart + 1, lend, &splitcost); assert(llpos > lstart); assert(llpos < lend); origcost = EstimateCost(lz77, lstart, lend); if (splitcost > origcost || llpos == lstart + 1 || llpos == lend) { done[lstart] = 1; } else { AddSorted(llpos, splitpoints, npoints); numblocks++; } if (!FindLargestSplittableBlock( lz77->size, done, *splitpoints, *npoints, &lstart, &lend)) { break; /* No further split will probably reduce compression. */ } if (lend - lstart < 10) { break; } } if (options->verbose) { PrintBlockSplitPoints(lz77, *splitpoints, *npoints); } free(done); }
void Sort( CDblCharNode * & sroot ) { if (left) left->Sort( sroot ); AddSorted( sroot ); if (right) right->Sort( sroot ); }
void ZopfliBlockSplitLZ77(const ZopfliOptions* options, const unsigned short* litlens, const unsigned short* dists, size_t llsize, size_t maxblocks, size_t** splitpoints, size_t* npoints, size_t startnpoints) { size_t lstart, lend; size_t i; size_t llpos = 0; size_t numblocks = 1; unsigned char* done; double splitcost, origcost; if (llsize < 10) return; /* This code fails on tiny files. */ done = (unsigned char*)malloc(llsize); if (!done) exit(-1); /* Allocation failed. */ for (i = 0; i < llsize; i++) done[i] = 0; lstart = 0; lend = llsize; for (;;) { SplitCostContext c; if (maxblocks > 0 && numblocks >= maxblocks) { break; } c.litlens = litlens; c.dists = dists; c.llsize = llsize; c.start = lstart; c.end = lend; c.ohh = options->optimizehuffmanheader; assert(lstart < lend); llpos = FindMinimum(SplitCost, &c, lstart + 1, lend,options); assert(llpos > lstart); assert(llpos < lend); splitcost = EstimateCost(litlens, dists, lstart, llpos, c.ohh) + EstimateCost(litlens, dists, llpos, lend, c.ohh); origcost = EstimateCost(litlens, dists, lstart, lend, c.ohh); if (splitcost > origcost || llpos == lstart + 1 || llpos == lend) { done[lstart] = 1; } else { AddSorted(llpos, splitpoints, npoints); ++numblocks; if(options->verbose>0 && options->verbose<5) fprintf(stderr,"Initializing blocks: %lu \r",(unsigned long)(startnpoints+numblocks)); } if (!FindLargestSplittableBlock( llsize, done, *splitpoints, *npoints, &lstart, &lend)) { break; /* No further split will probably reduce compression. */ } if (lend - lstart < 10) { break; } } if (options->verbose>3) { PrintBlockSplitPoints(litlens, dists, llsize, *splitpoints, *npoints); } if(options->verbose>2) { fprintf(stderr, "Total blocks: %lu \n\n",(unsigned long)numblocks); } free(done); }
/* * Total Order Broadcast with good throughput. * Note that like every functions defined here, they have the type NodesFct. */ void TOBThroughputBroadcast(int id, Message m){ PipelineAckData_t data; int argSize, ackOrigin; char *event, *eventArg, *msgTxt; Message mOut; NumberedMessage NumMsg, temp; if((data=(PipelineAckData_t)getData(id))==NULL){ //Initialization if((data=malloc(sizeof(struct _PipleineAckData_t)))==NULL){ fprintf(stderr,"malloc fail at TOBThroughputBroadcast\n"); exit(EXIT_FAILURE); } data->clock=0; data->next=(id+1)%getNbNodes(); // data->pred=(id==0)?getNbNodes()-1:id-1; data->pending=newSortedList(NumberedMsgComp); setData(id,data); } if(m==NULL){ // Event Rules if((event = getNextExternalEvent(id)) != NULL){ printf("event received %i %s\n",id, event); // Read the first event if(event==strstr(event, "broadcast")){ //there is an event : increment the clock data->clock++; //event is someting like broadcast <string> if((eventArg=malloc(sizeof(char)*maxArgSize))==NULL){ fprintf(stderr,"malloc fail at TOBThroughputBroadcast\n"); exit(EXIT_FAILURE); } if(1>sscanf(event, "broadcast %s", eventArg)){ sscanf("hello","%s",eventArg); } //eventArg is either the string to broadcast, or "hello" argSize=strlen(eventArg); if((msgTxt=malloc((intToStringSize+1+argSize)*sizeof(char)))==NULL){ fprintf(stderr,"malloc fail at TOBThroughputBroadcast\n"); exit(EXIT_FAILURE); } sprintf(msgTxt, "%d ",data->clock); strncat(msgTxt, eventArg, argSize); //Now the message text have the format: clk <arg> if((NumMsg=malloc(sizeof(struct _NumberedMessage)))==NULL){ fprintf(stderr,"malloc fail at TOBThroughputBroadcast\n"); exit(EXIT_FAILURE); } NumMsg->clk=data->clock; //waiting for 1 ack //creating the actual message NumMsg->m=initMessage(msgTxt,id,id,data->next); free(msgTxt); NumMsg->origin=id; if(data->next==id){ //we are the only process of the system deliver(NumMsg->m,id); free(NumMsg->m->msg); free(NumMsg->m); free(NumMsg); }else{ //and send it Send(copyMessage(NumMsg->m)); //Finally store the numbered msg in the pending list AddSorted(NumMsg,data->pending); } } free(event); } }else{ // Message Rules if((NumMsg=malloc(sizeof(struct _NumberedMessage)))==NULL){ fprintf(stderr,"malloc fail at TOBThroughputBroadcast\n"); exit(EXIT_FAILURE); } //this is a new message at the format: //clock <text> where text is either ack or the text of the //message //We have to parse the clock of the broadcast extractInt(m->msg, &(NumMsg->clk)); NumMsg->origin=m->origin; //don't forget to update the clock data->clock=MAX(NumMsg->clk,data->clock)+1; if(strstr(m->msg,"ack")==NULL){ printf("%s received by %d from %d but not delivered yet\n", m->msg, id, m->sender); //m is an actual message NumMsg->m=m; if(m->origin!=data->next){ //We need to forward the message mOut=initMessage(m->msg,m->origin,id,data->next); Send(mOut); AddSorted(NumMsg,data->pending); }else{ AddSorted(NumMsg,data->pending); //we deliver all the older messages mOut=NULL; while((temp=getFirst(data->pending))!=NULL && NumMsg!=NULL && NumberedMsgComp(temp,NumMsg)<=0){ //we remove the message from the pending list RemoveFirst(data->pending); if(!NumberedMsgComp(temp,NumMsg)){ //to ensure we stop after that NumMsg=NULL; } //prepare an ack if(temp->origin!=id){ if(mOut!=NULL){ free(mOut); } mOut=Ack(temp->clk,temp->origin,id,data->next); } //deliver deliver(temp->m,id); //we can free the message free(temp->m->msg); free(temp->m); free(temp); } //if we have managed to deliver a message //we acknowleged the older message delvered if(mOut){ Send(mOut); } } }else{ //this is an ack printf("%s received by %d from %d origin %d\n", m->msg, id, m->sender, m->origin); //we deliver all the older messages mOut=NULL; while((temp=getFirst(data->pending))!=NULL && NumMsg!=NULL && NumberedMsgComp(temp,NumMsg)<=0){ //we remove the message from the pending list RemoveFirst(data->pending); //prepare an ack ackOrigin=((temp->origin+getNbNodes()-1)%getNbNodes()); if(ackOrigin!=data->next){ if(mOut!=NULL){ free(mOut); } mOut=Ack(temp->clk,temp->origin,id,data->next); } //deliver deliver(temp->m,id); //we can free the message free(temp->m->msg); free(temp->m); free(temp); } //if we have managed to deliver a message //we acknowleged the older message delvered ackOrigin=((NumMsg->origin+getNbNodes()-1)%getNbNodes()); if(mOut){ Send(mOut); }else if(Size(data->pending)==0 && ackOrigin!=data->next){ //we haven't be able to deliver a message because our pending //list is empty, but the message may be blocked in someon else //queue so we forward the ack Send(Ack(NumMsg->clk,NumMsg->origin,id,data->next)); } // Free the local message free(m->msg); free(m); } } }