indexm.c 11 KB

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