static int div_one(UserNode *punode, short div, int distance) { UserNode *tmp; DivParam dp; tmp = punode; dp.pList = init_queue(); if(dp.pList == NULL) return -1; dp.div = div; dp.distance = distance; dp.flag = 1; //"to" punode->div = -dp.div; apply_queue(punode->to, add_node, &dp); while(!is_empty(dp.pList)) { punode = read_first(dp.pList); apply_queue(punode->to, add_node, &dp); delete_node(dp.pList, 0, 0); } punode = tmp; dp.flag = 0; //"from" punode->div = dp.div; apply_queue(punode->from, add_node, &dp); while(!is_empty(dp.pList)) { punode = read_first(dp.pList); apply_queue(punode->from, add_node, &dp); delete_node(dp.pList, 0, 0); } free_queue(dp.pList, 1); return 0; }
//产生一个随机地图, rate决定地图的渲染率. 地图大小应在mcMap中设定 int gen_map(McMap * mcMap, double rate) { int i, j, retv = 0, count = 0; int prev = 0, trans = 0, next = 0; double sum; pLinkList pList; pQueuePoint pqp; pList = init_queue(); if (pList == NULL) return -1; srandom(time(NULL)); //随机选择一个渲染点 i = random() % (mcMap->row - 2) + 1; j = random() % (mcMap->col - 2) + 1; sum = mcMap->row * mcMap->col + 0.1; while (count / sum < rate) { //保证一定的渲染率 pqp = calloc(1, sizeof (QueuePoint)); if (pqp == NULL) { retv = -1; goto END; } pqp->i = i; pqp->j = j; insert_node(pList, pqp); //加入渲染队列 mcMap->map[i][j].exit |= NODE_QUEUED; while (!is_empty(pList)) { pqp = read_first(pList); //取队列首点 //产生该点的随机出口 gen_exit(mcMap, pqp->i, pqp->j, count / (rate * sum), &prev, &trans, &next); //产生该点的东东, ncp, box, boss... gen_item(mcMap, pqp->i, pqp->j); //加入该点的出口点 if (check_exit_node(pList, mcMap, pqp->i, pqp->j, 0) || check_exit_node(pList, mcMap, pqp->i, pqp->j, 1) || check_exit_node(pList, mcMap, pqp->i, pqp->j, 2) || check_exit_node(pList, mcMap, pqp->i, pqp->j, 3)) { retv = -1; goto END; } delete_node(pList, 0, 1); //从队列中删除该点 if ((++count) / sum > rate) //渲染率达到就跳出 goto END; } //end of while(...) random_select(mcMap, &i, &j); //如果一个点不够渲染率, 则再选择一个 } END: free_queue(pList, 1); //释放队列资源 return retv; }
blargg_err_t Remaining_Reader::read( void* out, long count ) { count = max( 0l, count ); long first = read_first( out, count ); long second = max( 0l, count - first ); if ( !second ) return 0; return in->read( (char*) out + first, second ); }
long Remaining_Reader::read_avail( void* out, long count ) { count = max( 0l, count ); long first = read_first( out, count ); long second = max( 0l, count - first ); if ( second ) { second = in->read_avail( (char*) out + first, second ); if ( second <= 0 ) return second; } return first + second; }
static void test_read_first_next() { char teststr[] = "test1 test2\n\rtest3\t(test4+test5),test6;-test7"; char* token = read_first(teststr, " \n\r\t,;", "()+-/*"); assert(!strcmp(token, "test1")); token = read_next(); assert(!strcmp(token, "test2")); token = read_next(); assert(!strcmp(token, "test3")); token = read_next(); assert(!strcmp(token, "(")); token = read_next(); assert(!strcmp(token, "test4")); token = read_next(); assert(!strcmp(token, "+")); token = read_next(); assert(!strcmp(token, "test5")); token = read_next(); assert(!strcmp(token, ")")); token = read_next(); assert(!strcmp(token, "test6")); token = read_next(); assert(!strcmp(token, "-")); token = read_next(); assert(!strcmp(token, "test7")); token = read_next(); assert(token == 0); fprintf(stdout, "%s:\tpassed\n", __FUNCTION__); }
int main () { char line[40], *str, cmd, last_cmd = 0; if (!(ld = open_list ())) { fprintf (stderr, "Error opening List\n"); exit (1); } printf ("Type \'-?\' or \'-h\' for Help contents\n"); while (1) { printf ("list> "); fgets (line, 40, stdin); for (str = line; *str == ' ' || *str == '\t'; str++) ; str[strchr (str, '\n') - str] = '\0'; switch (*str) { case '\0' : continue; default : append (ld, str); break; case '.' : cmd = last_cmd; case '-' : if (*str != '.') cmd = last_cmd = *++str; switch (cmd) { default : fprintf (stderr, "Invalid Command\n"); continue; case '?' : case 'h' : case 'H' : printf ("Type any name to add to list\n" "-r<name> to remove from list\n" "\'-d\' to display all list items\n" "\'-f\' to print the first item in list\n" "\'-l\' to print the last item in list\n" "\'-n\' to print the next list item\n" "\'-p\' to print the previous list item\n" "\'.\' to repeat last command\n" //"\'-s\' to sort list items\n" "\'-e\' to exit\n"); break; case 'd' : case 'D' : print_list (ld); break; case 'f' : case 'F' : if (read_first (ld, str)) PRINT_MSG case 'l' : case 'L' : if (read_last (ld, str)) PRINT_MSG case 'n' : case 'N' : if (read_next (ld, str)) PRINT_MSG case 'p' : case 'P' : if (read_prev (ld, str)) PRINT_MSG case 'r' : case 'R' : if (delete (ld, ++str)) { fprintf (stderr, "%s not on list\n", str); continue; } else printf ("%s removed\n", str); break; case 'e' : case 'E' : print_list (ld); close_list (ld); printf ("===> End of App <===\n"); exit (0); } } } }