indexm.c 11 KB

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