static value_t read_vector(value_t label, u_int32_t closer) { value_t v=the_empty_vector, elt; u_int32_t i=0; PUSH(v); if (label != UNBOUND) ptrhash_put(&readstate->backrefs, (void*)label, (void*)v); while (peek() != closer) { if (ios_eof(F)) lerror(ParseError, "read: unexpected end of input"); if (i >= vector_size(v)) { v = Stack[SP-1] = vector_grow(v); if (label != UNBOUND) ptrhash_put(&readstate->backrefs, (void*)label, (void*)v); } elt = do_read_sexpr(UNBOUND); v = Stack[SP-1]; vector_elt(v,i) = elt; i++; } take(); if (i > 0) vector_setsize(v, i); return POP(); }
int vector_read(vec_p vec, const char* filename) { assert(vec); assert(vec->len == 0); FILE* fp; double r; int ret; fp = fopen(filename, "r"); if(!fp) { printf("Cannot open %s\n", filename); exit(1); } //double temp; char temp[128]; while (!feof(fp)) { if (vec->len == vec->cap) { vector_grow(vec, vec->cap * 2); } ret = fscanf(fp, "%s", temp); if (ret == EOF) break; PINT(vec->len); errno = 0; vec->value[vec->len++] = strtod(temp, NULL); //vector_print(vec); if (errno != 0) { perror("strtod"); fprintf(stderr, "%s\n", temp); exit(EXIT_FAILURE); } } fclose(fp); return vec->len; }
void vector_add(vector *vector, void *element) { if (vector->allocatedLength == vector->logicalLength) { vector_grow(vector); } void *target = vector_address(vector, vector->logicalLength++); memcpy(target, element, vector->elementSize); }
/* * User function to append a value to the vector. It grows the * underlying array if necessary, using the vector_grow utility * function. * * \return zero on success. */ int vector_append (Vector * vec, int value) { if (vec->len >= vec->cap) { if (0 != vector_grow (vec)) return -1; } vec->arr[vec->len++] = value; return 0; }
void vector_new(vector *vector, int elementSize, freeFunction freeFn) { assert(elementSize > 0); vector->elementSize = elementSize; vector->logicalLength = 0; vector->allocatedLength = 2; vector->elements = NULL; vector->freeFn = freeFn; vector_grow(vector); }
void vector_insert_at(kld_vector_t * v, int i, void * data) { if(vector_is_empty(v)) { v->data = (void **) calloc(DEFAULT_VECTOR_CAPACITY, sizeof(void*)); } if (i > v->size) { // If the desired index is outside of our current size // We need to fill the vector up to the desired index point // Then insert the value we want. int idx; for(idx = v->size; idx < i; idx++) { // Grow if at capacity // TODO The following six lines could be refactored // with the others below. if(v->size == v->capacity) { vector_grow(v); } v->data[v->size] = NULL; v->size++; } } else { // If the desired index is within our current size // We want to shift all the values after the insertion point to the right. // Then insert our desired value. int idx; for(idx = v->size; idx > i; idx--) { v->data[idx] = v->data[idx-1]; } } // Grow if at capacity // TODO The following six lines could be refactored // with the others above. if(v->size == v->capacity) { vector_grow(v); } v->data[i] = data; v->size++; }
void *_vector_push(struct vector *v, size_t elemsize) { if (v->count == v->allocated_count) { if (!vector_grow(v)) { return NULL; } } vector_resize(v, v->count+1); return _vector_get(v, elemsize, v->count-1); }
unsigned dv_vector_push(DV_Vector* dv, void* chunk, unsigned* grown_by) { unsigned index = index_stack_empty(dv->available_stack) ? dv->size : index_stack_pop(dv->available_stack); *grown_by = 0; /* vector full */ if (dv->size >= dv->size_hint) { *grown_by = vector_grow(dv); } memcpy(dv->data+(dv->size*dv->chunk_size), chunk, dv->chunk_size); dv->indices[index] = dv->size; dv->size++; index_stack_push(dv->last_stack, index); return index; }