当前位置: 首页>>代码示例>>C++>>正文


C++ PQ类代码示例

本文整理汇总了C++中PQ的典型用法代码示例。如果您正苦于以下问题:C++ PQ类的具体用法?C++ PQ怎么用?C++ PQ使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了PQ类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Node

VectorXf GeodesicDistance::compute(int nd) const {
	PQ q;
	q.push( Node( nd, 0 ) );
	VectorXf d = 1e10*VectorXf::Ones( N_ );
	updatePQ( d, q );
	return d;
}
开发者ID:ClarkWang12,项目名称:object-proposals,代码行数:7,代码来源:geodesics.cpp

示例2: benchOpsThread

void benchOpsThread(bench_ops_thread_arg_t * arg)
{
    wbmm_thread_init(arg->tid);

    uint32_t seed1 = arg->tid;
    uint32_t seed2 = seed1 + 1;

    uint64_t ops = 0;
    PQ * set = (PQ *)arg->set;

    while (!bench_begin);

    while (!bench_stop) {
        int op  = rand_r_32(&seed1) % 100;
        int key = rand_r_32(&seed2) % KEY_RANGE;
        if (op < 50) {
            set->add(key);
        }
        else {
            key = set->remove();
        }
        for (int i = 0; i < DELAY; i++) spin64();
        ops++;
    }
    arg->ops = ops;
}
开发者ID:Gwinel,项目名称:nonblocking,代码行数:26,代码来源:pqbench.cpp

示例3: dijdij

int dijdij(int st,int ed){
    v[0].reset(); v[1].reset();
    for(int i=0;i<111;++i)d[i].clear();
    d[st].pb(0);
    PQ<pii,vector<pii>,greater<pii>> pq; pq.push({0,st});
    while(pq.size()){
        while(pq.size() && v[1][pq.top().Y])pq.pop();
        if(pq.empty())break;
        pii now=pq.top(); pq.pop();
        int stp=v[0][now.Y]?1:0;
        PDE3(now,stp,pq);
        v[stp][now.Y]=1;
        for(pii e:G[now.Y]){
            d[e.X].pb(now.X+e.Y);
            sort(d[e.X].begin(),d[e.X].end());
            d[e.X].resize(unique(d[e.X].begin(),d[e.X].end())-d[e.X].begin());
            if(d[e.X].size()>2u){
                sort(d[e.X].begin(),d[e.X].end());
                d[e.X].pop_back();
            }
            if(now.X+e.Y<=d[e.X].back()){
                pq.push({now.X+e.Y,e.X});
            }
        }
    }
    if(d[ed].size()<2u)return -1;
    else return d[ed][1];
}
开发者ID:edisonhello,项目名称:cpp,代码行数:28,代码来源:1561.cpp

示例4: test2

void test2() {
  PQ p;
  for (int i = 0;i < 10;i++) {
    p.push(10-i);
  }

  while (p.empty() == false) {
    cout << p.top() << endl;
    p.pop();
  }
}
开发者ID:5730059021KS,项目名称:data2015,代码行数:11,代码来源:simple_pq_test.cpp

示例5: sanityCheck

static bool sanityCheck()
{
    PQ set;

    uint32_t seed = 0;
    for (uint32_t i = 0; i < INIT_SIZE; i++) {
        int key = rand_r_32(&seed) % KEY_RANGE;
        set.add(key);
    }

    bench_begin = false;
    bench_stop = false;

    thread *              thrs[NUM_THREADS];
    sanity_thread_arg_t   args[NUM_THREADS];

    for (uint32_t j = 0; j < NUM_THREADS; j++) {
        sanity_thread_arg_t & arg = args[j];
        arg.tid = j + 1;
        arg.set = &set;
        arg.ops = 0;
        thrs[j] = new thread(sanityThread<PQ>, &arg);
    }

    // broadcast begin signal
    bench_begin = true;
    sleep(DURATION);
    bench_stop = true;

    for (uint32_t j = 0; j < NUM_THREADS; j++) thrs[j]->join();

    uint32_t old = 0;
    for (uint32_t i = 0; i < INIT_SIZE; i++) {
        uint32_t num = set.remove();
        if (old > num) {
            cout << "error: heap invariant violated: "
                      << "prev = " << old << " "
                      << "curr = " << num << endl;
            return false;
        }
        if (num == PQ_VAL_MAX) {
            cout << "error: missing element(not linearizable)" << endl;
            return false;
        }
        old = num;
    }
    uint32_t num = set.remove();
    if (num != PQ_VAL_MAX) {
        cout << "error: extra element(not linearizable)" << endl;
        return false;
    }
    cout << "Sanity check: okay." << endl;
    return true;
}
开发者ID:Gwinel,项目名称:nonblocking,代码行数:54,代码来源:pqbench.cpp

示例6: pq_delete_min

