int get_parcel(PQueue *parcels, Parcel *parcel) { Parcel *data; if (pqueue_size(parcels) == 0) { /* Return that there are no parcels. */ return -1; } else { if (pqueue_extract(parcels, (void **)data) != 0) { /* Return that a parcel could not be retrieved. */ return -1; } else { /* Pass back the highest-priority parcel. */ memcpy(parcel, data, sizeof(Parcel)); free(data); } } return 0; }
/* ** Load the record ID rid and up to N-1 closest descendants into ** the "ok" table. */ void compute_descendants(int rid, int N) { Bag seen; PQueue queue; Stmt ins; Stmt q; bag_init(&seen); pqueue_init(&queue); bag_insert(&seen, rid); pqueue_insert(&queue, rid, 0.0, 0); db_prepare(&ins, "INSERT OR IGNORE INTO ok VALUES(:rid)"); db_prepare(&q, "SELECT cid, mtime FROM plink WHERE pid=:rid"); while( (N--)>0 && (rid = pqueue_extract(&queue, 0))!=0 ) { db_bind_int(&ins, ":rid", rid); db_step(&ins); db_reset(&ins); db_bind_int(&q, ":rid", rid); while( db_step(&q)==SQLITE_ROW ) { int pid = db_column_int(&q, 0); double mtime = db_column_double(&q, 1); if( bag_insert(&seen, pid) ) { pqueue_insert(&queue, pid, mtime, 0); } } db_reset(&q); } bag_clear(&seen); pqueue_clear(&queue); db_finalize(&ins); db_finalize(&q); }
static int build_tree(int *freqs, BiTree **tree) { BiTree *init, *merge, *left, *right; PQueue pqueue; HuffNode *data; int size, c; /***************************************************************************** * * * Initialize the priority queue of binary trees. * * * *****************************************************************************/ *tree = NULL; pqueue_init(&pqueue, compare_freq, destroy_tree); for (c = 0; c <= UCHAR_MAX; c++) { if (freqs[c] != 0) { /*********************************************************************** * * * Set up a binary tree for the current symbol and its frequency. * * * ***********************************************************************/ if ((init = (BiTree *)malloc(sizeof(BiTree))) == NULL) { pqueue_destroy(&pqueue); return -1; } bitree_init(init, free); if ((data = (HuffNode *)malloc(sizeof(HuffNode))) == NULL) { pqueue_destroy(&pqueue); return -1; } data->symbol = c; data->freq = freqs[c]; if (bitree_ins_left(init, NULL, data) != 0) { free(data); bitree_destroy(init); free(init); pqueue_destroy(&pqueue); return -1; } /*********************************************************************** * * * Insert the binary tree into the priority queue. * * * ***********************************************************************/ if (pqueue_insert(&pqueue, init) != 0) { bitree_destroy(init); free(init); pqueue_destroy(&pqueue); return -1; } } } /***************************************************************************** * * * Build a Huffman tree by merging trees in the priority queue. * * * *****************************************************************************/ size = pqueue_size(&pqueue); for (c = 1; c <= size - 1; c++) { /************************************************************************** * * * Allocate storage for the next merged tree. * * * **************************************************************************/ if ((merge = (BiTree *)malloc(sizeof(BiTree))) == NULL) { pqueue_destroy(&pqueue); return -1; } /************************************************************************** * * * Extract the two trees whose root nodes have the smallest frequencies. * * * **************************************************************************/ if (pqueue_extract(&pqueue, (void **)&left) != 0) { pqueue_destroy(&pqueue); free(merge); return -1; } if (pqueue_extract(&pqueue, (void **)&right) != 0) { pqueue_destroy(&pqueue); free(merge); return -1; } /************************************************************************** * * * Allocate storage for the data in the root node of the merged tree. * * * **************************************************************************/ if ((data = (HuffNode *)malloc(sizeof(HuffNode))) == NULL) { pqueue_destroy(&pqueue); free(merge); return -1; } memset(data, 0, sizeof(HuffNode)); /************************************************************************** * * * Sum the frequencies in the root nodes of the trees being merged. * * * **************************************************************************/ data->freq = ((HuffNode *)bitree_data(bitree_root(left)))->freq + ((HuffNode *)bitree_data(bitree_root(right)))->freq; /************************************************************************** * * * Merge the two trees. * * * **************************************************************************/ if (bitree_merge(merge, left, right, data) != 0) { pqueue_destroy(&pqueue); free(merge); return -1; } /************************************************************************** * * * Insert the merged tree into the priority queue and free the others. * * * **************************************************************************/ if (pqueue_insert(&pqueue, merge) != 0) { pqueue_destroy(&pqueue); bitree_destroy(merge); free(merge); return -1; } free(left); free(right); } /***************************************************************************** * * * The last tree in the priority queue is the Huffman tree. * * * *****************************************************************************/ if (pqueue_extract(&pqueue, (void **)tree) != 0) { pqueue_destroy(&pqueue); return -1; } else { pqueue_destroy(&pqueue); } return 0; }
int fastmarch(float *time /* time */, float *source /* source */, int *list /* list */, int *mask /* recv */, float *data /* reco */, float *rhs /* rhs */, upgrad upg /* stencil */) /*< run fast marching eikonal solver >*/ { int its; float xs[3], *p; int npoints, i, j, k, count, length; #ifdef _OPENMP its = omp_get_thread_num(); #else its = 0; #endif /* point t to time for pqueue operations */ t[its] = time; /* source distance from origin */ xs[0] = source[0]-o[0]; xs[1] = source[1]-o[1]; xs[2] = source[2]-o[2]; /* initialize priority queue */ xn[its] = x[its]; x1[its] = x[its]+1; count = 0; length = 0; /* fast marching */ for (npoints = neighbors_nearsource(time,xs); npoints > 0; npoints -= neighbours(time,i)) { /* smallest value in queue */ p = pqueue_extract(); if (p == NULL) { sf_warning("%s: shot (%g,%g,%g) heap exausted!",__FILE__,source[0],source[1],source[2]); break; } i = p-time; /* update wave front */ in[its][i] = SF_IN; /* update stencil */ upgrad_set(upg,time,i,in[its],length); length++; /* update rhs */ k = list[0]; for (j=0; j < list[1]; j++) { if (i == mask[j]) { rhs[k+j] = data[j]-time[i]; count++; break; } } /* break for limited acquisition */ if (count == list[1]) break; } return(length); }
int main(int argc, char **argv) { PQueue pqueue; void *data; int intval[30], i; /***************************************************************************** * * * Initialize the priority queue. * * * *****************************************************************************/ pqueue_init(&pqueue, compare_int, NULL); /***************************************************************************** * * * Perform some priority queue operations. * * * *****************************************************************************/ i = 0; intval[i] = 5; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 10; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 20; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 1; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 25; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 22; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 12; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; while (pqueue_size(&pqueue) > 0) { fprintf(stdout, "Peeking at the highest priority element..Value=%03d\n", *(int *)pqueue_peek(&pqueue)); if (pqueue_extract(&pqueue, (void **)&data) != 0) return 1; fprintf(stdout, "Extracting %03d\n", *(int *)data); print_pqueue(&pqueue); } /***************************************************************************** * * * Destroy the priority queue. * * * *****************************************************************************/ fprintf(stdout, "Destroying the pqueue\n"); pqueue_destroy(&pqueue); return 0; }
/* ** Propagate the tag given by tagid to the children of pid. ** ** This routine assumes that tagid is a tag that should be ** propagated and that the tag is already present in pid. ** ** If tagtype is 2 then the tag is being propagated from an ** ancestor node. If tagtype is 0 it means a propagating tag is ** being blocked. */ static void tag_propagate( int pid, /* Propagate the tag to children of this node */ int tagid, /* Tag to propagate */ int tagType, /* 2 for a propagating tag. 0 for an antitag */ int origId, /* Artifact of tag, when tagType==2 */ const char *zValue, /* Value of the tag. Might be NULL */ double mtime /* Timestamp on the tag */ ){ PQueue queue; /* Queue of check-ins to be tagged */ Stmt s; /* Query the children of :pid to which to propagate */ Stmt ins; /* INSERT INTO tagxref */ Stmt eventupdate; /* UPDATE event */ assert( tagType==0 || tagType==2 ); pqueue_init(&queue); pqueue_insert(&queue, pid, 0.0, 0); /* Query for children of :pid to which to propagate the tag. ** Three returns: (1) rid of the child. (2) timestamp of child. ** (3) True to propagate or false to block. */ db_prepare(&s, "SELECT cid, plink.mtime," " coalesce(srcid=0 AND tagxref.mtime<:mtime, %d) AS doit" " FROM plink LEFT JOIN tagxref ON cid=rid AND tagid=%d" " WHERE pid=:pid AND isprim", tagType==2, tagid ); db_bind_double(&s, ":mtime", mtime); if( tagType==2 ){ /* Set the propagated tag marker on checkin :rid */ db_prepare(&ins, "REPLACE INTO tagxref(tagid, tagtype, srcid, origid, value, mtime, rid)" "VALUES(%d,2,0,%d,%Q,:mtime,:rid)", tagid, origId, zValue ); db_bind_double(&ins, ":mtime", mtime); }else{ /* Remove all references to the tag from checkin :rid */ zValue = 0; db_prepare(&ins, "DELETE FROM tagxref WHERE tagid=%d AND rid=:rid", tagid ); } if( tagid==TAG_BGCOLOR ){ db_prepare(&eventupdate, "UPDATE event SET bgcolor=%Q WHERE objid=:rid", zValue ); } while( (pid = pqueue_extract(&queue, 0))!=0 ){ db_bind_int(&s, ":pid", pid); while( db_step(&s)==SQLITE_ROW ){ int doit = db_column_int(&s, 2); if( doit ){ int cid = db_column_int(&s, 0); double mtime = db_column_double(&s, 1); pqueue_insert(&queue, cid, mtime, 0); db_bind_int(&ins, ":rid", cid); db_step(&ins); db_reset(&ins); if( tagid==TAG_BGCOLOR ){ db_bind_int(&eventupdate, ":rid", cid); db_step(&eventupdate); db_reset(&eventupdate); } if( tagid==TAG_BRANCH ){ leaf_eventually_check(cid); } } } db_reset(&s); } pqueue_clear(&queue); db_finalize(&ins); db_finalize(&s); if( tagid==TAG_BGCOLOR ){ db_finalize(&eventupdate); } }