int main(int argc, char *argv[]) { Point myPoint = point_new(0.0, 0.0); Vector unitX = vector_new(1.0, 0.0); Vector unitY = vector_new(0.0, 1.0); printf("%s + %s + %s = ", point_toString(myPoint), vector_toString(unitX), vector_toString(unitY) ); point_addVector(myPoint, unitX); point_addVector(myPoint, unitY); printf("As a tuple: %s\n", tuple_toString((Tuple)myPoint)); printf("As a point: %s\n", point_toString(myPoint)); printf( "After scaling unitX by -0.5: %s\n", vector_toString(vector_scale(unitX, -0.5)) ); printf("unitX has length %lf\n", vector_length(unitX)); vector_delete(unitX); vector_delete(unitY); point_delete(myPoint); return 0; }
static void init_vector(t_svr_vector *vec, t_select *slt) { vec->slt = slt; vec->client = vector_new(NULL); vec->action = vector_new(NULL); vec->graph = vector_new(NULL); }
void hashmap_new(hashmap* hm, int elem_size, int num_buckets, hashmap_hash_fun hash_fun, hashmap_compare_fun compare_fun, hashmap_free_fun free_fun, key_type type) { assert(elem_size > 0 || num_buckets > 0 || hash_fun != NULL || compare_fun != NULL); if ((hm->pairs = malloc((num_buckets + 1) * sizeof(vector))) == NULL) { printf("memory allocation error in hm->pairs creation.\n"); exit(0); } // Problem with position initilization at 0, so i use a dirty solution, added 1 in // some places, but no in hm->num_buckets, for simpliest code. for (int i = 1; i < num_buckets + 1; ++i) vector_new(hm->pairs + i, sizeof(pair), free_fun, 4); if ((hm->used_buckets = malloc(sizeof(vector))) == NULL) { printf("memory allocation error in hm->used_buckets creation.\n"); return ; } vector_new(hm->used_buckets, sizeof(int), NULL, 4); hm->k_type = type; hm->pairs->elem_size = sizeof(vector); hm->num_buckets = num_buckets; hm->compare_fun = compare_fun; hm->free_fun = free_fun; hm->hash_fun = hash_fun; hm->value_size = elem_size; }
/* build orthonormal basis matrix Q = Y; for j=1:k vj = Q(:,j); for i=1:(j-1) vi = Q(:,i); vj = vj - project_vec(vj,vi); end vj = vj/norm(vj); Q(:,j) = vj; end */ void build_orthonormal_basis_from_mat(mat *A, mat *Q){ int m,n,i,j,ind,num_ortos=2; double vec_norm; vec *vi,*vj,*p; m = A->nrows; n = A->ncols; vi = vector_new(m); vj = vector_new(m); p = vector_new(m); matrix_copy(Q, A); for(ind=0; ind<num_ortos; ind++){ for(j=0; j<n; j++){ matrix_get_col(Q, j, vj); for(i=0; i<j; i++){ matrix_get_col(Q, i, vi); project_vector(vj, vi, p); vector_sub(vj, p); } vec_norm = vector_get2norm(vj); vector_scale(vj, 1.0/vec_norm); matrix_set_col(Q, j, vj); } } vector_delete(vi); vector_delete(vj); vector_delete(p); }
t_ray *ray_new(void) { t_ray *rayon; rayon = (t_ray *)malloc(sizeof(t_ray)); rayon->o = vector_new(0, 0, 0); rayon->d = vector_new(0, 0, 0); return (rayon); }
// ---------------------------------------------------------------------------- vertex_buffer_t * vertex_buffer_new( const char *format ) { size_t i, index = 0, stride = 0; const char *start = 0, *end = 0; char *pointer = 0; vertex_buffer_t *self = (vertex_buffer_t *) malloc (sizeof(vertex_buffer_t)); if( !self ) { return NULL; } self->format = strdup( format ); for( i=0; i<MAX_VERTEX_ATTRIBUTE; ++i ) { self->attributes[i] = 0; } start = format; do { end = (char *) (strchr(start+1, ':')); char *desc = 0; if (end == NULL) { desc = strdup(start); } else { desc = strdup(start); desc[end-start] = '0'; } vertex_attribute_t *attribute = vertex_attribute_parse( desc ); start = end+1; free(desc); attribute->pointer = pointer; stride += attribute->size*GL_TYPE_SIZE( attribute->type ); pointer+= attribute->size*GL_TYPE_SIZE( attribute->type ); self->attributes[index] = attribute; index++; } while ( end && (index < MAX_VERTEX_ATTRIBUTE) ); for( i=0; i<index; ++i ) { self->attributes[i]->stride = stride; } self->vertices = vector_new( stride ); self->vertices_id = 0; self->indices = vector_new( sizeof(GLuint) ); self->indices_id = 0; self->dirty = 1; return self; }
btc_tx* btc_tx_new() { btc_tx* tx; tx = calloc(1, sizeof(*tx)); tx->vin = vector_new(8, btc_tx_in_free_cb); tx->vout = vector_new(8, btc_tx_out_free_cb); tx->version = 1; tx->locktime = 0; return tx; }
void module_init() { ModuleList = vector_new(0, sizeof(Module)); ModulePaths = vector_new(0, sizeof(char *)); ModuleSection = config_register_section("module", false); config_register_field(ModuleSection, "path", json_type_string, module_config_set_path); }
END_TEST START_TEST (new_should_return_error_if_invalid_elem_size_or_initial) { vector *v; v = vector_new(0, NULL, 10); fail_unless(v == NULL, "should fail if invalid size"); v = vector_new(sizeof(int), NULL, 0); fail_unless(v == NULL, "should fail if invalid initial"); vector_free(v); }
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 hashset_add(Hashset *self, const char *key) { assert(self); assert(key); u64 hash = _hashset_djb2(key); u32 index = _hashset_index(self, key); Vector *bucket = NULL; if (!self->buckets[index]) { self->buckets[index] = vector_new(8, free); } bucket = self->buckets[index]; for (u32 i = 0; i < bucket->size; ++i) { void *pkvp = vector_index(bucket, i); HashsetNode *kvp = (HashsetNode *)pkvp; // key already exists in hashset, set it to true and return success if (kvp->key == hash) { kvp->value = true; return; } } HashsetNode *kvp = (HashsetNode *)calloc(1, sizeof(HashsetNode)); kvp->key = hash; kvp->value = true; vector_add(bucket, kvp); }
text_buffer_t * text_buffer_new_with_program( size_t depth, GLuint program ) { text_buffer_t *self = (text_buffer_t *) malloc (sizeof(text_buffer_t)); self->buffer = vertex_buffer_new( "vertex:3f,tex_coord:2f,color:4f,ashift:1f,agamma:1f" ); self->manager = font_manager_new( 512, 512, depth ); self->shader = program; self->shader_texture = glGetUniformLocation(self->shader, "tex"); self->shader_pixel = glGetUniformLocation(self->shader, "pixel"); self->line_start = 0; self->line_ascender = 0; self->base_color.r = 0.0; self->base_color.g = 0.0; self->base_color.b = 0.0; self->base_color.a = 1.0; self->line_descender = 0; self->lines = vector_new( sizeof(line_info_t) ); self->bounds.left = 0.0; self->bounds.top = 0.0; self->bounds.width = 0.0; self->bounds.height = 0.0; return self; }
vector_t * vector_deep_copy(vector_t * vector, libcontain_copy_func_t vector_valcopy_func) { size_t i; size_t size; vector_t * retval; void * val; rw_spinlock_read_lock(vector->lock); do { size = vector->size; } while (!size); retval = vector_new(size); if (!retval) goto abort_vector_deep_copy1; for (i = 0; i < size; i++) { val = vector_get(vector, i); if (val != NULL) if (vector_put(retval, i, vector_valcopy_func(vector->nodes[i].val)) != 0) goto abort_vector_deep_copy2; } rw_spinlock_read_unlock(vector->lock); return retval; abort_vector_deep_copy2: vector_free(retval); abort_vector_deep_copy1: rw_spinlock_read_unlock(vector->lock); return NULL; }
vector *ol_bulk_unjar(ol_database *db, const ol_key_array keys, const size_t num_keys) { ol_transaction *tx = NULL; vector *to_return = NULL; check(db != NULL, "Cannot unjar on NULL database."); check((tx = olt_begin(db)) != NULL, "Could not begin transaction."); to_return = vector_new(sizeof(unsigned char *), 256); unsigned int i; for (i = 0; i < num_keys; i++) { const char *key = keys[i]; unsigned char *item = NULL; size_t item_size = 0; olt_unjar(tx, key, strnlen(key, KEY_SIZE), &item, &item_size); if (item != NULL) { vector_append_ptr(to_return, item); } else { vector_append_ptr(to_return, NULL); } } check(olt_commit(tx) == 0, "Could not commit unjar transaction."); return to_return; error: return NULL; }
void ft_set_plan(t_data *data, char **line, int fd) { int cons; char ref; data->origin = '0'; data->color = '0'; data->co = '0'; ref = 0; while (ft_strcmp("#end_object", *line) != 0) { if (ft_strcmp("#const", *line) == 0 && (data->co = '1')) ft_set_plan_const(&cons, line, fd); else if (ft_strcmp("#origin", *line) == 0 && (data->origin = '1')) ft_set_plan_origin(data, line, fd); else if (ft_strcmp("#color", *line) == 0 && (data->color = '1')) ft_set_plan_color(data, line, fd); else if (ft_strcmp("#ref", *line) == 0) ft_set_plan_ref(&ref, line, fd); if (get_next_line(fd, line) <= 0) ft_error("[ERROR OBJECT] - Object plan has no end"); } if (data->origin == '0' || data->co == '0' || data->color == '0') ft_error("[ERROR OBJECT] - Plan's spec missing"); plan_new(vector_new(data->x, data->y, data->z), cons , color_set((int)data->x2, (int)data->y2, (int)data->z2, ref)); }
/* Performs [Q,R] = qr(M,'0') compact QR factorization M is mxn ; Q is mxn ; R is min(m,n) x min(m,n) */ void compact_QR_factorization(mat *M, mat *Q, mat *R){ int i,j,m,n,k; m = M->nrows; n = M->ncols; k = min(m,n); mat *R_full = matrix_new(m,n); matrix_copy(R_full,M); vec *tau = vector_new(m); // get R //LAPACKE_dgeqrf(CblasRowMajor, m, n, R_full->d, n, tau->d); culaDgeqrf(m, n, R_full->d, m, tau->d); for(i=0; i<k; i++){ for(j=0; j<k; j++){ if(j>=i){ matrix_set_element(R,i,j,matrix_get_element(R_full,i,j)); } } } // get Q matrix_copy(Q,R_full); //LAPACKE_dorgqr(CblasRowMajor, m, n, n, Q->d, n, tau->d); culaDorgqr(m, n, n, Q->d, m, tau->d); // clean up matrix_delete(R_full); vector_delete(tau); }
struct vector* vector_normalize(struct vector* v) { struct vector* vnorm = vector_new(v->length); double norm = vector_norm(v); assert(norm != 0); vector_normalize_into(vnorm, v); return vnorm; }
END_TEST START_TEST (delete_element_should_shift_elements) { char *sport1 = strdup("winsurf"); char *sport2 = strdup("kitesurf"); char *sport3 = strdup("motocross"); char *sport4 = strdup("surf"); vector *v = vector_new(sizeof(char *), free_string, 5); vector_append(v, &sport1); vector_append(v, &sport2); vector_append(v, &sport3); vector_append(v, &sport4); fail_unless(vector_delete(v, 1) == VECT_OK); fail_unless(vector_length(v) == 3); char *elem0 = *(char **)vector_get(v, 0); char *elem1 = *(char **)vector_get(v, 1); char *elem2 = *(char **)vector_get(v, 2); fail_unless(elem0 == sport1, "element on `0' should continue there. found '%s'", elem0); fail_unless(elem1 == sport3, "element on `2' should go to `1'. found '%s'", elem1); fail_unless(elem2 == sport4, "element on `3' should go to `2'. found '%s'", elem2); vector_free(v); }
END_TEST START_TEST (append_should_grown_if_needed) { int num1 = 10, num2 = 20, num3 = 30, num4 = 40; int *found1, *found2, *found3, *found4; vector *v = vector_new(sizeof(int *), NULL, 2); vector_append(v, &num1); vector_append(v, &num2); vector_append(v, &num3); /* grow before append */ vector_append(v, &num4); found1 = vector_get(v, 0); found2 = vector_get(v, 1); found3 = vector_get(v, 2); found4 = vector_get(v, 3); fail_unless(vector_length(v) == 4, "length should be 4"); fail_unless(v->alloc_length == 4, ".alloc_length should be 10"); fail_unless(*found1 == 10, "element 0 should be 10 not %d", *found1); fail_unless(*found2 == 20, "element 1 should be 20 not %d", *found2); fail_unless(*found3 == 30, "element 2 should be 30 not %d", *found3); fail_unless(*found4 == 40, "element 3 should be 40 not %d", *found4); vector_free(v); }
struct vector* vector_scalar_multiply(struct vector* v, double s) { struct vector* w = vector_new(v->length); for(int i = 0; i < v->length; i++) { VECTOR_IDX_INTO(w, i) = VECTOR_IDX_INTO(v, i) * s; } return w; }
int main (int argc, char **argv) { int error = 0; Vector *vector = vector_new (); if (vector_load_from_file (vector, argc, argv)) { Stopwatch *stopwatch = stopwatch_new (); uint64_t inversions = 0; stopwatch_start (stopwatch); inversions = count_inversions (vector); stopwatch_stop (stopwatch); printf ("%llu inversions of %zu numbers in %llu.%03u ms.\n", inversions, vector->length, stopwatch->elapsed, stopwatch->usec); stopwatch_free (stopwatch); } else { error = 1; } vector_free (vector); return error; }
/* ------------------------------------------------------------------------- */ TextureAtlas * texture_atlas_new( size_t width, size_t height, size_t depth ) { assert( (depth == 1) || (depth == 3) ); TextureAtlas *self = (TextureAtlas *) malloc( sizeof(TextureAtlas) ); if( !self ) { return NULL; } self->nodes = vector_new( sizeof(Node) ); self->used = 0; self->width = width; self->height = height; self->depth = depth; Node node = {0,0,width}; vector_push_back( self->nodes, &node ); self->texid = 0; self->data = (unsigned char *) calloc( width*height*depth, sizeof(unsigned char) ); // This is a special region that is used for background and underlined // decorations of glyphs int n = 4; unsigned char buffer[n*n]; memset(buffer, 255, n*n); Region1 r = texture_atlas_get_region( self, n, n ); texture_atlas_set_region( self, r.x, r.y, r.width, r.height, buffer, 1); self->black.x = r.x + 1; self->black.y = r.y + 1; self->black.width = r.width - 2; self->black.height= r.height - 2; return self; }
int main(int argc, char *argv[]) { Point myPoint = point_new(0.0, 0.0); Vector unitX = vector_new(1.0, 0.0); Vector unitY = vector_new(0.0, 1.0); printf("%s + %s + %s = ", point_toString(myPoint), vector_toString(unitX), vector_toString(unitY) ); point_addVector(&myPoint, unitX); point_addVector(&myPoint, unitY); printf("%s\n", point_toString(myPoint)); return 0; }
void test_sort() { struct vector* v = vector_new(7, sizeof(struct test)); struct test* t = NULL; t = vector_get(v,0); t->id = 4; t = vector_get(v,1); t->id = 1; t = vector_get(v,2); t->id = 2; t = vector_get(v,3); t->id = 0; t = vector_get(v,4); t->id = 3; t = vector_get(v,5); t->id = 6; t = vector_get(v,6); t->id = 5; vector_sort(v, &compare_test); size_t i=0; struct test* iter = vector_front(v); struct test* end = vector_end(v); while (iter != end) { assert(iter->id == i); iter++; i++; } }
struct point_hash* point_hash_new(int64 hpixid) { struct point_hash* pth = alloc_or_die(sizeof(struct point_hash),"hash entry"); pth->points = vector_new(0,sizeof(struct point*)); pth->hpixid=hpixid; return pth; }
/* allocates the vector, and wraps it up into a data structure that ruby can understand */ static VALUE vector_alloc(VALUE klass) { Vector *vector; VALUE obj; vector = vector_new(); obj = Data_Wrap_Struct(klass, 0, vector_free, vector); return obj; }
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); }
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; }
// ------------------------------------------------------ texture_glyph_new --- texture_glyph_t * texture_glyph_new( void ) { texture_glyph_t *self = (texture_glyph_t *) malloc( sizeof(texture_glyph_t) ); if( self == NULL) { fprintf( stderr, "line %d: No more memory for allocating data\n", __LINE__ ); exit( EXIT_FAILURE ); } self->id = 0; self->width = 0; self->height = 0; self->outline_type = 0; self->outline_thickness = 0.0; self->offset_x = 0; self->offset_y = 0; self->advance_x = 0.0; self->advance_y = 0.0; self->s0 = 0.0; self->t0 = 0.0; self->s1 = 0.0; self->t1 = 0.0; self->kerning = vector_new( sizeof(kerning_t) ); return self; }
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); }