int main(void){ char c; int i=0, j=0, k=0; PQueue *min = pq_init(); char array[MAX_UNIQUE_CHARS]; char freq[MAX_UNIQUE_CHARS]; char path[MAX_LENGTH]; gets(array); gets(freq); gets(path); int r=0; while(array[r]!=NULL){ Tree *node= tree_init(freq[r]-'0', array[r]); pq_enqueue(min, node, freq[r]-'0'); r+=2; } while(min->size > 1){ Tree *left=pq_dequeue(min); Tree *right=pq_dequeue(min); Tree *node=tree_merge(left, right); pq_enqueue(min, node, node->root->freq); } List *l=init_list(9); Stack *s=init_stack(); l=recursive_Encoding(min->data[0]->root, s, l); read_path(min->data[0]->root, path); }
void echo_test(){ #ifdef USE_FREERTOS for( ;; ){ /* Wait to receive data for echo test */ if( xQueueReceive( echo_queue, &echo_data, portMAX_DELAY )){ /* * The data can be processed here and send back * Since it is simple echo test, the data is sent without * processing */ xQueueSend( OpenAMPInstPtr.send_queue, &echo_data, portMAX_DELAY ); } } #else /* check whether data is received for echo test */ if(pq_qlength(echo_queue) > 0){ echo_data = pq_dequeue(echo_queue); /* * The data can be processed here and send back * Since it is simple echo test, the data is sent without * processing */ pq_enqueue(OpenAMPInstPtr.send_queue, echo_data); } #endif }
int main() { prio_q* pq = calloc(1,sizeof(prio_q)); printf("%u elements inserted.\n", pq->n); pq_insert(pq, 5); pq_insert(pq, 50); pq_insert(pq, 60); pq_insert(pq, 70); pq_insert(pq, 80); pq_insert(pq, 90); pq_insert(pq, 342); pq_insert(pq, 3); printf("%u elements inserted.\n", pq->n); pq_print_tree(pq); printf("Trying to dequeue..\n"); int foo = pq_dequeue(pq); printf("Got %d..\n", foo); pq_print_tree(pq); printf("Trying to run through..\n"); pq_run(pq); int a[5] = {4543,-33,4,54,2}; printf("Original: "); int j = 0; for(; j < 5; ++j) printf("%d ", a[j]); printf("\n"); heapsort(a, 5); printf("Heapsorted: "); for(j=0; j < 5; ++j) printf("%d ", a[j]); printf("\n"); free(pq); return 0; }
int test_move_priority_full(void) { int i; p_queue pq; pq_init(&pq); for (i = 0; i < N_ELEMS; i++) { if (pq_enqueue(&pq, i, HIGH) == PQ_FAILURE) { return TEST_FAILURE; } } for (i = 0; i < N_ELEMS; i++) { if (pq_front(&pq) != i) { return TEST_FAILURE; } if (pq_move( &pq, i, HIGH, LOWEST ) != PQ_SUCCESS) { return TEST_FAILURE; } } for (i = 0; i < N_ELEMS; i++) { if (pq_front(&pq) != i) { return TEST_FAILURE; } if (pq_dequeue(&pq) != PQ_SUCCESS) { return TEST_FAILURE; } } return TEST_SUCCESS; }
int test_move_interleave(void) { int i; p_queue pq; pq_init(&pq); for (i = 0; i < N_ELEMS; i += 2) { if (pq_enqueue(&pq, i, LOWEST) == PQ_FAILURE) { return TEST_FAILURE; } } for (i = 0; i < N_ELEMS; i += 2) { pq_move(&pq, i, LOWEST, HIGH); pq_enqueue(&pq, i + 1, HIGH); } for (i = 0; i < N_ELEMS; i++) { if (pq_front(&pq) != i) { return TEST_FAILURE; } if (pq_dequeue(&pq) != PQ_SUCCESS) { return TEST_FAILURE; } } return TEST_SUCCESS; }
int test_full_flush_all(void) { int i; priority_t p; p_queue pq; pq_init(&pq); for (p = HIGH; p < N_PRIORITIES; p++) { for (i = 0; i < N_ELEMS; i++) { if (pq_enqueue(&pq, i, p) == PQ_FAILURE) { return TEST_FAILURE; } } for (i = 0; i < N_ELEMS; i++) { if (pq_front(&pq) != i) { return TEST_FAILURE; } if (pq_dequeue(&pq) != PQ_SUCCESS) { return TEST_FAILURE; } } } for (p = LOWEST; p != HIGH - 1; p--) { for (i = 0; i < N_ELEMS; i++) { if (pq_enqueue(&pq, i, p) == PQ_FAILURE) { return TEST_FAILURE; } } for (i = 0; i < N_ELEMS; i++) { if (pq_front(&pq) != i) { return TEST_FAILURE; } if (pq_dequeue(&pq) != PQ_SUCCESS) { return TEST_FAILURE; } } } return TEST_SUCCESS; }
// Run through all the elements in the queue in order void pq_run(prio_q* pq) { int i = pq->n; int p; while(i--) { p = pq_dequeue(pq); printf("%d ", p); } printf("\n"); }
int test_cycle(void) { int i; int limit = N_ELEMS / 2; p_queue pq; pq_init(&pq); for (i = 0; i < N_ELEMS; i++) { if (pq_enqueue(&pq, i, HIGH) == PQ_FAILURE) { return TEST_FAILURE; } } for (i = 0; i < limit; i++) { if (pq_front(&pq) != i) { return TEST_FAILURE; } if (pq_dequeue(&pq) != PQ_SUCCESS) { return TEST_FAILURE; } } for (i = N_ELEMS; i < N_ELEMS + limit; i++) { if (pq_enqueue(&pq, i, HIGH) == PQ_FAILURE) { return TEST_FAILURE; } } for (i = 0; i < N_ELEMS; i++) { if (pq_front(&pq) != i + limit) { return TEST_FAILURE; } if (pq_dequeue(&pq) != PQ_SUCCESS) { return TEST_FAILURE; } } return TEST_SUCCESS; }
/* * low_level_input(): * * Should allocate a pbuf and transfer the bytes of the incoming * packet from the interface into the pbuf. * */ static struct pbuf * low_level_input(struct netif *netif) { struct xemac_s *xemac = (struct xemac_s *)(netif->state); xemacliteif_s *xemacliteif = (xemacliteif_s *)(xemac->state); /* see if there is data to process */ if (pq_qlength(xemacliteif->recv_q) == 0) return NULL; /* return one packet from receive q */ return (struct pbuf *)pq_dequeue(xemacliteif->recv_q); }
/* * low_level_output(): * * Should do the actual transmission of the packet. The packet is * contained in the pbuf that is passed to the function. This pbuf * might be chained. * */ static err_t low_level_output(struct netif *netif, struct pbuf *p) { SYS_ARCH_DECL_PROTECT(lev); struct xemac_s *xemac = (struct xemac_s *)(netif->state); xemacliteif_s *xemacliteif = (xemacliteif_s *)(xemac->state); XEmacLite *instance = xemacliteif->instance; struct pbuf *q; SYS_ARCH_PROTECT(lev); /* check if space is available to send */ if (XEmacLite_TxBufferAvailable(instance) == TRUE) { if (pq_qlength(xemacliteif->send_q)) { /* send backlog */ _unbuffered_low_level_output(instance, (struct pbuf *)pq_dequeue(xemacliteif->send_q)); } else { /* send current */ _unbuffered_low_level_output(instance, p); SYS_ARCH_UNPROTECT(lev); return ERR_OK; } } /* if we cannot send the packet immediately, then make a copy of the whole packet * into a separate pbuf and store it in send_q. We cannot enqueue the pbuf as is * since parts of the pbuf may be modified inside lwIP. */ q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_POOL); if (!q) { #if LINK_STATS lwip_stats.link.drop++; #endif SYS_ARCH_UNPROTECT(lev); return ERR_MEM; } for (q->len = 0; p; p = p->next) { memcpy(q->payload + q->len, p->payload, p->len); q->len += p->len; } if (pq_enqueue(xemacliteif->send_q, (void *)q) < 0) { #if LINK_STATS lwip_stats.link.drop++; #endif SYS_ARCH_UNPROTECT(lev); return ERR_MEM; } SYS_ARCH_UNPROTECT(lev); return ERR_OK; }
void communication_task(){ int status; rsc_info.rsc_tab = (struct resource_table *)&resources; rsc_info.size = sizeof(resources); zynqMP_r5_gic_initialize(); /* Initialize RPMSG framework */ status = remoteproc_resource_init(&rsc_info, rpmsg_channel_created, rpmsg_channel_deleted, rpmsg_read_cb ,&proc); if (status < 0) { return; } #ifdef USE_FREERTOS comm_queueset = xQueueCreateSet( 2 ); xQueueAddToSet( OpenAMPInstPtr.send_queue, comm_queueset); xQueueAddToSet( OpenAMPInstPtr.lock, comm_queueset); #else env_create_sync_lock(&OpenAMPInstPtr.lock,LOCKED); #endif env_enable_interrupt(VRING1_IPI_INTR_VECT,0,0); while (1) { #ifdef USE_FREERTOS QueueSetMemberHandle_t xActivatedMember; xActivatedMember = xQueueSelectFromSet( comm_queueset, portMAX_DELAY); if( xActivatedMember == OpenAMPInstPtr.lock ) { env_acquire_sync_lock(OpenAMPInstPtr.lock); process_communication(OpenAMPInstPtr); } if (xActivatedMember == OpenAMPInstPtr.send_queue) { xQueueReceive( OpenAMPInstPtr.send_queue, &send_data, 0 ); rpmsg_send(app_rp_chnl, send_data.data, send_data.length); } #else env_acquire_sync_lock(OpenAMPInstPtr.lock); process_communication(OpenAMPInstPtr); echo_test(); /* Wait for the result data on queue */ if(pq_qlength(OpenAMPInstPtr.send_queue) > 0) { send_data = pq_dequeue(OpenAMPInstPtr.send_queue); /* Send the result of echo_test back to master. */ rpmsg_send(app_rp_chnl, send_data->data, send_data->length); } #endif } }
static void xemacif_send_handler(void *arg) { struct xemac_s *xemac = (struct xemac_s *)(arg); xemacliteif_s *xemacliteif = (xemacliteif_s *)(xemac->state); XEmacLite *instance = xemacliteif->instance; struct xtopology_t *xtopologyp = &xtopology[xemac->topology_index]; XIntc_AckIntr(xtopologyp->intc_baseaddr, 1 << xtopologyp->intc_emac_intr); if (pq_qlength(xemacliteif->send_q) && (XEmacLite_TxBufferAvailable(instance) == XTRUE)) { struct pbuf *p = pq_dequeue(xemacliteif->send_q); _unbuffered_low_level_output(instance, p); pbuf_free(p); } }
// Sort a list void heapsort(int s[], int i) { prio_q* pq = calloc(1,sizeof(prio_q)); int j; // Insert them all in a heap for(j=0; j<i; ++j) { pq_insert(pq, s[j]); } pq_print_tree(pq); // Now replace them in the original array for(j=0; j<i; ++j) { s[j] = pq_dequeue(pq); } free(pq); }
int test_basic(void) { int i; p_queue pq; pq_init(&pq); pq_enqueue(&pq, 0, HIGH); pq_enqueue(&pq, 1, MED); pq_enqueue(&pq, 2, LOW); pq_enqueue(&pq, 3, LOWEST); for (i = 0; i < 4; i++) { if (pq_front(&pq) != i) { return TEST_FAILURE; } if (pq_dequeue(&pq) != PQ_SUCCESS) { return TEST_FAILURE; } } return TEST_SUCCESS; }
int test_reverse(void) { int i; p_queue pq; pq_init(&pq); pq_enqueue(&pq, 0, LOWEST); pq_enqueue(&pq, 1, LOW); pq_enqueue(&pq, 2, MED); pq_enqueue(&pq, 3, HIGH); for (i = 3; i >= 0; i--) { if (pq_front(&pq) != i) { return TEST_FAILURE; } if (pq_dequeue(&pq) != PQ_SUCCESS) { return TEST_FAILURE; } } return TEST_SUCCESS; }
void matrix_mul(){ #ifdef USE_FREERTOS for( ;; ){ if( xQueueReceive(mat_mul_queue, &mat_array, portMAX_DELAY )){ env_memcpy(matrix_array, mat_array.data, mat_array.length); Matrix_Multiply(&matrix_array[0], &matrix_array[1], &matrix_result); mat_result_array.length = sizeof(matrix); mat_result_array.data = &matrix_result; xQueueSend( OpenAMPInstPtr.send_queue, &mat_result_array, portMAX_DELAY ); } } #else if(pq_qlength(mat_mul_queue) > 0){ mat_array = pq_dequeue(mat_mul_queue); env_memcpy(matrix_array, mat_array->data, mat_array->length); /* Process received data and multiple matrices. */ Matrix_Multiply(&matrix_array[0], &matrix_array[1], &matrix_result); mat_result_array.length = sizeof(matrix); mat_result_array.data = &matrix_result; pq_enqueue(OpenAMPInstPtr.send_queue, &mat_result_array); } #endif }
/* find the lowest cost path and mark it on the map */ void make_path(struct map *map, int x0, int y0, int x1, int y1) { int start, end, cost, i, index, mdistance; pq_t * pq; int edges[4]; pq = pq_create(); cost = 0; start = y0 * map->width + x0; end = y1 * map->width + x1; map->grid[start].flags |= SQ_FLAG_START; map->grid[end].flags |= SQ_FLAG_GOAL; map->grid[start].glyph = 'A'; map->grid[end].glyph = 'B'; if(map->grid[start].cost == -1 || map->grid[end].cost == -1) { map->cost = -1; return; } curses_draw_map(map); mdistance = man_distance(start, end, map); if(!pq_enqueue(pq, start, map->grid[start].cost + mdistance)) exit(EXIT_FAILURE); while(1) { if(!pq_dequeue(pq, &index, &cost)) exit(EXIT_FAILURE); if(map->grid[index].parent) /* * Kill two birds with one stone (Visited == enqueued in this case) */ map->grid[index].flags |= SQ_FLAG_VISITED; map->grid[index].flags &= ~SQ_FLAG_ENQUEUED; if(index == end) break; get_edges(index, edges, map); curses_draw_map(map); for(i = 0; i < 4; i++) { if(edges[i] != -1) { map->grid[edges[i]].parent = index; mdistance = man_distance(edges[i], end, map); if(!pq_enqueue(pq, edges[i], (cost) + map->grid[edges[i]].cost)) exit(EXIT_FAILURE); map->grid[edges[i]].flags |= SQ_FLAG_ENQUEUED; map->grid[edges[i]].flags |= SQ_FLAG_VISITED; } } } /* * This next section will fill the path array in the map struct by iterating * through the path found in the above code, which is done by following each * node's parent. * It will begin with setting the total cost of the path to the cost of the * end node, then working backwards towards the start node which has a zero cost * (which is actually appended to the total cost). * It will then begin iteration, setting the glyph and flags, then setting the * start and end node's glyphs, as these will be set with 'o' during the loop. * If the start and the end have the same coordinates, there will be a zero cost * because the only node which is appended to the total is the end, which is * the start, which has a zero cost. */ i = end; /* * Instantiate the path array */ map->path = safe_malloc(map->width * map->height * sizeof(int)); map->path_index = 0; map->cost = -1; /* * Dont include the start node's cost in the calculation */ map->grid[start].cost = 0; /* * Make sure the start node's parent is 0; * */ map->grid[start].parent = 0; map->cost = map->grid[i].cost; while(map->grid[i].parent) { /* This will miss the start node, therefore we will append it after the loop */ map->grid[i].flags |= SQ_FLAG_PATH; map->grid[i].glyph = 'o'; map->path[map->path_index++] = i; i = map->grid[i].parent; map->cost += map->grid[i].cost; } map->path[map->path_index++] = i; map->grid[start].glyph = 'A'; map->grid[end].glyph = 'B'; curses_draw_map(map); pq_destroy(pq); }
/* * When the LPs REMOVE timer fires, we should dequeue the next agent * and send it on to it's next location. We must set the next remove * timer appropriately for the agents remaining in our PQ. * * If the remove is from location 0, then this is the first move of the * day, i.e., from home. We activate agent logic to see if agent * will stay home today, due to quarantine, being ill, or having * ill spouse or children to care for. * * NOTE: We want the agent in the event to be consistent with the * agent in the timer, even if they have the same remove timestamp. * The reason for this is that in the reverse computation, I need to put * the agent back into the PQ, and where else can I get it from but the * timer event? * */ void epi_agent_remove(epi_state * state, tw_bf * bf, tw_lp * lp) { tw_memory *buf; epi_agent *a; while(NULL != (buf = pq_dequeue(g_epi_pq[lp->id]))) { a = tw_memory_data(buf); if(a->ts_next != tw_now(lp)) { pq_enqueue(g_epi_pq[lp->id], buf); break; } if(epi_agent_stage_tran(state, bf, buf, lp)) continue; // if days_remaining > 0, then go through motions by moving curr, but // do not move agent. Thus, a->loc[a->curr] may not be home. Need // to do this in order to send network traffic. // only change days_remaining at midnight, so when days_remaining goes // to zero, curr will go to zero and agent will be at home. // Make sure agent is at home. If agent becomes worried well at // work, must continue to move until he is at home. if(a->days_remaining && lp->gid == a->loc[0]) { if ((int) tw_now(lp) % TWENTY_FOUR_HOURS == 0) { if(--a->days_remaining) { epi_agent_add_back(state, lp, buf); continue; } } else { epi_agent_add_back(state, lp, buf); continue; } } if(5 == a->id || 1) printf("%lld: REM %d at %lf \n", lp->gid, a->id, tw_now(lp)); //epi_num_remove(state, bf, a, lp); // if agent is contagious, decrement number of // contagious agents in this location epi_agent_contagious(state, buf, -1); if (state->ncontagious < 0) tw_error(TW_LOC, "Less than zero contagious agents in %lld", lp->gid); // increment location if(++a->curr >= a->nloc) a->curr = 0; epi_agent_send(lp, buf, 0.0, a->loc[a->curr]); } }
int test_dequeue_empty(void) { p_queue pq; pq_init(&pq); if (pq_dequeue(&pq) == PQ_SUCCESS) return TEST_FAILURE; return TEST_SUCCESS; }