本文整理汇总了C++中PQ::compare方法的典型用法代码示例。如果您正苦于以下问题:C++ PQ::compare方法的具体用法?C++ PQ::compare怎么用?C++ PQ::compare使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PQ
的用法示例。
在下文中一共展示了PQ::compare方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: assert
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;
}
}
}
示例2: while
void
pq_insert(PQ pq, const void *elt)
{
int floater; /* new element */
while(pq->n + 1 > pq->size) {
pq->size *= 2;
pq->data = realloc(pq->data, pq->element_length * pq->size);
assert(pq->data);
}
/* copy the new element in */
floater = pq->n++;
memcpy(REF(pq, floater), elt, pq->element_length);
/* float it up until it is at the top */
/* or it is no smaller than its parent */
while(floater > 0 && pq->compare(REF(pq, floater), REF(pq, Parent(floater))) <= 0) {
/* it's smaller than its parent */
pq_swap(pq, floater, Parent(floater));
floater = Parent(floater);
}
}