indexf.c 14 KB

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