indexf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /****************************************************************************
  2. * MODULE: R-Tree library
  3. *
  4. * AUTHOR(S): Antonin Guttman - original code
  5. * Daniel Green (green@superliminal.com) - major clean-up
  6. * and implementation of bounding spheres
  7. * Markus Metz - file-based and memory-based R*-tree
  8. *
  9. * PURPOSE: Multidimensional index
  10. *
  11. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *****************************************************************************/
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <assert.h>
  22. #include <grass/config.h>
  23. #include <grass/gis.h>
  24. #include "index.h"
  25. //#include "card.h"
  26. int RTreeValidChildF(union RTree_Child *child)
  27. {
  28. return (child->pos > -1);
  29. }
  30. /*
  31. * Search in an index tree for all data retangles that
  32. * overlap the argument rectangle.
  33. * Return the number of qualifying data rects.
  34. */
  35. int RTreeSearchF(struct RTree *t, struct RTree_Rect *r,
  36. SearchHitCallback *shcb, void *cbarg)
  37. {
  38. struct RTree_Node *n;
  39. int hitCount = 0, notfound, currlevel;
  40. int i;
  41. int top = 0;
  42. struct fstack *s = t->fs;
  43. /* stack size of t->rootlevel + 1 is enough because of depth first search */
  44. /* only one node per level on stack at any given time */
  45. /* add root node position to stack */
  46. currlevel = t->rootlevel;
  47. s[top].pos = t->rootpos;
  48. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  49. s[top].branch_id = i = 0;
  50. while (top >= 0) {
  51. if (s[top].sn.level > 0) { /* this is an internal node in the tree */
  52. n = &(s[top].sn);
  53. notfound = 1;
  54. currlevel = s[top].sn.level - 1;
  55. for (i = s[top].branch_id; i < t->nodecard; i++) {
  56. if (s[top].sn.branch[i].child.pos > -1 &&
  57. RTreeOverlap(r, &(s[top].sn.branch[i].rect), t)) {
  58. s[top++].branch_id = i + 1;
  59. /* add next node to stack */
  60. s[top].pos = n->branch[i].child.pos;
  61. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  62. s[top].branch_id = 0;
  63. notfound = 0;
  64. break;
  65. }
  66. }
  67. if (notfound) {
  68. /* nothing else found, go back up */
  69. s[top].branch_id = t->nodecard;
  70. top--;
  71. }
  72. }
  73. else { /* this is a leaf node */
  74. for (i = 0; i < t->leafcard; i++) {
  75. if (s[top].sn.branch[i].child.id &&
  76. RTreeOverlap(r, &(s[top].sn.branch[i].rect), t)) {
  77. hitCount++;
  78. if (shcb) { /* call the user-provided callback */
  79. if (!shcb(s[top].sn.branch[i].child.id,
  80. &s[top].sn.branch[i].rect, cbarg)) {
  81. /* callback wants to terminate search early */
  82. return hitCount;
  83. }
  84. }
  85. }
  86. }
  87. top--;
  88. }
  89. }
  90. return hitCount;
  91. }
  92. /*
  93. * Inserts a new data rectangle into the index structure.
  94. * Non-recursively descends tree, propagates splits back up.
  95. * Returns 0 if node was not split. Old node updated.
  96. * If node was split, returns 1 and sets the pointer pointed to by
  97. * new_node to point to the new node. Old node updated to become one of two.
  98. * The level argument specifies the number of steps up from the leaf
  99. * level to insert; e.g. a data rectangle goes in at level = 0.
  100. */
  101. static int RTreeInsertRect2F(struct RTree_Rect *r, union RTree_Child child, int level,
  102. struct RTree_Node *newnode, off_t *newnode_pos,
  103. struct RTree *t,
  104. struct RTree_ListBranch **ee, char *overflow)
  105. {
  106. int i, currlevel;
  107. struct RTree_Rect *cover;
  108. struct RTree_Node *n, *n2;
  109. int top = 0, down = 0;
  110. struct RTree_Branch *b = &(t->tmpb2);
  111. int result;
  112. struct fstack *s = t->fs;
  113. static struct RTree_Node nn;
  114. static struct RTree_Rect nr;
  115. static int rect_init = 0;
  116. if (!rect_init) {
  117. for (i = 0; i < MAXCARD; i++) {
  118. RTreeNewRect(&(nn.branch[i].rect), t);
  119. }
  120. RTreeNewRect(&nr, t);
  121. rect_init = t->ndims_alloc;
  122. }
  123. n2 = &nn;
  124. /* add root node position to stack */
  125. currlevel = t->rootlevel;
  126. s[top].pos = t->rootpos;
  127. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  128. /* go down to level of insertion */
  129. while (s[top].sn.level > level) {
  130. n = &(s[top].sn);
  131. i = RTreePickBranch(r, n, t);
  132. s[top++].branch_id = i;
  133. /* add next node to stack */
  134. s[top].pos = n->branch[i].child.pos;
  135. currlevel--;
  136. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  137. assert(s[top].sn.level == currlevel);
  138. }
  139. /* Have reached level for insertion. Add rect, split if necessary */
  140. RTreeCopyRect(&(b->rect), r, t);
  141. /* child field of leaves contains tid of data record */
  142. b->child = child;
  143. /* add branch, may split node or remove branches */
  144. cover = NULL;
  145. if (top)
  146. cover = &(s[top - 1].sn.branch[s[top - 1].branch_id].rect);
  147. result = RTreeAddBranch(b, &(s[top].sn), &n2, ee, cover, overflow, t);
  148. /* write out new node if node was split */
  149. if (result == 1) {
  150. *newnode_pos = RTreeGetNodePos(t);
  151. RTreeWriteNode(n2, t);
  152. t->n_nodes++;
  153. }
  154. /* update node */
  155. RTreePutNode(&(s[top].sn), s[top].pos, t);
  156. /* go back up */
  157. while (top) {
  158. down = top--;
  159. i = s[top].branch_id;
  160. if (result == 0) { /* branch was added */
  161. RTreeCombineRect(&(s[top].sn.branch[i].rect), &nr, r, t);
  162. /* rewrite rect */
  163. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  164. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  165. }
  166. }
  167. else if (result == 2) { /* branches were removed */
  168. /* get node cover of previous node */
  169. RTreeNodeCover(&(s[down].sn), &nr, t);
  170. /* rewrite rect */
  171. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  172. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  173. }
  174. }
  175. else if (result == 1) { /* node was split */
  176. /* get node cover of previous node */
  177. RTreeNodeCover(&(s[down].sn), &(s[top].sn.branch[i].rect), t);
  178. /* add new branch for new node previously added by RTreeAddBranch() */
  179. b->child.pos = *newnode_pos;
  180. RTreeNodeCover(n2, &(b->rect), t);
  181. /* add branch, may split node or remove branches */
  182. cover = NULL;
  183. if (top)
  184. cover = &(s[top - 1].sn.branch[s[top - 1].branch_id].rect);
  185. result = RTreeAddBranch(b, &(s[top].sn), &n2, ee, cover, overflow, t);
  186. /* write out new node if node was split */
  187. if (result == 1) {
  188. *newnode_pos = RTreeGetNodePos(t);
  189. RTreeWriteNode(n2, t);
  190. t->n_nodes++;
  191. }
  192. /* update node */
  193. RTreePutNode(&(s[top].sn), s[top].pos, t);
  194. }
  195. }
  196. /* copy node */
  197. RTreeCopyNode(newnode, n2, t);
  198. return result;
  199. }
  200. /*
  201. * Insert a data rectangle into an index structure.
  202. * RTreeInsertRect1 provides for splitting the root;
  203. * returns 1 if root was split, 0 if it was not.
  204. * The level argument specifies the number of steps up from the leaf
  205. * level to insert; e.g. a data rectangle goes in at level = 0.
  206. * RTreeInsertRect2 does the actual insertion.
  207. */
  208. int RTreeInsertRectF(struct RTree_Rect *r, union RTree_Child child, int level,
  209. struct RTree *t)
  210. {
  211. struct RTree_ListBranch *e, *reInsertList = NULL;
  212. int i, result;
  213. char overflow[MAXLEVEL];
  214. off_t newnode_pos = -1;
  215. static struct RTree_Node oldroot, newroot, newnode;
  216. struct RTree_Branch *b = &(t->tmpb1);
  217. static int rect_init = 0;
  218. if (!rect_init) {
  219. for (i = 0; i < MAXCARD; i++) {
  220. RTreeNewRect(&(oldroot.branch[i].rect), t);
  221. RTreeNewRect(&(newroot.branch[i].rect), t);
  222. RTreeNewRect(&(newnode.branch[i].rect), t);
  223. }
  224. rect_init = t->ndims_alloc;
  225. }
  226. /* R*-tree forced reinsertion: for each level only once */
  227. memset(overflow, 1, MAXLEVEL);
  228. result =
  229. RTreeInsertRect2F(r, child, level, &newnode, &newnode_pos, t,
  230. &reInsertList, overflow);
  231. if (result == 1) { /* root split */
  232. RTreeGetNode(&oldroot, t->rootpos, t->rootlevel, t);
  233. /* grow a new root, & tree taller */
  234. t->rootlevel++;
  235. RTreeInitNode(t, &newroot, NODETYPE(t->rootlevel, t->fd));
  236. newroot.level = t->rootlevel;
  237. /* branch for old root */
  238. RTreeNodeCover(&oldroot, &(b->rect), t);
  239. b->child.pos = t->rootpos;
  240. RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
  241. /* branch for new node created by RTreeInsertRect2F() */
  242. RTreeNodeCover(&newnode, &(b->rect), t);
  243. b->child.pos = newnode_pos; /* offset to new node as returned by RTreeInsertRect2F() */
  244. RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
  245. /* write new root node */
  246. t->rootpos = RTreeGetNodePos(t);
  247. RTreeWriteNode(&newroot, t);
  248. t->n_nodes++;
  249. return result;
  250. }
  251. if (result == 2) { /* branches were removed */
  252. while (reInsertList) {
  253. /* get next branch in list */
  254. RTreeCopyBranch(b, &(reInsertList->b), t);
  255. level = reInsertList->level;
  256. e = reInsertList;
  257. reInsertList = reInsertList->next;
  258. RTreeFreeListBranch(e);
  259. /* reinsert branches */
  260. result =
  261. RTreeInsertRect2F(&(b->rect), b->child, level, &newnode, &newnode_pos, t,
  262. &reInsertList, overflow);
  263. if (result == 1) { /* root split */
  264. RTreeGetNode(&oldroot, t->rootpos, t->rootlevel, t);
  265. /* grow a new root, & tree taller */
  266. t->rootlevel++;
  267. RTreeInitNode(t, &newroot, NODETYPE(t->rootlevel, t->fd));
  268. newroot.level = t->rootlevel;
  269. /* branch for old root */
  270. RTreeNodeCover(&oldroot, &(b->rect), t);
  271. b->child.pos = t->rootpos;
  272. RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
  273. /* branch for new node created by RTreeInsertRect2F() */
  274. RTreeNodeCover(&newnode, &(b->rect), t);
  275. b->child.pos = newnode_pos;
  276. RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
  277. /* write new root node */
  278. t->rootpos = RTreeGetNodePos(t);
  279. RTreeWriteNode(&newroot, t);
  280. t->n_nodes++;
  281. }
  282. }
  283. }
  284. return result;
  285. }
  286. /*
  287. * Delete a rectangle from non-root part of an index structure.
  288. * Called by RTreeDeleteRect. Descends tree non-recursively,
  289. * merges branches on the way back up.
  290. * Returns 1 if record not found, 0 if success.
  291. */
  292. static int
  293. RTreeDeleteRect2F(struct RTree_Rect *r, union RTree_Child child, struct RTree *t,
  294. struct RTree_ListNode **ee)
  295. {
  296. int i, notfound = 1, currlevel;
  297. struct RTree_Node *n;
  298. int top = 0, down = 0;
  299. int minfill;
  300. struct fstack *s = t->fs;
  301. static struct RTree_Rect nr;
  302. static int rect_init = 0;
  303. assert(ee);
  304. if (!rect_init) {
  305. RTreeNewRect(&(nr), t);
  306. rect_init = 1;
  307. }
  308. /* add root node position to stack */
  309. currlevel = t->rootlevel;
  310. s[top].pos = t->rootpos;
  311. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  312. s[top].branch_id = 0;
  313. while (notfound && top >= 0) {
  314. /* go down to level 0, remember path */
  315. if (s[top].sn.level > 0) {
  316. n = &(s[top].sn);
  317. currlevel = s[top].sn.level - 1;
  318. for (i = s[top].branch_id; i < t->nodecard; i++) {
  319. if (n->branch[i].child.pos > -1 && RTreeOverlap(r, &(n->branch[i].rect), t)) {
  320. s[top++].branch_id = i + 1;
  321. /* add next node to stack */
  322. s[top].pos = n->branch[i].child.pos;
  323. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  324. s[top].branch_id = 0;
  325. notfound = 0;
  326. break;
  327. }
  328. }
  329. if (notfound) {
  330. /* nothing else found, go back up */
  331. s[top].branch_id = t->nodecard;
  332. top--;
  333. }
  334. else /* found a way down but not yet the item */
  335. notfound = 1;
  336. }
  337. else {
  338. for (i = 0; i < t->leafcard; i++) {
  339. if (s[top].sn.branch[i].child.id &&
  340. s[top].sn.branch[i].child.id == child.id) { /* found item */
  341. RTreeDisconnectBranch(&(s[top].sn), i, t);
  342. RTreePutNode(&(s[top].sn), s[top].pos, t);
  343. t->n_leafs--;
  344. notfound = 0;
  345. break;
  346. }
  347. }
  348. if (notfound) /* continue searching */
  349. top--;
  350. }
  351. }
  352. if (notfound) {
  353. return notfound;
  354. }
  355. /* go back up */
  356. while (top) {
  357. down = top--;
  358. i = s[top].branch_id - 1;
  359. assert(s[down].sn.level == s[top].sn.level - 1);
  360. minfill = (s[down].sn.level ? t->min_node_fill : t->min_leaf_fill);
  361. if (s[down].sn.count >= minfill) {
  362. /* just update node cover */
  363. RTreeNodeCover(&(s[down].sn), &nr, t);
  364. /* rewrite rect */
  365. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  366. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  367. }
  368. }
  369. else {
  370. /* not enough entries in child, eliminate child node */
  371. assert(s[top].sn.branch[i].child.pos == s[down].pos);
  372. n = RTreeNewNode(t, s[down].sn.level);
  373. /* copy node */
  374. RTreeCopyNode(n, &(s[down].sn), t);
  375. RTreeAddNodePos(s[top].sn.branch[i].child.pos, s[down].sn.level, t);
  376. RTreeReInsertNode(n, ee);
  377. RTreeDisconnectBranch(&(s[top].sn), i, t);
  378. RTreePutNode(&(s[top].sn), s[top].pos, t);
  379. }
  380. }
  381. return notfound;
  382. }
  383. /*
  384. * should be called by RTreeDeleteRect() only
  385. *
  386. * Delete a data rectangle from an index structure.
  387. * Pass in a pointer to a Rect, the tid of the record, ptr RTree.
  388. * Returns 1 if record not found, 0 if success.
  389. * RTreeDeleteRect1 provides for eliminating the root.
  390. */
  391. int RTreeDeleteRectF(struct RTree_Rect *r, union RTree_Child child, struct RTree *t)
  392. {
  393. int i;
  394. struct RTree_Node *n;
  395. struct RTree_ListNode *e, *reInsertList = NULL;
  396. static struct RTree_Node rn;
  397. static int rect_init = 0;
  398. if (!rect_init) {
  399. for (i = 0; i < MAXCARD; i++) {
  400. RTreeNewRect(&(rn.branch[i].rect), t);
  401. }
  402. rect_init = 1;
  403. }
  404. if (!RTreeDeleteRect2F(r, child, t, &reInsertList)) {
  405. /* found and deleted a data item */
  406. /* reinsert any branches from eliminated nodes */
  407. while (reInsertList) {
  408. t->n_nodes--;
  409. n = reInsertList->node;
  410. if (n->level > 0) { /* reinsert node branches */
  411. for (i = 0; i < t->nodecard; i++) {
  412. if (n->branch[i].child.pos > -1) {
  413. RTreeInsertRectF(&(n->branch[i].rect),
  414. n->branch[i].child, n->level, t);
  415. }
  416. }
  417. }
  418. else { /* reinsert leaf branches */
  419. for (i = 0; i < t->leafcard; i++) {
  420. if (n->branch[i].child.id) {
  421. RTreeInsertRectF(&(n->branch[i].rect),
  422. n->branch[i].child, n->level, t);
  423. }
  424. }
  425. }
  426. e = reInsertList;
  427. reInsertList = reInsertList->next;
  428. RTreeFreeNode(e->node);
  429. RTreeFreeListNode(e);
  430. }
  431. /* check for redundant root (not leaf, 1 child) and eliminate */
  432. RTreeGetNode(&rn, t->rootpos, t->rootlevel, t);
  433. if (rn.count == 1 && rn.level > 0) {
  434. for (i = 0; i < t->nodecard; i++) {
  435. if (rn.branch[i].child.pos > -1)
  436. break;
  437. }
  438. RTreeAddNodePos(t->rootpos, t->rootlevel, t);
  439. t->rootpos = rn.branch[i].child.pos;
  440. t->rootlevel--;
  441. }
  442. return 0;
  443. }
  444. return 1;
  445. }