void
pq_delete_min(PQ pq, void *retval)
{
    int floater;        /* previous loser floating down */
    int small_child;    /* smaller child of floater */

    assert(!pq_is_empty(pq));

    /* first copy out the winner */
    memcpy(retval, REF(pq, 0), pq->element_length);

    --(pq->n);

    if(pq_is_empty(pq)) {
        /* pq empty, nothing to do */
        return;
    }

    /* else */
    memcpy(REF(pq, 0), REF(pq, pq->n), pq->element_length);

    floater = 0;

    for(;;) {
        /* find smaller child of floater */
        if(Child(floater, 0) >= pq->n) {
            return;     /* no children, bail out */
        } else if(Child(floater, 1) >= pq->n) {
            small_child = Child(floater, 0);
        } else if(pq->compare(REF(pq, Child(floater, 0)), REF(pq, Child(floater, 1))) < 0) {
            small_child = Child(floater, 0);
        } else {
            small_child = Child(floater, 1);
        }

        /* is floater <= small_child? */
        if(pq->compare(REF(pq, floater), REF(pq, small_child)) <= 0) {
            /* yes, we are done */
            return;
        } else {
            /* no, swap and continue floating down */
            pq_swap(pq, floater, small_child);
            floater = small_child;
        }
    }
}
开发者ID:Arkham,项目名称:c_examples,代码行数:46,代码来源:dijkstra.c

示例7: testHeap

void  testHeap ( int n ) {
   T* A = new T[2*n/3]; //创建容量为2*n/3的数组,并
   for ( int i = 0; i < 2 * n / 3; i++ ) A[i] = dice ( ( T ) 3 * n ); //在其中随机生成2*n/3个词条
   /*DSA*/printf ( "%d random keys created:\n", 2 * n / 3 );
   /*DSA*/for ( int i = 0; i < 2 * n / 3; i++ ) print ( A[i] ); printf ( "\n" );
   PQ heap ( A + n / 6, n / 3 ); //批量建堆(PQ_ComplHeap实现了Robert Floyd算法)
   delete [] A;
   /*DSA*/system("cls"); print ( heap );  Sleep(100);
   while ( heap.size() < n ) { //随机测试
      if ( dice ( 100 ) < 70 ) { //70%概率插入新词条
         T e = dice ( ( T ) 3 * n ); /*DSA*/printf ( "Inserting" ); print ( e ); printf ( " ...\n" );
         heap.insert ( e ); /*DSA*/printf ( "Insertion done\n" );
      } else { //30%概率摘除最大词条
         if ( !heap.empty() ) {
            /*DSA*/printf ( "Deleting max ...\n" );
            T e = heap.delMax();/*DSA*/printf ( "Deletion done with" ); print ( e ); printf ( "\n" );
         }
      }
      /*DSA*/system("cls"); print ( heap ); Sleep(100);
   }
   while ( !heap.empty() ) { //清空
      T e = heap.delMax();/*DSA*/printf ( "Deletion done with" ); print ( e ); printf ( "\n" );
      /*DSA*/system("cls"); print ( heap ); Sleep(100);
   }
}
开发者ID:HillBamboo,项目名称:MOOCs,代码行数:25,代码来源:pq_test.cpp

示例8: runBench

static void runBench()
{
    PQ set;

    uint32_t seed = 0;
    for (uint32_t i = 0; i < INIT_SIZE; i++) {
        int key = rand_r_32(&seed) % KEY_RANGE;
        set.add(key);
    }

    bench_begin = false;
    bench_stop = false;

    thread *                 thrs[NUM_THREADS];
    bench_ops_thread_arg_t   args[NUM_THREADS];

    for (uint32_t j = 0; j < NUM_THREADS; j++) {
        bench_ops_thread_arg_t & arg = args[j];
        arg.tid = j + 1;
        arg.set = &set;
        arg.ops = 0;
        thrs[j] = new thread(benchOpsThread<PQ>, &arg);
    }

    // broadcast begin signal
    bench_begin = true;

    sleep(DURATION);

    bench_stop = true;

    for (uint32_t j = 0; j < NUM_THREADS; j++)
        thrs[j]->join();

    uint64_t totalOps = 0;
    for (uint32_t j = 0; j < NUM_THREADS; j++) {
        totalOps += args[j].ops;
    }

    cout << ("Throughput(ops/ms): ")
         << std::setprecision(6)
         << (double)totalOps / DURATION / 1000 << endl;
}
开发者ID:Gwinel,项目名称:nonblocking,代码行数:43,代码来源:pqbench.cpp

示例9: HuffHuff

/**
  *Requires that all Tree Nodes representing Active Characters have been created
  *And Enqueued into the Priorty Queue
  *Generates a huffman tree by combining the nodes in th PQ
  *the root of the tree is at the head of the priorty queue
*/
	void Huffman:: HuffHuff()		//now it creates the huffman tree
	{
   	if(!(q->getSize1())) //single element
         q->enq(mTreeNodes(q->deq(),NULL));
		while(q->getSize1())
		{
			q->enq(mTreeNodes(q->deq(),q->deq()));
		}
		cout<<"\n\n\t\tLegend Tree along with char at end\n"<<endl;
		q->getHead()->displayTree();
		system("pause");
	}
