/* * This is called at the beginning of a cubist run to clear out * all "files" generated on the previous run and to prepare it * for the next run. */ void rbm_removeall() { /* Check if there actually is anything to remove */ if (strbufv != NULL) { /* * Destroy all STRBUF's in the hash table. * Note that this loop leaves the hash table full of * pointers to deallocated STRBUF's until ht_destroy * is called below. */ ht_reset(strbufv); /* just in case */ while (1) { void *e = ht_next(strbufv); if (e == NULL) break; strbuf_destroy((STRBUF *) ht_value(e)); } /* Destroy the hash table itself */ ht_destroy(strbufv); } /* Create/recreate the hash table for subsequent use */ strbufv = ht_new(HASH_LEN); }
static bool ht_entry_prevents_null_move(struct node *node, ht_entry entry) { if (!ht_is_set(entry) || ht_depth(entry) < node->depth - 2 * PLY) return false; if (ht_no_null(entry)) return true; if (ht_value_is_upper_bound(entry)) { if (ht_value(entry) <= node->beta) return true; } return false; }
static int check_hash_value_upper_bound(struct node *node, ht_entry entry) { int hash_bound = ht_value(entry); if (hash_bound < 0 && node->has_repetition_in_history) hash_bound = 0; if (node->upper_bound > hash_bound) { node->upper_bound = hash_bound; if (node->upper_bound <= node->alpha) { node->value = node->upper_bound; return hash_cutoff; } } return 0; }
static int check_hash_value_lower_bound(struct node *node, ht_entry entry) { int hash_bound = ht_value(entry); if (hash_bound > 0 && node->has_repetition_in_history) hash_bound = 0; if (node->lower_bound < hash_bound) { node->lower_bound = hash_bound; if (node->lower_bound >= node->beta) { node->best_move = ht_move(entry); node->value = node->lower_bound; return hash_cutoff; } } return 0; }
static int check_hash_value(struct node *node, ht_entry entry) { if (node->root_distance == 0) return 0; if (node[-1].forced_pv != 0 && node->alpha == -max_value && node->beta == max_value) return 0; if (node->depth > ht_depth(entry)) { if (ht_value_is_lower_bound(entry)) { if (ht_value(entry) >= mate_value) { if (!node->has_repetition_in_history) { node->value = ht_value(entry); node->best_move = ht_move(entry); return hash_cutoff; } else if (node->lower_bound < 0) { node->lower_bound = 0; if (0 >= node->beta) { node->value = node->beta; return hash_cutoff; } } } } if (ht_value_is_upper_bound(entry)) { if (ht_value(entry) <= -mate_value) { if (!node->has_repetition_in_history) { node->value = ht_value(entry); return hash_cutoff; } else if (node->upper_bound > 0) { node->upper_bound = 0; if (node->alpha >= 0) { node->value = node->alpha; return hash_cutoff; } } } } return 0; } if (ht_value_is_lower_bound(entry)) if (check_hash_value_lower_bound(node, entry) == hash_cutoff) return hash_cutoff; if (ht_value_is_upper_bound(entry)) if (check_hash_value_upper_bound(node, entry) == hash_cutoff) return hash_cutoff; if (node->lower_bound >= node->upper_bound) { node->value = node->lower_bound; return hash_cutoff; } return 0; }
/** * Processes the given command line according to the configuration of the optin object * * o - The optin object * argc - Pointer to the argument count, should include the program name * argv - Pointer to the arguments, *argv[0] should be the program name * * On exit, argc and argv will be modified to be the arguments left over after option processing * RETURNS: zero if options were parsed successfully, nonzero if there was an error */ int optin_process(optin* o, int* argc, char** argv) { int i, ret; int is_long_option; int next_argv; /* Used to keep track of the next valid argv slot for shuffling non-option args */ char* arg, *opt, *value; _option* option; _option_wrapper* wrapper; hashtable_iter* opt_iter; enum { STATE_NORMAL, STATE_IN_OPTION} state; is_long_option = 0; state = STATE_NORMAL; o->argc = *argc; o->argv = argv; next_argv = 1; ret = 0; i = 1; /* Skip over the program name */ while (i < o->argc) { arg = o->argv[i]; switch(state) { case STATE_NORMAL: if (*arg == '-') { state = STATE_IN_OPTION; opt = arg+1; continue; } argv[next_argv++] = o->argv[i]; break; case STATE_IN_OPTION: (*argc)--; is_long_option = (*opt == '-'); value = 0; if (is_long_option) { opt++; if (*opt == '\0') { /* We have a lone --, that means to stop arg processing NOW */ i++; goto done; } /* We need to check if there's an equal sign in the longopt somewhere */ value = opt; while (*value) { if (*value == '=' && is_long_option) { *value++ = '\0'; /* Modify string so opt ends at where equals sign was */ break; } else if (*value == '=') { fprintf(stderr, "Equals sign not allowed in short option\n"); ret = OPTIN_ERR_INVALID_OPTION; goto done; } value++; } if (!*value) { value = 0; } } if (*opt != '\0') { /* TODO: What do we do if it does? */ option = _query(o->options, opt); if (!option) { fprintf(stderr, "Unrecognized option: '%s'\n", opt); ret = OPTIN_ERR_INVALID_OPTION; goto done; } else if (value && !option->accepts_value) { fprintf(stderr, "Option '%s' does not take a value\n", opt); ret = OPTIN_ERR_INVALID_VALUE; goto done; } if (option->accepts_value && !value) { /* Let's see if our next arg can function as a value */ if ((i + 1) == o->argc || *(o->argv[i+1]) == '-') { fprintf(stderr, "Option '%s' requires a value\n", opt); ret = OPTIN_ERR_VALUE_MISSING; goto done; } else { (*argc)--; } value = o->argv[i+1]; } ret = optin_process_option(o, opt, value); if (ret != 0) { goto done; } } state = STATE_NORMAL; break; } if (value && value == o->argv[i+1]) { /* Need to skip over the value we used */ i += 2; } else { i++; } } done: /* Analyze required options */ opt_iter = ht_iter_begin(o->options); while (opt_iter) { wrapper = ht_value(opt_iter); /* Only consider the real options, ignore aliases so we don't check the same option twice */ if (wrapper && !wrapper->alias) { option = wrapper->o; if (option && option->has_default == OPTIN_REQUIRED && !option->set) { fprintf(stderr, "Missing required option '%s'\n", option->name); ret = OPTIN_ERR_OPTION_MISSING; break; } } opt_iter = ht_iter_next(opt_iter); } /* Reorder any args after the options in the caller's argv array */ for (; i < o->argc; i++) { argv[next_argv++] = o->argv[i]; } return ret; }