index.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #ifndef _INDEX_
  18. #define _INDEX_
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. /* PGSIZE is normally the natural page size of the machine */
  22. #define PGSIZE 512
  23. #define NUMDIMS 3 /* maximum number of dimensions */
  24. typedef double RectReal;
  25. /*-----------------------------------------------------------------------------
  26. | Global definitions.
  27. -----------------------------------------------------------------------------*/
  28. #ifndef TRUE
  29. #define TRUE 1
  30. #endif
  31. #ifndef FALSE
  32. #define FALSE 0
  33. #endif
  34. #define NUMSIDES 2*NUMDIMS
  35. /* max branching factor of a node */
  36. /* was (PGSIZE-(2 * sizeof(int))) / sizeof(struct Branch)
  37. * this is LFS dependent, not good
  38. * on 32 bit without LFS this is 9.69
  39. * on 32 bit with LFS and on 64 bit this is 9 */
  40. #define MAXCARD 18
  41. /* R*-tree: number of branches to be force-reinserted when adding a branch */
  42. #define FORCECARD 2
  43. /* maximum no of levels = tree depth */
  44. #define MAXLEVEL 20 /* 8^MAXLEVEL items are guaranteed to fit into the tree */
  45. #define NODETYPE(l, fd) ((l) == 0 ? 0 : ((fd) < 0 ? 1 : 2))
  46. struct Rect
  47. {
  48. RectReal boundary[NUMSIDES]; /* xmin,ymin,...,xmax,ymax,... */
  49. };
  50. struct Node; /* node for spatial index */
  51. union Child
  52. {
  53. int id; /* child id */
  54. struct Node *ptr; /* pointer to child node */
  55. off_t pos; /* position of child node in file */
  56. };
  57. struct Branch /* branch for spatial index */
  58. {
  59. struct Rect rect;
  60. union Child child;
  61. };
  62. struct Node /* node for spatial index */
  63. {
  64. int count; /* number of branches */
  65. int level; /* 0 is leaf, others positive */
  66. struct Branch branch[MAXCARD];
  67. };
  68. /*
  69. * If passed to a tree search, this callback function will be called
  70. * with the ID of each data rect that overlaps the search rect
  71. * plus whatever user specific pointer was passed to the search.
  72. * It can terminate the search early by returning 0 in which case
  73. * the search will return the number of hits found up to that point.
  74. */
  75. typedef int SearchHitCallback(int id, void *arg);
  76. struct RTree;
  77. typedef int rt_search_fn(struct RTree *, struct Rect *,
  78. SearchHitCallback *, void *);
  79. typedef int rt_insert_fn(struct Rect *, union Child, int, struct RTree *);
  80. typedef int rt_delete_fn(struct Rect *, union Child, struct RTree *);
  81. typedef int rt_valid_child_fn(union Child *);
  82. struct RTree
  83. {
  84. /* RTree setup info */
  85. int fd; /* file descriptor */
  86. unsigned char ndims; /* number of dimensions */
  87. unsigned char nsides; /* number of sides = 2 * ndims */
  88. int nodesize; /* node size in bytes */
  89. int branchsize; /* branch size in bytes */
  90. int rectsize; /* rectangle size in bytes */
  91. /* RTree info, useful to calculate space requirements */
  92. int n_nodes; /* number of nodes */
  93. int n_leafs; /* number of data items (level 0 leafs) */
  94. int rootlevel; /* root level = tree depth */
  95. /* settings for RTree building */
  96. int nodecard; /* max number of childs in node */
  97. int leafcard; /* max number of childs in leaf */
  98. int min_node_fill; /* balance criteria for node removal */
  99. int min_leaf_fill; /* balance criteria for leaf removal */
  100. int minfill_node_split; /* balance criteria for splitting */
  101. int minfill_leaf_split; /* balance criteria for splitting */
  102. /* free node positions for recycling */
  103. struct _recycle {
  104. int avail; /* number of available positions */
  105. int alloc; /* number of allcoated positions in *pos */
  106. off_t *pos; /* array of available positions */
  107. } free_nodes;
  108. /* node buffer for file-based index, two nodes per level
  109. * more than two nodes per level would require too complex cache management:
  110. * lru or pseudo-lru replacement, searching for buffered nodes */
  111. struct NodeBuffer
  112. {
  113. struct Node n; /* buffered node */
  114. off_t pos; /* file position of buffered node */
  115. char dirty; /* node in buffer was modified */
  116. } nb[MAXLEVEL][2];
  117. char mru[MAXLEVEL]; /* most recently used buffered node per level */
  118. /* insert, delete, search */
  119. rt_insert_fn *insert_rect;
  120. rt_delete_fn *delete_rect;
  121. rt_search_fn *search_rect;
  122. rt_valid_child_fn *valid_child;
  123. struct Node *root; /* pointer to root node */
  124. off_t rootpos; /* root node position in file */
  125. };
  126. struct ListNode
  127. {
  128. struct ListNode *next;
  129. struct Node *node;
  130. };
  131. struct ListFNode
  132. {
  133. struct ListFNode *next;
  134. off_t node_pos;
  135. };
  136. struct ListBranch
  137. {
  138. struct ListBranch *next;
  139. struct Branch b;
  140. int level;
  141. };
  142. /* index.c */
  143. extern int RTreeSearch(struct RTree *, struct Rect *, SearchHitCallback *,
  144. void *);
  145. extern int RTreeInsertRect(struct Rect *, int, struct RTree *);
  146. extern int RTreeDeleteRect(struct Rect *, int, struct RTree *);
  147. extern struct RTree *RTreeNewIndex(int, off_t, int);
  148. extern void RTreeFreeIndex(struct RTree *);
  149. extern struct ListNode *RTreeNewListNode(void);
  150. extern void RTreeFreeListNode(struct ListNode *);
  151. extern void RTreeReInsertNode(struct Node *, struct ListNode **);
  152. extern void RTreeFreeListBranch(struct ListBranch *);
  153. /* indexm.c */
  154. extern int RTreeSearchM(struct RTree *, struct Rect *, SearchHitCallback *,
  155. void *);
  156. extern int RTreeInsertRectM(struct Rect *, union Child, int, struct RTree *);
  157. extern int RTreeDeleteRectM(struct Rect *, union Child, struct RTree *);
  158. extern int RTreeValidChildM(union Child *child);
  159. /* indexf.c */
  160. extern int RTreeSearchF(struct RTree *, struct Rect *, SearchHitCallback *,
  161. void *);
  162. extern int RTreeInsertRectF(struct Rect *, union Child, int, struct RTree *);
  163. extern int RTreeDeleteRectF(struct Rect *, union Child, struct RTree *);
  164. extern int RTreeValidChildF(union Child *child);
  165. /* node.c */
  166. extern struct Node *RTreeNewNode(struct RTree *, int);
  167. extern void RTreeInitNode(struct Node *, int);
  168. extern void RTreeFreeNode(struct Node *);
  169. extern void RTreeDestroyNode(struct Node *, int);
  170. extern struct Rect RTreeNodeCover(struct Node *, struct RTree *);
  171. extern int RTreeAddBranch(struct Branch *, struct Node *, struct Node **,
  172. struct ListBranch **, struct Rect *, int *, struct RTree *);
  173. extern int RTreePickBranch(struct Rect *, struct Node *, struct RTree *);
  174. extern void RTreeDisconnectBranch(struct Node *, int, struct RTree *);
  175. extern void RTreePrintNode(struct Node *, int, struct RTree *);
  176. extern void RTreeTabIn(int);
  177. /* rect.c */
  178. extern void RTreeInitRect(struct Rect *);
  179. extern struct Rect RTreeNullRect(void);
  180. extern RectReal RTreeRectArea(struct Rect *, struct RTree *);
  181. extern RectReal RTreeRectSphericalVolume(struct Rect *, struct RTree *);
  182. extern RectReal RTreeRectVolume(struct Rect *, struct RTree *);
  183. extern RectReal RTreeRectMargin(struct Rect *, struct RTree *);
  184. extern struct Rect RTreeCombineRect(struct Rect *, struct Rect *, struct RTree *);
  185. extern int RTreeCompareRect(struct Rect *, struct Rect *, struct RTree *);
  186. extern int RTreeOverlap(struct Rect *, struct Rect *, struct RTree *);
  187. extern void RTreePrintRect(struct Rect *, int);
  188. /* split.c */
  189. extern void RTreeSplitNode(struct Node *, struct Branch *, struct Node *, struct RTree *);
  190. /* card.c */
  191. extern int RTreeSetNodeMax(int, struct RTree *);
  192. extern int RTreeSetLeafMax(int, struct RTree *);
  193. extern int RTreeGetNodeMax(struct RTree *);
  194. extern int RTreeGetLeafMax(struct RTree *);
  195. /* fileio.c */
  196. extern void RTreeGetNode(struct Node *, off_t, int, struct RTree *);
  197. extern size_t RTreeReadNode(struct Node *, off_t, struct RTree *);
  198. extern void RTreePutNode(struct Node *, off_t, struct RTree *);
  199. extern size_t RTreeWriteNode(struct Node *, struct RTree *);
  200. extern size_t RTreeRewriteNode(struct Node *, off_t, struct RTree *);
  201. extern void RTreeUpdateRect(struct Rect *, struct Node *, off_t, int, struct RTree *);
  202. extern void RTreeAddNodePos(off_t, int, struct RTree *);
  203. extern off_t RTreeGetNodePos(struct RTree *);
  204. extern void RTreeFlushBuffer(struct RTree *);
  205. #endif /* _INDEX_ */