indexf.c 13 KB

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