开发者ID:umar-qureshi2,项目名称:UG-Courses,代码行数:18,代码来源:Compresser.cpp

示例10: insertNext

//Insert limit elements of the file fin into heap.
void insertNext(PQ & pq, ifstream & fin, int limit = 0)
{
	if (limit == 0)
		limit = numeric_limits<int>::max();
	string word;
	int ct;
	while (!fin.eof() && pq.size < limit){
		fin >> word >> ct;
		pq.insert(ItemType(word, ct));
	}
}
开发者ID:jenniferballing,项目名称:CS3Program5,代码行数:12,代码来源:TestPQ,.cpp

示例11: sanityThread

void sanityThread(sanity_thread_arg_t * arg)
{
    wbmm_thread_init(arg->tid);

    uint32_t seed1 = arg->tid;
    uint32_t seed2 = seed1 + 1;

    uint64_t ops = 0;
    PQ * set = (PQ *)arg->set;

    while (!bench_begin);

    while (!bench_stop) {
        int key = rand_r_32(&seed2) % KEY_RANGE;
        set->add(key);
        key = set->remove();
        ops++;
    }
    arg->ops = ops;
}
开发者ID:Gwinel,项目名称:nonblocking,代码行数:20,代码来源:pqbench.cpp

示例12: main

int main() {
    int n, m;
    scanf("%d %d", &n, &m);

    vector<vector<pii>> edges(n);
    while(m--) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        edges[u].push_back({w, v});
        edges[v].push_back({w, u});
    }

    PQ pq;
    pq.push({0,0});
    while(!pq.empty()) {
        int dist = pq.top().first;
        int curr = pq.top().second;
        pq.pop();

        if (vis[curr] != false) continue;
        vis[curr] = true;

        /*
         * curr visited in "shortest path" order here
         */

        for(pii it : edges[curr]) {
            int ndist = it.first + dist;
            int next = it.second;
            pq.push({ndist, next});
        }
    }

    return 0;
}
开发者ID:jeffiar,项目名称:comp-algs,代码行数:35,代码来源:disjkstra.cpp

示例13: dijkstra

int dijkstra(int start ,int goal ,int maxRank)
	{
	memset(dis , 0x7F ,sizeof(dis));
	memset(apr , 0 	  ,sizeof(apr));
	dis [start] = 0;
	que . size =0;
	que . push(start);
	while(!que . empty())
		{
		PQNode t;
		bool notGet = true;
		while(!que . empty())
			{
			t = que.top();
			que.pop();
			if(!apr[t . no]) 
				{
				notGet = false;
				break;
				}
			}
		if(notGet)break;
		for(Node *p = adj[t . no];p ; p=p->next)
			{
			if( rank [p->y] > maxRank) continue;
			if( rank [p->y] < maxRank - limRank) continue;
			if(dis[p->y] > dis[t .no] + p->w)
				{
				dis[p->y] = dis[t .no] + p->w;
				que .push(p->y);
				}
			}
		apr [t. no] = true;
		}
	if( !apr[goal] ) return -1;
	return dis [goal];
	}
开发者ID:hzhua,项目名称:ojprogram,代码行数:37,代码来源:1062.cpp

示例14: sanityCheckSequential

bool sanityCheckSequential()
{
    const int max = 10000;
    std::priority_queue<int32_t, vector<int32_t>, pqcompare> contrast;
    PQ m;
    uint32_t seed = 0;
    for (int i = 0; i < max; i++) {
        int32_t temp = rand_r_32(&seed) % KEY_RANGE;
        contrast.push(temp);
        m.add(temp);
    }
    for (int i = 0; i < max - 1; i++) {
        uint32_t r1 = m.remove();
        uint32_t r2 = contrast.empty() ? PQ_VAL_MAX : contrast.top();
        if (!contrast.empty()) contrast.pop();
        if (r1 != r2) {
            cout << "different element at index " << i << ":"
                 << r1 << " " << r2 <<  endl;
            return false;
        }
    }
    cout << "Sanity check: okay." << endl;
    return true;
}
开发者ID:Gwinel,项目名称:nonblocking,代码行数:24,代码来源:pqbench.cpp

示例15: test1

void test1() {
  PQ p;
  p.push(1);
  p.push(2);
  p.push(3);
  p.push(4);
  p.push(5);

  while (p.empty() == false) {
    cout << p.top() << endl;
    p.pop();
  }

}
开发者ID:5730059021KS,项目名称:data2015,代码行数:14,代码来源:simple_pq_test.cpp


注:本文中的PQ类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。