Skip to content

Commit b443be2

Browse files
committed
Added example with 1MM entries and a 50 dimension key
1 parent c47eb90 commit b443be2

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

examples/test3.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*! gcc -std=c89 -pedantic -Wall -g -o test2 test2.c libkdtree.a -lm */
2+
/* Extended test program, contributed by David Underhill */
3+
#include <assert.h>
4+
#include <math.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <ctype.h>
8+
#include <time.h>
9+
#include "kdtree.h"
10+
11+
#define DEF_NUM_PTS 1000000
12+
13+
/* returns the distance squared between two dims-dimensional double arrays */
14+
static double dist_sq( double *a1, double *a2, int dims );
15+
16+
/* get a random double between -10 and 10 */
17+
static double rd( void );
18+
19+
/* generate an array of random values for dimensions */
20+
static void generate_dims(int num, double *nums);
21+
22+
int main(int argc, char **argv) {
23+
int i, num_pts = DEF_NUM_PTS;
24+
void *ptree;
25+
char *data, *pch;
26+
struct kdres *presults;
27+
double pos[50], dist;
28+
double pt[50];
29+
double nums[50];
30+
double radius = 40;
31+
32+
for (i=0; i<50; ++i) {
33+
pt[i] = rd();
34+
}
35+
36+
if(!(data = malloc(num_pts))) {
37+
perror("malloc failed");
38+
return 1;
39+
}
40+
41+
srand( time(0) );
42+
43+
/* create a k-d tree for 50-dimensional points */
44+
ptree = kd_create(50);
45+
46+
/* add some random nodes to the tree (assert nodes are successfully inserted) */
47+
printf("Seeding %d entries of %d dimensions...\n", num_pts, 50);
48+
for( i=0; i<num_pts; ++i ) {
49+
data[i] = 'a' + i;
50+
generate_dims(50, nums);
51+
assert( 0 == kd_insert( ptree, nums, &data[i] ) );
52+
}
53+
54+
/* find points closest to the origin and within distance radius */
55+
printf("Searching in radius of %1.02f...\n", radius);
56+
presults = kd_nearest_range( ptree, pt, radius );
57+
58+
/* print out all the points found in results */
59+
printf( "found %d results:\n", kd_res_size(presults) );
60+
61+
while( !kd_res_end( presults ) ) {
62+
/* get the data and position of the current result item */
63+
pch = (char*)kd_res_item( presults, pos );
64+
65+
/* compute the distance of the current result from the pt */
66+
dist = sqrt( dist_sq( pt, pos, 50 ) );
67+
68+
/* print out the retrieved data */
69+
//printf( "node at (%.3f, %.3f, %.3f) is %.3f away and has data=%c\n",
70+
// pos[0], pos[1], pos[2], dist, *pch );
71+
72+
/* go to the next entry */
73+
kd_res_next( presults );
74+
}
75+
76+
/* free our tree, results set, and other allocated memory */
77+
free( data );
78+
kd_res_free( presults );
79+
kd_free( ptree );
80+
81+
return 0;
82+
}
83+
84+
static double dist_sq( double *a1, double *a2, int dims ) {
85+
double dist_sq = 0, diff;
86+
while( --dims >= 0 ) {
87+
diff = (a1[dims] - a2[dims]);
88+
dist_sq += diff*diff;
89+
}
90+
return dist_sq;
91+
}
92+
93+
static double rd( void ) {
94+
return (double)rand()/RAND_MAX * 20.0 - 10.0;
95+
}
96+
97+
static void generate_dims(int num, double *nums)
98+
{
99+
int i = 0;
100+
for (i=0; i<50; ++i) {
101+
nums[i] = rd();
102+
}
103+
}

0 commit comments

Comments
 (0)