static void remember_previous_move_as_undecidable(void) { append_to_table(exclusive_chess_undecidable_continuations[parent_ply[nbply]]); TraceValue("%u",nbply); TraceValue("%u",parent_ply[nbply]); TraceValue("%u\n",table_length(exclusive_chess_undecidable_continuations[parent_ply[nbply]])); }
/* Try to solve in solve_nr_remaining half-moves. * @param si slice index * @note assigns solve_result the length of solution found and written, i.e.: * previous_move_is_illegal the move just played is illegal * this_move_is_illegal the move being played is illegal * immobility_on_next_move the moves just played led to an * unintended immobility on the next move * <=n+1 length of shortest solution found (n+1 only if in next * branch) * n+2 no solution found in this branch * n+3 no solution found in next branch * (with n denominating solve_nr_remaining) */ void exclusive_chess_legality_tester_solve(slice_index si) { TraceFunctionEntry(__func__); TraceFunctionParam("%u",si); TraceFunctionParamListEnd(); if ((table_length(exclusive_chess_undecidable_continuations[parent_ply[nbply]]) +exclusive_chess_nr_continuations_reaching_goal[parent_ply[nbply]]) >1) { if (is_current_move_in_table(exclusive_chess_undecidable_continuations[parent_ply[nbply]])) solve_result = this_move_is_illegal; else switch (conditional_pipe_solve_delegate(temporary_hack_mate_tester[advers(trait[nbply])])) { case this_move_is_illegal: solve_result = this_move_is_illegal; break; case previous_move_has_not_solved: pipe_solve_delegate(si); break; default: solve_result = previous_move_has_solved; break; } } else pipe_solve_delegate(si); TraceFunctionExit(__func__); TraceFunctionResultEnd(); }
void ws(char *name, FILE *fp) { table_t table ; char buf[128]; char *word; int *count; void **ar; int i; table = NULL; word = NULL; ar = NULL; memset(buf, 0, sizeof(buf)); table = table_new(4096, cmpstring, hashcode); while(getword(fp, buf, sizeof(buf))) { word = (char *) mem_calloc(1, strlen(buf) + 1); assert(word); strcpy(word, buf); count = table_get(table, word); if(count) { (*count)++; }else { count = (int *) mem_calloc(1, sizeof(*count)); assert(count); *count = 1; table_put(table, word, count); } }/*while*/ if(name) printf("%s:\n",name); printf("table has keys:%d\n", table_length(table)); ar = table_to_array(table, NULL); qsort(ar, table_length(table), 2 * sizeof(*ar), cmp); for(i = 0; ar[i]; i += 2) { printf("%d\t%s\n", *(int *)ar[i+1], (char *)ar[i]); } }
/* Try to solve in solve_nr_remaining half-moves. * @param si slice index * @note assigns solve_result the length of solution found and written, i.e.: * previous_move_is_illegal the move just played is illegal * this_move_is_illegal the move being played is illegal * immobility_on_next_move the moves just played led to an * unintended immobility on the next move * <=n+1 length of shortest solution found (n+1 only if in next * branch) * n+2 no solution found in this branch * n+3 no solution found in next branch * (with n denominating solve_nr_remaining) */ void exclusive_chess_nested_exclusivity_detector_solve(slice_index si) { TraceFunctionEntry(__func__); TraceFunctionParam("%u",si); TraceFunctionParamListEnd(); assert(ply_horizon<maxply); if (nbply>ply_horizon) { TraceText("stopping recursion"); remember_previous_move_as_undecidable(); solve_result = previous_move_is_illegal; } else { ply const save_ply_horizon = ply_horizon; exclusive_chess_undecidable_continuations[nbply] = allocate_table(); detect_exclusivity_and_solve_accordingly(si); TraceValue("%u",nbply); TraceValue("%u",exclusive_chess_nr_continuations_reaching_goal[nbply]); TraceValue("%u",nr_decidable_continuations_not_reaching_goal[nbply]); TraceValue("%u\n",table_length(exclusive_chess_undecidable_continuations[nbply])); if (nr_decidable_continuations_not_reaching_goal[nbply]==0 && exclusive_chess_nr_continuations_reaching_goal[nbply]<=1 && table_length(exclusive_chess_undecidable_continuations[nbply])+exclusive_chess_nr_continuations_reaching_goal[nbply]>1) remember_previous_move_as_undecidable(); free_table(exclusive_chess_undecidable_continuations[nbply]); ply_horizon = save_ply_horizon; } TraceFunctionExit(__func__); TraceFunctionResultEnd(); }
/* Try to solve in solve_nr_remaining half-moves. * @param si slice index * @note assigns solve_result the length of solution found and written, i.e.: * previous_move_is_illegal the move just played is illegal * this_move_is_illegal the move being played is illegal * immobility_on_next_move the moves just played led to an * unintended immobility on the next move * <=n+1 length of shortest solution found (n+1 only if in next * branch) * n+2 no solution found in this branch * n+3 no solution found in next branch * (with n denominating solve_nr_remaining) */ void exclusive_chess_goal_reaching_move_counter_solve(slice_index si) { unsigned int const nr_undecidable_before = table_length(exclusive_chess_undecidable_continuations[parent_ply[nbply]]); TraceFunctionEntry(__func__); TraceFunctionParam("%u",si); TraceFunctionParamListEnd(); pipe_solve_delegate(si); if (table_length(exclusive_chess_undecidable_continuations[parent_ply[nbply]])==nr_undecidable_before) { if (solve_result==MOVE_HAS_SOLVED_LENGTH()) { ++exclusive_chess_nr_continuations_reaching_goal[parent_ply[nbply]]; TraceValue("%u",nbply); TraceValue("%u",parent_ply[nbply]); TraceValue("%u\n",exclusive_chess_nr_continuations_reaching_goal[parent_ply[nbply]]); if (exclusive_chess_nr_continuations_reaching_goal[parent_ply[nbply]]==1) /* look for one more */ solve_result = MOVE_HAS_NOT_SOLVED_LENGTH(); } else if (solve_result==MOVE_HAS_NOT_SOLVED_LENGTH()) { ++nr_decidable_continuations_not_reaching_goal[parent_ply[nbply]]; TraceText("remembering defined continuation"); TraceValue("%u",nbply); TraceValue("%u",parent_ply[nbply]); TraceValue("%u\n",nr_decidable_continuations_not_reaching_goal[parent_ply[nbply]]); } } else if (solve_result==MOVE_HAS_SOLVED_LENGTH()) solve_result = MOVE_HAS_NOT_SOLVED_LENGTH(); TraceFunctionExit(__func__); TraceFunctionResultEnd(); }
/* Try to solve in solve_nr_remaining half-moves. * @param si slice index * @note assigns solve_result the length of solution found and written, i.e.: * previous_move_is_illegal the move just played is illegal * this_move_is_illegal the move being played is illegal * immobility_on_next_move the moves just played led to an * unintended immobility on the next move * <=n+1 length of shortest solution found (n+1 only if in next * branch) * n+2 no solution found in this branch * n+3 no solution found in next branch * (with n denominating solve_nr_remaining) */ void threat_enforcer_solve(slice_index si) { ply const threats_ply = parent_ply[nbply]; stip_length_type const len_threat = threat_lengths[threats_ply]; TraceFunctionEntry(__func__); TraceFunctionParam("%u",si); TraceFunctionParamListEnd(); TraceValue("%u",len_threat); TraceEOL(); if (len_threat<=slack_length) /* the move has something stronger than threats (typically, it * delivers check) */ pipe_solve_delegate(si); else if (len_threat<=MOVE_HAS_SOLVED_LENGTH()) { /* there are >=1 threats - don't report variations shorter than * the threats or variations that don't refute any threat */ table const threats_table = threats[threats_ply]; stip_length_type len_test_threats; nr_threats_to_be_confirmed = table_length(threats_table); len_test_threats = testing_pipe_solve_delegate(si,len_threat); if (len_test_threats>len_threat) /* variation is longer than threat */ pipe_solve_delegate(si); else if (len_test_threats>len_threat-2 && nr_threats_to_be_confirmed>0) /* variation has same length as the threat(s), but it has * defeated at least one threat */ pipe_solve_delegate(si); else /* variation is shorter than threat */ solve_result = len_test_threats; } else /* zugzwang, or we haven't looked for threats yet */ pipe_solve_delegate(si); TraceFunctionExit(__func__); TraceFunctionResultEnd(); }
/* Try to solve in solve_nr_remaining half-moves. * @param si slice index * @note assigns solve_result the length of solution found and written, i.e.: * previous_move_is_illegal the move just played is illegal * this_move_is_illegal the move being played is illegal * immobility_on_next_move the moves just played led to an * unintended immobility on the next move * <=n+1 length of shortest solution found (n+1 only if in next * branch) * n+2 no solution found in this branch * n+3 no solution found in next branch * (with n denominating solve_nr_remaining) */ void output_plaintext_tree_try_writer_solve(slice_index si) { TraceFunctionEntry(__func__); TraceFunctionParam("%u",si); TraceFunctionParamListEnd(); assert(refutations!=table_nil); if (table_length(refutations)>0) { protocol_fprintf(stdout,"%s"," ?"); pipe_solve_delegate(si); } else output_plaintext_tree_key_writer_solve(si); TraceFunctionExit(__func__); TraceFunctionResultEnd(); }
/* Try to solve in solve_nr_remaining half-moves. * @param si slice index * @note assigns solve_result the length of solution found and written, i.e.: * previous_move_is_illegal the move just played is illegal * this_move_is_illegal the move being played is illegal * immobility_on_next_move the moves just played led to an * unintended immobility on the next move * <=n+1 length of shortest solution found (n+1 only if in next * branch) * n+2 no solution found in this branch * n+3 no solution found in next branch * (with n denominating solve_nr_remaining) */ void exclusive_chess_undecidable_writer_tree_solve(slice_index si) { TraceFunctionEntry(__func__); TraceFunctionParam("%u",si); TraceFunctionParamListEnd(); if (is_current_move_in_table(exclusive_chess_undecidable_continuations[parent_ply[nbply]])) SignalExclusiveRefutedUndecidable(); else { pipe_solve_delegate(si); if (solve_result==previous_move_has_solved && exclusive_chess_nr_continuations_reaching_goal[parent_ply[nbply]]<2 && table_length(exclusive_chess_undecidable_continuations[parent_ply[nbply]])+exclusive_chess_nr_continuations_reaching_goal[parent_ply[nbply]]>1) SignalChecklessUndecidable(); } TraceFunctionExit(__func__); TraceFunctionResultEnd(); }
/* Detect exclusivity and solve accordingly * @param si slice index * @param n maximum number of half moves * @return see exclusive_chess_exclusivity_detector_solve * @note The caller must have initialized ply_horizon for recursive testing for * exclusivity and is responsible of allocating * exclusive_chess_undecidable_continuations[nbply] before and deallocating it * after the invokation. */ static void detect_exclusivity_and_solve_accordingly(slice_index si) { TraceFunctionEntry(__func__); TraceFunctionParam("%u",si); TraceFunctionParamListEnd(); exclusive_chess_nr_continuations_reaching_goal[nbply] = 0; nr_decidable_continuations_not_reaching_goal[nbply] = 0; conditional_pipe_solve_delegate(temporary_hack_exclusive_mating_move_counter[SLICE_STARTER(si)]); TraceValue("%u",nbply); TraceValue("%u",nr_decidable_continuations_not_reaching_goal[nbply]); TraceValue("%u",exclusive_chess_nr_continuations_reaching_goal[nbply]); TraceValue("%u\n",table_length(exclusive_chess_undecidable_continuations[nbply])); ply_horizon = maxply; pipe_solve_delegate(si); TraceFunctionExit(__func__); TraceFunctionResultEnd(); }
void wf(const char *name, FILE *fp) { table_t table = table_new(0, NULL, NULL); char buf[BUFSIZ]; while (getword(fp, buf, sizeof(buf), first, rest)) { const char *word; int i, *count; for (i=0; buf[i] != '\0'; i++) buf[i] = tolower(buf[i]); word = atom_string(buf); count = table_get(table, word); if (count) { (*count) ++; } else { count = (int *)zalloc(sizeof (*count)); *count = 1; table_put(table, word, count); } } if (name) { printf("%s:\n", name); } /* print the words */ int i; void **array = table_to_array(table, NULL); qsort(array, table_length(table), 2*sizeof(*array), cmp); for (i = 0; array[i]; i+=2) { printf("%d\t%s\n", *(int *)array[i+1], (char *)array[i]); } zfree(array); /* destroy the table */ table_free(&table, vfree); }
int main() { table_t tab = table_new(1024, cmp, dictGenHashFunction); table_put(tab, "append", "cmd_append"); table_put(tab, "bitcount", "cmd_bitcount"); table_put(tab, "brpop", "cmd_brpop"); table_put(tab, "brpoplpush", "cmd_brpoplpush"); table_put(tab, "decr", "cmd_decr"); table_put(tab, "decrby", "cmd_decrby"); table_put(tab, "del", "cmd_del"); table_put(tab, "exists", "cmd_exists"); table_put(tab, "get", "cmd_get"); table_put(tab, "getbit", "cmd_getbit"); table_put(tab, "getrange", "cmd_getrange"); table_put(tab, "incr", "cmd_incr"); table_put(tab, "incrby", "cmd_incrby"); table_put(tab, "keys", "cmd_keys"); table_put(tab, "lindex", "cmd_lindex"); table_put(tab, "linsert", "cmd_linsert"); table_put(tab, "llen", "cmd_llen"); table_put(tab, "lpop", "cmd_lpop"); table_put(tab, "lpush", "cmd_lpush"); table_put(tab, "lpushx", "cmd_lpushx"); table_put(tab, "lrange", "cmd_lrange"); table_put(tab, "lrem", "cmd_lrem"); table_put(tab, "lset", "cmd_lset"); table_put(tab, "ltrim", "cmd_ltrim"); table_put(tab, "mget", "cmd_mget"); table_put(tab, "msetnx", "cmd_msetnx"); table_put(tab, "randomkey", "cmd_randomkey"); table_put(tab, "rename", "cmd_rename"); table_put(tab, "rpop", "cmd_rpop"); table_put(tab, "rpoplpush", "cmd_rpoplpush"); table_put(tab, "rpush", "cmd_rpush"); table_put(tab, "rpushx", "cmd_rpushx"); table_put(tab, "set", "cmd_set"); table_put(tab, "setbit", "cmd_setbit"); table_put(tab, "setrange", "cmd_setrange"); table_put(tab, "strlen", "cmd_strlen"); table_put(tab, "type", "cmd_type"); table_map(tab, apply); printf("len = %d\n", table_length(tab)); char key[32]; scanf("%s", key); printf("%s\n", (char *)table_get(tab, key)); printf("remove set\n"); table_remove(tab, "set"); table_map(tab, apply); printf("len = %d\n", table_length(tab)); scanf("%s", key); printf("%s\n", (char *)table_get(tab, key)); table_free(&tab); return 0; }