indexf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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, found, 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. found = 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. found = 0;
  64. break;
  65. }
  66. }
  67. if (found) {
  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, int *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. if (s[top].sn.level == level) {
  141. RTreeCopyRect(&(b->rect), r, t);
  142. /* child field of leaves contains tid of data record */
  143. b->child = child;
  144. /* add branch, may split node or remove branches */
  145. if (top)
  146. cover = &(s[top - 1].sn.branch[s[top - 1].branch_id].rect);
  147. else
  148. cover = NULL;
  149. result = RTreeAddBranch(b, &(s[top].sn), &n2, ee, cover, overflow, 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. /* update node */
  157. RTreePutNode(&(s[top].sn), s[top].pos, t);
  158. }
  159. else {
  160. /* Not supposed to happen */
  161. assert(s[top].sn.level == level);
  162. return 0;
  163. }
  164. /* go back up */
  165. while (top) {
  166. down = top--;
  167. i = s[top].branch_id;
  168. if (result == 0) { /* branch was added */
  169. RTreeCombineRect(&(s[top].sn.branch[i].rect), &nr, r, 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 == 2) { /* branches were removed */
  176. /* get node cover of previous node */
  177. RTreeNodeCover(&(s[down].sn), &nr, t);
  178. /* rewrite rect */
  179. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  180. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  181. }
  182. }
  183. else if (result == 1) { /* node was split */
  184. /* get node cover of previous node */
  185. RTreeNodeCover(&(s[down].sn), &(s[top].sn.branch[i].rect), t);
  186. /* add new branch for new node previously added by RTreeAddBranch() */
  187. b->child.pos = *newnode_pos;
  188. RTreeNodeCover(n2, &(b->rect), t);
  189. /* add branch, may split node or remove branches */
  190. if (top)
  191. cover = &(s[top - 1].sn.branch[s[top - 1].branch_id].rect);
  192. else
  193. cover = NULL;
  194. result = RTreeAddBranch(b, &(s[top].sn), &n2, ee, cover, overflow, t);
  195. /* write out new node if node was split */
  196. if (result == 1) {
  197. *newnode_pos = RTreeGetNodePos(t);
  198. RTreeWriteNode(n2, t);
  199. t->n_nodes++;
  200. }
  201. /* update node */
  202. RTreePutNode(&(s[top].sn), s[top].pos, t);
  203. }
  204. }
  205. /* copy node */
  206. RTreeCopyNode(newnode, n2, t);
  207. return result;
  208. }
  209. /*
  210. * Insert a data rectangle into an index structure.
  211. * RTreeInsertRect1 provides for splitting the root;
  212. * returns 1 if root was split, 0 if it was not.
  213. * The level argument specifies the number of steps up from the leaf
  214. * level to insert; e.g. a data rectangle goes in at level = 0.
  215. * RTreeInsertRect2 does the actual insertion.
  216. */
  217. int RTreeInsertRectF(struct RTree_Rect *r, union RTree_Child child, int level,
  218. struct RTree *t)
  219. {
  220. struct RTree_ListBranch *e, *reInsertList = NULL;
  221. int result;
  222. int i, overflow[MAXLEVEL];
  223. off_t newnode_pos = -1;
  224. static struct RTree_Node oldroot, newroot, newnode;
  225. struct RTree_Branch *b = &(t->tmpb1);
  226. static int rect_init = 0;
  227. if (!rect_init) {
  228. for (i = 0; i < MAXCARD; i++) {
  229. RTreeNewRect(&(oldroot.branch[i].rect), t);
  230. RTreeNewRect(&(newroot.branch[i].rect), t);
  231. RTreeNewRect(&(newnode.branch[i].rect), t);
  232. }
  233. rect_init = t->ndims_alloc;
  234. }
  235. /* R*-tree forced reinsertion: for each level only once */
  236. for (i = 0; i < MAXLEVEL; i++)
  237. overflow[i] = 1;
  238. result =
  239. RTreeInsertRect2F(r, child, level, &newnode, &newnode_pos, t,
  240. &reInsertList, overflow);
  241. if (result == 1) { /* root split */
  242. RTreeGetNode(&oldroot, t->rootpos, t->rootlevel, t);
  243. /* grow a new root, & tree taller */
  244. t->rootlevel++;
  245. RTreeInitNode(t, &newroot, NODETYPE(t->rootlevel, t->fd));
  246. newroot.level = t->rootlevel;
  247. /* branch for old root */
  248. RTreeNodeCover(&oldroot, &(b->rect), t);
  249. b->child.pos = t->rootpos;
  250. RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
  251. /* branch for new node created by RTreeInsertRect2F() */
  252. RTreeNodeCover(&newnode, &(b->rect), t);
  253. b->child.pos = newnode_pos; /* offset to new node as returned by RTreeInsertRect2F() */
  254. RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
  255. /* write new root node */
  256. t->rootpos = RTreeGetNodePos(t);
  257. RTreeWriteNode(&newroot, t);
  258. t->n_nodes++;
  259. }
  260. else if (result == 2) { /* branches were removed */
  261. while (reInsertList) {
  262. /* get next branch in list */
  263. RTreeCopyBranch(b, &(reInsertList->b), t);
  264. level = reInsertList->level;
  265. e = reInsertList;
  266. reInsertList = reInsertList->next;
  267. RTreeFreeListBranch(e);
  268. /* reinsert branches */
  269. result =
  270. RTreeInsertRect2F(&(b->rect), b->child, level, &newnode, &newnode_pos, t,
  271. &reInsertList, overflow);
  272. if (result == 1) { /* root split */
  273. RTreeGetNode(&oldroot, t->rootpos, t->rootlevel, t);
  274. /* grow a new root, & tree taller */
  275. t->rootlevel++;
  276. RTreeInitNode(t, &newroot, NODETYPE(t->rootlevel, t->fd));
  277. newroot.level = t->rootlevel;
  278. /* branch for old root */
  279. RTreeNodeCover(&oldroot, &(b->rect), t);
  280. b->child.pos = t->rootpos;
  281. RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
  282. /* branch for new node created by RTreeInsertRect2F() */
  283. RTreeNodeCover(&newnode, &(b->rect), t);
  284. b->child.pos = newnode_pos;
  285. RTreeAddBranch(b, &newroot, NULL, NULL, NULL, NULL, t);
  286. /* write new root node */
  287. t->rootpos = RTreeGetNodePos(t);
  288. RTreeWriteNode(&newroot, t);
  289. t->n_nodes++;
  290. }
  291. }
  292. }
  293. return result;
  294. }
  295. /*
  296. * Delete a rectangle from non-root part of an index structure.
  297. * Called by RTreeDeleteRect. Descends tree non-recursively,
  298. * merges branches on the way back up.
  299. * Returns 1 if record not found, 0 if success.
  300. */
  301. static int
  302. RTreeDeleteRect2F(struct RTree_Rect *r, union RTree_Child child, struct RTree *t,
  303. struct RTree_ListNode **ee)
  304. {
  305. int i, notfound = 1, currlevel;
  306. struct RTree_Node *n;
  307. int top = 0, down = 0;
  308. int minfill;
  309. struct fstack *s = t->fs;
  310. static struct RTree_Rect nr;
  311. static int rect_init = 0;
  312. assert(ee);
  313. if (!rect_init) {
  314. RTreeNewRect(&(nr), t);
  315. rect_init = 1;
  316. }
  317. /* add root node position to stack */
  318. currlevel = t->rootlevel;
  319. s[top].pos = t->rootpos;
  320. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  321. s[top].branch_id = 0;
  322. while (notfound) {
  323. /* go down to level 0, remember path */
  324. if (s[top].sn.level > 0) {
  325. n = &(s[top].sn);
  326. currlevel = s[top].sn.level - 1;
  327. for (i = s[top].branch_id; i < t->nodecard; i++) {
  328. if (n->branch[i].child.pos > -1 && RTreeOverlap(r, &(n->branch[i].rect), t)) {
  329. s[top++].branch_id = i + 1;
  330. /* add next node to stack */
  331. s[top].pos = n->branch[i].child.pos;
  332. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  333. s[top].branch_id = 0;
  334. notfound = 0;
  335. break;
  336. }
  337. }
  338. if (notfound) {
  339. /* nothing else found, go back up */
  340. s[top].branch_id = t->nodecard;
  341. top--;
  342. }
  343. else /* found a way down but not yet the item */
  344. notfound = 1;
  345. }
  346. else {
  347. for (i = 0; i < t->leafcard; i++) {
  348. if (s[top].sn.branch[i].child.id &&
  349. s[top].sn.branch[i].child.id == child.id) { /* found item */
  350. RTreeDisconnectBranch(&(s[top].sn), i, t);
  351. RTreePutNode(&(s[top].sn), s[top].pos, t);
  352. t->n_leafs--;
  353. notfound = 0;
  354. break;
  355. }
  356. }
  357. if (notfound) /* continue searching */
  358. top--;
  359. }
  360. }
  361. if (notfound) {
  362. return notfound;
  363. }
  364. /* go back up */
  365. while (top) {
  366. down = top--;
  367. i = s[top].branch_id - 1;
  368. assert(s[down].sn.level == s[top].sn.level - 1);
  369. minfill = (s[down].sn.level ? t->min_node_fill : t->min_leaf_fill);
  370. if (s[down].sn.count >= minfill) {
  371. /* just update node cover */
  372. RTreeNodeCover(&(s[down].sn), &nr, t);
  373. /* rewrite rect */
  374. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  375. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  376. }
  377. }
  378. else {
  379. /* not enough entries in child, eliminate child node */
  380. assert(s[top].sn.branch[i].child.pos == s[down].pos);
  381. n = RTreeNewNode(t, s[down].sn.level);
  382. /* copy node */
  383. RTreeCopyNode(n, &(s[down].sn), t);
  384. RTreeAddNodePos(s[top].sn.branch[i].child.pos, s[down].sn.level, t);
  385. RTreeReInsertNode(n, ee);
  386. RTreeDisconnectBranch(&(s[top].sn), i, t);
  387. RTreePutNode(&(s[top].sn), s[top].pos, t);
  388. }
  389. }
  390. return notfound;
  391. }
  392. /*
  393. * should be called by RTreeDeleteRect() only
  394. *
  395. * Delete a data rectangle from an index structure.
  396. * Pass in a pointer to a Rect, the tid of the record, ptr RTree.
  397. * Returns 1 if record not found, 0 if success.
  398. * RTreeDeleteRect1 provides for eliminating the root.
  399. */
  400. int RTreeDeleteRectF(struct RTree_Rect *r, union RTree_Child child, struct RTree *t)
  401. {
  402. int i;
  403. struct RTree_Node *n;
  404. struct RTree_ListNode *e, *reInsertList = NULL;
  405. static struct RTree_Node rn;
  406. static int rect_init = 0;
  407. if (!rect_init) {
  408. for (i = 0; i < MAXCARD; i++) {
  409. RTreeNewRect(&(rn.branch[i].rect), t);
  410. }
  411. rect_init = 1;
  412. }
  413. if (!RTreeDeleteRect2F(r, child, t, &reInsertList)) {
  414. /* found and deleted a data item */
  415. /* reinsert any branches from eliminated nodes */
  416. while (reInsertList) {
  417. t->n_nodes--;
  418. n = reInsertList->node;
  419. if (n->level > 0) { /* reinsert node branches */
  420. for (i = 0; i < t->nodecard; i++) {
  421. if (n->branch[i].child.pos > -1) {
  422. RTreeInsertRectF(&(n->branch[i].rect),
  423. n->branch[i].child, n->level, t);
  424. }
  425. }
  426. }
  427. else { /* reinsert leaf branches */
  428. for (i = 0; i < t->leafcard; i++) {
  429. if (n->branch[i].child.id) {
  430. RTreeInsertRectF(&(n->branch[i].rect),
  431. n->branch[i].child, n->level, t);
  432. }
  433. }
  434. }
  435. e = reInsertList;
  436. reInsertList = reInsertList->next;
  437. RTreeFreeNode(e->node);
  438. RTreeFreeListNode(e);
  439. }
  440. /* check for redundant root (not leaf, 1 child) and eliminate */
  441. RTreeGetNode(&rn, t->rootpos, t->rootlevel, t);
  442. if (rn.count == 1 && rn.level > 0) {
  443. for (i = 0; i < t->nodecard; i++) {
  444. if (rn.branch[i].child.pos > -1)
  445. break;
  446. }
  447. RTreeAddNodePos(t->rootpos, t->rootlevel, t);
  448. t->rootpos = rn.branch[i].child.pos;
  449. t->rootlevel--;
  450. }
  451. return 0;
  452. }
  453. else {
  454. return 1;
  455. }
  456. }