int_vec* gen_int_vec(int la, int* a) { int_vec* unit = (int_vec*) R_alloc(1,sizeof(*unit)); if(la == 0 & a != 0) printf("error in 'gen_int_vec': length of non-empty array is 0"); else{ if(a == 0) a = gen_int_array(la); unit -> lv = la; unit -> v = a; } return(unit); }
void fread_int_list(int_list *list, FILE* fp) { int_node* node; int* v; int lv; int i; int ll; if(fread(&ll,sizeof(int),1,fp)!=1) printf("error in reading the length of a list\n"); for(i=0; i < ll;i++){ if(fread(&lv,sizeof(int),1,fp) != 1) printf("error in reading the length of a vector\n"); v = gen_int_array(lv); if(fread(v,sizeof(int),lv,fp) != lv) printf("error in reading value of a vector\n"); node = gen_int_node(gen_int_vec(lv,v)); add_int_node(list,node); } }
static void test_bst_int(const size_t n, const gsl_bst_type * T, const enum array_order order, gsl_rng * r) { int *data = malloc(n * sizeof(int)); int *data_delete = malloc(n * sizeof(int)); int *sorted_data = malloc(n * sizeof(int)); gsl_bst_workspace * w = gsl_bst_alloc(T, NULL, compare_ints, NULL); gsl_bst_trav trav; int *p; int i; size_t nodes; /* generate data to be inserted in tree */ gen_int_array(n, order, data, r); for (i = 0; i < (int) n; ++i) sorted_data[i] = data[i]; gsl_sort_int(sorted_data, 1, n); if (order != ORD_RANDOM_DUP) { /* generate random order to delete data from tree */ gen_int_array(n, ORD_RANDOM, data_delete, r); } else { for (i = 0; i < (int) n; ++i) data_delete[i] = sorted_data[i]; } /* insert data */ for (i = 0; i < (int) n; ++i) { p = gsl_bst_insert(&data[i], w); gsl_test(p != NULL, "bst_int %s[n=%zu,order=%d] insert i=%d", gsl_bst_name(w), n, order, i); } if (order != ORD_RANDOM_DUP) { nodes = gsl_bst_nodes(w); gsl_test(nodes != n, "bst_int %s[n=%zu,order=%d] after insertion count = %zu/%zu", gsl_bst_name(w), n, order, nodes, n); } /* test data was inserted and can be found */ for (i = 0; i < (int) n; ++i) { p = gsl_bst_find(&data[i], w); gsl_test(*p != data[i], "bst_int %s[n=%zu,order=%d] find [%d,%d]", gsl_bst_name(w), n, order, *p, data[i]); p = gsl_bst_trav_find(&data[i], &trav, w); gsl_test(p == NULL, "bst_int %s[n=%zu,order=%d] trav_find unable to find item %d", gsl_bst_name(w), n, order, data[i]); check_traverser(n, order, &trav, data[i], "post-insertion", w); } /* traverse tree in-order */ p = gsl_bst_trav_first(&trav, w); i = 0; while (p != NULL) { int *q = gsl_bst_trav_cur(&trav); gsl_test(*p != sorted_data[i], "bst_int %s[n=%zu,order=%d] traverse i=%d [%d,%d]", gsl_bst_name(w), n, order, i, *p, sorted_data[i]); gsl_test(*p != *q, "bst_int %s[n=%zu,order=%d] traverse cur i=%d [%d,%d]", gsl_bst_name(w), n, order, i, *p, *q); p = gsl_bst_trav_next(&trav); ++i; } gsl_test(i != (int) n, "bst_int %s[n=%zu,order=%d] traverse number=%d", gsl_bst_name(w), n, order, i); /* traverse tree in reverse order */ p = gsl_bst_trav_last(&trav, w); i = n - 1; while (p != NULL) { int *q = gsl_bst_trav_cur(&trav); gsl_test(*p != sorted_data[i], "bst_int %s[n=%zu,order=%d] traverse reverse i=%d [%d,%d]", gsl_bst_name(w), n, order, i, *p, sorted_data[i]); gsl_test(*p != *q, "bst_int %s[n=%zu,order=%d] traverse reverse cur i=%d [%d,%d]", gsl_bst_name(w), n, order, i, *p, *q); p = gsl_bst_trav_prev(&trav); --i; } gsl_test(i != -1, "bst_int %s[n=%zu,order=%d] traverse reverse number=%d", gsl_bst_name(w), n, order, i); /* test traversal during tree modifications */ for (i = 0; i < (int) n; ++i) { gsl_bst_trav x, y, z; gsl_bst_trav_find(&data[i], &x, w); check_traverser(n, order, &x, data[i], "pre-deletion", w); if (data[i] == data_delete[i]) continue; p = gsl_bst_remove(&data_delete[i], w); gsl_test(*p != data_delete[i], "bst_int %s[n=%zu,order=%d] remove i=%d [%d,%d]", gsl_bst_name(w), n, order, i, *p, data_delete[i]); p = gsl_bst_trav_copy(&y, &x); gsl_test(*p != data[i], "bst_int %s[n=%zu,order=%d] copy i=%d [%d,%d]", gsl_bst_name(w), n, order, i, *p, data[i]); /* re-insert item */ p = gsl_bst_trav_insert(&data_delete[i], &z, w); check_traverser(n, order, &x, data[i], "post-deletion", w); check_traverser(n, order, &y, data[i], "copied", w); check_traverser(n, order, &z, data_delete[i], "insertion", w); #if 0 /* delete again */ gsl_bst_remove(&data[i], w); #endif } /* emmpty tree */ gsl_bst_empty(w); nodes = gsl_bst_nodes(w); gsl_test(nodes != 0, "bst_int %s[n=%zu,order=%d] empty count = %zu", gsl_bst_name(w), n, order, nodes); gsl_bst_free(w); free(data); free(data_delete); free(sorted_data); }