//TODO: Do we care that this can fail? void stack_push(struct stack *stack, void *elem) { if (!stack || !elem) return; if (stack->index == stack->vec.len) { vector_resize(&stack->vec, stack->index * 2); } vector_set(&stack->vec, stack->index, elem); stack->index++; }
/* Passive interface. */ static int ripng_passive_interface_set (struct vty *vty, const char *ifname) { if (ripng_passive_interface_lookup (ifname) >= 0) return CMD_WARNING; vector_set (Vripng_passive_interface, strdup (ifname)); ripng_passive_interface_apply_all (); return CMD_SUCCESS; }
static void do_normal(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2, GLfloat x3, GLfloat y3, GLfloat z3) { plane plane; vector n; vector_set(&plane.p1, x1, y1, z1); vector_set(&plane.p2, x2, y2, z2); vector_set(&plane.p3, x3, y3, z3); plane_normal(plane, &n); n.x = -n.x; n.y = -n.y; n.z = -n.z; glNormal3f(n.x, n.y, n.z); #ifdef DEBUG /* Draw a line in the direction of this face's normal. */ { GLfloat ax = n.x > 0 ? n.x : -n.x; GLfloat ay = n.y > 0 ? n.y : -n.y; GLfloat az = n.z > 0 ? n.z : -n.z; GLfloat mx = (x1 + x2 + x3) / 3; GLfloat my = (y1 + y2 + y3) / 3; GLfloat mz = (z1 + z2 + z3) / 3; GLfloat xx, yy, zz; GLfloat max = ax > ay ? ax : ay; if (az > max) max = az; max *= 2; xx = n.x / max; yy = n.y / max; zz = n.z / max; glBegin(GL_LINE_LOOP); glVertex3f(mx, my, mz); glVertex3f(mx+xx, my+yy, mz+zz); glEnd(); } #endif /* DEBUG */ }
/* Standard C does not have a procedure to get the modification time of a file, but if stat() exists we can use it. If not, return a vector containing midnight, January 1, 1970 always. This is consistent with the result returned by osdep_access(), below. */ void osdep_mtime( word w_fn, word w_buf ) { int r = 0; struct tm *tm; #ifdef HAVE_STAT struct stat s; const char *fn = string2asciiz( w_fn ); r = stat( fn, &s ); #else struct { time_t st_mtime; } s; s.st_mtime = 0; #endif if (r != 0) { globals[ G_RESULT ] = fixnum(-1); return; } tm = localtime( &s.st_mtime ); vector_set( w_buf, 0, fixnum( tm->tm_year + 1900 ) ); vector_set( w_buf, 1, fixnum( tm->tm_mon + 1 ) ); vector_set( w_buf, 2, fixnum( tm->tm_mday ) ); vector_set( w_buf, 3, fixnum( tm->tm_hour ) ); vector_set( w_buf, 4, fixnum( tm->tm_min ) ); vector_set( w_buf, 5, fixnum( tm->tm_sec ) ); globals[ G_RESULT ] = fixnum( 0 ); }
int leFichMusica(char * nome_ficheiro, vector_t ** notas, vector_t ** tempos) { // le a sequencia de notas e duracao (melodia+ritmo) de um ficheiro de texto com o // formato: // // nota1 tempo1 // nota2 tempo2 // ... // Retorna em *notas e *tempos ponteiros para vectores com notas e tempos. // int freq, tempo, num=0; FILE *fid; char str[32]; char *r; printf("Le musica no ficheiro '%s'\n", nome_ficheiro); fid=fopen(nome_ficheiro, "r"); *notas = vector_new(); *tempos = vector_new(); if (fid!=NULL && *notas!=NULL && *tempos!=NULL) { do{ r=fgets(str,32,fid); if (r!=NULL) { sscanf(str,"%d %d", &freq, &tempo); //leitura do par (frequencia,tempo) printf(" Leitura: nota = %d\ttempo = %d.\n", freq, tempo); vector_set(*notas, num, freq); vector_set(*tempos, num++, tempo); } }while (r!=NULL); printf("Numero de notas musicais lidas = %d.\n\n",num); fclose(fid); return num; } else { printf("Erro ao abrir o arquivo de dados ou ao alocar memória!\n\n"); exit(-1); } } // leFichMusica
int vector_push_back(struct vector *vector, void *data) { if (vector->size >= vector->capacity) { if (vector_realloc(vector) < 0) { _ERROR("%s: out of memory reallocating vector array to %zu items.\n", __FUNCTION__, vector->capacity); return -1; } } vector_set(vector, vector->size++, data); return 0; }
/* returns the joint second anchoring point */ void joint_anchor2 (t_real *anchor, dJointID j) { dVector3 joint_position; int joint_type; joint_type = dJointGetType (j); if (joint_type == dJointTypeBall) { dJointGetBallAnchor2 (j, joint_position); } else { err_message ("Unrecognized joint type\n"); exit (EXIT_FAILURE); } vector_set (anchor, joint_position [0], joint_position [1], joint_position [2]); }
char *test_set() { vector_p vector = vector_create(sizeof(int)); vector_add(vector, test_data(1)); int set = vector_set(vector, 0, test_data(2)); mu_assert(*(int*)vector_get(vector, 0) == 2, "expected 2"); mu_assert(set, "expected to have set"); vector_free(vector); return NULL; }
/* Add interface to stp_enable_if. */ static int stp_enable_if_add (const char *ifname) { int ret; ret = stp_enable_if_lookup (ifname); if (ret >= 0) return -1; vector_set (stp_enable_interface, strdup (ifname)); return 1; }
/** * Calculate the minimum bounding box. * \todo Better comparison of floats? */ static void get_bounding_box(const struct transform *t, struct bounding_box *b){ vector_t transformedNormal = vector_transform_direction(PLANE_NORMAL, t); vector_t transformedPoint = vector_transform(vector_set(0, 0, 0), t); for(int i = 0; i < 3; ++i){ if(transformedNormal.f[(i + 1) % 3] == 0 && transformedNormal.f[(i + 2) % 3] == 0){ b->p[0].f[i] = transformedPoint.f[i]; b->p[1].f[i] = transformedPoint.f[i]; }else{ b->p[0].f[i] = -INFINITY; b->p[1].f[i] = INFINITY; } } }
void vect_normal(t_vector *normal, t_vector *impact, void *object) { if (type_object(object) == 's') { vector_sub_assoc(normal, impact, ((t_sphere *)object)->pos); } if (type_object(object) == 'p') { vector_set_copy(normal, ((t_plan *)object)->normal); vector_mult(normal, normal, -1); } if (type_object(object) == 'c') { vector_set(normal, impact->x - ((t_cylind *)object)->pos->x, 0 , impact->z - ((t_cylind *)object)->pos->z); } if (type_object(object) == 'o') { vector_set(normal, impact->x - ((t_cone *)object)->pos->x , 0, impact->z - ((t_cone *)object)->pos->z); } vector_normalize(normal); }
vector_t* vector_from_array(array_t* input) { vector_t* output=vector_new(input->length); output->length=input->length; arrayelement_t* element=input->first; size_t i=0; while(element) { vector_set(output,i,element->data); element=element->next; i++; } output->length=input->length; return output; }
/* Add interface to ripng_enable_if. */ static int ripng_enable_if_add (const char *ifname) { int ret; ret = ripng_enable_if_lookup (ifname); if (ret >= 0) return -1; vector_set (ripng_enable_if, strdup (ifname)); ripng_enable_apply_all(); return 1; }
monomial *compute_m_of_x_xor_a (monomial *m, bitvector *a) { uscalar_t i, length; length = vector_length (m); for (i = 0; i < length; i++) { lit l; l = (lit) vector_get (m, i); assert (boolformula_var_from_lit (l) < bitvector_length (a)); if (bitvector_get (a, boolformula_var_from_lit (l))) { l = boolformula_lit_complement (l); } vector_set (m, i, (pointer_t) l); } return m; }
void Tri::Set_field(char *string){ register int i; int qt; Coord X; Vert *v; /* find max quadrature points*/ qt = QGmax*QGmax; vector_def("x y",string); X.x = dvector(0,qt-1); X.y = dvector(0,qt-1); qt = qa*qb; v = vert; this->coord(&X); for(i = 0;i < Nverts;++i) vector_set(1,&v[i].x,&v[i].y,v[i].hj); vector_set(qt,X.x,X.y,h[0]); state = 'p'; free(X.x); free(X.y); }
obj_t make_vector_from_list(obj_t list) { obj_t p = list; size_t i, size = 0; while (!is_null(p)) { size++; p = pair_cdr(p); } obj_t vec = make_vector_uninitialized(size); for (i = 0, p = list; i < size; i++) { vector_set(vec, i, pair_car(p)); p = pair_cdr(p); } return vec; }
static conjunction *get_conjecture (vector *bases) { conjunction *conjecture; uscalar_t i, length; length = vector_length (bases); conjecture = cdnfformula_conjunction_new (length); for (i = 0; i < length; i++) { disjunction *H; H = ((basis *)vector_get (bases, i))->H; vector_set (conjecture, i, H); } return conjecture; }
/* Add interface to rip_enable_if. */ static int rip_enable_if_add (const char *ifname) { int ret; ret = rip_enable_if_lookup (ifname); if (ret >= 0) return -1; vector_set (rip_enable_interface, strdup (ifname)); rip_enable_apply_all(); /* TODOVJ */ return 1; }
/* Breaking up string into each command piece. I assume given character is separated by a space character. Return value is a vector which includes char ** data element. */ vector cmd_make_strvec (const char *string) { const char *cp, *start; char *token; int strlen; vector strvec; if (string == NULL) return NULL; cp = string; /* Skip white spaces. */ while (isspace ((int) *cp) && *cp != '\0') cp++; /* Return if there is only white spaces */ if (*cp == '\0') return NULL; if (*cp == '!' || *cp == '#') return NULL; /* Prepare return vector. */ strvec = vector_init (VECTOR_MIN_SIZE); /* Copy each command piece and set into vector. */ while (1) { start = cp; while (!(isspace ((int) *cp) || *cp == '\r' || *cp == '\n') && *cp != '\0') cp++; strlen = cp - start; token = calloc(1, sizeof(char) * (strlen + 1)); memcpy (token, start, strlen); *(token + strlen) = '\0'; vector_set (strvec, token); while ((isspace ((int) *cp) || *cp == '\n' || *cp == '\r') && *cp != '\0') cp++; if (*cp == '\0') return strvec; } }
int main(int argc, char **argv) { vector_t *v; printf("Calling vector_new()\n"); v = vector_new(); printf("Calling vector_delete()\n"); vector_delete(v); printf("vector_new() again\n"); v = vector_new(); printf("These should all return 0 (vector_get()): "); printf("%d ", vector_get(v, 0)); printf("%d ", vector_get(v, 1)); printf("%d\n", vector_get(v, 2)); printf("Doing a bunch of vector_set()s\n"); vector_set(v, 0, 98); vector_set(v, 11, 15); vector_set(v, 15, -23); vector_set(v, 24, 65); vector_set(v, 500, 3); vector_set(v, 12, -123); vector_set(v, 15, 21); vector_set(v, 25, 43); printf("These should be equal:\n"); printf("98 = %d\n", vector_get(v, 0)); printf("15 = %d\n", vector_get(v, 11)); printf("65 = %d\n", vector_get(v, 24)); printf("-123 = %d\n", vector_get(v, 12)); printf("21 = %d\n", vector_get(v, 15)); printf("43 = %d\n", vector_get(v, 25)); printf("0 = %d\n", vector_get(v, 23)); printf("0 = %d\n", vector_get(v, 1)); printf("0 = %d\n", vector_get(v, 501)); printf("3 = %d\n", vector_get(v, 500)); vector_delete(v); printf("Test complete.\n"); return 0; }
static void init_rec(rec_descriptor_t *desc) { /* Count the fields. */ const field_descriptor_t *fields = desc->rd_fields; size_t i; for (i = 0; ; i++) { field_mutability_t m = fields[i].fd_mutability; if (m == FM_END) break; assert(m == FM_MUTABLE || m == FM_IMMUTABLE); } size_t field_count = i; obj_t field_vec = make_vector_uninitialized(field_count); for (i = 0; i < field_count; i++) { field_mutability_t m = fields[i].fd_mutability; obj_t msym = make_symbol_from_C_str(m == FM_MUTABLE ? L"mutable" : L"immutable"); obj_t nsym = make_symbol_from_C_str(fields[i].fd_name); vector_set(field_vec, i, CONS(msym, CONS(nsym, EMPTY_LIST))); } /* Construct the rtd. */ obj_t nsym = make_symbol_from_C_str(desc->rd_name); obj_t parent = desc->rd_parent ? *desc->rd_parent : FALSE_OBJ; obj_t rtd = make_rtd(desc->rd_flags, nsym, parent, FALSE_OBJ, FALSE_OBJ, field_vec); *desc->rd_root = rtd; if (desc->rd_flags & RF_OPAQUE) { /* Do not create bindings for an opaque record type. */ return; } /* Bind the rtd to name. */ obj_t env = root_environment(); env_bind(env, nsym, BT_LEXICAL, M_IMMUTABLE, rtd); /* Construct the constructor descriptor, constructor, predicate, * field accessors, and field mutators. */ // XXX implement me. Maybe the rest should be in Scheme. }
/* Build a vector from a list. XXX move this to obj_vector.c. */ static obj_t *build_vector(obj_t *list) { PUSH_ROOT(list); obj_t *p = list; size_t i, size = 0; while (!is_null(p)) { size++; p = pair_cdr(p); } AUTO_ROOT(vec, make_vector(size, NIL)); for (i = 0, p = list; i < size; i++) { vector_set(vec, i, pair_car(p)); p = pair_cdr(p); } POP_FUNCTION_ROOTS(); return vec; }
//one arg: arglst cellpoint vector(void) { int len, i=0; args_push(args_ref(1)); len = get_integer(list_len()); reg = make_vector(len, NIL); stack_push(&vars_stack, args_ref(1)); while (is_false(is_null(stack_top(&vars_stack)))){ vector_set(reg, i, car(stack_top(&vars_stack))); ++i; stack_push(&vars_stack, cdr(stack_pop(&vars_stack))); } stack_pop(&vars_stack); args_pop(1); return reg; }
int sem_init(int start_val) { int init = -1; if(checkEmpty(&sem_stack)){ init = vector_add(&sem_list, &start_val); } else{ // use top stack value init = pop(&sem_stack); vector_set(&sem_list, init, &start_val); } return init; // I'm not sure where we should return the ENOMEM error val.. // ..probably in the vector.c file }
/* Generic CLI Installation. */ int cli_install_gen (struct cli_tree *ctree, int mode, u_char privilege, u_int16_t flags, struct cli_element *cel) { struct cli_builder cb; struct cli_node *node; vector parent; int index, max; /* Set flags. */ if (flags) SET_FLAG (cel->flags, flags); /* Check help string is there. */ cli_check_help (cel, &index, &max); if (mode > MAX_MODE) return -1; /* Lookup root node. */ node = vector_lookup_index (ctree->modes, mode); /* Install a new root node. */ if (! node) { node = cli_node_new (); vector_set_index (ctree->modes, mode, node); } /* Update IFNAME token and help string. */ if (ifname_expand_token) cli_ifname_reflect (cel, index, max); /* Set initial value before calling cli_build(). */ parent = vector_init (VECTOR_MIN_SIZE); vector_set (parent, node); cb.str = cel->str; cb.index = 0; cli_build (parent, NULL, NULL, &cb, cel, privilege, 0); vector_free (parent); return 0; }
void BackgroundSystem::init(Propmap* properties) { vector_set(light, 0.7, 0.9, -0.3); light[3] = 0.0f; heightshift = fmax(-20, fmin(properties->getProperty("backscape.heightshift", 0.0f), 0)); windspeed = fmax(0, fmin(properties->getProperty("backscape.windspeed", 0.0f), 10000)); raininess = fmax(0, fmin(properties->getProperty("backscape.raininess", 0.0f), 1)); cloudiness = fmax(0, fmin(properties->getProperty("backscape.cloudiness", 0.0f), 1)); dustiness = fmax(0, fmin(properties->getProperty("backscape.dustiness", 0.0f), 1)); hour = (World::getInstance() == NULL) ? 0 : World::getInstance()->getTiming()->getTime24(); if (textures.empty()) { initTextures(); } seed = 131; }
// TKE interface for isv enclaves sgx_status_t SGXAPI sgx_ra_close( sgx_ra_context_t context) { if(vector_size(&g_ra_db) <= context) return SGX_ERROR_INVALID_PARAMETER; ra_db_item_t* item = NULL; if(0 != vector_get(&g_ra_db, context, reinterpret_cast<void**>(&item)) || item == NULL ) return SGX_ERROR_INVALID_PARAMETER; sgx_spin_lock(&g_ra_db_lock); //safe clear private key and RA key before free memory to defense in depth memset_s(&item->a,sizeof(item->a),0,sizeof(sgx_ec256_private_t)); memset_s(&item->vk_key,sizeof(item->vk_key),0,sizeof(sgx_ec_key_128bit_t)); memset_s(&item->mk_key,sizeof(item->mk_key),0,sizeof(sgx_ec_key_128bit_t)); memset_s(&item->sk_key,sizeof(item->sk_key),0,sizeof(sgx_ec_key_128bit_t)); memset_s(&item->smk_key,sizeof(item->smk_key),0,sizeof(sgx_ec_key_128bit_t)); SAFE_FREE(item); vector_set(&g_ra_db, context, NULL); sgx_spin_unlock(&g_ra_db_lock); return SGX_SUCCESS; }
int sem_release(int sem_num) { int* release = vector_get(&sem_list, sem_num); if(release == NULL){ return EINVAL; } // if there are processes waiting for this semaphore, then it is still in use if( (Qvector_get(&waiting_list, sem_num))->front != NULL){ return EINUSE; } else{ vector_set(&sem_list, sem_num, NULL); // set to inactive state (&sem_list)->count -= 1; // decrease vector count push(&sem_stack, sem_num); // add sem_num to stack } return OK; }
void test_create_and_access() { size_t n=10; struct vector* v = vector_new(n, sizeof(struct test)); assert(v->size == n); assert(n == vector_size(v)); assert(v->capacity == n); struct test* iter = vector_front(v); struct test* end = vector_end(v); size_t i=0; while (iter != end) { iter->id = i; iter->x = 2*i; wlog(" id: %d x: %g\n", iter->id, iter->x); iter++; i++; } iter = vector_front(v); i=0; while (iter != end) { assert(iter->id == i); assert(iter->x == 2*i); iter++; i++; } i=7; struct test t; t.id = 57; t.x = -8.2457; vector_set(v, i, &t); struct test* el = vector_get(v, i); assert(el->id == t.id); assert(el->x == t.x); v = vector_delete(v); assert(v == NULL); }
// ---------------------------------------------------------- vector_insert --- void vector_insert( vector_t *self, const size_t index, const void *item ) { assert( self ); assert( index <= self->size); if( self->capacity <= self->size ) { vector_reserve(self, 2 * self->capacity ); } if( index < self->size ) { memmove( (char *)(self->items) + (index + 1) * self->item_size, (char *)(self->items) + (index + 0) * self->item_size, (self->size - index) * self->item_size); } self->size++; vector_set( self, index, item ); }