indexf.c 13 KB

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