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 Node *sn; /* stack node */
  26. int branch_id; /* branch no to follow down */
  27. };
  28. int RTreeValidChildM(union 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 Rect *r,
  38. SearchHitCallback *shcb, void *cbarg)
  39. {
  40. struct 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, 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.
  93. * Returns 0 if node was not split and nothing was removed.
  94. * Returns 1 if root node was split. Old node updated to become one of two.
  95. * Returns 2 if branches need to be reinserted.
  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 RTreeInsertRect2M(struct Rect *r, union Child child, int level,
  100. struct Node **newnode,
  101. struct RTree *t,
  102. struct ListBranch **ee, int *overflow)
  103. {
  104. int i;
  105. struct Branch b;
  106. struct Node *n, *n2;
  107. struct Rect *cover;
  108. struct stack s[MAXLEVEL];
  109. int top = 0, down = 0;
  110. int result;
  111. /* add root node to stack */
  112. s[top].sn = t->root;
  113. /* go down to level of insertion */
  114. while (s[top].sn->level > level) {
  115. n = s[top].sn;
  116. i = RTreePickBranch(r, n, t);
  117. s[top++].branch_id = i;
  118. /* add next node to stack */
  119. s[top].sn = n->branch[i].child.ptr;
  120. }
  121. /* Have reached level for insertion. Remove p rectangles or split */
  122. if (s[top].sn->level == level) {
  123. b.rect = *r;
  124. /* child field of leaves contains tid of data record */
  125. b.child = child;
  126. /* add branch, may split node or remove branches */
  127. if (top)
  128. cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
  129. else
  130. cover = NULL;
  131. result = RTreeAddBranch(&b, s[top].sn, &n2, ee, cover, overflow, t);
  132. /* update node count */
  133. if (result == 1) {
  134. t->n_nodes++;
  135. }
  136. }
  137. else {
  138. /* Not supposed to happen */
  139. assert(0);
  140. return 0;
  141. }
  142. /* go back up */
  143. while (top) {
  144. down = top--;
  145. i = s[top].branch_id;
  146. if (result == 0) { /* branch was added */
  147. s[top].sn->branch[i].rect =
  148. RTreeCombineRect(r, &(s[top].sn->branch[i].rect), t);
  149. }
  150. else if (result == 2) { /* branches were removed */
  151. /* get node cover of previous node */
  152. s[top].sn->branch[i].rect = RTreeNodeCover(s[down].sn, t);
  153. }
  154. else if (result == 1) { /* node was split */
  155. /* get node cover of previous node */
  156. s[top].sn->branch[i].rect = RTreeNodeCover(s[down].sn, t);
  157. /* add new branch for new node previously added by RTreeAddBranch() */
  158. b.child.ptr = n2;
  159. b.rect = RTreeNodeCover(b.child.ptr, t);
  160. /* add branch, may split node or remove branches */
  161. if (top)
  162. cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
  163. else
  164. cover = NULL;
  165. result =
  166. RTreeAddBranch(&b, s[top].sn, &n2, ee, cover, overflow, t);
  167. /* update node count */
  168. if (result == 1) {
  169. t->n_nodes++;
  170. }
  171. }
  172. }
  173. *newnode = n2;
  174. return result;
  175. }
  176. /*
  177. * Insert a data rectangle into an index structure.
  178. * RTreeInsertRectM provides for splitting the root;
  179. * returns 1 if root was split, 0 if it was not.
  180. * The level argument specifies the number of steps up from the leaf
  181. * level to insert; e.g. a data rectangle goes in at level = 0.
  182. * RTreeInsertRect2 does the actual insertion.
  183. */
  184. int RTreeInsertRectM(struct Rect *r, union Child child, int level,
  185. struct RTree *t)
  186. {
  187. struct Node *newnode;
  188. struct Node *newroot;
  189. struct Branch b;
  190. struct ListBranch *reInsertList = NULL;
  191. struct 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 Rect *r, union Child child, struct RTree *t,
  258. struct ListNode **ee)
  259. {
  260. int i, notfound = 1;
  261. struct 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 Rect *r, union Child child, struct RTree *t)
  338. {
  339. int i;
  340. struct Node *n;
  341. struct ListNode *reInsertList = NULL;
  342. struct 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. }