Commit e81961ff authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

kdtree: replaced uint32_t by size_t.

parent 2e652bbb
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
_compPoints: compare index if points[axis] are equal _compPoints: compare index if points[axis] are equal
replace qsortR by libc:qsort (speedup 25%) replace qsortR by libc:qsort (speedup 25%)
*/ */
#ifndef _KDTREE_H_ #ifndef KDTREE_H_
#define _KDTREE_H_ #define KDTREE_H_
#include <math.h> #include <math.h>
#include <pthread.h> #include <pthread.h>
...@@ -59,7 +59,7 @@ typedef long kdata_t; ...@@ -59,7 +59,7 @@ typedef long kdata_t;
typedef struct kd_point { typedef struct kd_point {
kdata_t point[KD_MAX_DIM]; kdata_t point[KD_MAX_DIM];
unsigned index; size_t index;
} kd_point; } kd_point;
...@@ -84,7 +84,7 @@ typedef struct kdNode { ...@@ -84,7 +84,7 @@ typedef struct kdNode {
kdata_t min[KD_MAX_DIM]; /*!<vector to the min coordinates of the hyperrectangle */ kdata_t min[KD_MAX_DIM]; /*!<vector to the min coordinates of the hyperrectangle */
kdata_t max[KD_MAX_DIM]; /*!<vector to the max coordinates of the hyperrectangle */ kdata_t max[KD_MAX_DIM]; /*!<vector to the max coordinates of the hyperrectangle */
int split; /*!<axis along which the tree bifurcates */ int split; /*!<axis along which the tree bifurcates */
unsigned index; /*!<optional index value */ size_t index; /*!<optional index value */
} kdNode; } kdNode;
/*! /*!
...@@ -102,9 +102,9 @@ typedef struct resItem { ...@@ -102,9 +102,9 @@ typedef struct resItem {
*/ */
typedef struct pqueue { typedef struct pqueue {
struct resItem **d; /*!<pointer to an array of result items */ struct resItem **d; /*!<pointer to an array of result items */
uint32_t size; /*!<current length of the queue */ size_t size; /*!<current length of the queue */
uint32_t avail; /*!<currently allocated queue elements */ size_t avail; /*!<currently allocated queue elements */
uint32_t step; /*!<step size in which new elements are allocated */ size_t step; /*!<step size in which new elements are allocated */
} pqueue; } pqueue;
/*! /*!
...@@ -126,7 +126,7 @@ typedef struct kd_thread_data { ...@@ -126,7 +126,7 @@ typedef struct kd_thread_data {
#define KD_UNORDERED (0) #define KD_UNORDERED (0)
/* functions for the priority queue */ /* functions for the priority queue */
struct pqueue *pqinit(struct pqueue *q, uint32_t n); struct pqueue *pqinit(struct pqueue *q, size_t n);
int pqinsert(struct pqueue *q, struct resItem *d); int pqinsert(struct pqueue *q, struct resItem *d);
struct resItem **pqremove_min(struct pqueue *q, struct resItem **d); struct resItem **pqremove_min(struct pqueue *q, struct resItem **d);
struct resItem **pqremove_max(struct pqueue *q, struct resItem **d); struct resItem **pqremove_max(struct pqueue *q, struct resItem **d);
...@@ -178,12 +178,11 @@ struct pqueue *kd_ortRangeSearch(struct kdNode *node, kdata_t *min, kdata_t *max ...@@ -178,12 +178,11 @@ struct pqueue *kd_ortRangeSearch(struct kdNode *node, kdata_t *min, kdata_t *max
int dim); int dim);
int kd_doOrtRangeSearch(struct kdNode *node, kdata_t *min, kdata_t *max, int dim, int kd_doOrtRangeSearch(struct kdNode *node, kdata_t *min, kdata_t *max, int dim,
struct pqueue *res); struct pqueue *res);
struct kdNode *kd_nearest(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, struct kdNode *kd_nearest(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, int dim);
int dim);
struct pqueue *kd_qnearest(struct kdNode *node, kdata_t *p, struct pqueue *kd_qnearest(struct kdNode *node, kdata_t *p,
kdata_t *max_dist_sq, unsigned int q, int dim); kdata_t *max_dist_sq, size_t q, int dim);
int kd_doQnearest(struct kdNode *node, kdata_t *p, int kd_doQnearest(struct kdNode *node, kdata_t *p,
kdata_t *max_dist_sq, unsigned int q, int dim, kdata_t *max_dist_sq, size_t q, int dim,
struct pqueue *res); struct pqueue *res);
struct pqueue *kd_range(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, struct pqueue *kd_range(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq,
int dim, int ordered); int dim, int ordered);
...@@ -200,9 +199,9 @@ int kd_sph_doOrtRangeSearch(struct kdNode *node, kdata_t *min, kdata_t *max, ...@@ -200,9 +199,9 @@ int kd_sph_doOrtRangeSearch(struct kdNode *node, kdata_t *min, kdata_t *max,
struct kdNode *kd_sph_nearest(struct kdNode *node, kdata_t *p, struct kdNode *kd_sph_nearest(struct kdNode *node, kdata_t *p,
kdata_t *max_dist_sq); kdata_t *max_dist_sq);
struct pqueue *kd_sph_qnearest(struct kdNode *node, kdata_t *p, struct pqueue *kd_sph_qnearest(struct kdNode *node, kdata_t *p,
kdata_t *max_dist_sq, unsigned int q); kdata_t *max_dist_sq, size_t q);
int kd_sph_doQnearest(struct kdNode *node, kdata_t *p, int kd_sph_doQnearest(struct kdNode *node, kdata_t *p,
kdata_t *max_dist_sq, unsigned int q, struct pqueue *res); kdata_t *max_dist_sq, size_t q, struct pqueue *res);
struct pqueue *kd_sph_range(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, struct pqueue *kd_sph_range(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq,
int ordered); int ordered);
int kd_sph_doRange(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, int kd_sph_doRange(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq,
...@@ -212,4 +211,4 @@ int kd_sph_doRange(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, ...@@ -212,4 +211,4 @@ int kd_sph_doRange(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq,
int kd_insertResTree(struct kdNode *node, struct pqueue *res); int kd_insertResTree(struct kdNode *node, struct pqueue *res);
#endif /* _KDTREE_H_ */ #endif /* KDTREE_H_ */
...@@ -163,12 +163,11 @@ struct pqueue * ...@@ -163,12 +163,11 @@ struct pqueue *
kd_ortRangeSearch(struct kdNode *node, kdata_t *min, kdata_t *max, int dim) kd_ortRangeSearch(struct kdNode *node, kdata_t *min, kdata_t *max, int dim)
{ {
struct pqueue *res; struct pqueue *res;
uint32_t i;
if ((res = pqinit(NULL, 1)) == NULL) if ((res = pqinit(NULL, 1)) == NULL)
return NULL; return NULL;
if (!kd_doOrtRangeSearch(node, min, max, dim, res)) { if (!kd_doOrtRangeSearch(node, min, max, dim, res)) {
for(i = 0; i < res->size; i++) { for(size_t i = 0; i < res->size; i++) {
free(res->d[i]); free(res->d[i]);
} }
free(res->d); free(res->d);
...@@ -304,13 +303,13 @@ kd_nearest(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, int dim) ...@@ -304,13 +303,13 @@ kd_nearest(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, int dim)
*/ */
struct pqueue * struct pqueue *
kd_qnearest(struct kdNode *node, kdata_t *p, kd_qnearest(struct kdNode *node, kdata_t *p,
kdata_t *max_dist_sq, unsigned int q, int dim) kdata_t *max_dist_sq, size_t q, int dim)
{ {
struct pqueue *res = pqinit(NULL, q + 2); struct pqueue *res = pqinit(NULL, q + 2);
if ( res == NULL) return NULL; if ( res == NULL) return NULL;
if ( !kd_doQnearest(node, p, max_dist_sq, q + 1, dim, res) ) { if ( !kd_doQnearest(node, p, max_dist_sq, q + 1, dim, res) ) {
for ( uint32_t i = 0; i < res->size; ++i ) free(res->d[i]); for ( size_t i = 0; i < res->size; ++i ) free(res->d[i]);
free(res->d); free(res->d);
free(res); free(res);
return NULL; return NULL;
...@@ -365,7 +364,7 @@ kd_check_dist(struct kdNode *node, kdata_t *p, ...@@ -365,7 +364,7 @@ kd_check_dist(struct kdNode *node, kdata_t *p,
int int
kd_doQnearest(struct kdNode *node, kdata_t *p, kd_doQnearest(struct kdNode *node, kdata_t *p,
kdata_t *max_dist_sq, unsigned int q, int dim, struct pqueue *res) kdata_t *max_dist_sq, size_t q, int dim, struct pqueue *res)
{ {
if ( !node ) return 1; if ( !node ) return 1;
...@@ -423,7 +422,7 @@ kd_range(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, ...@@ -423,7 +422,7 @@ kd_range(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq,
if ( res == NULL ) return NULL; if ( res == NULL ) return NULL;
if ( !kd_doRange(node, p, max_dist_sq, dim, res, ordered) ) { if ( !kd_doRange(node, p, max_dist_sq, dim, res, ordered) ) {
for( uint32_t i = 0; i < res->size; ++i ) { for( size_t i = 0; i < res->size; ++i ) {
free(res->d[i]); free(res->d[i]);
} }
free(res->d); free(res->d);
......
...@@ -59,7 +59,7 @@ kd_printNode(struct kdNode *node) ...@@ -59,7 +59,7 @@ kd_printNode(struct kdNode *node)
printf("Corners: (%f, %f)\t(%f, %f)\n", node->min[0], node->min[1], printf("Corners: (%f, %f)\t(%f, %f)\n", node->min[0], node->min[1],
node->max[0], node->max[1]); node->max[0], node->max[1]);
printf("Children: %p\t%p\n", (void *) node->left, (void *) node->right); printf("Children: %p\t%p\n", (void *) node->left, (void *) node->right);
printf("Index: %u\n", node->index); printf("Index: %zu\n", node->index);
printf("\n"); printf("\n");
} }
......
...@@ -277,12 +277,11 @@ struct pqueue * ...@@ -277,12 +277,11 @@ struct pqueue *
kd_sph_ortRangeSearch(struct kdNode *node, kdata_t *min, kdata_t *max) kd_sph_ortRangeSearch(struct kdNode *node, kdata_t *min, kdata_t *max)
{ {
struct pqueue *res; struct pqueue *res;
uint32_t i;
if ((res = pqinit(NULL, 1)) == NULL) if ((res = pqinit(NULL, 1)) == NULL)
return NULL; return NULL;
if (!kd_sph_doOrtRangeSearch(node, min, max, res)) { if (!kd_sph_doOrtRangeSearch(node, min, max, res)) {
for(i = 0; i < res->size; i++) { for(size_t i = 0; i < res->size; i++) {
free(res->d[i]); free(res->d[i]);
} }
free(res->d); free(res->d);
...@@ -432,15 +431,14 @@ kd_sph_nearest(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq) ...@@ -432,15 +431,14 @@ kd_sph_nearest(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq)
*/ */
struct pqueue * struct pqueue *
kd_sph_qnearest(struct kdNode *node, kdata_t *p, kd_sph_qnearest(struct kdNode *node, kdata_t *p,
kdata_t *max_dist_sq, unsigned int q) kdata_t *max_dist_sq, size_t q)
{ {
struct pqueue *res; struct pqueue *res;
uint32_t i;
if ((res = pqinit(NULL, q + 2)) == NULL) if ((res = pqinit(NULL, q + 2)) == NULL)
return NULL; return NULL;
if (!kd_sph_doQnearest(node, p, max_dist_sq, q + 1, res)) { if (!kd_sph_doQnearest(node, p, max_dist_sq, q + 1, res)) {
for(i = 0; i < res->size; i++) { for(size_t i = 0; i < res->size; i++) {
free(res->d[i]); free(res->d[i]);
} }
free(res->d); free(res->d);
...@@ -461,7 +459,7 @@ kd_sph_qnearest(struct kdNode *node, kdata_t *p, ...@@ -461,7 +459,7 @@ kd_sph_qnearest(struct kdNode *node, kdata_t *p,
*/ */
int int
kd_sph_doQnearest(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, kd_sph_doQnearest(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq,
unsigned int q, struct pqueue *res) size_t q, struct pqueue *res)
{ {
struct kdNode *nearer, *further; struct kdNode *nearer, *further;
struct resItem *point, *item; struct resItem *point, *item;
...@@ -588,12 +586,11 @@ struct pqueue * ...@@ -588,12 +586,11 @@ struct pqueue *
kd_sph_range(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, int ordered) kd_sph_range(struct kdNode *node, kdata_t *p, kdata_t *max_dist_sq, int ordered)
{ {
struct pqueue *res; struct pqueue *res;
uint32_t i;
if ((res = pqinit(NULL, 1)) == NULL) if ((res = pqinit(NULL, 1)) == NULL)
return NULL; return NULL;
if (!kd_sph_doRange(node, p, max_dist_sq, res, ordered)) { if (!kd_sph_doRange(node, p, max_dist_sq, res, ordered)) {
for(i = 0; i < res->size; i++) { for(size_t i = 0; i < res->size; i++) {
free(res->d[i]); free(res->d[i]);
} }
free(res->d); free(res->d);
......
...@@ -22,9 +22,9 @@ ...@@ -22,9 +22,9 @@
Ripped off wikipedia. Ripped off wikipedia.
*/ */
inline int inline int
floorLog2(uint32_t n) floorLog2(size_t n)
{ {
uint32_t pos = 0; size_t pos = 0;
if (n >= 1 << 16) { if (n >= 1 << 16) {
n >>= 16; n >>= 16;
pos += 16; pos += 16;
...@@ -54,7 +54,7 @@ floorLog2(uint32_t n) ...@@ -54,7 +54,7 @@ floorLog2(uint32_t n)
(min-sorted level). (min-sorted level).
*/ */
inline int inline int
is_max_level(int i) is_max_level(size_t i)
{ {
return floorLog2(i) % 2; return floorLog2(i) % 2;
} }
...@@ -63,7 +63,7 @@ is_max_level(int i) ...@@ -63,7 +63,7 @@ is_max_level(int i)
/* Swap nodes i and j in the priority queue q */ /* Swap nodes i and j in the priority queue q */
void void
pq_swap_nodes(struct pqueue *q, uint32_t i, uint32_t j) pq_swap_nodes(struct pqueue *q, size_t i, size_t j)
{ {
struct resItem *tmp; struct resItem *tmp;
...@@ -74,11 +74,11 @@ pq_swap_nodes(struct pqueue *q, uint32_t i, uint32_t j) ...@@ -74,11 +74,11 @@ pq_swap_nodes(struct pqueue *q, uint32_t i, uint32_t j)
/* Return the array index of the maximum node */ /* Return the array index of the maximum node */
uint32_t size_t
get_max_index(struct pqueue *q) get_max_index(struct pqueue *q)
{ {
uint32_t i; size_t i;
if (!q) if (!q)
return 0; return 0;
...@@ -128,7 +128,7 @@ get_max_index(struct pqueue *q) ...@@ -128,7 +128,7 @@ get_max_index(struct pqueue *q)
/* Move a node up the tree */ /* Move a node up the tree */
void void
bubble_up_min(struct pqueue *q, uint32_t i) bubble_up_min(struct pqueue *q, size_t i)
{ {
/* /*
* if node has a grandparent * if node has a grandparent
...@@ -141,7 +141,7 @@ bubble_up_min(struct pqueue *q, uint32_t i) ...@@ -141,7 +141,7 @@ bubble_up_min(struct pqueue *q, uint32_t i)
void void
bubble_up_max(struct pqueue *q, uint32_t i) bubble_up_max(struct pqueue *q, size_t i)
{ {
/* /*
* if node has a grandparent * if node has a grandparent
...@@ -154,7 +154,7 @@ bubble_up_max(struct pqueue *q, uint32_t i) ...@@ -154,7 +154,7 @@ bubble_up_max(struct pqueue *q, uint32_t i)
void void
bubble_up(struct pqueue *q, uint32_t i) bubble_up(struct pqueue *q, size_t i)
{ {
if (!is_max_level(i)) { if (!is_max_level(i)) {
if (i > 1 && PQPRIO(q->d[i]) > PQPRIO(q->d[i / 2])) { if (i > 1 && PQPRIO(q->d[i]) > PQPRIO(q->d[i / 2])) {
...@@ -182,10 +182,10 @@ bubble_up(struct pqueue *q, uint32_t i) ...@@ -182,10 +182,10 @@ bubble_up(struct pqueue *q, uint32_t i)
/* Get index of the smallest child or grandchild of q->d[i]. /* Get index of the smallest child or grandchild of q->d[i].
Caller must ensure that q->d[i] has at least one child. */ Caller must ensure that q->d[i] has at least one child. */
uint32_t size_t
pq_get_min_child_index(struct pqueue *q, uint32_t i) pq_get_min_child_index(struct pqueue *q, size_t i)
{ {
uint32_t m; size_t m;
/* /*
* First Child * First Child
...@@ -226,10 +226,10 @@ pq_get_min_child_index(struct pqueue *q, uint32_t i) ...@@ -226,10 +226,10 @@ pq_get_min_child_index(struct pqueue *q, uint32_t i)
/* Get index of the largest children and grandchildren of q->d[i]. /* Get index of the largest children and grandchildren of q->d[i].
Caller must ensure that q->d[i] has at least one child. */ Caller must ensure that q->d[i] has at least one child. */
uint32_t size_t
pq_get_max_child_index(struct pqueue * q, uint32_t i) pq_get_max_child_index(struct pqueue * q, size_t i)
{ {
uint32_t m; size_t m;
/* /*
* First Child * First Child
...@@ -271,7 +271,7 @@ pq_get_max_child_index(struct pqueue * q, uint32_t i) ...@@ -271,7 +271,7 @@ pq_get_max_child_index(struct pqueue * q, uint32_t i)
/* Move a node down the tree */ /* Move a node down the tree */
void void
trickle_down(struct pqueue *q, uint32_t i) trickle_down(struct pqueue *q, size_t i)
{ {
if (is_max_level(i)) if (is_max_level(i))
trickle_down_max(q, i); trickle_down_max(q, i);
...@@ -281,9 +281,9 @@ trickle_down(struct pqueue *q, uint32_t i) ...@@ -281,9 +281,9 @@ trickle_down(struct pqueue *q, uint32_t i)
void void
trickle_down_max(struct pqueue *q, uint32_t i) trickle_down_max(struct pqueue *q, size_t i)
{ {
uint32_t m; size_t m;
/* /*
* if A[i] has children * if A[i] has children
...@@ -313,9 +313,9 @@ trickle_down_max(struct pqueue *q, uint32_t i) ...@@ -313,9 +313,9 @@ trickle_down_max(struct pqueue *q, uint32_t i)
} }
void void
trickle_down_min(struct pqueue *q, uint32_t i) trickle_down_min(struct pqueue *q, size_t i)
{ {
uint32_t m; size_t m;
/* /*
* if A[i] has children * if A[i] has children
...@@ -369,7 +369,7 @@ trickle_down_min(struct pqueue *q, uint32_t i) ...@@ -369,7 +369,7 @@ trickle_down_min(struct pqueue *q, uint32_t i)
* *
*/ */
struct pqueue * struct pqueue *
pqinit(struct pqueue *q, uint32_t n) pqinit(struct pqueue *q, size_t n)
{ {
struct pqueue *tmp = q; struct pqueue *tmp = q;
...@@ -403,7 +403,7 @@ int ...@@ -403,7 +403,7 @@ int
pqinsert(struct pqueue *q, struct resItem *d) pqinsert(struct pqueue *q, struct resItem *d)
{ {
struct resItem **tmp; struct resItem **tmp;
uint32_t i, newsize; size_t i, newsize;
if (!q) if (!q)
return 0; return 0;
...@@ -473,7 +473,7 @@ pqremove_min(struct pqueue *q, struct resItem **d) ...@@ -473,7 +473,7 @@ pqremove_min(struct pqueue *q, struct resItem **d)
struct resItem ** struct resItem **
pqremove_max(struct pqueue *q, struct resItem **d) pqremove_max(struct pqueue *q, struct resItem **d)
{ {
uint32_t i; size_t i;
if (!q || q->size == 1) if (!q || q->size == 1)
return NULL; return NULL;
......
...@@ -8,17 +8,17 @@ ...@@ -8,17 +8,17 @@
#define PQPRIO(p) (p->dist_sq) #define PQPRIO(p) (p->dist_sq)
int floorLog2(uint32_t n); int floorLog2(size_t n);
int is_max_level(int i); int is_max_level(int i);
uint32_t get_max_index(struct pqueue *q); size_t get_max_index(struct pqueue *q);
uint32_t pq_get_min_child_index(struct pqueue *q, uint32_t i); size_t pq_get_min_child_index(struct pqueue *q, size_t i);
uint32_t pq_get_max_child_index(struct pqueue *q, uint32_t i); size_t pq_get_max_child_index(struct pqueue *q, size_t i);
void pq_swap_nodes(struct pqueue *q, uint32_t i, uint32_t j); void pq_swap_nodes(struct pqueue *q, size_t i, size_t j);
void trickle_down(struct pqueue *q, uint32_t i); void trickle_down(struct pqueue *q, size_t i);
void trickle_down_min(struct pqueue *q, uint32_t i); void trickle_down_min(struct pqueue *q, size_t i);
void trickle_down_max(struct pqueue *q, uint32_t i); void trickle_down_max(struct pqueue *q, size_t i);
void bubble_up(struct pqueue *q, uint32_t i); void bubble_up(struct pqueue *q, size_t i);
void bubble_up_min(struct pqueue *q, uint32_t i); void bubble_up_min(struct pqueue *q, size_t i);
void bubble_up_max(struct pqueue *q, uint32_t i); void bubble_up_max(struct pqueue *q, size_t i);
#endif #endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment