void CQGroupBox:: drawArcShape(QPainter *painter, double xc, double yc, double r, double startAngle, int sides) const { auto Deg2Rad = [](double d) -> double { return M_PI*d/180.0; }; //auto Rad2Deg = [](double r) -> double { return 180.0*r/M_PI; }; double x1 = xc - r; double y1 = yc - r; double x2 = xc + r; double y2 = yc + r; double xm = (x1 + x2)/2; double ym = (y1 + y2)/2; double da = 360.0/sides; double dc = 360.0/40; QPainterPath path; for (int i = 0; i < sides; ++i) { double angle = startAngle + i*da; double a1 = Deg2Rad(angle - dc); double a2 = Deg2Rad(angle + dc); double c1 = cos(a1), s1 = sin(a1); double c2 = cos(a2), s2 = sin(a2); QPointF p1(xm + r*c1, ym + r*s1); QPointF p2(xm + r*c2, ym + r*s2); if (i == 0) path.moveTo(p1); else path.lineTo(p1); //--- QPointF p12 = (p1 + p2)/2; double ar = 2*hypot(p1.x() - p12.x(), p1.y() - p12.y())/sides; double a = Deg2Rad(angle); double c = cos(a), s = sin(a); QPointF pq(xm + (r + ar)*c, ym + (r + ar)*s); path.quadTo(pq, p2); } path.closeSubpath(); painter->drawPath(path); }
// static Status LiteParsedQuery::make(const string& ns, int ntoskip, int ntoreturn, int queryOptions, const BSONObj& query, const BSONObj& proj, const BSONObj& sort, LiteParsedQuery** out) { auto_ptr<LiteParsedQuery> pq(new LiteParsedQuery()); pq->_sort = sort; Status status = pq->init(ns, ntoskip, ntoreturn, queryOptions, query, proj, false); if (status.isOK()) { *out = pq.release(); } return status; }
/** * Returns 0 if p->q is colinear with p->r, 1 if counter-clockwise, or -1 if clockwise. */ int orientation(const csce::point<T>& p, const csce::point<T>& q, const csce::point<T>& r) const { csce::vector2d<T> pq(p, q); csce::vector2d<T> pr(p, r); T cross_product = pq.cross(pr); if(csce::math_utility::equals_zero(cross_product)) return 0; return (cross_product < 0) ? -1 : 1; }
bool greedy::_run(Model& m, std::vector<Node*>& solution){ std::set<Node*> explored; std::priority_queue<Node*, std::vector<Node*>,compareNode> pq(compareNode(*this,m)); std::map<Node*,Node*> cameFrom; Node* currentNode; pq.push(m.start_tile); cameFrom[currentNode] = NULL; while(pq.empty() == false){ currentNode = pq.top(); pq.pop(); numNodesVisited++; // already explored this node. done. if( explored.find(currentNode) != explored.end()){ continue; } // this is the goal tile, we are done. if( currentNode == m.goal_tile){ // create the solution chain back to the start tile while(currentNode != NULL){ solution.insert(solution.begin(), currentNode); currentNode = cameFrom[currentNode]; } return true; } // mark as explored. explored.insert(currentNode); // add in all the neighbours into the priority queue Node* cand; for(int i =0 ;i <6; ++i) { cand = (*currentNode)[i]; if( currentNode->canMove(i) == false){continue;} if( explored.find(cand) != explored.end()){continue;} numGenNodes++; pq.push(cand); // record where we came from cameFrom[cand] = currentNode; } } // didn't find a solution return false; }
// Fill LVector with the basis functions corresponding to each real DOF void LVector::fillBasis(double x, double y, double sigma) { take_ownership(); // fill with psi_pq(z), where psi now defined to have 1/sigma^2 in // front. std::complex<double> z(x,-y); double rsq = norm(z); double tq = std::exp(-0.5*rsq) / (2*M_PI*sigma*sigma); double tqm1=tq; double tqm2; // Ascend m=0 first (*_v)[PQIndex(0,0).rIndex()]=tq; if (_order>=2) { tq = (rsq-1.)*tqm1; (*_v)[PQIndex(1,1).rIndex()] = tq; } PQIndex pq(2,2); for (int p=2; 2*p<=_order; ++p, pq.incN()) { tqm2 = tqm1; tqm1 = tq; tq = ((rsq-2.*p+1.)*tqm1 - (p-1.)*tqm2)/p; (*_v)[pq.rIndex()] = tq; } // Ascend all positive m's std::complex<double> zm = 2* (*_v)[PQIndex(0,0).rIndex()] * z; for (int m=1; m<=_order; m++) { pq.setPQ(m,0); double *r = &(*_v)[pq.rIndex()]; *r = zm.real(); *(r+1) = zm.imag(); tq = 1.; tqm1 = 0.; for (pq.incN(); !pq.pastOrder(_order); pq.incN()) { tqm2 = tqm1; tqm1 = tq; int p=pq.getP(); int q=pq.getQ(); tq = ( (rsq-(p+q-1.))*tqm1 - sqrtn(p-1)*sqrtn(q-1)*tqm2) / (sqrtn(p)*sqrtn(q)); double *r = &(*_v)[pq.rIndex()]; *r = tq*zm.real(); *(r+1) = tq*zm.imag(); } zm *= z/sqrtn(m+1); } }
TEST(FCPriQueue, bounded) { FCPQ pq(1); CHECK(pq.try_push(1)); CHECK(!pq.try_push(1)); CHECK_EQ(pq.size(), 1); CHECK(!pq.empty()); int v; CHECK(pq.try_pop(v)); CHECK_EQ(v, 1); CHECK_EQ(pq.size(), 0); CHECK(pq.empty()); }
int main() { MinIndexedPQ pq(10); for(int i=0; i<10; i++) pq.insert(i, 10 - i); printf("%d %d\n", pq.minKey(), pq.size()); pq.deleteKey(8); printf("%d %d\n", pq.minKey(), pq.size()); pq.deleteMin(); printf("%d %d\n", pq.minKey(), pq.size()); return 0; }
int shortestPathAllKeys(vector<string>& g) { int rsz = g.size(), csz = g[0].size(); int sx, sy, k = -1; for (int i=0; i<rsz; ++i) { for (int j=0; j<csz; ++j) { if (g[i][j] == '@') { sx = i; sy = j; } else if (g[i][j]>='a' && g[i][j]<'z') { k = max(k, g[i][j]-'a'); } } } ++k; int ssz = 1 << k, full = (1<<k) - 1;; vector<vector<vector<int>>> dist(rsz, vector<vector<int>>(csz, vector<int>(ssz, -1))); dist[sx][sy][0] = 0; auto cmp = [](const State &s1, const State &s2) { return s1.c > s2.c; }; priority_queue<State, vector<State>, decltype(cmp)> pq(cmp); pq.emplace(sx, sy, 0, 0); int ans = -1; const int mov[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; while (!pq.empty()) { auto cs = pq.top(); pq.pop(); if (cs.c > dist[cs.x][cs.y][cs.s]) { continue; } if (cs.s == full) { if (ans==-1 || cs.c<ans) { ans = cs.c; } continue; } int xx, yy, ss, cc = cs.c + 1; for (int i=0; i<4; ++i) { xx = cs.x + mov[i][0], yy = cs.y + mov[i][1]; ss = cs.s; if (xx<0||xx>=rsz||yy<0||yy>=csz||g[xx][yy]=='#') { continue; } if (g[xx][yy]>='A' && g[xx][yy]<='Z') { int l = g[xx][yy] - 'A'; if ((cs.s & (1<<l)) == 0) { continue; } } if (g[xx][yy]>='a' && g[xx][yy]<='z') { int key = g[xx][yy] - 'a'; ss |= 1 << key; } if (dist[xx][yy][ss]!=-1 && cc>=dist[xx][yy][ss]) { continue; } dist[xx][yy][ss] = cc; pq.emplace(xx, yy, ss, cc); } } return ans; }
int main() { int i; int len = 10; int a[] = {3, 2, 1, 7, 6, 5, 9, 4, 10, 8}; MinPQ<int> pq(10); for (i = 0; i < 10; i++) pq.insert(a[i]); for (i = 0; i < 10; i++) std::cout << pq.delMin() << std::endl; return 0; }
int main() { ip = 0; op = 0; n = 0; int ret; char chr; char c; printf("功能选择:ESC 退出:- 输出一个字符;+ 打印当前队列;0-9、A-Z进入队列,其他抛弃。\n"); while (1) { c = _getche(); if (c == 0x1B) { break; } if (c == '-') { ret = oq(buf, &op, &chr); if (ret == 0) { printf("\nEMPTY!\n"); } else { printf("输出元素为:%c\n", chr); } continue; } if (c == '+') { printf("\n当前队列内容:"); pq(buf, ip, op); printf("队首下标:%d 队尾下标:%d\n", op, ip); continue; } if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z')) { ret = iq(buf, &ip, c); if (ret == 0) { printf("\nFULL!\n"); } continue; } } return 0; }
int main() { size_t i; char a[] = {'P', 'R', 'I', 'O', '*', 'R', '*', '*', 'I', '*', 'T', '*', 'Y', '*', '*', '*', 'Q', 'U', 'E', '*', '*', '*', 'U', '*', 'E'}; MaxPQ<char> pq(10); for (i = 0; i < (sizeof(a) / sizeof(*a)); i++) { if (a[i] == '*') std::cout << pq.delMax() << std::endl; else pq.insert(a[i]); } return 0; }
LPDbBoxType * TfrmRetrieveMain::getBoxType( ) { std::set < int > aliquots, boxes, projects, tubes; for( const GridEntry & ge : rows ) { aliquots.insert( ge.aid ); boxes.insert( ge.bid ); projects.insert( ge.pid ); } if( aliquots.size( ) > 3 ) { throw Exception( "Cannot include more than three aliquot types" ); } LQuery cq( LIMSDatabase::getCentralDb( ) ); LPDbBoxNames boxList; for( int boxID : boxes ) { const LPDbBoxName * box = boxList.readRecord( cq, boxID ); int tubeSizeID = 0; if( box != NULL ) { const LCDbBoxSize * size = box->getLayout( ); if( size != NULL ) { tubeSizeID = size->getTubeType( ); } } tubes.insert( tubeSizeID ); } if( tubes.size( ) != 1 || tubes.count( 0 ) == 1 ) { throw Exception( "Tubes must all be the same size" ); } LPDbBoxType *boxType = NULL; frmNewBoxType->init( aliquots, *tubes.begin( ) ); if( frmNewBoxType->ShowModal( ) == mrOk ) { boxType = frmNewBoxType->getDetails( ); } if( boxType != NULL ) { if( projects.size( ) > 1 ) { boxType->setProjectCID( 0 ); } else if( boxType->getID( ) == 0 ) { boxType->setProjectCID( *projects.begin( ) ); } for( int projID : projects ) { LQuery pq( LIMSDatabase::getProjectDb( projID ) ); if( !boxType->saveRecord( pq, cq ) ) { throw Exception( "Cannot create selected box type" ); } } } return boxType; }
int findKthLargest(vector<int>& nums, int k) { priority_queue<int, vector<int>, greater<int> > pq(nums.begin(), nums.begin() + k); for (auto itr = nums.begin() + k; itr != nums.end(); ++itr) { if (*itr > pq.top()) { pq.pop(); pq.push(*itr); } } return pq.top(); }
void test(int size, int tries, int maxnum) { FixedMinQueue<int> pq(size); std::vector<int> elems; for (int i = 0; i < size; ++i) { int num = rand() % (maxnum); pq.push(num); elems.push_back(num); } assert(min_elem(elems, size) == pq.min()); for (int i = 0; i < tries; ++i) { int num = rand() % (2 * tries); pq.push(num); elems.push_back(num); assert(min_elem(elems, size) == pq.min()); } }
vector<Node> findKClosestPoint(vector<Node> nodes, int k) { if (k >= nodes.size()) return nodes; priority_queue<Node, vector<Node>, cmp> pq(nodes.begin(), nodes.begin() + k); for (int i = k; i < nodes.size(); i++) { if (distance(nodes[i]) < distance(pq.top())) { pq.push(nodes[i]); pq.pop(); } } vector<Node> res; for (int i = 0; i < k; i++) { res.push_back(pq.top()); pq.pop(); } return res; }
void LVector::rotate(const Angle& theta) { take_ownership(); double s, c; theta.sincos(s,c); std::complex<double> z(c, -s); std::complex<double> imz(1., 0.); for (int m=1; m<=_order; m++) { imz *= z; for (PQIndex pq(m,0); !pq.pastOrder(_order); pq.incN()) { int r = pq.rIndex(); std::complex<double> newb = std::complex<double>((*_v)[r], (*_v)[r+1]) * imz; (*_v)[r] = newb.real(); (*_v)[r+1] = newb.imag(); } } }
// static Status LiteParsedQuery::make(const string& ns, int ntoskip, int ntoreturn, int queryOptions, const BSONObj& query, const BSONObj& proj, const BSONObj& sort, const BSONObj& hint, const BSONObj& minObj, const BSONObj& maxObj, bool snapshot, bool explain, LiteParsedQuery** out) { auto_ptr<LiteParsedQuery> pq(new LiteParsedQuery()); pq->_sort = sort; pq->_hint = hint; pq->_min = minObj; pq->_max = maxObj; pq->_snapshot = snapshot; pq->_explain = explain; Status status = pq->init(ns, ntoskip, ntoreturn, queryOptions, query, proj, false); if (status.isOK()) { *out = pq.release(); } return status; }
void Bubble:: raw_initialize() { Tracer *p = root; for( size_t i=size;i>0;--i,p=p->next ) { pbc(p->vertex); const Tracer *q = p->next; assert(q!=NULL); Vertex pq(p->vertex,q->vertex); pbc(pq); p->edge = pq; p->s2 = p->edge.norm2(); p->s = Sqrt( p->s2 ); } compute_area(); content = pressure * area; compute_geometry(); }
hash_enum_remove_queues(const void *aThread_ptr, nsCOMPtr<nsIEventQueue>& aEldestQueue, void* closure) { // 'aQueue' should be the eldest queue. nsCOMPtr<nsPIEventQueueChain> pie(do_QueryInterface(aEldestQueue)); nsCOMPtr<nsIEventQueue> q; // stop accepting events for youngest to oldest pie->GetYoungest(getter_AddRefs(q)); while (q) { q->StopAcceptingEvents(); nsCOMPtr<nsPIEventQueueChain> pq(do_QueryInterface(q)); pq->GetElder(getter_AddRefs(q)); } return PL_DHASH_REMOVE; }
void Pathfinder::plot_track(std::vector<int> track) { std::priority_queue<int, std::vector<int>, std::greater<int> > pq(std::greater<int>(), track); for (const auto& n: nodes ) { if ( n.index % cols == 0 ) std::cout << "\n"; if ( n.index == pq.top() ) { pq.pop(); if ( n.index == track.back() ) std::cout << "\033[0;36mS\033[0;37m"; else std::cout << "\033[0;36m.\033[0;37m"; } else { if ( n.type == 'T' ) std::cout << "\033[0;36m" << n.type << "\033[0;37m"; else std::cout << n.type ; } } std::cout << "\n\n"; }
void cisstTriangleClosestPointSolver::SolveLamMuNu(const vct3& a, const vct3& p, const vct3& q, const vct3& r, double &lambda, double &mu, double &nu) { vct3 pa=a-p; // b(0)=pa.x; b(1)=pa.y; b(2)=pa.z; vct3 pq=q-p; // A(0,0)=pq.x; A(1,0)=pq.y; A(2,0)=pq.z; vct3 pr=r-p; // A(0,1)=pr.x; A(1,1)=pr.y; A(2,1)=pr.z; for (int i=0;i<3;i++) { b(i)=pa(i); A(i,0)=pq(i); A(i,1)=pr(i); B(i,0)=b(i); // because using dumb interface to solver }; LeastSquaresSolver.Solve(A,B); // replace with HFTI call when can do so lambda=B(0,0); mu=B(1,0); // ditto // nmrAlgorithmHFTI(A,h,P,Tau,g,b,x,0); // x := lambda and mu // lambda = x(0); // mu = x(1); nu = 1.-lambda-mu; };
Node<int>* create_tree(double *freqs, int n_freqs) { auto comp = [](Node<int>* a, Node<int>* b) { return a->get_frequency() > b->get_frequency(); }; priority_queue<Node<int>*, vector<Node<int>*>, decltype(comp)> pq(comp); for(int i = 0; i < n_freqs; ++i) pq.push(new Node<int>(i, freqs[i])); while(pq.size() > 1) { Node<int> *left = pq.top(); pq.pop(); Node<int> *right = pq.top(); pq.pop(); pq.push(new Node<int>(left, right)); } return pq.top(); }
void LVector::write(std::ostream& os, int maxorder) const { int oldprec = os.precision(8); std::ios::fmtflags oldf = os.setf(std::ios::scientific,std::ios::floatfield); if (maxorder < 0 || maxorder > _order) maxorder = _order; os << _order << std::endl; for (int n=0; n<=maxorder; n++) { for(PQIndex pq(n,0); !pq.needsConjugation(); pq.decm()) { os << " " << std::setw(2) << pq.getP() << " " << std::setw(2) << pq.getQ() ; if (pq.isReal()) { os << " " << std::setw(15) << (*this)[pq].real() << std::endl; } else { os << " " << std::setw(15) << (*this)[pq].real() << " " << std::setw(15) << (*this)[pq].imag() << std::endl; } } } os.precision(oldprec); os.flags(oldf); }
/** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */ vector<int> getNewsFeed(int userId) { vector<int> result; auto comp = [](const pair<int,int>& l, const pair<int,int>& r) {return l.second > r.second;}; priority_queue<pair<int,int>, vector<pair<int, int>>, decltype(comp)> pq (comp); unordered_set<int> followingIds = following[userId]; followingIds.insert(userId); for(int followeeId : followingIds) { int count = tweets[followeeId].size(); for(int i=count-1; i>=0 && i>count-11; i--) { pq.push(tweets[followeeId][i]); if(pq.size()>10) pq.pop(); } } while(!pq.empty()) { result.push_back(pq.top().first); pq.pop(); } reverse(result.begin(), result.end()); return result; }
std::vector<int> compute_fair_bonuses (const std::vector<int>& locs) { auto cmp = [](const std::pair<int, int>& lhs, const std::pair<int, int>& rhs) { return lhs.first > rhs.first; }; std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, decltype(cmp)> pq(cmp); int i = 0; for (const auto l: locs) { pq.push(std::make_pair(l, i++)); } std::vector<int> res(locs.size(), 0); while (!pq.empty()) { auto idx = pq.top().second; res[idx] = std::max(idx > 0 ? res[idx - 1] : 0, (idx < locs.size() - 1) ? res[idx+1] : 0) + 1; pq.pop(); } return res; }
int main(int, char *[]) { typedef CGAL::Kinetic::Inexact_simulation_traits::Kinetic_kernel::Function_kernel FK; typedef CGAL::Kinetic::Heap_pointer_event_queue<FK> Q; Q pq(0, 10000, FK()); typedef Q::Key Key; std::vector<Key> items; for (unsigned int i=0; i< 10000; ++i) { Time t(std::rand()/100000.0); items.push_back(pq.insert(t,Event(t))); } for (unsigned int i=0; i< 5000; ++i) { pq.erase(items[i]); } Time last_time = -1; while (!pq.empty()) { Time t= pq.front_priority(); if (t < last_time) { std::cerr << "ERROR: priority of next event (" << pq.front_priority() << ") is before the last one (" << last_time << ").\n"; } assert(t >= last_time); last_time=t; pq.process_front(); if (t < last_time) { std::cerr << "ERROR: wrong event processed (" << pq.front_priority() << ") instead of (" << proc_time_ << ").\n"; } if (proc_time_!= t) { std::cerr << "ERROR: wrong event time. Expected: " << t << " got " << proc_time_ <<std::endl; } assert(proc_time_==t); } return EXIT_SUCCESS; }
vector<pair<int, int>> getSkyline(vector<vector<int>>& b) { int sz = b.size(); vector<pair<int, int>> ans; if (sz == 0) { return ans; } vector<VEdge> edges(sz*2); for (int i=0; i<sz; ++i) { edges[i*2] = { i, b[i][0], b[i][2], true }; edges[i*2+1] = { i, b[i][1], b[i][2], false }; } sort(edges.begin(), edges.end(), [](const VEdge &e1, const VEdge &e2) { return e1.pos < e2.pos; }); auto cmp = [](const VEdge &e1, const VEdge &e2) { return e1.h < e2.h; }; priority_queue<VEdge, vector<VEdge>, decltype(cmp)> pq(cmp); vector<bool> act(sz, false); int pos = edges[0].pos, mxh = -1; for (int i=0, end=sz<<1; i<=end; ++i) { if (i==end || edges[i].pos!=pos) { while (!pq.empty() && !act[pq.top().id]) { pq.pop(); } if (pq.empty() || pq.top().h != mxh) { mxh = pq.empty() ? 0 : pq.top().h; ans.push_back(make_pair(pos, mxh)); } if (i == end) { break; } } if (edges[i].start) { act[edges[i].id] = true; pq.push(edges[i]); } else { act[edges[i].id] = false; } pos = edges[i].pos; } return ans; }
void mergeLookaround(vector<LookEntry> &lookaround, const vector<LookEntry> &more_lookaround) { if (lookaround.size() >= MAX_LOOKAROUND_ENTRIES) { DEBUG_PRINTF("big enough!\n"); return; } // Don't merge lookarounds at offsets we already have entries for. ue2::flat_set<s8> offsets; for (const auto &e : lookaround) { offsets.insert(e.offset); } map<s32, CharReach> more; LookPriority cmp(more); priority_queue<s32, vector<s32>, LookPriority> pq(cmp); for (const auto &e : more_lookaround) { if (!contains(offsets, e.offset)) { more.emplace(e.offset, e.reach); pq.push(e.offset); } } while (!pq.empty() && lookaround.size() < MAX_LOOKAROUND_ENTRIES) { const s32 offset = pq.top(); pq.pop(); const auto &cr = more.at(offset); DEBUG_PRINTF("added {%d,%s}\n", offset, describeClass(cr).c_str()); lookaround.emplace_back(verify_s8(offset), cr); } // Order by offset. sort(begin(lookaround), end(lookaround), [](const LookEntry &a, const LookEntry &b) { return a.offset < b.offset; }); }
double Malandain::findDiameter(const PointsVec& input, Point& A, Point& B) { double diam = 0; bool stop = false; bool empty = false; pii pq(-1, -1); pii res; do { double curDiam = findDoubleNormal(input, pq); if (diam < curDiam) { diam = curDiam; res = pq; pq.first = getOuterPoint(input, pq); if (pq.first < 0) { stop = true; empty = true; } } else { stop = true; } } while (!stop); if (!empty) { diam = findDiamExtraPoints(input, res); } A = input.col(res.first); B = input.col(res.second); return diam; }
TEST(FCPriQueue, push_pop) { int ops = 1000; int work = 0; std::chrono::steady_clock::time_point when = std::chrono::steady_clock::now() + std::chrono::hours(24); for (auto n : nthr) { nthreads = n; FCPQ pq(10000); auto fn = [&](uint32_t tid) { for (int i = tid; i < ops; i += nthreads) { CHECK(pq.try_push(i)); CHECK(pq.try_push_until(i, when)); pq.push(i); doWork(work); int v; CHECK(pq.try_pop(v)); EXPECT_NE(pq.try_pop_until(when), folly::none); pq.pop(v); doWork(work); } }; run_once(pq, fn); } }