Example #1
0
//undoes the last literal decision and the corresponding implications obtained by unit resolution
//
//if the current decision level is L in the beginning of the call, it should be updated 
//to L-1 before the call ends
void sat_undo_decide_literal(SatState* sat_state) {
	Lit* lit = vector_top(&sat_state->ds);
	sat_restore_literal(sat_state, lit);
	vector_pop(&sat_state->ds);
	vector_pop(&sat_state->s);
	sat_undo_unit_resolution(sat_state);
}
Example #2
0
//undoes sat_unit_resolution(), leading to un-instantiating variables that have been instantiated
//after sat_unit_resolution()
void sat_undo_unit_resolution(SatState* sat_state) {
	while (vector_size(&sat_state->il) > 0) {
		Lit* lit = vector_top(&sat_state->il);
		if (lit->var->level > vector_size(&sat_state->ds) + 1) {
			sat_restore_literal(sat_state, lit);
			vector_pop(&sat_state->il);
			vector_pop(&sat_state->s);
		} else
			break;
	}
}
Example #3
0
File: thread.c Project: mtmiron/toi
VALUE
pop_scope()
{
	VALUE tmp;

	vector_t *stk = thr_stk;
	VALUE thr;

	thr = (VALUE)vector_pop(stk);
	while ( (tmp = (VALUE)vector_pop(THREAD(thr)->obj_stk)) )
		memdealloc(tmp);

	cur_thr = THREAD(thr)->up;
	return thr;
}
Example #4
0
void run() {
    int t = 0;
    vector* stack = vector_new(100);
    vector_push(stack, (void*)0);
    while (1) {
        t = cur_token();
        action a = table[(int)vector_peek(stack)][t];
        if (a.action == 's') {
            vector_push(stack, (void*)a.attr);
            index++;
            printf("shift %d\n", a.attr);
        } else if (a.action == 'r') {
            for (int i=0; i<rules[a.attr][0]-1; i++)
                vector_pop(stack);
            int head_of_production = rules[a.attr][1];
            int top_of_stack = (int)vector_peek(stack);
            int new_state = table[top_of_stack][head_of_production].attr;
            vector_push(stack, (void*)new_state);
            printf("Reduce by rule %d\n", a.attr);
            printf("Stack top: %d\n", (int)vector_peek(stack));
        } else if (a.action == 'a') {
            printf("Accept\n");
            break;
        } else {
            error("Parsing failed", "");
        }
    }
}
Example #5
0
void test_vector() {
    vector *v = vector_make_size(1, sizeof(int));
    int a[] = {1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15,16};
    *(int *) vector_in(v, 0) = 12;
    int i = 0;
    for (; i < 15; i++) {
        vector_push(v, a + i);
        printf("-- %d %d\n",a[i], v->len);
    }
    for (i = 0; i < 15; i++) {
        vector_push(v, &i);
    }
    printf("%d\n",v->len);
    for (i = 0; i < (int)v->len; i++) {
        printf("%d\n", *((int *) vector_in(v, i)));
    }
    for (i = 0; i < 50; i++) {
        vector_pop(v);
    }
    for (i = 0; i < (int)v->len; i++) {
        printf("%d\n", *((int *) vector_in(v, i)));
    }
    char c[] = "123456789";
    string b;
    b.str = c;
    b.len = 7;
    vector *v2 = vector_make(sizeof(string));
    memcpy(v2->array, &b, v2->size);
    printf("%s", ((string *) vector_in(v2, 0))->str);
}
Example #6
0
int network_manager_unregister_peer_for_polling(struct network_manager* nm, struct network_peer* peer)
{
    uintptr_t p = (uintptr_t)peer; 
    int* psock = NULL;
    int sock;

    JLG(psock, nm->poll_socket_by_peer, p);
    if(psock == NULL) return -1; // doesn't exist
    sock = *psock;

    int rc;
    JLD(rc, nm->poll_socket_by_peer, p);

    FD_CLR(sock, &nm->poll_read_fds);
    FD_CLR(sock, &nm->poll_write_fds);
    FD_CLR(sock, &nm->poll_exception_fds);

    if(sock == nm->poll_max_fd) {
        size_t num_fds = vector_count(&nm->poll_fds);

        // Need to figure out what the previous max fd was, which means..
        qsort(vector_data(&nm->poll_fds), (size_t)num_fds, sizeof(uintptr_t), compint);
        int old_fd = (int)vector_pop(&nm->poll_fds);
        assert(old_fd == sock);

        nm->poll_max_fd = 0;
        if(num_fds > 1) {
            nm->poll_max_fd = vector_get(&nm->poll_fds, num_fds - 2);
        }
    }

    return 0;
}
Example #7
0
File: test.c Project: esheldon/misc
void test_long() {

    struct vector* v = vector_new(0, sizeof(long));
    long n=10;
    long i=0;
    for (i=0; i<n; i++) {
        vector_push(v, &i);
    }

    long* iter = vector_front(v);
    long* end  = vector_end(v);
    i=0;
    while (iter != end) {
        assert(i == *iter);
        iter++;
        i++;
    }

    long* lptr = vector_get(v,3);
    assert(3 == *lptr);

    lptr = vector_pop(v);
    assert((n-1) == *lptr);

    v=vector_delete(v);
    assert(v==NULL);
}
Example #8
0
File: test.c Project: esheldon/misc
void test_pushpop() {
    struct vector* v = vector_new(0, sizeof(struct test));

    size_t i=0;
    size_t n=10;
    struct test t;
    for (i=0; i<n; i++) {
        t.id = i;
        t.x = 2*i;
        vector_push(v, &t);
    }

    struct test *tptr=NULL;
    for (i=0; i<n; i++) {
        tptr = vector_get(v,i);
        assert(tptr->id == i);
        assert(tptr->x == 2*i);
    }

    i=n-1;
    while (NULL != (tptr=vector_pop(v))) {
        assert(tptr->id == i);
        assert(tptr->x == 2*i);
        i--;
    }

    assert(v->size == 0);
    assert(v->capacity >= n);

    v = vector_delete(v);
    assert(v == NULL);
}
Example #9
0
void su_vector_pop(su_state *s, int idx, int num) {
	int i;
	int n = (int)STK(TOP(num))->obj.num;
	value_t vec = *STK(TOP(idx));
	for (i = 0; i < n; i++)
		vec = vector_pop(s, vec.obj.vec);
	push_value(s, &vec);
}
Example #10
0
int reduce_sequence(vector_char_t* stack) {
    stack_sym_t sym;
    int seq = 0;
    while ((sym = vector_pop(stack)) != S_SEP) {
        seq |= sym;
        seq <<= 8;
    }
    return seq >> 8;
}
void test_vector_pop() {
    array *arr = vector_create();
    vector_append(arr, 5);
    vector_append(arr, 4);
    vector_append(arr, 3);
    data_t popped_el = 0;
    vector_pop(arr, &popped_el);
    ASSERT(popped_el == 3);
    ASSERT(arr->size == 2);

    vector_free(arr);
}
Example #12
0
int		trim_left_path(char *arg, t_vector *stack,
			       t_vector *dirs, char *path)
{
  if (!my_strncmp(arg, path, my_strlen(path)))
    {
      vector_push(dirs, path);
      add_matching_files(arg + my_strlen(path), stack, dirs);
      vector_pop(dirs);
      return (1);
    }
  return (0);
}
Ensure(generate_logarithmic_bins, sets_bins_boundries_correct) {
	generate_logarithmic_bins(&bins_vector, indexed_mode_modulus, &conf);

	bins_struct bin;
	while (bins_vector.log_length > 0) {
		vector_pop(&bins_vector, &bin);

		assert_that(bin.k_min, is_equal_to(pow(jump, 2 * bins_vector.log_length)));
		assert_that(bin.k, is_equal_to(pow(jump, 2 * bins_vector.log_length + 1)));
		assert_that(bin.k_max, is_equal_to(pow(jump, 2 * bins_vector.log_length + 2)));
	}

}
Example #14
0
void		matching_file(char *arg, t_vector *stack,
			      t_vector *dirs, struct dirent *dp)
{
  char		*file_name;

  if (my_strstr(arg, "/"))
    {
      vector_push(dirs, (void *)complete_path(dp->d_name, "/"));
      add_matching_files(my_strstr(arg, "/") + 1, stack, dirs);
      xsfree(vector_pop(dirs));
    }
  else
    {
      file_name = dir_from_vector(dirs);
      vector_push(stack, (void *)complete_path(file_name, dp->d_name));
      xsfree(file_name);
    }
}
Example #15
0
int		add_files_to_arg_tab(t_cmd *cmd, int iarg, t_vector *vect)
{
  int		i, j;
  char		**new_arg;

  i = -1;
  while (cmd->arg[++i]);
  new_arg = xsmalloc(sizeof(*new_arg) * (vector_size(vect) + i));
  i = -1;
  while (++i < iarg)
    new_arg[i] = cmd->arg[i];
  j = iarg;
  while (vector_size(vect))
    new_arg[i++] = vector_pop(vect);
  while (cmd->arg[j++])
    new_arg[i++] = cmd->arg[j];
  xsfree(cmd->arg);
  cmd->arg = new_arg;
  return (0);
}
Example #16
0
void verr_try(void (*action)(), void (*handle)(error_t error), void (*cleanup)()) {
  TryFrame* frame = vector_push(try_stack);
  error_t error = setjmp(frame->env);
  if (error == 0) {
    action();
  } else {
    if (handle) {
      // Wrap the handler in another try frame, so that the cleanup function is still called
      error_t reraise = setjmp(frame->env);
      if (reraise == 0) {
        handle(error);
        error = 0;
      } else {
        error = reraise;
      }
    }
  }
  vector_pop(try_stack);
  if (cleanup) cleanup();
  if (error) verr_raise(error);
}
Example #17
0
File: vectest.c Project: SGBon/misc
int main(){
	struct vector vec;
	/* can make vector like this */
	//vector_init(&vec, sizeof(float));
	/* or like this */
	VECTOR_CREATE(&vec,float);

	float r = 12.3f;
	vector_add(&vec,&r);
	r = 1.0f;
	vector_add(&vec,&r);
	vector_get(&vec,0,&r);

	printf("Value at %d is %f\n",0,r);

	printf("Testing expansion\n");
	for(size_t i = 0; i < 50; ++i){
		float j = r*i;
		vector_add(&vec, &j);
	}

	printf("Testing retrieval\n");
	printf("[");
	for(size_t i = 0; i < 50;++i){
		vector_get(&vec,i,&r);
		printf("%f, ",r);
	}
	printf("]\n");

	printf("Testing shrinking\n");
	const size_t before = vec.size;
	for(size_t i = 0; i < 25; ++i){
		vector_pop(&vec,&r);
		printf("%f\n",r);
	}
	printf("before: %lu, after: %lu\n",before,vec.size);

	vector_destroy(&vec);
	return 0;
}
Example #18
0
expression_t parse_expr() {
    expression_t parsed_expr;
    stack_sym_t top;
    stack_sym_t next;
    vector_char_t* stack = vector_init(VI_CHAR);
    vector_token_t* token_buffer = vector_init(VI_TOKEN);
    vector_expr_t* expr_buffer = vector_init(VI_EXPR);
    vector_push(stack, S_END);

    bool use_cached = cached_identificator.type != TT_NONE;
    token_t last_token;
    token_t expr_token = use_cached ? cached_identificator : next_token ;
    next = token_to_sym(&expr_token);

    do {
        top = vector_find(stack, top_term_cmp);
        switch (prec_table[top][next]) {
            case TAB_SP:
                vector_insert_after(stack, S_SEP, top_term_cmp);
            case TAB_P:
                vector_push(stack, next);
                //next is now top terminal on stack
                top = next;
                //store last token
                vector_push(token_buffer, expr_token);
                //choose between cached_identificator and next_token
                if (use_cached) {
                    use_cached = false;
                    cached_identificator.type = TT_NONE;
                    expr_token = next_token;
                } else {
                    expr_token = next_token = get_next_token(token_stream);
                }
                //convert next token to table symbol
                next = token_to_sym(&expr_token);
                break;
            case TAB_R:
                last_token = vector_pop(token_buffer);
                switch (reduce_sequence(stack)) {
                    case RULE_REL:
                        check_rule_rel(last_token.op_rel, expr_buffer);
                        break;
                    case RULE_ADDSUB:
                        check_rule_arith(last_token.op_arith, expr_buffer);
                        break;
                    case RULE_MULDIV:
                        check_rule_arith(last_token.op_arith, expr_buffer);
                        break;
                    case RULE_PAR:
                        //only pop next token(TT_PARENTHESES_OPEN) from token buffer
                        vector_pop(token_buffer);
                        break;
                    case RULE_ID:
                        check_rule_id(&last_token, expr_buffer);
                        break;
                    default:
                        error("Syntactic error: Failed to parse the expression", ERROR_SYN);
                }
                vector_push(stack, S_EXPR);
                break;
            case TAB_END:
                if (vector_pop(stack) != S_EXPR) {
                    error("Syntactic error: Failed to parse the expression", ERROR_SYN);
                }
                parsed_expr = vector_top(expr_buffer);
                break;
            case TAB_ERR:
            default:
                error("Syntactic error: Failed to parse the expression", ERROR_SYN);
        }
    } while (top != S_END || next != S_END);

    ifj15_free(stack);
    ifj15_free(token_buffer);
    ifj15_free(expr_buffer);
    return parsed_expr;
}
Example #19
0
TEST(VectorTest, AccessorsManipulators) {
	status_t err = NO_ERROR;
	vector_handle_t vector = NULL;
	size_t count = 0;
	size_t capacity = 0;
	char element = 0;
	char* p_element = NULL;

	err = vector_create(1, 1, &vector);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(vector != NULL);

	err = vector_capacity(vector, &capacity);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(capacity, (size_t)1);


	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)0);

	err = vector_append(vector, &element);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)1);

	err = vector_pop(vector);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)0);

	for (element = 0; element < 10; element++) {
		err = vector_append(vector, &element);
		ASSERT_EQ(err, NO_ERROR);
	}

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)10);

	err = vector_capacity(vector, &capacity);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_GT(capacity, (size_t)1);

	err = vector_element_address(vector, 5, (void**)&p_element);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(NULL != p_element);
	ASSERT_EQ(*p_element, (char)5);

	err = vector_remove(vector, 5);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_element_address(vector, 5, (void**)&p_element);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(NULL != p_element);
	ASSERT_EQ(*p_element, (char)6);

	err = vector_count(vector, &count);
	while ((err == NO_ERROR) && (count > 4)) {
		err = vector_pop(vector);
		ASSERT_EQ(err, NO_ERROR);
		err = vector_count(vector, &count);
	}

	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)4);

	err = vector_remove(vector, 6);
	ASSERT_NE(err, NO_ERROR);

	err = vector_array(vector, (void**)&p_element);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(NULL != p_element);

	element = 0;
	while (element < 4) {
		ASSERT_EQ(*p_element, element);
		p_element++;
		element++;
	}

	err = vector_clear(vector);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)0);

	err = vector_pop(vector);
	ASSERT_NE(err, NO_ERROR);

	vector_release(vector);
}
Example #20
0
int
main(int argc, char *argv[]) {

    vector_t *vector = vector_alloc(data_free_func);
    if (vector == NULL) {
        log_error("Failed to alloc vector");
        return RET_FAILED;
    }

    for (int i = 0; i < TEST_NUM; i ++) {
        data_t *data = my_malloc(sizeof(data_t));
        data->num = i;

        vector_unshift(vector, data);
        for (int j = 0; j <= i; j ++) {
            data_t *unshift = vector_get(vector, j);
            test(vector_get, i - j, unshift->num);
        }

    }


    for (int i = 0; i < TEST_NUM; i ++) {
        data_t *data = vector_pop(vector);
        test(vector_pop, i, data->num);
        data_free_func(data);
    }

    vector_empty(vector);
    for (int i = 0; i < TEST_NUM; i ++) {
        data_t *data = my_malloc(sizeof(data_t));
        data->num = i;
        if (i & 0x1) {
            vector_unshift(vector, data);
        } else {
            vector_push_back(vector, data);
        }
    }

    for (int i = 0; i < TEST_NUM / 2; i ++) {
        data_t *data = vector_shift(vector);
        test(vector_shift, TEST_NUM - 2 * i - 1, data->num);
        data_free_func(data);
    }
    
    for (int i = 0; i < TEST_NUM / 2; i ++) {
        data_t *data = vector_shift(vector);
        test(vector_shift, 2 * i, data->num);
        data_free_func(data);
    }
    
    vector_empty(vector);

    for (int i = 0; i < TEST_NUM; i ++) {
        data_t *data = my_malloc(sizeof(data_t));
        data->num = i;

        vector_push_back(vector, data);
    }

    for (int i = 0; i < vector_size(vector); i ++) {
        data_t *data = vector_get(vector, i);
        test(vector_get, i, data->num);
    }


    vector_destroy(vector);
}