main() { int Sqlist[MaxSize]; int len; int i; printf("Please input six integer number\n"); for (i = 0; i <6; i++) { scanf("%d", &Sqlist[i]); } len = 6; for (i=0; i<len; i++) { printf("%d ", Sqlist[i]); } printf("\nThe spare length is %d\n", MaxSize-len); insertElem(Sqlist, &len, 3, 0); for (i=0; i<len; i++) printf("%d ", Sqlist[i]); printf("\nThe spare length is %d\n", MaxSize-len); insertElem(Sqlist, &len, 11, 0); DelElem(Sqlist, &len, 6); for (i=0; i<len; i++) printf("%d ", Sqlist[i]); printf("\nThe spare length is %d\n", MaxSize-len); return; }
void Queue::on_pushButton_4_clicked() { int randNum[10]; Ui::genRand(randNum, 10); for (int i=0; i<10; i++) { insertElem(QString::number(randNum[i])); } }
int splitNonleaf(int elem, Node r_subtree, Node u, Node v) { int median_index = (order - 2) / 2; int median = u->keys[median_index]; memcpy(v->keys, u->keys + median_index + 1, sizeof (int) * (order - 1 - (median_index + 1))); u->organized_keys = v->organized_keys = median_index; // move child pointers memcpy(v->child, u->child + median_index + 1, sizeof (Node) * (order - 1 - median_index)); // insert $elem into proper position if (elem < median) insertElem(elem, r_subtree, u); else insertElem(elem, r_subtree, v); return median; }
// Inserting a new element to the // front of the queue. void Queue::on_pushButton_2_clicked() { QString val; val = ui->lineEdit->text(); if (val == "") { return; //No value -- No-op! } ui->lineEdit->setText(""); insertElem(val); }
int main () { std::cout << "Starting " << std::endl; // creating the head element IntElem *head = new IntElem; head->data = 5; //insert element int data = 5; head= insertElem(head, data); head= insertElem(head, 10); head = insertElem(head, 12); std::cout << "head element is now: " << head->data << std::endl; std::cout << " Finding elem 5 " << findElem(head, 5) << std::endl; std::cout << " Finding elem 7 " << findElem(head, 7) << std::endl; deleteList(head); return 0; }
void EEMS::propose_birthdeath_qVoronoi(Proposal &proposal) { int newqtiles = nowqtiles,r; double u = draw.runif(); double pBirth = 0.5; double pDeath = 0.5; boost::math::normal pnorm(0.0,sqrt(nowqrateS2)); proposal.newqEffcts = nowqEffcts; proposal.newqSeeds = nowqSeeds; // If there is exactly one tile, rule out a death proposal if ((nowqtiles==1) || (u<0.5)) { // Propose birth if (nowqtiles==1) { pBirth = 1.0; } newqtiles++; MatrixXd newqSeed = MatrixXd::Zero(1,2); randpoint_in_habitat(newqSeed); pairwise_distance(nowqSeeds,newqSeed).col(0).minCoeff(&r); // The new tile is assigned a rate by perturbing the current rate at the new seed double nowqEffct = nowqEffcts(r); double newqEffct = draw.rtrnorm(nowqEffct,params.qEffctProposalS2,params.qEffctHalfInterval); insertRow(proposal.newqSeeds,newqSeed.row(0)); insertElem(proposal.newqEffcts,newqEffct); // Compute log(proposal ratio) and log(prior ratio) proposal.newratioln = log(pDeath/pBirth) - dtrnormln(newqEffct,nowqEffct,params.qEffctProposalS2,params.qEffctHalfInterval) - log(cdf(pnorm,params.qEffctHalfInterval) - cdf(pnorm,-params.qEffctHalfInterval)); proposal.newpi = nowpi + log((nowqtiles+params.negBiSize)/(newqtiles/params.negBiProb)) - 0.5 * log(nowqrateS2) - 0.5 * newqEffct*newqEffct/nowqrateS2; } else { // Propose death if (nowqtiles==2) { pBirth = 1.0; } newqtiles--; int qtileToRemove = draw.runif_int(0,newqtiles); MatrixXd oldqSeed = nowqSeeds.row(qtileToRemove); removeRow(proposal.newqSeeds,qtileToRemove); removeElem(proposal.newqEffcts,qtileToRemove); pairwise_distance(proposal.newqSeeds,oldqSeed).col(0).minCoeff(&r); double nowqEffct = proposal.newqEffcts(r); double oldqEffct = nowqEffcts(qtileToRemove); // Compute log(prior ratio) and log(proposal ratio) proposal.newratioln = log(pBirth/pDeath) + dtrnormln(oldqEffct,nowqEffct,params.qEffctProposalS2,params.qEffctHalfInterval) + log(cdf(pnorm,params.qEffctHalfInterval) - cdf(pnorm,-params.qEffctHalfInterval)); proposal.newpi = nowpi + log((nowqtiles/params.negBiProb)/(newqtiles+params.negBiSize)) + 0.5 * log(nowqrateS2) + 0.5 * oldqEffct*oldqEffct/nowqrateS2; } proposal.move = Q_VORONOI_BIRTH_DEATH; proposal.newqtiles = newqtiles; proposal.newll = eval_birthdeath_qVoronoi(proposal); }
// If a given ray intersect the object's bounding sphere then add it // to the priority queue. void PriorityQueueSphere::checkAndEnqueue( const BSphereTree *Node, const BSphere &sphere, const TRay &ray ) { TVector3 rayToCenter = sphere.m_Center - ray.origin; float B = ( ray.direction | rayToCenter ); float C = rayToCenter.magnitudeSquared() - sphere.m_Radius2; float discriminant = B*B-C; if ( discriminant < 0.0f ) return; float sqrtDiscr = sqrtf(discriminant); // store smallest positive solution float dmin = B-sqrtDiscr; if ( dmin <= 0.0f ) dmin = B+sqrtDiscr; if ( dmin <= 0.0f ) return; insertElem( dmin, Node ); }
int insertR(int key, Node r_child_of_key, Node node, stack path_stack, int tid) { if (node->is_leaf) { // set the $thread_on bit to indicate thread $tid is entering. pthread_mutex_lock(&node->mutex); node->thread_on |= 0x01 << tid; pthread_mutex_unlock(&node->mutex); while (node->reorganize_bit & 0x01) { //pthread_mutex_lock(&node->mutex); //can u get this lock??? //pthread_cond_wait(&node->is_under_reorganizing, &node->mutex); //pthread_mutex_unlock(&node->mutex); } // check its own region capacity; not full is safe, and release locks // of ancestors, including parent. if (node->private_region_capacity[tid] != 0) { while (!path_stack->is_empty(path_stack)) { pthread_mutex_unlock(&((Node) path_stack->top(path_stack)->data)->mutex); path_stack->pop(path_stack); } } // search in organized region (linear) int i; for (i = 0; i < node->organized_keys; ++i) { if (key == node->keys[i]) return 0; else if (key < node->keys[i]) break; } // key is between [i - 1] and [i]; follow child[i] // if it is leaf, linear search in private region (in leaf); or insert. // path_stack only contains from root to the parent of this $node. // insert the key record when $capacity != 0 if (node->private_region_capacity[tid] != 0) { int insert_position = node->private_region_index[tid] + node->private_region_keys[tid]; node->keys[insert_position] = key; //node->values[insert_position] = val; node->private_region_keys[tid] += 1; // if two threads meet the same situation and wait for each other to // complete the reorganization... $reorganize_bit $thread_on??? // how to deal with them in here. if (node->private_region_capacity[tid] == node->private_region_keys[tid]) { pthread_mutex_lock(&node->mutex); if (!(node->reorganize_bit & 0x01) && (node->private_region_capacity[tid] == node->private_region_keys[tid]) && (node->private_region_capacity[tid] != 0)) { node->reorganize_bit |= 0x01; reorganize(node); node->reorganize_bit &= 0x00; //pthread_cond_broadcast(&node->is_under_reorganizing); } pthread_mutex_unlock(&node->mutex); } } else { // split the $node //TODO: // check whether other threads are manipulating the node by // using the $thread_on in each node. And wait to lock the node. pthread_mutex_lock(&node->mutex); while (node->thread_on ^ (0x01 << tid)) { //!!!careful: if it needs to reset the $thread_on // and set it after condition wait. node->thread_on ^= 0x01 << tid; //!? right or wrong? pthread_cond_wait(&node->is_going_splitting, &node->mutex); node->thread_on |= 0x01 << tid; //!? right or wrong? } // start to split; reorganize first node->reorganize_bit |= 0x01; reorganize(node); node->reorganize_bit &= 0x00; Node u = node; Node v = createNode(1); // 1 => is leaf; Node r_subtree = r_child_of_key; int median = splitLeaf(u, v); int elem = key; int finish = 0; if (u == root) { root = createNode(0); root->keys[0] = median; root->organized_keys++; root->child[0] = u; root->child[1] = v; finish = 1; } else { elem = median; r_subtree = v; u = (Node) path_stack->top(path_stack)->data; path_stack->pop(path_stack); } pthread_mutex_unlock(&node->mutex); while (/*!path_stack->is_empty() && */!finish) { if (u->organized_keys < (order - 1)) { insertElem(elem, r_subtree, u); finish = 1; } else { v = createNode(0); median = splitNonleaf(elem, r_subtree, u, v); if (u == root) { root = createNode(0); root->keys[0] = median; root->organized_keys++; root->child[0] = u; root->child[1] = v; finish = 1; } else { pthread_mutex_unlock(&u->mutex); elem = median; r_subtree = v; u = (Node) path_stack->top(path_stack)->data; path_stack->pop(path_stack); } } } pthread_mutex_unlock(&u->mutex); } pthread_mutex_lock(&node->mutex); node->thread_on ^= 0x01 << tid; pthread_cond_signal(&node->is_going_splitting); pthread_mutex_unlock(&node->mutex); } else { // lock this node, and follow child[i] pthread_mutex_lock(&node->mutex); // if current node is safe, then release all ancestors. if (node->organized_keys < (order - 1)) { while (!path_stack->is_empty()) { pthread_mutex_unlock(&((Node) path_stack->top(path_stack)->data)->mutex); path_stack->pop(path_stack); } } // search in organized region (linear) int i; for (i = 0; i < node->organized_keys; ++i) { if (key == node->keys[i]) return 0; else if (key < node->keys[i]) break; } // key is between [i - 1] and [i]; follow child[i] struct stack_node_struct stack_node; stack_node.data = node; path_stack->push(path_stack, &stack_node); insertR(key, r_child_of_key, node->child[i], path_stack); } }