int main() { queue q; q_push(&q, 1); q_push(&q, 2); q_push(&q, 3); q_push(&q, 4); q_push(&q, 5); q_pop(&q); q_pop(&q); q_pop(&q); q_pop(&q); q_pop(&q); return 0; }
/* * Application entry point. */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { char *string_command; double *outArray; mwSize M; //mwSize is a platform independent alternative to int mwSize i; if(nrhs!=1) { mexErrMsgIdAndTxt("myex:nrhs:invalidN", "Invalid input: One command string required.\n\nValid commands are:\n myex('connect')\n myex('getdata')\n myex('disconnect')"); } string_command = mxArrayToString(prhs[0]); //mexPrintf(">> %s\n", string_command); switch (string_command[0]) { case 'c': Connect(); break; case 'g': // retrieve (any) data from the internal buffer - return as plhs[0] M = q_nelements(); i = M; // start at the end of the output array, because we're actually using a FILO data structure for the internal buffer (stack) plhs[0] = mxCreateDoubleMatrix(q_nelements(),12,mxREAL); outArray = mxGetPr(plhs[0]); // iterate through internal buffer while (!q_isempty()) { // get next item from the internal buffer struct node *topelement = q_topelement(); i--; // add gaze data to output row outArray[i] = topelement->X_px; outArray[i+M] = topelement->Y_px; // N.B. C indexing is like the tranpose of MATLAB variable: http://uk.mathworks.com/matlabcentral/newsreader/view_thread/249954 outArray[i+2*M] = topelement->Timestamp; // add eye position data to output row outArray[i+3*M] = topelement->HasLeftEyePosition; outArray[i+4*M] = topelement->HasRightEyePosition; outArray[i+5*M] = topelement->LeftEyeX_mm; outArray[i+6*M] = topelement->LeftEyeY_mm; outArray[i+7*M] = topelement->LeftEyeZ_mm; outArray[i+8*M] = topelement->RightEyeX_mm; outArray[i+9*M] = topelement->RightEyeY_mm; outArray[i+10*M] = topelement->RightEyeZ_mm; outArray[i+11*M] = topelement->EyePosTimestamp; // remove this item from the internal buffer q_pop(); } break; case 'd': Disconnect(); break; default: mexErrMsgIdAndTxt("myex:nrhs:unrecognised", "Invalid input: Unrecognised command.\n\nValid commands are:\n myex('connect')\n myex('getdata')\n myex('disconnect')"); break; } mxFree(string_command); return; }
critical void getBSPacket(packet_t *myPacket) { uint8_t *packet_ptr = (uint8_t *) myPacket; uint8_t plen, i; plen = q_front() + HEADER_SIZE; for (i = 0; i < plen; i++) packet_ptr[i] = q_pop(); newBSPacket--; }
/** Returns the char data from the top Node. Returns NULL if queue is empty */ char q_pop_char (FIFOQ *q) { if (q->size <= 0) return (char) NULL; // return null if empty queue Node * oldNode = q_pop(q); // remove head Node char result = oldNode->data; // store data from head Node free(oldNode); // oldNode can be safely freed return result; // return the data from the old head Node }
void C (void) { Q_Queue local_mailbox; int sender_pid; q_init(&local_mailbox); while (1) { msg* p; if (local_mailbox.first == NULL) { p = (msg*)(receive_message(&sender_pid)); } else { p = (msg*)q_pop(&local_mailbox); } if (p->mtype == 3) { int msg_value = atoi(p->mtext); if (msg_value%20 == 0) { msg* MessageToSend = (msg*)request_memory_block(); msg* q = (msg*)request_memory_block(); MessageToSend->mtype = 2; MessageToSend->mtext = "Process C"; send_message(13, MessageToSend); q->mtype = 4; q->mtext = ""; delayed_send(7,q,10000); while (1) { msg* r; r = (msg*)(receive_message(&sender_pid)); //block and let other processes execute if (r->mtype == 4) { release_memory_block(r); break; } else { q_push(&local_mailbox, r); } } } } release_memory_block(p); release_processor(); } }
/* Pair : row,column */ int lee_algo(Pair* source, Pair* target){ int i, j, v1, v2; Pair p, v, step[4] = {{1,0},{0,1},{-1,0},{0,-1}}; /* initialize all cells like unvisited */ for(i=0; i<sz.r; ++i){ for(j=0; j<sz.c; ++j){ W[i][j] = -1; } } q_clear(); p_set(W,source,0); q_push(source); while(!q_empty()){ p = q_pop(); for(i=0; i<4; ++i){ v = p_sum(step+i,&p); v1 = p_get(Z,&p); v2 = p_get(Z,&v); #ifdef DEBUG printf("try: %d,%d\n",v.r,v.c); #endif if(p_in(&v) && p_get(W,&v)==-1 && (v1-v2 <= 3) && (v1-v2 >= -1) && can_go(&v)){ #ifdef DEBUG printf("from: %d,%d go: %d,%d\n",p.r,p.c,v.r,v.c); #endif p_set(W,&v,p_get(W,&p)+1); q_push(&v); if(p_eq(&v,target)) break; } } if(p_eq(&v,target)) break; } #ifdef DEBUG printf("q: %d\n",q_empty()); #endif return p_get(W,target); }
void level_order_main(struct node* node, struct DLLHead_q **head_q) { if(node == NULL) { return; } struct node* next_node = NULL; printf("[%d]", node->data); if(node->left) { q_push(head_q, (data_t)node->left); } if(node->right) { q_push(head_q, (data_t)node->right); } next_node = (struct node*)q_pop(head_q); level_order_main(next_node, head_q); }
int hadlock_algo(Pair* source, Pair* target){ int i, j, v1, v2, res; Pair p, v, step[4] = {{1,0},{0,1},{-1,0},{0,-1}}; /* initialize all cells like unvisited */ for(i=0; i<sz.r; ++i){ for(j=0; j<sz.c; ++j){ W[i][j] = -1; } } q_clear(); p_set(W,source,0); q_push(source); while(!q_empty()){ p = q_pop(); for(i=0; i<4; ++i){ v = p_sum(step+i,&p); v1 = p_get(Z,&p); v2 = p_get(Z,&v); if(p_in(&v) && (p_get(W,&v)==-1 || (p_get(W,&v)>p_get(W,&p))) && (v1-v2 <= 3) && (v1-v2 >= -1) && can_go(&v)){ if(p_mhtn_norm(&v,target) < p_mhtn_norm(&p,target)){ p_set(W,&v,p_get(W,&p)); q_push_back(&v); }else{ /* > */ /*if(!(p_get(W,&v)!=-1 || (p_get(W,&v)==p_get(W,&p)+1))){ */ p_set(W,&v,p_get(W,&p)+1); q_push_front(&v); } if(p_eq(&v,target)){ break; } } } if(p_eq(&v,target)) break; } res = p_get(W,target); if(res != -1) res = 2*res+p_mhtn_norm(source,target); return res; }
/* User calls this to get something from the front of the queue. Returns a * timestamp to indicate when the item was removed as well as the value. */ void *get_from_queue(QUEUE *queue, DWORD *time) { void *value; #ifdef SYNCHRONISE primitive_wait_for_simple_lock(&queue->lock); #endif while(q_empty(queue)) { #ifdef SYNCHRONISE primitive_wait_for_notification(&queue->notification, &queue->lock); #endif } *time = GetTickCount(); value = q_pop(queue); #ifdef SYNCHRONISE primitive_release_simple_lock(&queue->lock); #endif return(value); }
static struct entry *update_hotspot_queue(struct smq_policy *mq, dm_oblock_t b, struct bio *bio) { unsigned hi; dm_oblock_t hb = to_hblock(mq, b); struct entry *e = h_lookup(&mq->hotspot_table, hb); if (e) { stats_level_accessed(&mq->hotspot_stats, e->level); hi = get_index(&mq->hotspot_alloc, e); q_requeue(&mq->hotspot, e, test_and_set_bit(hi, mq->hotspot_hit_bits) ? 0u : mq->hotspot_level_jump); } else { stats_miss(&mq->hotspot_stats); e = alloc_entry(&mq->hotspot_alloc); if (!e) { e = q_pop(&mq->hotspot); if (e) { h_remove(&mq->hotspot_table, e); hi = get_index(&mq->hotspot_alloc, e); clear_bit(hi, mq->hotspot_hit_bits); } } if (e) { e->oblock = hb; q_push(&mq->hotspot, e); h_insert(&mq->hotspot_table, e); } } return e; }
//m,n pointers temporarily store adress of B until another B is visited int shortestDistance_2(char** maze,int N,Q* qx,Q* qy,int** depth,int** visited,char searchsymbol,int* m,int* n,int** v){ while(qx->head!=NULL){ int x = q_top(qx); int y = q_top(qy); if(x<0||x>N-1||y<0||y>N-1){ q_pop(qx); q_pop(qy); return shortestDistance_2(maze,N,qx,qy,depth,visited,searchsymbol,m,n,v); } else if(maze[x][y] == searchsymbol && v[x][y]==0){ v[x][y]=1;//mark B visited *m=x;*n=y;//store adress of coordinates of B return depth[x][y]; } else if(maze[x][y] == '#'){ q_pop(qx); q_pop(qy); return shortestDistance_2(maze,N,qx,qy,depth,visited,searchsymbol,m,n,v); } else { q_pop(qx);q_pop(qy); if(visited[x][y+1]==0){ //pushes adjacent vertices which are non visited into the queue q_push(qx,x);q_push(qy,y+1);visited[x][y+1]=1;} if(visited[x][y-1]==0){ q_push(qx,x);q_push(qy,y-1);visited[x][y-1]=1;} if(visited[x-1][y]==0){ q_push(qx,x-1);q_push(qy,y);visited[x-1][y]=1;} if(visited[x+1][y]==0){ q_push(qx,x+1);q_push(qy,y);visited[x+1][y]=1;} depth[x][y+1]=depth[x][y-1]=depth[x-1][y] =depth[x+1][y] =1+depth[x][y]; return shortestDistance_2(maze,N,qx,qy,depth,visited,searchsymbol,m,n,v); } } return -1; //if there is no path returns -1 }
//used in SD,from coordinates at qx_top qy_top to searchsymbol(bfs) int shortestDistance2(char** maze,int N,Q* qx,Q* qy,int** depth,int** visited,char searchsymbol){ while(qx->head!=NULL){ int x = q_top(qx); int y = q_top(qy); if(x<0||x>N-1||y<0||y>N-1){ q_pop(qx); q_pop(qy); return shortestDistance2(maze,N,qx,qy,depth,visited,searchsymbol); } else if(maze[x][y] == searchsymbol) return depth[x][y]; else if(maze[x][y] == '#'){ q_pop(qx); q_pop(qy); return shortestDistance2(maze,N,qx,qy,depth,visited,searchsymbol); } else { q_pop(qx);q_pop(qy); if(visited[x][y+1]==0){ //pushes adjacent vertices which are non visited into the queue q_push(qx,x);q_push(qy,y+1);visited[x][y+1]=1;} if(visited[x][y-1]==0){ q_push(qx,x);q_push(qy,y-1);visited[x][y-1]=1;} if(visited[x-1][y]==0){ q_push(qx,x-1);q_push(qy,y);visited[x-1][y]=1;} if(visited[x+1][y]==0){ q_push(qx,x+1);q_push(qy,y);visited[x+1][y]=1;} depth[x][y+1]=depth[x][y-1]=depth[x-1][y] =depth[x+1][y] =1+depth[x][y]; return shortestDistance2(maze,N,qx,qy,depth,visited,searchsymbol); } } return -1; //if there is no path returns -1 }
int main(int argc, char *argv[]) { size_t s = sizeof(int); int foo; { /*#Test_0*/ queue q; q_init(&q, s); test(q_size(&q) == 0 && q_is_empty(&q)); } { /*#Test_1*/ queue q; q_init(&q, s); foo = 1; q_push(&q, &foo); test(q_size(&q) == 1 && !q_is_empty(&q) && *(int *)q_front(&q) == 1); } { /*#Test_2*/ queue q; q_init(&q, s); foo = 2; q_push(&q, &foo); foo = 1; q_push(&q, &foo); test( q_size(&q) == 2 && *(int *)q_front(&q) == 1 && *(int *)q_back(&q) == 2); } { /*#Test_3*/ queue q; q_init(&q, s); foo = 3; q_push(&q, &foo); foo = 2; q_push(&q, &foo); foo = 1; q_push(&q, &foo); q_pop(&q); BOOL bar = *(int *)q_front(&q) == 1 && *(int *)q_back(&q) == 2; q_pop(&q); bar &= *(int *)q_front(&q) == 1 && *(int *)q_back(&q) == 1; q_pop(&q); test( bar && q_is_empty(&q)); } { /*#Test_4*/ queue q; q_init(&q, s); BOOL bar = TRUE; for (uint i = 0; i < N; ++i) { foo = i; q_push(&q, &foo); } for (uint i = 0; i < N; ++i) { bar &= *(int *)q_back(&q) == i; if (!bar) break; q_pop(&q); } test( bar& q_is_empty(&q)); } return EXIT_SUCCESS; }
//------------------------------------------------------------------------ // char * make_msg_k1(void) //------------------------------------------------------------------------ char * make_msg_k1(void) { char tmp_buf[50]; //char t_buf[15]; // 38 byte + '\0' // 0.........1.........2.........3.........4.........5.........6.........7.........8.........9......... // 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 // |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| // K0S010yymmddhhmmLat---Longi----1234123Sensor--ws-wd-at--ap---hu-2at-2ap--sc--st--dcs-dc- // #H11,150310160005,-84,11.9,0000.0000,00000.0000,,,1.348E+01 debugstring( "-------------------------------\r\n"); // debugstring( " make msg K1\r\n"); // debugstring( "-------------------------------\r\n"); memset(s_msg,0,MSG_LENGTH); // Header strcpy(s_msg, "#J"); // strcat(s_msg, env.id); // strcat(s_msg, "env.id"); // date time rtc_isSecUpdate(); sprintf(tmp_buf,"11,%02d%02d%02d%02d%02d00,",rtc_time.year%100,rtc_time.mon,rtc_time.day,rtc_time.hour,rtc_time.min); strcat(s_msg, tmp_buf); // debugprintf("%s\r\n", s_msg); // CDMA RSSI sprintf(tmp_buf,"%d,",lblRSSI); strcat(s_msg, tmp_buf); // BAT sprintf(tmp_buf,"%.1f,", (float)(get_battery_level()/10.0f)); strcat(s_msg, tmp_buf); // debugprintf("%s \r\n", s_msg); // Position strcat(s_msg, "0000.0000,00000.0000,,,"); // debugprintf("%s \r\n", s_msg); if (is_dcs_valid()==1) { // sprintf(tmp_buf,"%.3f",get_dcs_speed()); // dcs.pitch == temperature sprintf(tmp_buf,"%.3E",get_dcs_speed()); // dcs.pitch == temperature strcat(s_msg, tmp_buf); // (-)150518 // strcat(s_msg, get_4050_temp()); // (+)150518 set_dcs_valid(0); init_dcs_data(); } // debugprintf("%s \r\n", s_msg); // PRINTVAR(strlen(s_msg)); sensor_status.w = DFLT_SENSOR; // sensor_status.w = 0xFFFFFFFF; // 5 5 5 5 5 5 5 5 // 0101 0101 0101 0101 0101 0101 0101 0101 // 0111 1101 0101 0101 0111 0111 0111 0101 // 7 D 5 5 7 7 7 5 // 7D557775 : DEFAULT VALUE // F7 77 55 D5 // (-)131017 --->>> if (is_q_full()) { q_pop(); } q_putData(); // <<<------ // (+)131017 ---->>> // fgMsgC_ready = 1; // { // int i; // for (i=0;i<MSG_LENGTH; i++) // { // s_msg_c[i] = s_msg[i]; // } // } // <<<------ //saveDeferredMsgFile(); debugstring(s_msg); // debugstring(s_msg_c); debugstring("\r\n"); debugprintf("q_put --> Q[%d]\r\n",is_q_dataNum()); strcat(s_msg, "\r\n"); if (sdc_read_detectPin()==SDC_INSERTED) { u32 fsz; //PRINTLINE; sdc_saveDataToFile(FN_SEND, s_msg, &fsz); //PRINTVAR(fsz); if (fsz > FSZ_MAX) { SensorBakSize.b.send = 1; } } return (s_msg); }