void insertRecursive(BTree* tree, Node* node, int key) { int index = closestIndex(node, key, 0, node->n-1); if(!node->leaf) { if(node->keys[index] < key) { if(node->children[index+1]->n == tree->arity -1) { fork(tree, node, node->children[index+1], false); index = closestIndex(node, key, 0, node->n-1); } } else { if(node->children[index]->n == tree->arity -1) { fork(tree, node, node->children[index], false); index = closestIndex(node, key, 0, node->n-1); } } } if(node->leaf == true) { int i=0; while(node->keys[i] < key) i++; for(int j=node->n;j>i;j--) { node->keys[j] = node->keys[j-1]; } node->keys[i] = key; (node->n)++; } else { if(node->keys[index]>key) insertRecursive(tree, node->children[index], key); else insertRecursive(tree, node->children[index+1], key); } }
int closestIndex(Node* node, int key, int begin, int end) { if(begin == end) return begin; int middle = (begin + end)/2; if(node->keys[middle] == key) return middle; if(node->keys[middle] > key) { if(middle!=begin) return closestIndex(node, key, begin, middle-1); else return begin; } else { if(middle!=end) return closestIndex(node, key, middle+1, end); else return end; } }
Node* recursiveSearch(Node* node, int key) { if(node == NULL) return NULL; int index = closestIndex(node, key, 0, node->n-1); if(index < 0) return NULL; if(node->keys[index] == key) return node; else if(node->keys[index] > key) return recursiveSearch(node->children[index], key); else return recursiveSearch(node->children[index+1], key); }
void resampleParticles( ) { int N = tracking_problem->Nparticles(); gen_rand::random_vector(sampler,1.0); double wmin = 1.0/N; for( int k = 0; k < N; k++ ) { if( weights[k] < wmin ) { double sampval = sampler[k]; int sampidx = closestIndex( cdf, sampval ); // EVIL SLOW ALGORITHM particles_pred[sampidx].copyTo(particles_estm[k]); resample_index[k] = sampidx; // where k-th was resampled from weights[k] = weights[sampidx]/2.0; weights[sampidx] = weights[sampidx]/2.0; } else { resample_index[k] = k; } } }
Size TimeGrid::index(Time t) const { Size i = closestIndex(t); if (close_enough(t,times_[i])) { return i; } else { if (t < times_.front()) { QL_FAIL("using inadequate time grid: all nodes " "are later than the required time t = " << std::setprecision(12) << t << " (earliest node is t1 = " << std::setprecision(12) << times_.front() << ")"); } else if (t > times_.back()) { QL_FAIL("using inadequate time grid: all nodes " "are earlier than the required time t = " << std::setprecision(12) << t << " (latest node is t1 = " << std::setprecision(12) << times_.back() << ")"); } else { Size j, k; if (t > times_[i]) { j = i; k = i+1; } else { j = i-1; k = i; } QL_FAIL("using inadequate time grid: the nodes closest " "to the required time t = " << std::setprecision(12) << t << " are t1 = " << std::setprecision(12) << times_[j] << " and t2 = " << std::setprecision(12) << times_[k]); } } }
//! returns the time on the grid closest to the given t Time closestTime(Time t) const { return times_[closestIndex(t)]; }
bool DisplayOptions::zoomIn() { if ( !canZoomIn() ) return false; _magnification = allowedMagnifications[ closestIndex() + 1 ]; return true; }
bool DisplayOptions::canZoomIn() const { return unsigned( closestIndex() ) < ( numberOfMagnifications - 1 ); }
bool DisplayOptions::canZoomOut() const { return closestIndex() > 0; }