void CBotSchedule :: execute ( CBot *pBot ) { // current task static CBotTask *pTask; static eTaskState iState; if ( m_Tasks.IsEmpty() ) { m_bFailed = true; return; } pTask = m_Tasks.GetFrontInfo(); if ( pTask == NULL ) { m_bFailed = true; return; } iState = pTask->isInterrupted(pBot); if ( iState == STATE_FAIL ) pTask->fail(); else if ( iState == STATE_COMPLETE ) pTask->complete(); else // still running { // timed out ?? if ( pTask->timedOut() ) pTask->fail(); // fail else { if ( CClients::clientsDebugging(BOT_DEBUG_TASK) ) { char dbg[512]; pTask->debugString(dbg); CClients::clientDebugMsg(BOT_DEBUG_TASK,dbg,pBot); } pTask->execute(pBot,this); // run } } if ( pTask->hasFailed() ) { m_bFailed = true; } else if ( pTask->isComplete() ) { removeTop(); } }
ClassifiableEntry* TaxonomyCreator :: prepareTS ( ClassifiableEntry* cur ) { // we just found that TS forms a cycle -- return stop-marker if ( waitStack.contains(cur) ) return cur; // starting from the topmost entry addTop(cur); // true iff CUR is a reason of the cycle bool cycleFound = false; // for all the told subsumers... for ( ss_iterator p = told_begin(), p_end = told_end(); p < p_end; ++p ) if ( !(*p)->isClassified() ) // need to classify it first { if ( unlikely((*p)->isNonClassifiable()) ) continue; // prepare TS for *p ClassifiableEntry* v = prepareTS(*p); // if NULL is returned -- just continue if ( v == NULL ) continue; if ( v == cur ) // current cycle is finished, all saved in Syns { // after classification of CUR we need to mark all the Syns as synonyms cycleFound = true; // continue to prepare its classification continue; } else { // arbitrary vertex in a cycle: save in synonyms of a root cause Syns.push_back(cur); // don't need to classify it removeTop(); // return the cycle cause return v; } } // all TS are ready here -- let's classify! classifyTop(); // now if CUR is the reason of cycle mark all SYNs as synonyms if ( cycleFound ) { TaxonomyVertex* syn = cur->getTaxVertex(); for ( std::vector<ClassifiableEntry*>::iterator q = Syns.begin(), q_end = Syns.end(); q != q_end; ++q ) syn->addSynonym(*q); Syns.clear(); } // here the cycle is gone return NULL; }
static void test_removeTop(void **state) { const int a = 1001; LinkedList *list = createList(); assert_non_null(list); for (int i = 0; i < a; i++) { insertTop(list, malloc(sizeof(int))); } assert_int_equal(listLength(list), a); for (int i = a; i > 0; i--) { removeTop(list); } assert_int_equal(listLength(list), 0); destroyList(list); }
void setCover(Universe *Uni,int num_houses) { Heap *Q = createHeap(); set_t *covered = create_set(num_houses); for(int i=0;i<Uni->size_of_universe;i++) { insert_in_heap(Q,i,Uni->sets[i].covered_items,max_func); } while(covered->covered_items < num_houses) { if(isEmpty(Q)) { break; } HeapItem max = removeTop(Q,max_func); //updating the covered set for(int i=0;i<Uni->sets[max.dataIndex].covered_items;i++) { //if its not covered if(!covered->set[Uni->sets[max.dataIndex].set[i]]) { //set to the house being covered covered->set[Uni->sets[max.dataIndex].set[i]] = 1; //increment number of covered houses covered->covered_items++; } } //flag the current set to be covered Uni->sets[max.dataIndex].covered = 1; //update the other sets for(int i=0;i<Uni->size_of_universe;i++) { //if they are not already covered if(!Uni->sets[i].covered) { //find set diff and update the heap int diff = set_diff(&Uni->sets[i],covered); changeKey(Q,i,diff,max_func); } } } if(covered->covered_items < num_houses) { fprintf(stderr, "Not enough houses to cover all houses.\n"); exit(EXIT_FAILURE); } for(int i=0; i<Uni->size_of_universe;i++) { if(Uni->sets[i].covered) { printf("%d\n",i); } } destroyHeap(Q); free_set(covered); free_Universe(Uni); }
int main()//int argc, char **argv) { cardType *topDeck = NULL, *bottomDeck = NULL, *topTable = NULL; cardType *nextBottom = NULL; // the topDeck unsigned int rounds = 0; BOOL done = FALSE; unsigned short deckSize = 0; unsigned short *key; unsigned short *copy; /*if (argc != 2) return 0; sscanf_s(argv[1], "%d", &deckSize);*/ scanf("%d", &deckSize); key = (unsigned short*) malloc(sizeof(unsigned short) * deckSize); copy = (unsigned short*) malloc(sizeof(unsigned short) * deckSize); createDeck(&topDeck, &bottomDeck, deckSize); nextBottom = topDeck; // we know our current top will be the bottom of our next deck // keep removing and shifting until deck is empty do { removeTop(&topDeck, &topTable); if (topDeck) { if (topDeck != bottomDeck) // cannot shift if already bottom shiftDeck(&topDeck, &bottomDeck); } else { rounds++; // all cards on table topDeck = topTable; // table deck becomes our new deck topTable = NULL; bottomDeck = nextBottom; nextBottom = topDeck; done = TRUE; } } while (!done); // We only needed to analyze how cards moved around once to create a key // of indexes which lets us get the next position of a card in O(1). makeKey(topDeck, key, copy, deckSize); // Next we keep adjusting the deck following the key until cards are back in place. while (!checkDeck(topDeck, rounds)) { adjustDeck(topDeck, key, copy, deckSize); rounds++; } // find the lcm of all the r's rounds = lcmLoop(topDeck); destroyDeck(&topDeck); free(copy); free(key); printf("rounds for %d numbers = %d\n", deckSize, rounds); return 0; }
void Dijkstra(Graph *g,int source,Universe *Uni,int (cmp)(int,int)) { int *dist; dist = (int*)malloc(sizeof(int)*(g->number_of_vertices)); assert(dist); //set all vertices distance to INF for(int i=0;i<g->number_of_vertices;i++) { dist[i] = INF; } Heap *Q = createHeap(); //insert everything into heap for(int i=0;i<g->number_of_vertices;i++) { if(i==source) { insert_in_heap(Q,i,0,cmp); } else { insert_in_heap(Q,i,INF,cmp); } } // update the distance of source dist[source] = 0; while(!isEmpty(Q)) { HeapItem min = removeTop(Q,cmp); // min can't be > 1000, if so we can stop if(min.key > MAX_KM) { break; } // prune any schools, only houses if(min.dataIndex < g->num_houses) { insert(Uni, source-g->num_houses, min.dataIndex); } int n_edges; //get all edges from min.dataIndex Edge *edges = graph_get_edge_array(g,min.dataIndex,&n_edges); for(int i=0;i<n_edges;i++) { //update all the edges distances if there is a minimum if(dist[edges[i].u] > (dist[min.dataIndex] +edges[i].weight)) { dist[edges[i].u] = dist[min.dataIndex]+edges[i].weight; // update the heap changeKey(Q,edges[i].u,dist[edges[i].u],cmp); } } } }