-
Notifications
You must be signed in to change notification settings - Fork 3
/
testspat.cxx
344 lines (310 loc) · 8.42 KB
/
testspat.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//-< TESTSPAT.CXX >--------------------------------------------------*--------*
// GOODS Version 1.0 (c) 1997 GARRET * ? *
// (Generic Object Oriented Database System) * /\| *
// * / \ *
// Created: 7-Jun-97 K.A. Knizhnik * / [] \ *
// Last update: 17-Oct-97 K.A. Knizhnik * GARRET *
//-------------------------------------------------------------------*--------*
// Test program for R-tree class
//-------------------------------------------------------------------*--------*
#include "object.h"
#include "rtree.h"
#include "hashtab.h"
#include <time.h>
USE_POST_NAMESPACE
class spatial_object : public object {
public:
rectangle rect;
int id;
void print() const {
printf("Object '%s' (%d,%d,%d,%d)\n", name,
rect.boundary[0], rect.boundary[1],
rect.boundary[2], rect.boundary[3]);
}
static spatial_object* create(storage& store,
int x0, int y0, int x1, int y1,
const char* name, int id)
{
return new (self_class, store, strlen(name)) spatial_object(x0, y0,
x1, y1,
id, name);
}
bool is(const char* id) const { return strcmp(name, id) == 0; }
CLASSINFO(spatial_object, NO_REFS);
protected:
char name[1];
spatial_object(int x0, int y0, int x1, int y1,
int ident, const char* so_name)
{
id = ident;
rect.boundary[0] = x0;
rect.boundary[1] = y0;
rect.boundary[2] = x1;
rect.boundary[3] = y1;
strcpy(name, so_name);
}
};
REGISTER(spatial_object);
class spatial_base : public R_tree {
protected:
hash_table* hash;
class spatial_object_callback : public callback {
virtual void apply(object* obj) {
((spatial_object*)obj)->print();
}
};
static bool print_object(const char*, object* obj) {
((spatial_object*)obj)->print();
return false;
}
public:
void find(int x0, int y0, int x1, int y1) const {
rectangle rect;
spatial_object_callback cb;
rect.boundary[0] = x0;
rect.boundary[1] = y0;
rect.boundary[2] = x1;
rect.boundary[3] = y1;
printf("Found %d objects\n", search(rect, cb));
}
void find(const char* name) const {
spatial_object* obj = (spatial_object*)hash->get(name);
if (obj == NULL) {
printf("No object found\n");
} else {
obj->print();
}
}
void insert(int x0, int y0, int x1, int y1, const char* name, int id = 0) {
spatial_object* obj = spatial_object::create(*get_storage(),
x0, y0, x1, y1,
name, id);
R_tree::insert(obj->rect, obj);
hash->put(name, obj);
}
bool remove(const char* name) {
spatial_object* obj = (spatial_object*)hash->get(name);
if (obj != NULL) {
hash->del(name);
R_tree::remove(obj->rect, obj);
delete obj;
return true;
}
return false;
}
void clear() {
purge(true);
hash->purge();
}
spatial_base(size_t hash_size) {
hash = hash_table::create(*get_storage(), hash_size);
}
CLASSINFO(spatial_base, REF(hash));
};
REGISTER(spatial_base);
static void input(char* prompt, char* buf, size_t buf_size)
{
char* p;
do {
printf(prompt);
*buf = '\0';
fgets(buf, buf_size, stdin);
p = buf + strlen(buf);
} while (p <= buf+1);
if (*(p-1) == '\n') {
*--p = '\0';
}
}
struct test_record {
union {
test_record* next_free;
struct {
int x0;
int y0;
int x1;
int y1;
} coord;
};
unsigned data[2];
};
inline unsigned random(unsigned mod) { return rand() % mod; }
static void random_test(spatial_base* base, long n_iterations)
{
const int max_records = 1024*1024;
const int max_x = 32*1024;
const int max_y = 32*1024;
const int max_width = 1024;
const int max_height = 1024;
test_record* record = new test_record[max_records];
test_record* free_chain;
int i;
int n_inserts = 0, n_searches = 0, n_removes = 0;
test_record* rp;
int x0, y0, x1, y1;
rectangle rect;
search_buffer sb;
char buf[64];
srand(time(NULL));
for (i = 0; i < max_records; i++) {
record[i].data[0] = 0;
record[i].next_free = &record[i+1];
}
record[max_records-1].next_free = NULL;
free_chain = record;
base->clear();
time_t start_time = time(NULL);
while (--n_iterations >= 0) {
switch (random(8)) {
default: // insert
if (free_chain != NULL) {
rp = free_chain;
free_chain = free_chain->next_free;
rp->coord.x0 = x0 = random(max_x*2) - max_x;
rp->coord.x1 = x1 = x0 + random(max_width);
rp->coord.y0 = y0 = random(max_y*2) - max_y;
rp->coord.y1 = y1 = y0 + random(max_height);
rp->data[0] = rand();
if (rp->data[0] == 0) {
rp->data[0] += 1;
}
rp->data[1] = rand();
i = rp - record;
sprintf(buf, "%x%x.%x", rp->data[0], rp->data[1], i);
base->insert(x0, y0, x1, y1, buf, i);
n_inserts += 1;
}
break;
case 0: // remove
i = random(max_records);
rp = &record[i];
if (rp->data[0]) {
sprintf(buf, "%x%x.%x", rp->data[0], rp->data[1], i);
bool removed = base->remove(buf);
assert(removed);
rp->data[0] = 0;
rp->next_free = free_chain;
free_chain = rp;
n_removes += 1;
}
break;
case 1: // search
x0 = random(max_x*2) - max_x;
x1 = x0 + random(max_width);
y0 = random(max_y*2) - max_y;
y1 = y0 + random(max_height);
rect.boundary[0] = x0;
rect.boundary[1] = y0;
rect.boundary[2] = x1;
rect.boundary[3] = y1;
sb.set_size(0);
int found = base->search(rect, sb);
rp = record;
for (i = 0; i < max_records; i++, rp++) {
if (rp->data[0]) {
if (x0 <= rp->coord.x1 && rp->coord.x0 <= x1
&& y0 <= rp->coord.y1 && rp->coord.y0 <= y1)
{
int j = sb.get_size();
while (--j >= 0 && ((spatial_object*)sb[j])->id != i);
assert(j >= 0);
spatial_object* obj = (spatial_object*)sb[j];
sprintf(buf, "%x%x.%x",
rp->data[0], rp->data[1], i);
assert(obj->rect.boundary[0] == rp->coord.x0 &&
obj->rect.boundary[1] == rp->coord.y0 &&
obj->rect.boundary[2] == rp->coord.x1 &&
obj->rect.boundary[3] == rp->coord.y1 &&
obj->is(buf));
found -= 1;
}
}
}
assert (found == 0);
n_searches += 1;
}
printf("Proceed %d inserts, %d removes, %d searches\r",
n_inserts, n_removes, n_searches);
}
printf("\nElapsed time: %ld\n", time(NULL) - start_time);
delete[] record;
}
int main()
{
storage db("rtree");
if (db.open(storage::fault_tolerant)) {
spatial_base* root = (spatial_base*)db.get_root_object();
if (root == NULL) {
root = new_in (db,spatial_base)(9719);
db.set_root_object(root);
}
while (true) {
char buf[256], name[256];
int action = 0;
int x0, y0, x1, y1;
int n_iterations;
printf("\nSpatial database menu:\n"
"\t1. Insert object\n"
"\t2. Remove object\n"
"\t3. Find object by name\n"
"\t4. Find object by coordinates\n"
"\t5. Random test\n"
"\t6. Exit\n");
input(">>", buf, sizeof buf);
sscanf(buf, "%d", &action);
switch (action) {
case 1:
input("Please specify object name: ", name, sizeof name);
while (true) {
input("Please specify object coordinates x0 y0 x1 y1: ",
buf, sizeof buf);
if (sscanf(buf, "%d %d %d %d", &x0, &y0, &x1, &y1) == 4) {
root->insert(x0, y0, x1, y1, name);
break;
}
}
continue;
case 2:
input("Please specify removed object name: ",
name, sizeof name);
if (!root->remove(name)) {
printf("Object is not in database\n");
}
continue;
case 3:
input("Please specify object name: ", name, sizeof name);
root->find(name);
continue;
case 4:
while (true) {
input("Please specify object coordinates x0 y0 x1 y1: ",
buf, sizeof buf);
if (sscanf(buf, "%d %d %d %d", &x0, &y0, &x1, &y1) == 4) {
root->find(x0, y0, x1, y1);
break;
}
}
continue;
case 5:
while (true) {
input("Please specify number of iterations: ",
buf, sizeof buf);
if (sscanf(buf, "%d", &n_iterations) == 1) {
random_test(root, n_iterations);
break;
}
}
continue;
case 6:
db.flush();
db.close();
printf("Session is closed\n");
return EXIT_SUCCESS;
default:
printf("Please chose action 1..7\n");
}
}
} else {
printf("Failed to open database\n");
return EXIT_FAILURE;
}
}