void slist_insert(SList *plist, unsigned int ipos, char *pdata, size_t isize) { ipos = ipos > plist->icount ? plist->icount : ipos; if (ipos == plist->icount) slist_push(plist, pdata, isize); else { ListNode *pnew = slistnode_create(pdata, isize); ListNode *pbefore = plist->pfirst; for (int i = 0; i < ipos-1; i++) pbefore = pbefore->pnext; ListNode *pafter = pbefore->pnext; pbefore->pnext = pnew; pnew->pnext = pafter; plist->icount++; } }
int main(int argc, char *argv[]) { struct slist *list; list = slist_new(); assert(list); assert(0 == slist_size(list)); assert(list == slist_idx(list, 0)); assert(NULL == slist_idx(list, -1)); assert(NULL == slist_idx(list, 1)); /* Build an array and set canary values */ struct slist *nodes1[NODECNT]; for (int i = 0; i < NODECNT; i++) { nodes1[i] = malloc(sizeof(struct slist)); nodes1[i]->s = malloc(sizeof(struct sample)); nodes1[i]->s->rx_bytes = i; } /* Then add array elements to a list */ for (int i = 0; i < NODECNT; i++) { slist_push(list, nodes1[i]); assert(1 + i == slist_size(list)); } /* check slist_size */ assert(NODECNT == slist_size(list)); /* check the list_idx function */ for (int i = 0; i < NODECNT; i++) { struct slist *n = slist_idx(list, i); assert(n); assert(n->s->rx_bytes == i); } /* check slist_clear */ slist_clear(list); assert(0 == slist_size(list)); }
static int frames_to_sample_list() { int new_samples = 0; pthread_mutex_lock(&unsent_frame_count_mutex); while (g_unsent_frame_count > 0) { for (int i = 0; i < SAMPLES_PER_FRAME; i++) { struct sample *s = malloc(sizeof(struct sample)); assert(s); memcpy(s, &(g_raw_samples->samples[i]), sizeof(struct sample)); struct slist *ln = malloc(sizeof(struct slist)); assert(ln); ln->s = s; slist_push(sample_list, ln); g_sample_count++; new_samples++; } g_unsent_frame_count--; } pthread_mutex_unlock(&unsent_frame_count_mutex); int overflow = (slist_size(sample_list) > MAX_LIST_LEN) ? slist_size(sample_list) - MAX_LIST_LEN : 0; while (overflow--) { struct slist *ln = slist_pop(sample_list); assert(ln); assert(ln->s); free(ln->s); free(ln); } if (g_sample_count > MAX_LIST_LEN) { g_sample_count -= MAX_LIST_LEN; } return new_samples; }
// --------------------- Evaluation of the final Position --------------------- void final_status_list(Position *pos, Status block_status[MAX_BLOCKS], Status point_status[BOARDSIZE], char *str_st, Point points[BOARDSIZE]) // Compute the status of all points (evaluation from owner_map[]) { Block b; Status st; for (char *p=str_st ; *p != 0 ; p++) *p = tolower(*p); if (strcmp(str_st, "dead") == 0) st = DEAD; else if (strcmp(str_st, "alive") == 0) st = ALIVE; else st = UNKNOWN; slist_clear(points); FORALL_POINTS(pos, pt) { b = point_block(pos, pt); if (b != 0 && block_status[b] == st) slist_push(points, pt); }
char* do_play(Game *game, Color c, Point pt) // Play the move (updating the Game struct) { char *ret; Color to_play_before; int played=0; Info m = 0; Position *pos = game->pos; to_play_before = board_color_to_play(pos); board_set_color_to_play(pos, c); if (point_color(pos,pt) == EMPTY) { ret = play_move(pos, pt); if (ret[0] == 0) { m = pt + (board_captured_neighbors(pos) << 9) + (board_ko_old(pos) << 13) + ((to_play_before) << 22); played = 1; } // else illegal move: nothing played } else if(pt == PASS_MOVE) { ret = pass_move(pos); m = pt + (board_captured_neighbors(pos) << 9) + (board_ko_old(pos) << 13) + ((to_play_before) << 22); played = 1; } else ret ="Error Illegal move: point not EMPTY\n"; if (played) { c2++; slist_push(game->moves, m); } return ret; }
/* * in - Source file to be interpreted * file_name - Name of the source file, for debugging purposes * sym_table - Global (master) symbol table */ void liten(FILE * in, char * file_name, slist * sym_table, token ** ret) { // *ret = NULL; slist * block_stack = slist_init(); slist * local_table = slist_init(); slist_push(sym_table, local_table); file_desc * desc = malloc(sizeof(file_desc)); desc->file = in; desc->name = file_name; desc->line = 1; int tok; char * temp = malloc(sizeof(char)*MAX_STR); strcpy(temp, "\0"); int type = -1; slist * tok_queue = slist_init(); while (1) { if (in == stdin) { printf(":> "); } else if (tok == EOF) { break; } tok = getc(in); while (1) { if (tok == '\n' || tok == EOF) { desc->line++; tokenize(desc, &temp, &type, tok_queue); type = -1; // slist_print(tok_queue, slist_print_token); // TODO: Check line for syntax errors if (tok_queue->len == 0) break; if (block_stack->len == 0) { rpn(desc, tok_queue, sym_table, block_stack, ret); } else { char * temp_str = ((token *) slist_get(tok_queue, 0)->obj)->obj; block * temp = (block *) slist_get(block_stack, -1)->obj; if (strcmp("end", temp_str) == 0) { rpn(desc, tok_queue, sym_table, block_stack, ret); } else if (strcmp("if", temp_str) == 0 || strcmp("while", temp_str) == 0) { if (temp->value == 1) rpn(desc, tok_queue, sym_table, block_stack, ret); else { block * temp1 = malloc(sizeof(block)); temp1->value = 0; if (strcmp("if", temp_str) == 0) { temp1->type = IF_B; } else if (strcmp("while", temp_str) == 0) { temp1->type = WHILE_B; } slist_push(block_stack, temp1); } } else if (strcmp("elif", temp_str) == 0) { if (temp->type != IF_B) lite_error(desc, "'elif' exists in 'if' block"); if (temp->value == 1) temp->value = 0; else rpn(desc, tok_queue, sym_table, block_stack, ret); } else if (strcmp("else", temp_str) == 0) { if (temp->type != IF_B) lite_error(desc, "'else' exists in 'if' block"); if (temp->value == 1) temp->value = 0; else temp->value = 1; } else { if (temp->value == 1) rpn(desc, tok_queue, sym_table, block_stack, ret); } } tok_queue = slist_init(); } else { token_parse(desc, tok, &temp, &type, tok_queue); } if (in == stdin && tok == '\n') break; tok = getc(in); } } return; }
void PropValue(void) // PropValue = "[" ValueType "]" { yyaccept('['); ValueType(); // Property Value was read : use it to modify the game int size = board_size(game->pos); if (board_nmoves(game->pos) < nmoves-1 && strcmp(prop_name[prop], "B") == 0) { if (yytext[0] != 'B') do_play(game, BLACK, parse_sgf_coord(yytext, size)); else do_play(game, BLACK, PASS_MOVE); } else if (board_nmoves(game->pos) < nmoves-1 && strcmp(prop_name[prop], "W") == 0) { if (yytext[0] != 'W') do_play(game, WHITE, parse_sgf_coord(yytext, size)); else do_play(game, WHITE, PASS_MOVE); } else if (strcmp(prop_name[prop], "AB") == 0) { Point pt = parse_sgf_coord(yytext, size); slist_push(game->placed_black_stones, pt); board_place_stone(game->pos, pt, BLACK); } else if (strcmp(prop_name[prop], "AW") == 0) { Point pt = parse_sgf_coord(yytext, size); slist_push(game->placed_white_stones, pt); board_place_stone(game->pos, pt, WHITE); } else if (strcmp(prop_name[prop], "KM") == 0) board_set_komi(game->pos, atof(yytext)); else if (strcmp(prop_name[prop], "SZ") == 0) { if (is_game_board_empty(game)) { board_set_size(game->pos, atoi(yytext)); game_clear_board(game); } else { // Can happen if SZ occurs after AB or AW Point bstones[BOARDSIZE], wstones[BOARDSIZE]; Position *pos=game->pos; slist_clear(bstones); slist_clear(wstones); slist_append(bstones, game->placed_black_stones); slist_append(wstones, game->placed_white_stones); board_set_size(game->pos, atoi(yytext)); game_clear_board(game); FORALL_IN_SLIST(bstones, pt) { board_set_color_to_play(pos, BLACK); play_move(pos, pt); slist_push(game->placed_black_stones, pt); } FORALL_IN_SLIST(wstones, pt) { board_set_color_to_play(pos, WHITE); play_move(pos, pt); slist_push(game->placed_white_stones, pt); } }