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++; } }
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_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); }
void test_front_and_back() { vector* v = create_test_vector(); int* first_element = (int*)vector_front(v); int* last_element = (int*)vector_back(v); printf("First element = %i\n", *first_element); printf("Last element = %i\n", *last_element); vector_free(v, NULL); }
void test_vector() { size_t arr[10], arr2[10]; size_t i, j; vector vec; j = 200; vector_init(&vec, &malloc, &free); for (i = 0; i < 10; ++i) { arr[i] = i; vector_push_back(&vec, &arr[i]); } for (i = 0; i < 10; ++i) { arr2[i] = i + 10; vector_push_back(&vec, &arr2[i]); } vector_insert(&vec, &j, vec.size); vector_remove(&vec, 0); printf("-----\nVector testing\n-----\nSize: %d\nMax size: %d\nContents: ", vector_size(&vec), vector_max_size(&vec)); for (i = 0; i < 20; ++i) printf("%d ", *(size_t*)vector_pop_back(&vec)); vector_push_back(&vec, &j); printf("\nFront: %d\nBack: %d\n", *(size_t*)vector_front(&vec), *(size_t*)vector_back(&vec)); vector_clear(&vec); printf("Vector cleared\nSize: %d\n", vec.size); vector_free(&vec); printf("\n"); }
void test_ptr() { struct vector* v = vector_new(0, sizeof(struct test*)); size_t i=0, n=10; // note we never own the pointers in the vector! So we must allocat and // free them separately struct test* tvec = calloc(n, sizeof(struct test)); for (i=0; i<n; i++) { struct test *t = &tvec[i]; t->id = i; t->x = 2*i; // this copies the pointer to t vector_push(v, &t); } // two different ways to use vector_get for pointers for (i=0; i<n; i++) { struct test **t = vector_get(v, i); assert((*t)->id == i); assert((*t)->x == 2*i); } for (i=0; i<n; i++) { struct test *t = *(struct test**) vector_get(v, i); assert(t->id == i); assert(t->x == 2*i); } // iteration i=0; struct test **iter = vector_front(v); struct test **end = vector_end(v); while (iter != end) { assert((*iter)->id == i); iter++; i++; } v=vector_delete(v); assert(v==NULL); free(tvec); }
void cat_match(struct cat* self, double ra, double dec, struct vector* matches) // vector of struct match { double x=0,y=0,z=0; int64 hpixid = hpix_eq2pix(self->hpix, ra, dec); vector_resize(matches,0); struct match match; struct point_hash* pthash = point_hash_find(self->pthash, hpixid); if (pthash != NULL) { hpix_eq2xyz(ra,dec,&x,&y,&z); struct point **ptp = vector_front(pthash->points); struct point **end = vector_end(pthash->points); while (ptp != end) { double cos_angle = (*ptp)->x*x + (*ptp)->y*y + (*ptp)->z*z; if (cos_angle > (*ptp)->cos_radius) { match.index = (*ptp)->index; match.cos_dist = cos_angle; // copies data, not pointer vector_push(matches, &match); } ptp++; } } return; }
struct cat* _cat_read(FILE* fptr, size_t nlines, int64 nside, double radius_arcsec, int verbose) { int radius_in_file=0; double ra=0, dec=0; double radius_radians=0; double cos_radius_global=0; struct vector* listpix = vector_new(0,sizeof(int64)); size_t index=0; int barsize=70; struct cat* self = cat_new(nlines, nside); if (verbose) { wlog(" reading and building hash table\n"); repeat_char('.', barsize); wlog("\n"); } radius_in_file = _cat_get_radius(radius_arcsec,&radius_radians, &cos_radius_global); struct point_hash* pthash = NULL; struct point* pt = &self->pts[0]; for (size_t i=0; i<self->size; i++) { pt->index=index; if (2 != fscanf(fptr, "%lf %lf", &ra, &dec)) { wlog("expected to read point at line %lu\n", i); exit(EXIT_FAILURE); } if (radius_in_file) { if (1 != fscanf(fptr, "%lf", &radius_arcsec)) { wlog("expected to read radius at line %lu\n", i); exit(EXIT_FAILURE); } radius_radians = radius_arcsec/3600.*D2R; pt->cos_radius = cos(radius_radians); } else { pt->cos_radius = cos_radius_global; } hpix_eq2xyz(ra,dec,&pt->x,&pt->y,&pt->z); hpix_disc_intersect(self->hpix, pt->x, pt->y, pt->z, radius_radians, listpix); // insert a pointer to this point for each pixel it intersected int64* pix_ptr = vector_front(listpix); int64* end = vector_end(listpix); while (pix_ptr != end) { pthash=point_hash_insert(pthash, (*pix_ptr), pt); pix_ptr++; } pt++; index++; if (verbose) incr_bar(i+1, self->size, barsize, '='); } self->pthash=pthash; listpix=vector_delete(listpix); if (verbose) wlog("\n"); if (index != self->size) { wlog("expected %lu lines but read %lu\n", self->size, index); exit(EXIT_FAILURE); } return self; }
void* priority_queue_top(const priority_queue_t* cpt_pqueue) { assert(cpt_pqueue != NULL); return vector_front(&cpt_pqueue->_t_vector); }