indexm.c 11 KB

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