bool update_pqueue(PQ *q, int elem, puint32 newcost) { remove_pqueue_elem(q, elem); q->l.cost[elem] = newcost; return insert_pqueue(&q, elem); }
/* * it returns the weight of huffman code, does not build huffman tree * */ static int huffman(pqueue *pq) { int weight = 0; int t1, t2; int p; build_min_heap(pq); while(pq->size > 1){ t1 = extract_min(pq); t2 = extract_min(pq); p = t1 + t2; insert_pqueue(pq, p); weight += p; } return weight; }
/** * Send a message to +dst_pid+ * @param plist List of processes in the system * @param pnum Number of processes in the system * @param pid Source process ID * @param dst_pid Destination process ID * @param type Message type */ void send(process plist, int pnum, int pid, int dst_pid, int type) { int recv_at, recv_at_pri; recv_at = random(1, 5); recv_at_pri = 5 - recv_at; if (request(plist[dst_pid].recv_queue, pid, recv_at_pri) == 0) { // schedule receive on receiving process insert_pqueue(plist[dst_pid].recvd_from, pid, plist[pid].timestamp, INT_MAX - time(), type); printf(" |- %s scheduled to occur on process %d at time %g\n", type == MSG_REQUEST ? "REQUEST" : "REPLY", dst_pid, time() + recv_at); schedule(EV_RECV, recv_at, dst_pid); } else { printf("\tError: Process %d tried to send a message to %d.\n", pid, dst_pid); printf("\tCause: Receive queue of process %d is full!\n", dst_pid); exit(1); } }