static int cluster_update(array_t *clusters) { int k = array_length(clusters); /* update clusters */ for (int j=0; j<k; j++) { point_t sum={0,0}; cluster_t *cl = array_at(clusters, j); point_t old_c = cl->centroid; int cl_size = array_length(cl->points); /* sum all points in this cluster */ for (int i=0; i<cl_size; i++) { point_t *p = array_at(cl->points, i); sum.x += p->x; sum.y += p->y; } cl->centroid.x = sum.x/cl_size; cl->centroid.y = sum.y/cl_size; if (point_equal(&cl->centroid, &old_c)) { /* cluster centroid didn't move */ return 0; } } return 1; }
void _hashtable_rehash(HASHTABLE *t, size_t newsize) { ARRAY table; DLIST *bucket; size_t i, j, hash; DLIST_ITER it, end; array_init(&table, sizeof(DLIST)); array_resize(&table, newsize); for (i = 0; i != newsize; ++i) { dlist_init((DLIST*)array_at(&table, i), t->element_size); } j = array_size(&t->table); for (i = 0; i != j; ++i) { bucket = (DLIST*)array_at(&t->table, i); if (dlist_size(bucket)) { end = dlist_end(bucket); for (it = dlist_begin(bucket); it != end; it = dlist_next(it)) { hash = _hashtable_hash(t, dlist_at(it)) % newsize; dlist_push((DLIST*)array_at(&table, hash), dlist_at(it)); } } } array_destroy(&t->table); memcpy(&t->table, &table, sizeof(ARRAY)); }
int main(){ Array a = array_create(100); printf("array size=%d\n",array_size(&a)); *array_at(&a,0)=10; printf("%d",*array_at(&a,0)); array_free(&a); }
char *irc_say(array *args, struct irc_message *msg) { int i; if (array_count(args) < 2) return NULL; fprintf(irc, "PRIVMSG %s :", *(char**)array_at(args, 0)); for (i = 1; i < array_count(args); ++i) { fprintf(irc, "%s ", *(char**)array_at(args, i)); } fputs("\r\n", irc); return NULL; }
void * minheap_pop(minheap_t *heap) { void * root; if (heap->len == 0) return NULL; heap->swp(array_at(heap->array, 0), array_at(heap->array, heap->len-1)); heap->len--; shiftup(heap, 0); root = array_at(heap->array, heap->len); /* note the previous first elm has been swapped to here. */ return root; }
int main() { CArray array; array_initial(&array); array_recap(&array, 10); assert(array_capacity(&array) == 10); ////////////////////////////////////////////////////////////////////////// for (int i = 0; i < 20; ++i) { array_append(&array, i); } assert(array_size(&array) == 20); // printarray(&array); for (int i = 0; i < array_size(&array); ++i) { assert(*array_at(&array, i) == i); } ////////////////////////////////////////////////////////////////////////// CArray array2, array3; array_initial(&array2); array_initial(&array3); array_copy(&array, &array2); assert(array_compare(&array, &array2) == true); array_copy(&array, &array3); assert(array_compare(&array, &array3) == true); // printarray(&array2); ////////////////////////////////////////////////////////////////////////// array_insert(&array2, 2, 3); assert(array_compare(&array, &array2) == false); // printarray(&array2); ////////////////////////////////////////////////////////////////////////// *array_at(&array3, 2) = 5; assert(array_compare(&array, &array3) == false); // printarray(&array3); ////////////////////////////////////////////////////////////////////////// array_destroy(&array); array_destroy(&array2); array_destroy(&array3); return 0; }
static mp_obj_t py_image_draw_keypoints(mp_obj_t image_obj, mp_obj_t kpts_obj) { image_t *image = NULL; py_kp_obj_t *kpts=NULL; /* get pointer */ image = py_image_cobj(image_obj); kpts = (py_kp_obj_t*)kpts_obj; /* sanity checks */ PY_ASSERT_TRUE_MSG(image->bpp == 1, "This function is only supported on GRAYSCALE images"); PY_ASSERT_TYPE(kpts_obj, &py_kp_type); color_t cl = {.r=0xFF, .g=0xFF, .b=0xFF}; for (int i=0; i<kpts->size; i++) { kp_t *kp = &kpts->kpts[i]; float co = arm_cos_f32(kp->angle); float si = arm_sin_f32(kp->angle); imlib_draw_line(image, kp->x, kp->y, kp->x+(co*10), kp->y+(si*10)); imlib_draw_circle(image, kp->x, kp->y, 4, &cl); } return mp_const_none; } static mp_obj_t py_image_find_blobs(mp_obj_t image_obj) { /* C stuff */ array_t *blobs; struct image *image; mp_obj_t blob_obj[6]; /* MP List */ mp_obj_t objects_list = mp_const_none; /* get image pointer */ image = py_image_cobj(image_obj); /* run color dector */ blobs = imlib_count_blobs(image); /* Create empty Python list */ objects_list = mp_obj_new_list(0, NULL); if (array_length(blobs)) { for (int j=0; j<array_length(blobs); j++) { blob_t *r = array_at(blobs, j); blob_obj[0] = mp_obj_new_int(r->x); blob_obj[1] = mp_obj_new_int(r->y); blob_obj[2] = mp_obj_new_int(r->w); blob_obj[3] = mp_obj_new_int(r->h); blob_obj[4] = mp_obj_new_int(r->c); blob_obj[5] = mp_obj_new_int(r->id); mp_obj_list_append(objects_list, mp_obj_new_tuple(6, blob_obj)); } } array_free(blobs); return objects_list; }
void mesh_delete(mesh_t *mesh) { // Delete the vertex & index buffer object glDeleteBuffers(1, &mesh->vbo); glDeleteBuffers(1, &mesh->ibo); // Delete the vertex attribute array array_delete(mesh->vattributes); // Delete the index array array_delete(mesh->indices); // Delete all the material groups size_t size = array_size(mesh->mtl_grps); for(uint64_t i = 0; i < size; i++) { material_group_t *grp = (material_group_t*)array_at(mesh->mtl_grps, i); // Delete the texture if it exists glDeleteTextures(1, &grp->mtl.tex.texID); } // Delete the material group array array_delete(mesh->mtl_grps); // Finally delete the vertex array object glDeleteVertexArrays(1, &mesh->vao); }
// update the coordinate from local to world void sprite_update_transform(struct sprite* self) { if (self->dirty) { for (int i = 0; i < array_size(self->children); ++i) { struct sprite* child = (struct sprite*)array_at(self->children, i); child->dirty = 1; } struct affine* local = &self->local_srt; af_srt(local, self->x, self->y, self->scale_x, self->scale_y, self->rotation); struct affine tmp; af_identify(&tmp); af_concat(&tmp, &self->local_srt); if (self->parent) { af_concat(&tmp, &(self->parent->world_srt)); } float left = tmp.x; float right = tmp.x + self->width; float bottom = tmp.y; float top = tmp.y + self->height; struct glyph* g = &self->glyph; SET_VERTEX_POS(g->bl, left, bottom); SET_VERTEX_POS(g->br, right, bottom); SET_VERTEX_POS(g->tl, left, top); SET_VERTEX_POS(g->tr, right, top); self->dirty = 0; self->world_srt = tmp; } }
Eval prim_array_ref(Expr args) { Array a = take_typed(&args, ARRAY).array; Expr idx = take_typed(&args, NUMBER); take_nil(args); return final_eval(*array_at(a,idx.num)); }
static void shiftdown(minheap_t *heap, long start, long end) { void *child, *parent; long i; /* index for the parent */ i = end; while (end > start) { child = array_at(heap->array, i); i = (end - 1) >> 1; parent = array_at(heap->array, i); if (heap->comp(child, parent) < 0) { heap->swp(child, parent); end = i; } else break; } }
char *factoid_get(array *args, struct irc_message *msg) { const char *value; if (array_count(args) < 1) return NULL; value = strmap_find(&factoids, *(char**)array_at(args, 0)); if (value == NULL) return NULL; return new_string(value); }
void server_free(server_t *server) { if (!server) return; int i; for (i = 0; i < server->receivers->size; i++) { receiver_free(array_at(server->receivers, i)); } for (i = 0; i < server->workers->size; i++) { worker_free(array_at(server->workers, i)); } conn_free(server->conn); array_free(server->endpoints); array_free(server->receivers); array_free(server->workers); free(server); }
static mp_obj_t py_image_find_features(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { struct image *image = NULL; struct cascade *cascade = NULL; array_t *objects_array=NULL; mp_obj_t objects_list = mp_const_none; /* sanity checks */ PY_ASSERT_TRUE_MSG(sensor.pixformat == PIXFORMAT_GRAYSCALE, "This function is only supported on GRAYSCALE images"); PY_ASSERT_TRUE_MSG(sensor.framesize <= OMV_MAX_INT_FRAME, "This function is only supported on "OMV_MAX_INT_FRAME_STR" and smaller frames"); /* read arguments */ image = py_image_cobj(args[0]); cascade = py_cascade_cobj(args[1]); /* set some defaults */ cascade->threshold = 0.65f; cascade->scale_factor = 1.65f; /* read kw args */ mp_map_elem_t *kw_thresh = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(qstr_from_str("threshold")), MP_MAP_LOOKUP); if (kw_thresh != NULL) { cascade->threshold = mp_obj_get_float(kw_thresh->value); } mp_map_elem_t *kw_scalef = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(qstr_from_str("scale")), MP_MAP_LOOKUP); if (kw_scalef != NULL) { cascade->scale_factor = mp_obj_get_float(kw_scalef->value); } /* Detect objects */ objects_array = imlib_detect_objects(image, cascade); /* Create empty Python list */ objects_list = mp_obj_new_list(0, NULL); /* Add detected objects to the list */ for (int i=0; i<array_length(objects_array); i++) { struct rectangle *r = array_at(objects_array, i); mp_obj_t rec_obj[4] = { mp_obj_new_int(r->x), mp_obj_new_int(r->y), mp_obj_new_int(r->w), mp_obj_new_int(r->h), }; mp_obj_list_append(objects_list, mp_obj_new_tuple(4, rec_obj)); } /* Free the objects array */ array_free(objects_array); return objects_list; }
void hashtable_destroy(HASHTABLE *t) { size_t i, j; j = array_size(&t->table); for (i = 0; i != j; ++i) { dlist_destroy((DLIST*)array_at(&t->table, i)); } array_destroy(&t->table); }
void hashtable_clear(HASHTABLE *t) { size_t i, j; j = array_size(&t->table); for (i = 0; i != j; ++i) { dlist_clear((DLIST*)array_at(&t->table, i)); } t->size = 0; }
static void shiftup(minheap_t *heap, long start) { long iend, istart, ichild, iright; iend = (long) heap->len; istart = start; ichild = 2 * istart + 1; while (ichild < iend) { iright = ichild + 1; if (iright < iend && heap->comp(array_at(heap->array, ichild), array_at(heap->array, iright)) > 0) { ichild = iright; } heap->swp(array_at(heap->array, istart), array_at(heap->array, ichild)); istart = ichild; ichild = 2 * istart + 1; } shiftdown(heap, start, istart); }
int server_dispatch(server_t *server, msg_t *msg) { if (server->workers->size == 0) return -1; worker_push_msg(*(worker_t **)array_at(server->workers, server->curr_worker), msg); server->curr_worker++; if (server->curr_worker == server->workers->size) server->curr_worker = 0; return 0; }
static void cluster_reset(array_t *clusters) { int k = array_length(clusters); /* reset clusters */ for (int j=0; j<k; j++) { cluster_t *cl = array_at(clusters, j); // array_resize(cl->points, 0); array_free(cl->points); array_alloc(&cl->points, NULL); } }
void hashtable_init(HASHTABLE *t, size_t element_size, int (*compare)(const void*, const void*)) { size_t i; array_init(&t->table, sizeof(DLIST)); array_resize(&t->table, 10); for (i = 0; i != 10; ++i) { dlist_init((DLIST*)array_at(&t->table, i), element_size); } t->size = 0; t->element_size = element_size; t->compare = compare; }
char* stack_pop(struct stack_s* self) { char* ret = NULL; if(self->top > 0) { self->top--; ret = array_at(self->array, self->top); } return ret; }
END_TEST START_TEST(test_array_resize) { array_t* array; const size_t capacity1 = 1; const size_t capacity2 = 2; uint32_t value1 = 0xf00fba11; uint32_t value2 = 0xdeadbeef; const size_t element_size = sizeof(typeof(value1)); array = array_create(element_size); array_reserve(array, capacity1); ck_assert_int_eq(array_get_capacity(array), capacity1); ck_assert(array_get(array, typeof(value1)) != NULL); array_at(array, 0, typeof(value1)) = value1; array_reserve(array, capacity2); ck_assert_int_eq(array_get_capacity(array), capacity2); ck_assert_int_eq(array_at(array, 0, typeof(value1)), value1); array_at(array, 1, typeof(value1)) = value2; ck_assert_int_eq(array_at(array, 1, typeof(value1)), value2); array_reserve(array, capacity1); ck_assert_int_eq(array_get_capacity(array), capacity2); ck_assert_int_eq(array_at(array, 0, typeof(value1)), value1); ck_assert_int_eq(array_at(array, 1, typeof(value1)), value2); array_destroy(array); }
bool array_set(struct array_s* self, int index, const void* data) { char* old_data = array_at(self, index); if(NULL != old_data) { memcpy(old_data, data, self->element_size); return true; } else { return false; } }
/* Returns a -1 when it doesn't find that parameter. */ int FunctionParam_index(Function *func, Token *ident) { FunctionParam *param = NULL; size_t i = 0; for(i = 0; i < array_length(func->params); i++) { param = (CORD)array_at(func->params, i); if(CORD_cmp(param->name, ident->data) == 0) { return i; } } return -1; }
static void cluster_points(array_t *clusters, array_t *points) { int n = array_length(points); int k = array_length(clusters); for (int i=0; i<n; i++) { float distance = FLT_MAX; cluster_t *cl_nearest = NULL; point_t *p = array_at(points, i); for (int j=0; j<k; j++) { cluster_t *cl = array_at(clusters, j); float d = point_distance(p, &cl->centroid); if (d < distance) { distance = d; cl_nearest = cl; } } if (cl_nearest == NULL) { __asm__ volatile ("BKPT"); } /* copy point and add to cluster */ array_push_back(cl_nearest->points, p); }
void SysTick_Handler(void) { // int i; // struct systick_task *task; ++sys_ticks; #if 0 for (i=0; i<array_length(task_list); i++) { task = array_at(task_list, i); if ((sys_ticks % task->period)==0) { task->cb(); } } #endif }
int main(int argc, char const *argv[]) { Array a = array_create(100); int number = 0; int cnt = 0; while (number != -1) { scanf("%d", &number); if (number != -1) { *array_at(&a, cnt++) = number; } } for (int i=cnt-1; i>=0; i--) { printf("%d ", a.array[i]); } array_free(&a); return 0; }
const void *hashtable_find(HASHTABLE *t, const void *key) { size_t hash; DLIST *bucket; DLIST_ITER it, end; hash = _hashtable_hash(t, key) % array_size(&t->table); bucket = (DLIST*)array_at(&t->table, hash); if (dlist_size(bucket) == 0) return NULL; end = dlist_end(bucket); for (it = dlist_begin(bucket); it != end; it = dlist_next(it)) { if (t->compare(key, dlist_at(it)) == 0) { return dlist_at(it); } } return NULL; }
int minheap_push(minheap_t *heap, const void *val) { void *item; if (heap->len == array_len(heap->array)) { if ((item=array_push(heap->array)) == NULL) { return -1; } } else { item = array_at(heap->array, heap->len); } heap->cpy(item, val); heap->len++; shiftdown(heap, 0, heap->len - 1); return 0; }
int hashtable_remove(HASHTABLE *t, const void *key) { size_t hash; DLIST *bucket; DLIST_ITER it, end; hash = _hashtable_hash(t, key) % array_size(&t->table); bucket = (DLIST*)array_at(&t->table, hash); if (dlist_size(bucket) == 0) return -1; end = dlist_end(bucket); for (it = dlist_begin(bucket); it != end; it = dlist_next(it)) { if (t->compare(key, dlist_at(it)) == 0) { dlist_remove(bucket, it); --t->size; return 0; } } return -1; }