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", ""); } } }
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); }
/* Recursively build list of input/output files from the given command. */ static void build_input_output_list(struct graph_node *node, const command_t cmd) { if (cmd == NULL) ERR("Graph ndoe command can not be null"); else if (cmd->type == SIMPLE_COMMAND) { /* Add redirects, then process arguments */ if (cmd->input != NULL) vector_push(node->input_files, cmd->input); if (cmd->output != NULL) vector_push(node->output_files, cmd->output); /* Note: consider ALL arguments of the command as input files */ char **word = cmd->u.word; if (strcmp(word[0], "exec") == 0) /* Skip "exec" */ ++word; for (char **w = word + 1; *w != NULL; ++w) /* Consider "-*" as options */ if (w[0][0] != '-') vector_push(node->input_files, *w); } else if (cmd->type == SUBSHELL_COMMAND) { if (cmd->input != NULL) vector_push(node->input_files, cmd->input); if (cmd->output != NULL) vector_push(node->output_files, cmd->output); build_input_output_list(node, cmd->u.subshell_command); } else /* All other commands have two sub-commands */ { build_input_output_list(node, cmd->u.command[0]); build_input_output_list(node, cmd->u.command[1]); } }
//sets the literal to true, and then runs unit resolution //returns a learned clause if unit resolution detected a contradiction, NULL otherwise // //if the current decision level is L in the beginning of the call, it should be updated //to L+1 so that the decision level of lit and all other literals implied by unit resolution is L+1 Clause* sat_decide_literal(Lit* lit, SatState* sat_state) { lit->implied = 1; vector_push(&sat_state->ds, lit); vector_push(&sat_state->s, lit); sat_literal_var(lit)->level = vector_size(&sat_state->ds) + 1; if (sat_unit_resolution(sat_state)) return NULL; return sat_state->ac; }
void hpix_in_ring( const struct healpix* hpix, int64 iz, double phi0, double dphi, struct vector* listpix) // vector of int64 { int64 nr, ir, ipix1; double shift=0.5; int64 nside = hpix->nside; if (iz<nside) { // north pole ir = iz; nr = ir*4; ipix1 = 2*ir*(ir-1); // lowest pixel number in the ring } else if (iz>(3*nside)) { // south pole ir = 4*nside - iz; nr = ir*4; ipix1 = hpix->npix - 2*ir*(ir+1); // lowest pixel number in the ring } else { // equatorial region ir = iz - nside + 1; // within {1, 2*nside + 1} nr = nside*4; if ((ir&1)==0) shift = 0; ipix1 = hpix->ncap + (ir-1)*nr; // lowest pixel number in the ring } int64 ipix2 = ipix1 + nr - 1; // highest pixel number in the ring if (dphi > (M_PI-1e-7)) { for (int64 i=ipix1; i<=ipix2; ++i) { vector_push(listpix, &i); } } else { // M_1_PI is 1/pi int64 ip_lo = (int64)( floor(nr*.5*M_1_PI*(phi0-dphi) - shift) )+1; int64 ip_hi = (int64)( floor(nr*.5*M_1_PI*(phi0+dphi) - shift) ); int64 pixnum = ip_lo+ipix1; if (pixnum<ipix1) { pixnum += nr; } for (int64 i=ip_lo; i<=ip_hi; ++i, ++pixnum) { if (pixnum>ipix2) { pixnum -= nr; } vector_push(listpix, &pixnum); } } }
char* get_lyrics(int initial_bottle_count) { int bottle_count; char* pluralized_bottle; const int MAX_LINE_LENGTH = 256; // just a guess char line_buffer[MAX_LINE_LENGTH]; int chars_to_write; struct vector lyrics_stream; char* buffer; vector_create(&lyrics_stream, 100); // create formated strings and put them into a vector. for (bottle_count = initial_bottle_count; bottle_count >= 0; bottle_count--) { if (bottle_count > 0) { pluralized_bottle = bottle_count == 1 ? "bottle" : "bottles"; chars_to_write = snprintf(line_buffer, MAX_LINE_LENGTH, "%d %s of beer on the wall, %d %s of beer.\n" \ "Take one down and pass it around, ", bottle_count, pluralized_bottle, bottle_count, pluralized_bottle); vector_push(&lyrics_stream, line_buffer, chars_to_write); if (bottle_count == 1) { chars_to_write = snprintf(line_buffer, MAX_LINE_LENGTH, "no more bottles of beer on the wall.\n"); vector_push(&lyrics_stream, line_buffer, chars_to_write); } else { pluralized_bottle = bottle_count-1 == 1 ? "bottle" : "bottles"; chars_to_write = snprintf(line_buffer, MAX_LINE_LENGTH, "%d %s of beer on the wall.\n", bottle_count-1, pluralized_bottle); vector_push(&lyrics_stream, line_buffer, chars_to_write); } } else { pluralized_bottle = initial_bottle_count == 1 ? "bottle" : "bottles"; chars_to_write = snprintf(line_buffer, MAX_LINE_LENGTH, "No more bottles of beer on the wall, no more bottles of beer.\n" \ "Go to the store and buy some more, " \ "%d %s of beer on the wall.\n", initial_bottle_count, pluralized_bottle); vector_push(&lyrics_stream, line_buffer, chars_to_write); } } // copy data to buffer so the vector can be freed. buffer = malloc(lyrics_stream.size); memcpy(buffer, lyrics_stream.data, lyrics_stream.size); vector_destroy(&lyrics_stream); return buffer; }
//applies unit resolution to the cnf of sat state //returns 1 if unit resolution succeeds, 0 if it finds a contradiction BOOLEAN sat_unit_resolution(SatState* sat_state) { BOOLEAN more = 1; while (more) { more = 0; for (c2dSize i = vector_size(&sat_state->q); i >= 1; i--) { Clause* clause = vector_get(&sat_state->q, i - 1); clause->subsumed = sat_check_subsumed_clause(clause); if (sat_subsumed_clause(clause)) { vector_erase(&sat_state->q, i - 1); continue; } BOOLEAN flag = 0; c2dSize pos; for (c2dSize j = 0; j < sat_clause_size(clause); j++) { Lit* lit = vector_get(&clause->lits, j); if (!sat_implied_literal(sat_opp_literal(lit))) { flag++; pos = j; if (flag == 2) break; } } if (flag == 0) { sat_state->ac = malloc(sizeof(Clause)); vector_init(&sat_state->ac->lits); sat_state->ac->subsumed = 0; sat_state->ac->mark = 0; if (vector_size(&sat_state->ds) > 0) { for (c2dSize j = 0; j < sat_var_count(sat_state); j++) { Var* var = vector_get(&sat_state->vars, j); var->u = var->level <= 1; } sat_get_assert_clause(sat_state, sat_state->ac, clause->index - 1, 0, vector_size(&sat_state->s) - 1); } return 0; } if (flag == 1) { Lit* lit = vector_get(&clause->lits, pos); lit->implied = 1; lit->locate = clause->index - 1; lit->var->level = vector_size(&sat_state->ds) + 1; vector_push(&sat_state->il, lit); vector_push(&sat_state->s, lit); clause->subsumed = 1; vector_erase(&sat_state->q, i - 1); more = 1; } } } return 1; }
//adds clause to the set of learned clauses, and runs unit resolution //returns a learned clause if unit resolution finds a contradiction, NULL otherwise // //this function is called on a clause returned by sat_decide_literal() or sat_assert_clause() //moreover, it should be called only if sat_at_assertion_level() succeeds Clause* sat_assert_clause(Clause* clause, SatState* sat_state) { clause->index = sat_clause_count(sat_state) + sat_learned_clause_count(sat_state) + 1; for (c2dSize i = 0; i < sat_clause_size(clause); i++) { Lit* lit = vector_get(&clause->lits, i); Var* var = sat_literal_var(lit); if (vector_size(&var->mentions_lc) == 0 || vector_top(&var->mentions_lc) != clause) vector_push(&var->mentions_lc, clause); } vector_push(&sat_state->lc, clause); vector_push(&sat_state->q, clause); if (sat_unit_resolution(sat_state)) return NULL; return sat_state->ac; }
static int add_nodes_from_edges(struct call_graph *graph) { int i; address_t last_addr = 0; int have_last_addr = 0; qsort(graph->edge_from.ptr, graph->edge_from.size, graph->edge_from.elemsize, cmp_branch_by_dst); /* Look for unique destination addresses */ for (i = 0; i < graph->edge_from.size; i++) { const struct cg_edge *br = CG_EDGE_FROM(graph, i); if (!have_last_addr || br->dst != last_addr) { struct cg_node n; n.offset = br->dst; last_addr = br->dst; have_last_addr = 1; if (vector_push(&graph->node_list, &n, 1) < 0) return -1; } } return 0; }
static int find_possible_edges(int offset, int len, uint8_t *memory, struct call_graph *graph) { int i; for (i = 0; i < len; i += 2) { struct msp430_instruction insn; if (dis_decode(memory + i, offset + i, len - i, &insn) < 0) continue; if (insn.dst_mode == MSP430_AMODE_IMMEDIATE && (insn.op == MSP430_OP_CALL || insn.op == MSP430_OP_BR) && !(insn.dst_addr & 1)) { struct cg_edge br; br.src = offset + i; br.dst = insn.dst_addr; br.is_tail_call = insn.op != MSP430_OP_CALL; if (vector_push(&graph->edge_from, &br, 1) < 0) return -1; } } return 0; }
t_vector *find_all_pattern(char **str) { t_vector *stack; char *name; char *inhib; int begin; int end; int k; int l; end = (k = begin = pre_pattern_dump(str, &name, &inhib, &stack)) - 1; while (str[0][++end] && (str[0][end] != CLOSE_BRACE)); if (!str[0][end]) return (stack); while (++k < end) { l = begin; while (((str[0][k] != ',') || str[1][k]) && (k < end)) { name[l] = str[0][k]; inhib[l++] = str[1][k++]; } post_pattern_dump(str, name + l, inhib + l, end); vector_push(stack, my_strdup(name)); } msfree(name, inhib, 0); return (stack); }
void gather_get_traces_for_aperture(int *m0s, su_trace_t *traces, int ntr, aperture_t *ap) { vector_init(ap->traces); float x0, y0; float x, y; su_get_receiver(&traces[*m0s], &x0, &y0); su_get_receiver(traces, &x, &y); float dist = SQR(x0 - x) + SQR(y0 - y); while (ntr-- && dist > SQR(ap->ap_m)) { traces++; su_get_receiver(traces, &x, &y); dist = SQR(x0 - x) + SQR(y0 - y); (*m0s)--; } ntr++; if (ntr == 0) return; while (ntr-- && dist <= SQR(ap->ap_m)) { vector_push(ap->traces, traces); traces++; su_get_receiver(traces, &x, &y); dist = SQR(x0 - x) + SQR(y0 - y); } }
static int push_option_name(void *user_data, const struct opdb_key *key, const union opdb_value *value) { (void)value; return vector_push((struct vector *)user_data, &key->name, 1); }
void read_this_path(t_vector *target, DIR *dir, char *path) { int already; int j; char buf[1024]; struct stat s; struct dirent *dp; while ((dp = readdir(dir)) != NULL) { strcpy(buf, path); strcat(buf, "/"); strcat(buf, dp->d_name); if ((access(buf, X_OK) != -1) && (stat(buf, &s) || 1)) if (S_ISREG(s.st_mode)) { j = -1; already = 0; while (++j < target->nb_elem) if (strcmp(target->data[j], dp->d_name) == 0) already = 1; if (already == 0) vector_push(target, my_strdup(dp->d_name)); } } }
void read_local_cmd(t_vector *target, DIR *dir) { struct dirent *dp; char buf[1024]; struct stat s; int already; int j; while ((dp = readdir(dir)) != NULL) if (access(dp->d_name, X_OK) != -1) { stat(dp->d_name, &s); j = -1; already = 0; while (++j < target->nb_elem) if (strcmp(target->data[j], dp->d_name) == 0) already = 1; if (already == 0) { strcpy(buf, dp->d_name); if (S_ISDIR(s.st_mode)) strcat(buf, "/"); vector_push(target, my_strdup(buf)); } } }
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); }
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); }
VALUE thread_new() { VALUE thr; thr = NEW_OBJ(Thread); OBJ_SETUP(thr, T_THREAD); BASIC(thr).klass = cThread; vector_push(thr_stk, (void*)thr); THREAD(thr)->alive_p = 1; if (cur_thr) THREAD(thr)->branch = THREAD(cur_thr)->branch; if ((thr != main_thread()) && (thr != cur_thr)) THREAD(thr)->recv = cur_thr; THREAD(thr)->up = cur_thr; THREAD(thr)->env_tbl = st_init_numtable(); THREAD(thr)->obj_stk = vector_new(); THREAD(thr)->self_stk = vector_new(); THREAD(thr)->env_stk = vector_new(); THREAD(thr)->tok_stk = vector_new(); THREAD(thr)->stack = vector_new(); #ifdef SYMBOL_CACHE THREAD(thr)->modified_syms = vector_new(); #endif return thr; }
void smbtree_import(BTree* tree, char* url) { FILE* file = fopen(url, "r"); char* word_to_insert = calloc(STRING_SIZE, sizeof(char)); while (fscanf(file, "%s", word_to_insert) != EOF) { char* word = calloc(STRING_SIZE, sizeof(char)); strcpy(word, word_to_insert); Vector* vec = vector_newVector(); int i, n_docs = 0; fscanf(file, "%d", &n_docs); for (i = 0; i < n_docs; i++) { char* doc_url = calloc(STRING_SIZE, sizeof(char)); int n_pos = 0, j; fscanf(file, "%s%d", doc_url, &n_pos); VectorNode* vec_node = vector_newVectorNode(doc_url); for (j = 0; j < n_pos; j++) { int pos = 0; fscanf(file, "%d", &pos); addPositionVectorNode(vec_node, pos); } vector_push(vec, vec_node); //Check possible bugs free(doc_url); } BTreeKey* key = btree_newBTreeKey(word, vec); btree_insert(tree, tree->root, key); } free(word_to_insert); fclose(file); }
void su_vector(su_state *s, int num) { int i; value_t vec = vector_create_empty(s); for (i = 0; i < num; i++) vec = vector_push(s, vec.obj.vec, STK(-(num - i))); s->stack_top -= num; push_value(s, &vec); }
void su_vector_push(su_state *s, int idx, int num) { int i; value_t vec = *STK(TOP(idx)); for (i = 0; i < num; i++) vec = vector_push(s, vec.obj.vec, STK(-(num - i))); s->stack_top -= num; push_value(s, &vec); }
int main() { /* Create the vector */ vector *v = vector_new(3); printf("Vector size: %d\n", vector_size(v)); /* Add elements to the vector */ vector_push(v, (void*)0); vector_push(v, (void*)11); vector_push(v, (void*)22); printf("Vector size: %d\n", vector_size(v)); vector_push(v, (void*)33); vector_push(v, (void*)44); vector_push(v, (void*)55); vector_push(v, (void*)66); /* Get the size and print the elements */ printf("Vector size: %d\n", vector_size(v)); printf("Element 1: %d\n", vector_get(v, 0)); printf("Element 2: %d\n", vector_get(v, 1)); printf("Element 3: %d\n", vector_get(v, 2)); printf("Element 4: %d\n", vector_get(v, 3)); printf("Element 5: %d\n", vector_get(v, 4)); printf("Element 6: %d\n", vector_get(v, 5)); printf("Element 7: %d\n", vector_get(v, 6)); /* Clear vector and print the size */ vector_clear(v); printf("Vector size: %d\n", vector_size(v)); /* We have not destructor at C, so clean up memory by ourselves */ vector_free(v); return 0; }
size_t vector_insert_sorted(struct vec *v, int item) { size_t j = vector_push(v, item); for (; j > 0 && v->vals[j] < v->vals[j-1]; j--) { int swap = v->vals[j]; v->vals[j] = v->vals[j-1]; v->vals[j-1] = swap; } return j; }
int query_result_push (query_result *r, const char *item) { __returns_int; __checked_call(0 == vector_push(&(r->items), item)); return 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); } }
/* Get lists of geoname indices for all of the given tokens and return no more than max_results entries from their intersection. */ static geoname_indices_t process_query(vector_t tokens, int max_results, geonames_by_token_func func) { int i, ntokens = vector_size(tokens); /* Lists of tokens. */ geoname_indices_t *lists = xmalloc(ntokens * sizeof(vector_t)); /* Size of each list. */ int *sizes = xmalloc(ntokens * sizeof(int)); /* Current index in the intersection algorithm in each of the lists. */ int *pos = xmalloc(ntokens * sizeof(int)); geoname_idx_t idx; geoname_indices_t res = vector_init(sizeof(geoname_idx_t)); /* Get geoname indices for each token. */ for (i = 0; i < ntokens; ++i) { char const *token = *(char const **) vector_at(tokens, i); lists[i] = func(token); sizes[i] = vector_size(lists[i]); pos[i] = 0; if (!sizes[i]) goto end; } idx = geoname_idx(lists[0], pos[0]); /* Find intersection of the results. */ for (;;) { geoname_idx_t prev = idx; for (i = 0; i < ntokens; ++i) { while (pos[i] < sizes[i] && geoname_idx(lists[i], pos[i]) < idx) ++pos[i]; if (pos[i] == sizes[i]) goto end; idx = geoname_idx(lists[i], pos[i]); } if (prev == idx) { vector_push(res, &idx); if (max_results == vector_size(res)) goto end; ++idx; } } end: free(pos); free(sizes); free(lists); return res; }
size_t vector_insert(struct vec *v, int item, size_t pos) { size_t j = vector_push(v, item); for (; j > pos; j--) { int swap = v->vals[j]; v->vals[j] = v->vals[j-1]; v->vals[j-1] = swap; } assert(j == pos); return j; }
Ensure(load_save_matches, saves_matches_to_disk_properly) { vector **primatches = allocate(N_PRI_HALOS, sizeof(vector*)); vector **secmatches = allocate(N_SEC_HALOS, sizeof(vector*)); int i, j; match dummy_match = {.matchid = MATCHID, .goodness = GOODNESS}; for(i = 0; i < N_PRI_HALOS; i++){ primatches[i] = new_vector(i, sizeof(match)); for(j = 0; j < i; j++) vector_push(primatches[i], &dummy_match); } for(i = 0; i < N_SEC_HALOS; i++){ secmatches[i] = new_vector(i, sizeof(match)); for(j = 0; j < i; j++) vector_push(secmatches[i], &dummy_match); } write_matches(MATCHES_FILE_ADDR, primatches, secmatches, N_PRI_HALOS, N_SEC_HALOS); vector **primatches_r = allocate(N_PRI_HALOS, sizeof(vector*)); vector **secmatches_r = allocate(N_SEC_HALOS, sizeof(vector*)); load_matches(MATCHES_FILE_ADDR, primatches_r, secmatches_r); match *thematch = allocate(1, sizeof(*thematch)); for(i = 0; i < N_PRI_HALOS; i++) for(j = 0; j < i; j++){ thematch = vector_get(primatches_r[i], j); assert_that(thematch->matchid, is_equal_to(MATCHID)); } free(primatches); free(secmatches); free(primatches_r); free(secmatches_r); return; }
extern bool table_field_add(TABLE **t, const char *field){ CREATE_TABLE char *f_name = (char *) malloc(strlen(field)); strcpy(f_name, field); if(vector_push(&(*t)->fields,f_name)) return true; return false; }
extern bool table_data_add(TABLE **t, const char *data){ CREATE_TABLE char *d_val = (char *) malloc(strlen(data)); strcpy(d_val, data); if(vector_push(&(*t)->value,d_val)) return true; return false; }