static dpll_variable new_dpll_variable() { dpll_variable var = (dpll_variable)malloc(sizeof(struct str_dpll_variable)); if (NULL == var) { return var; } var->assignment = ASSIGNMENT_UNKNOWN; var->pos_clauses = new_int_list(); var->neg_clauses = new_int_list(); setroom_int_list(var->pos_clauses, 512); setroom_int_list(var->neg_clauses, 512); var->pos_filter_clause = NULL; var->neg_filter_clause = NULL; var->delete_clause = NULL; return var; }
value new_large_lattice_value (int size, int *elat, string *lnames) { int_list il = new_int_list (); value nv = new_value (large_lattice_value); int ix; room_int_list (il, size); il -> size = size; for (ix = 0; ix < size; ix++) il -> array[ix] = (elat)?(elat[ix]):0; nv -> dptr = (void *) lnames; nv -> u.elat = il; return (nv); };
static tv_term build_term(const_gotcha_node current, const_tv_term_list_list systems) { register unsigned int ix; register unsigned int iy; int_list pos; int_list neg; tv_term result; if (systems->sz == 0) { return new_tv_term(new_int_list(), new_int_list()); } if (NULL == (result = rdup_tv_term(systems->arr[0]->arr[current->offsets[0]]))) { return NULL; } for (ix = 1; ix < current->depth; ix++) { iy = current->offsets[ix]; if (NULL == (pos = merge_sorted_int_list(result->pos, systems->arr[ix]->arr[iy]->pos))) { rfre_tv_term(result); return NULL; } if (NULL == (neg = merge_sorted_int_list(result->neg, systems->arr[ix]->arr[iy]->neg))) { rfre_tv_term(result); return NULL; } rfre_int_list(result->pos); rfre_int_list(result->neg); result->pos = pos; result->neg = neg; } return result; }
struct int_list *get_locations(char *start, size_t max_len, char to_find) { struct int_list *locations = new_int_list(); int i; for (i=0; i<max_len-2; i+=3) { printf("i=%d, codon=%c%c%c, aa=%c, looking for %c\n", i, *(start+i), *(start+i+1), *(start+i+2), aa_at(start+i), to_find); if (aa_at(start+i) == to_find) { printf("Found\n"); il_add(locations, i); } } return locations; }
int_list init_index(const_extent_list ranges, const_index_entry_list quantifier_indices, const_user_type_entry_list user_type_table) { register unsigned int ix; int_list indices = new_int_list(); for (ix = 0; ix < ranges->sz; ix++) { append_int_list(indices, eval_int_expr(ranges->arr[ix]->from, quantifier_indices, user_type_table)); } return indices; }
static int parse_DIMACS_file(FILE *f, tv_clause_list *clauses, variable_list *variables) { char buf[WORDSIZE + 1]; int lineno = 1; int start_of_line = 1; tv_clause_list cl = tv_clause_listNIL; int varcount; int tv_clausecount; int_list pos = new_int_list(); int_list neg = new_int_list(); for (;;) { int c = fgetc(f); int new_start_of_line = 0; if (c == EOF) { /* We're done. */ break; } if (c == '\n') { lineno++; new_start_of_line = 1; } if (start_of_line && c == 'p') { int res; /* A parameters line. */ if (cl != tv_clause_listNIL) { fprintf(stderr, "%d: Only one 'p' line allowed\n", lineno); return 0; } if ((res = fscanf(f, " cnf %d %d", &varcount, &tv_clausecount)) != 2) { fprintf( stderr, "%d: Malformed 'p' line\n", lineno ); return 0; } /* There should only be whitespace at this line. */ for (;;) { c = fgetc(f); if (c == '\n') { lineno++; new_start_of_line = 1; break; } } cl = setroom_tv_clause_list(new_tv_clause_list(), (unsigned int)tv_clausecount); } else if (start_of_line && c == 'c') { /* The start of a comment line. Eat the rest of it. */ for (;;) { c = fgetc( f ); if (c == EOF || c == '\n') { break; } } lineno++; new_start_of_line = 1; } else if (isspace(c)) { /* Skip. */ } else if (c == '-' || isdigit(c)) { read_word(f, c, buf, WORDSIZE); if (buf[0] == '-' && buf[1] == '\0') { /* A special case we want to complain about. */ fprintf( stderr, "%d: Solitary '-' ignored\n", lineno ); } else { if (buf[0] == '-') { int v = atoi(buf + 1); /* * We start counting the variables from '0', so * we need to do 'v-1'. */ neg = append_int_list(neg, v - 1); } else { int v = atoi(buf); if (v == 0) { /* * This terminates a tv_clause. Put it away, and * start a new one. */ if (cl != tv_clause_listNIL) { tv_clause c = new_tv_clause( pos, neg ); cl = append_tv_clause_list(cl, c); } pos = new_int_list(); neg = new_int_list(); } else { /* * We start counting the variables from '0', so * we need to do 'v-1'. */ pos = append_int_list(pos, v - 1); } } } } else { fprintf( stderr, "%d: Unexpected character '%c' ignored\n", lineno, c ); } start_of_line = new_start_of_line; } /* If there is a pending tv_clause, put it away. */ if (pos->sz != 0 || neg->sz != 0) { if (cl != tv_clause_listNIL) { tv_clause c = new_tv_clause(pos, neg); cl = append_tv_clause_list(cl, c); } } else { rfre_int_list(pos); rfre_int_list(neg); } *clauses = cl; *variables = build_dimacs_variable_list(varcount); return cl == tv_clause_listNIL ? 0 : 1; }
int main() { struct int_list* item = NULL; struct slist_queue queue; genc_slq_init(&queue); assert(queue.head == NULL); assert(queue.tail == &queue.head); genc_slq_push_back(&queue, &new_int_list(5)->head); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item != NULL); assert(item->val == 5); assert(queue.head == NULL); assert(queue.tail == &queue.head); free(item); genc_slq_push_front(&queue, &new_int_list(5)->head); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item != NULL); assert(item->val == 5); assert(queue.head == NULL); assert(queue.tail == &queue.head); free(item); genc_slq_push_back(&queue, &new_int_list(1)->head); genc_slq_push_back(&queue, &new_int_list(2)->head); genc_slq_push_back(&queue, &new_int_list(3)->head); genc_slq_push_front(&queue, &new_int_list(0)->head); genc_slq_push_back(&queue, &new_int_list(4)->head); slist_queue_t other_queue; genc_slq_init(&other_queue); genc_slq_push_back(&other_queue, &new_int_list(6)->head); genc_slq_push_back(&other_queue, &new_int_list(7)->head); genc_slq_push_front(&other_queue, &new_int_list(5)->head); genc_slq_push_back(&other_queue, &new_int_list(8)->head); assert(genc_slq_length(&queue) == 5); assert(genc_slq_length(&other_queue) == 4); genc_slq_splice_onto_end(&queue, &other_queue); assert(genc_slq_length(&queue) == 9); assert(genc_slq_length(&other_queue) == 0); assert(genc_slq_is_empty(&other_queue)); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item->val == 0); free(item); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item->val == 1); free(item); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item->val == 2); free(item); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item->val == 3); free(item); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item->val == 4); free(item); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item->val == 5); free(item); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item->val == 6); free(item); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item->val == 7); free(item); item = genc_slq_pop_front_object(&queue, struct int_list, head); assert(item->val == 8); free(item); assert(queue.head == NULL); assert(queue.tail == &queue.head); assert(genc_slq_is_empty(&queue)); return 0; }
/** * Creates a new DPLL problem for use by the DPLL consistency checker * or the CNF to DNF converter. This function is used internally by * the solvers. * * @param clauses the input set of clauses; * @param variables the number of variables in the CNF. * @returns the newly created DPLL problem. */ dpll_problem dpll_new_problem(tv_clause_list clauses, const unsigned int variables_count) { register unsigned int ix; const_int_list pos; const_int_list neg; dpll_problem problem = (dpll_problem)malloc(sizeof(struct str_dpll_problem)); if (NULL == problem) { return problem; } problem->variables_count = variables_count; if (option_simplify_clauses) { problem->clauses = simplify_clauses(clauses); } else { problem->clauses = clauses; } problem->assigned_variables = 0; problem->current_variable = (unsigned int)-1; problem->consistent = 1; problem->branch_points = 0; problem->satisfied_clauses = 0; problem->randomized = 0; problem->empty_clause = new_tv_clause(new_int_list(), new_int_list()); problem->assignment_history = (unsigned int *)malloc(sizeof(unsigned int) * variables_count); memset(problem->assignment_history, 0, sizeof(unsigned int) * variables_count); problem->satisfied = (unsigned int *)malloc(sizeof(unsigned int) * clauses->sz); problem->labelled = (unsigned int *)malloc(sizeof(unsigned int) * clauses->sz); memset(problem->satisfied, 0, sizeof(unsigned int) * clauses->sz); memset(problem->labelled, 0, sizeof(unsigned int) * clauses->sz); problem->unit_variables_pos = queue_new(NULL, NULL); problem->unit_variables_neg = queue_new(NULL, NULL); problem->unit_variables_pos_map = (unsigned char *)malloc(sizeof(unsigned char) * variables_count); problem->unit_variables_neg_map = (unsigned char *)malloc(sizeof(unsigned char) * variables_count); memset(problem->unit_variables_pos_map, 0, sizeof(unsigned char) * variables_count); memset(problem->unit_variables_neg_map, 0, sizeof(unsigned char) * variables_count); problem->variables = (dpll_variable *)malloc(sizeof(dpll_variable) * variables_count); for (ix = 0; ix < variables_count; ix++) { problem->variables[ix] = new_dpll_variable(); } for (ix = 0; ix < problem->clauses->sz; ix++) { initialize_clause(problem, ix); pos = clauses->arr[ix]->pos; neg = clauses->arr[ix]->neg; if (pos->sz == 0 && neg->sz == 0) { /* Problem is UNSAT due to an empty clause. */ problem->consistent = 0; } if (pos->sz == 1 && neg->sz == 0) { int var = pos->arr[0]; if (problem->variables[var]->pos_filter_clause != NULL) { /* Problem is UNSAT due to arc inconsistency. */ problem->consistent = 0; } if (problem->variables[var]->neg_filter_clause == NULL) { problem->variables[var]->neg_filter_clause = clauses->arr[ix]; queue_push(problem->unit_variables_pos, i2p(var)); problem->unit_variables_pos_map[var] = 1; } } if (neg->sz == 1 && pos->sz == 0) { int var = neg->arr[0]; if (problem->variables[var]->neg_filter_clause != NULL) { /* Problem is UNSAT due to arc inconsistency. */ problem->consistent = 0; } if (problem->variables[var]->pos_filter_clause == NULL) { problem->variables[var]->pos_filter_clause = clauses->arr[ix]; queue_push(problem->unit_variables_neg, i2p(var)); problem->unit_variables_neg_map[var] = 1; } } } return problem; }