void Color3DTree::Node::mNearestNeighbours(const Vector3<int>& color, unsigned int level, unsigned int maxneighbours, std::deque<Neighbour>& nearestNeighbours) const { const int distance = this->color.distance(color); const unsigned int component = level % 3; const int d = color[component] - this->color.get(component); insertSorted(nearestNeighbours, Neighbour(this->color, distance), maxneighbours); const int lastDistance = nearestNeighbours.back().getDistance(); if(d < 0) { if(below) { below->mNearestNeighbours(color, level + 1, maxneighbours, nearestNeighbours); } if(above && (-d) < lastDistance) { above->mNearestNeighbours(color, level + 1, maxneighbours, nearestNeighbours); } } else { if(above) { above->mNearestNeighbours(color, level + 1, maxneighbours, nearestNeighbours); } if(below && d < lastDistance) { below->mNearestNeighbours(color, level + 1, maxneighbours, nearestNeighbours); } } }
/* * * name: search * * Uses the A* algorithm to search for the path to the given goal from the * initial stateNode. * * @param initial the stateNode to start the search from * @param goal the stateNode to be searched for * @return the goal state found, with updated paths */ struct stateNode* search(struct stateNode* initial, struct stateNode* goal){ struct stateNode * current, * next, * prior; queue open; queue closed; queue children; initialize(&open); initialize(&closed); initialize(&children); insertSorted(&open, initial); while(!isEmpty(&open)){ current = min(&open); if(cmp(current, goal)){ return current; } insert(&closed, current); // get all the children nodes of then current generate(&children, current, goal); // inspect each child to see which list to place it while(!isEmpty(&children)){ next = min(&children); prior = in(&closed, next); if(prior != NULL){ if(cost(next) < cost(prior)){ searchAndDestroy(&closed, prior); insert(&closed, next); } else{ free(next); // not any better so chunk it. } } else{ insertSorted(&open, next); } } } return NULL; }
void EmptyDirectoriesRemover::removeEmptyParentDirectories(const QStringList &artifactFilePaths) { m_dirsToRemove.clear(); m_handledDirs.clear(); for (const QString &filePath : artifactFilePaths) insertSorted(QFileInfo(filePath).absolutePath()); while (!m_dirsToRemove.empty()) removeDirIfEmpty(); }
void addStudent(Students *st){ int i; FILE *fp; st->arr = (Student*)realloc(st->arr, ((st->size)+1)*sizeof(Student)); if (st->sorted==0){ printf("Insert the student's id: "); scanf("%d", &st->arr[st->size].id); printf("\n"); printf("Insert the student's first name: "); scanf("%s", &st->arr[st->size].name); printf("\n"); printf("Insert the student's last name: "); scanf("%s", &st->arr[st->size].surname); printf("\n"); }else if (st->sorted==1){ printf("Insert the student's id: "); scanf("%d", &st->arr[st->size].id); printf("\n"); printf("Insert the student's first name: "); scanf("%s", &st->arr[st->size].name); printf("\n"); printf("Insert the student's last name: "); scanf("%s", &st->arr[st->size].surname); printf("\n"); insertSorted(st->arr, surNameCmp, 0, st->size); }else if (st->sorted==2){ printf("Insert the student's id: "); scanf("%d", &st->arr[st->size].id); printf("\n"); printf("Insert the student's first name: "); scanf("%s", &st->arr[st->size].name); printf("\n"); printf("Insert the student's last name: "); scanf("%s", &st->arr[st->size].surname); printf("\n"); insertSorted(st->arr, idCmp, 0, st->size); } st->size=st->size+1; }
int main(int argc, char** argv) { int i, n; struct ListNode* head = NULL; struct ListNode* stack = NULL; printf("empty list: "); printList(head); for(i = 0; i < 23; ++i) { n = (i*17+11) % 23; head = insertSorted(head, n); stack = push(stack, n); } printf("filled list: "); printList(head); printf("list length = %d\n", listLength(head)); printf("filled stack: "); printList(stack); printf("stack size = %d\n", listLength(stack)); for(i = -4; i < 25; i+=4) { n = removeItem(&head, i); if(!n) printf("remove did not find %d\n", i); } printf("list after removes: "); printList(head); printf("list length = %d\n", listLength(head)); for(i = 0; i < 5; ++i) { printf("pop: %d\n", pop(&stack)); } printf("stack after pops: "); printList(stack); printf("stack size = %d\n", listLength(stack)); reverseList(&head); printf("list after reverse: "); printList(head); freeList(head); head = NULL; freeList(stack); stack = NULL; return 0; }
//return the index of the 6 closest neighboors vector<int> Mesh::getClosestNeighbors(Vertex* v){ //vector<Vertex*> result; vector<int> result; result.push_back(0); glm::vec3 pivot=faceVertices[0]->Position; int n; for (int i=0; i<faceVertices.size(); i++){ if(glm::distance(faceVertices[i]->Position,v->Position)<glm::distance(pivot,v->Position)){ pivot=faceVertices[i]->Position; if (result.size()<6){ result=insertSorted(result,i,v); } else { result.pop_back(); result=insertSorted(result,i,v); } } } return result; }
void huffmanEncode (NODE_ARRAY *array) { while(array->size > 1) { NODE *a, *b, *c; a = &(array->node[array->size-2]); b = &(array->node[array->size-1]); c = malloc(sizeof(NODE)); newNode(c, 0, a->frequency + b->frequency, a, b); //fprintf(stderr, "%u(%u) e %u(%u) ---> %u(%u)\n", a->symbol, a->frequency, b->symbol, b->frequency, c->symbol, c->frequency); removeLastNodes(array, 2); insertSorted(array, c); } }
int VKContainerDialog::appendMessage(QSharedPointer<VKContainerMessage> msg) { int pos = -1; if (!msg->isDeleted()) { pos = insertSorted(msg); } if (!m_messages.contains(msg->id())) { QObject::connect(msg.data(), &VKContainerMessage::deleted, this, &VKContainerDialog::onMessageDeleted); m_messages[msg->id()] = msg; } return pos; }
StateChange * StateChanges::get(state from) { StateChange * ret = NULL; states::iterator sq = findInQueue(from); if(sq!=scq.end()){ (*sq)->inc(); ret = *sq; upgrade(sq); return ret; } states::iterator s = find(from); if(s!=sc.end()){ (*s)->inc(); StateChange * ss = (*s); ret = ss; sc.erase(s); insertSorted(ss); } return ret; }
// Okay to call on an active timer. // However, an extra notification may still happen due to concurrency. // void activate (DeadlineTimer* timer, double secondsRecurring, Time const& when) { bassert (secondsRecurring >= 0); LockType::ScopedLockType lock (m_mutex); if (timer->m_isActive) { m_items.erase (m_items.iterator_to (*timer)); timer->m_isActive = false; } timer->m_secondsRecurring = secondsRecurring; timer->m_notificationTime = when; insertSorted (*timer); timer->m_isActive = true; m_thread.interrupt (); }
// Okay to call on an active timer. // However, an extra notification may still happen due to concurrency. // void activate (DeadlineTimer& timer, double secondsRecurring, RelativeTime const& when) { bassert (secondsRecurring >= 0); std::lock_guard <LockType> lock (m_mutex); if (timer.m_isActive) { m_items.erase (m_items.iterator_to (timer)); timer.m_isActive = false; } timer.m_secondsRecurring = secondsRecurring; timer.m_notificationTime = when; insertSorted (timer); timer.m_isActive = true; notify (); }
void makeHuffmanTree( Node** out, long* frequency, int numChars, Node** pListBuf ){ Node* start = makeNodeList( frequency, numChars ); // Store base pointers in the list for quick access while encoding. Node* sorted; Node* arr; //Assume sorted is a new array. sortNodes( start, &sorted, &arr ); for (int i = 0; i < numChars; i++ ) { pListBuf[i] = arr + i; } while (1) { Node* n1 = sorted; Node* n2 = sorted->next; if( n2 == NULL ) break; Node* merged = newNode( 0, n1->freq + n2->freq ); printf("Merging %ld, %ld\n", n1->freq, n2->freq);fflush( stdout ); merged->left = n1; merged->right = n2; n1->parent = merged; n2->parent = merged; sorted = (sorted->next->next); if( sorted != NULL ) sorted = insertSorted( sorted, merged ); else sorted = merged; } *out = sorted; }
void EmptyDirectoriesRemover::removeDirIfEmpty() { const QString dirPath = m_dirsToRemove.takeFirst(); m_handledDirs.insert(dirPath); QFileInfo fi(dirPath); if (fi.isSymLink() || !fi.exists() || !dirPath.startsWith(m_project->buildDirectory) || fi.filePath() == m_project->buildDirectory) { return; } QDir dir(dirPath); dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); if (dir.count() != 0) return; dir.cdUp(); if (!dir.rmdir(fi.fileName())) { m_logger.qbsWarning() << QStringLiteral("Cannot remove empty directory '%1'.") .arg(dirPath); return; } const QString parentDir = dir.path(); if (!m_handledDirs.contains(parentDir)) insertSorted(parentDir); }
int readPageFrom(off_t offset, u_int file_id) { char file_path[16]; snprintf(file_path,16,"./testFile%d.txt",file_id); printf("DEBUG :: readPageFrom() :: file_path ::= %s\n",file_path); int file_ptr = open(file_path, O_RDONLY); lseek(file_ptr,offset*PAGE_SIZE,SEEK_SET); char *entry_data = (char *)malloc(PAGE_SIZE * sizeof(char)); ssize_t b_read = read(file_ptr, entry_data, PAGE_SIZE); if(!fileCache) { fileCache = newGenericBinaryTree(); } u_int storKey = file_id << 4; storKey = storKey | (u_int) offset; insertSorted(fileCache, newSortableEntry((void *)entry_data, storKey)); close(file_ptr); return b_read; }
int main() { // Dateinamen einlesen char*filename = malloc(sizeof(char)*100); char puffer[313]; // 100+1+100+1+2+1+2+1+4+1+100 printf("Name der Datei: "); // Dateinamen einlesen scanf("%s", filename); // Sortierung bestimmen printf("Welche Sortierung?\n- 0 für Name\n- 1 für Wohnort\n- 2 für Datum\n"); int sort; fscanf(stdin, "%i", &sort); // Von einer korrekten Eingabe wird ausgegangen... if(sort<0 || sort>2) { printf("Ungültige Eingabe\n"); return EXIT_FAILURE; } // Funktionenpointer setzen - Liste konfigurieren int (*cmp)(void*, void*); switch(sort) { case 0: cmp=&cmp_names; break; case 1: cmp=&cmp_wohnort; break; case 2: cmp=&cmp_dates; } setComparer(cmp); setFormatter(&printItem); setDisposer(&freeItem); // Datei zum Lesen öffnen und einlesen FILE*f = fopen(filename, "r"); if(f==NULL) {printf("Datei konnte nicht gelesen werden\n"); return EXIT_FAILURE;} char*vorname; char*nachname; char*wohnort; while(fgets(puffer, 313, f)) { // Zeilenweise lesen vorname=malloc(100); nachname=malloc(100); wohnort=malloc(100); int mode=0; int b=0; int tag=0; int monat=0; int jahr=0; int i; for(i=0; i<sizeof(puffer); i++) { if(mode==0) { // Vornamen trennen if(puffer[i]!=' ') { vorname[i]=puffer[i]; } else { vorname[i]='\0'; b=i+1; mode=1; } } else if(mode==1) { // Nachnamen trennen if(puffer[i]!=' ') { nachname[i-b]=puffer[i]; } else { nachname[i-b]='\0'; mode=2; } } else if(mode==2) { // Geburtsdatum einlesen tag=atoi(&puffer[i]); monat=atoi(&puffer[i+3]); jahr=atoi(&puffer[i+6]); i+=10; b=i+1; mode=3; } else { // Ort einlesen if(puffer[i]!='\n' && puffer[i]!=-1) { // auch EOF beachten wohnort[i-b]=puffer[i]; } else { wohnort[i-b]='\0'; break; } } } // Neues Struct mit gesammelten Daten erzeugen struct mycontent*tmp = malloc(sizeof(mycontent)); tmp->vorname = vorname; tmp->nachname = nachname; tmp->wohnort = wohnort; tmp->tag = tag; tmp->monat = monat; tmp->jahr = jahr; // In die Liste einfügen insertSorted(tmp); } printList(); fclose(f); // Filestream schließen freeList(); // Speicherplatz freigeben free(filename); return 0; }
void threadRun () { while (! m_shouldStop) { Time const currentTime = Time::getCurrentTime (); double seconds = 0; { LockType::ScopedLockType lock (m_mutex); // Notify everyone whose timer has expired // if (! m_items.empty ()) { for (;;) { Items::iterator const iter = m_items.begin (); // Has this timer expired? if (iter->m_notificationTime <= currentTime) { // Yes, so call the listener. // // Note that this happens while the lock is held. // iter->m_listener->onDeadlineTimer (*iter); // Remove it from the list. m_items.erase (iter); // Is the timer recurring? if (iter->m_secondsRecurring > 0) { // Yes so set the timer again. iter->m_notificationTime = currentTime + RelativeTime (iter->m_secondsRecurring); // Keep it active. insertSorted (*iter); } else { // Not a recurring timer, deactivate it. iter->m_isActive = false; } } else { break; } } } // Figure out how long we need to wait. // This has to be done while holding the lock. // if (! m_items.empty ()) { seconds = (m_items.front ().m_notificationTime - currentTime).inSeconds (); } else { seconds = 0; } } // Note that we have released the lock here. // if (seconds > 0) { // Wait until interrupt or next timer. // m_thread.wait (static_cast <int> (seconds * 1000 + 0.5)); } else if (seconds == 0) { // Wait until interrupt // m_thread.wait (); } else { // Do not wait. This can happen if the recurring timer duration // is extremely short, or if a listener wastes too much time in // their callback. } } }
void run () { while (! threadShouldExit ()) { RelativeTime const currentTime ( RelativeTime::fromStartup ()); double seconds (0); DeadlineTimer* timer (nullptr); { std::lock_guard <LockType> lock (m_mutex); // See if a timer expired if (! m_items.empty ()) { timer = &m_items.front (); // Has this timer expired? if (timer->m_notificationTime <= currentTime) { // Expired, remove it from the list. bassert (timer->m_isActive); m_items.pop_front (); // Is the timer recurring? if (timer->m_secondsRecurring > 0) { // Yes so set the timer again. timer->m_notificationTime = currentTime + timer->m_secondsRecurring; // Put it back into the list as active insertSorted (*timer); } else { // Not a recurring timer, deactivate it. timer->m_isActive = false; } timer->m_listener->onDeadlineTimer (*timer); // re-loop seconds = -1; } else { seconds = ( timer->m_notificationTime - currentTime).inSeconds (); // Can't be zero and come into the else clause. bassert (seconds != 0); // Don't call the listener timer = nullptr; } } } // Note that we have released the lock here. if (seconds > 0) { // Wait until interrupt or next timer. // int const milliSeconds (std::max ( static_cast <int> (seconds * 1000 + 0.5), 1)); bassert (milliSeconds > 0); wait (milliSeconds); } else if (seconds == 0) { // Wait until interrupt // wait (); } else { // Do not wait. This can happen if the recurring timer duration // is extremely short, or if a listener wastes too much time in // their callback. } } }
//---------------------------------------------------------------------------- // void HypNode::layout(int up) //............................................................................ // //---------------------------------------------------------------------------- void HypNode::layout(int up) { int i; HypNode *ww, *base; SortKidArray sk; int numkids; double cumua = 0.0; // cumulative area double newa = 0.0; // new area double newr; if (children.size() == 0) { /* leaf node */ r = hd->leafrad; I.identity(); // if (!up) return; } if (up) { if (!parent) return; base = parent; // fprintf(stderr, "top of %s, set base to %s\n", // (char*)theurl, (char*)base->theurl); } else { base = this; } base->progeny = 0; numkids = base->children.size(); for (i = 0; i < numkids; i++) { SortKid *newkid = new SortKid(); ww = base->children[i]; if (! (up && ww == this)) { // fprintf(stderr, "descend to %s\n", (char*)ww->theurl); ww->layout(0); } newa = (cosh(ww->r)-1)*2*M_PI; cumua += newa; base->progeny += ww->progeny + 1; newkid->index =i; newkid->comparator = ww->progeny; insertSorted(&sk, newkid); } /* add some since the circle packing area < sphere area */ /* make sure to add hyperbolic amount, not euclidean amount! */ if (numkids == 0) { // cumua += cumua*tanh(cumua)*.0; } else if (numkids == 1) { cumua += cumua*tanh(cumua)*.2; } else if (numkids == 2) { cumua += cumua*tanh(cumua)*.9; } else if (numkids > 2 && numkids < 6 ) { cumua += cumua*tanh(cumua)*.6; } else { cumua += cumua*tanh(cumua)*.4; } base->r = asinh(sqrt(cumua/(2*M_PI))); base->r = (base->r < hd->leafrad) ? hd->leafrad : base->r; base->r = (base->r > hd->maxlength) ? hd->maxlength : base->r; placeKids(up, base, &sk); if (base->phi > 1.57) { // new phi 1.5 newr = asinh(sqrt((pow(sinh(base->r),2)*(1-cos(base->phi)))*1.076)); base->r = newr; // fprintf(stderr, "recomputing phi %g r %g new %g\n", base->phi, base->r, newr); placeKids(up, base, &sk); // fprintf(stderr, "take2 phi %g r %g\n", base->phi, base->r); } else if (up && !parent->parent) { // fprintf(stderr, "up %g %g\n", base->phi, base->r); } if (up) parent->layout(up); }
void Rig::addRunningAnimation(AnimationHandlePointer animationHandle) { insertSorted(_runningAnimations, animationHandle); }