AbstractSTRtree.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import ItemBoundable from './ItemBoundable'
  2. import hasInterface from '../../../../../hasInterface'
  3. import ItemVisitor from '../ItemVisitor'
  4. import AbstractNode from './AbstractNode'
  5. import Collections from '../../../../../java/util/Collections'
  6. import ArrayList from '../../../../../java/util/ArrayList'
  7. import Serializable from '../../../../../java/io/Serializable'
  8. import Assert from '../../util/Assert'
  9. import List from '../../../../../java/util/List'
  10. export default class AbstractSTRtree {
  11. constructor() {
  12. AbstractSTRtree.constructor_.apply(this, arguments)
  13. }
  14. static constructor_() {
  15. this._root = null
  16. this._built = false
  17. this._itemBoundables = new ArrayList()
  18. this._nodeCapacity = null
  19. if (arguments.length === 0) {
  20. AbstractSTRtree.constructor_.call(this, AbstractSTRtree.DEFAULT_NODE_CAPACITY)
  21. } else if (arguments.length === 1) {
  22. const nodeCapacity = arguments[0]
  23. Assert.isTrue(nodeCapacity > 1, 'Node capacity must be greater than 1')
  24. this._nodeCapacity = nodeCapacity
  25. }
  26. }
  27. static compareDoubles(a, b) {
  28. return a > b ? 1 : a < b ? -1 : 0
  29. }
  30. queryInternal() {
  31. if (hasInterface(arguments[2], ItemVisitor) && (arguments[0] instanceof Object && arguments[1] instanceof AbstractNode)) {
  32. const searchBounds = arguments[0], node = arguments[1], visitor = arguments[2]
  33. const childBoundables = node.getChildBoundables()
  34. for (let i = 0; i < childBoundables.size(); i++) {
  35. const childBoundable = childBoundables.get(i)
  36. if (!this.getIntersectsOp().intersects(childBoundable.getBounds(), searchBounds))
  37. continue
  38. if (childBoundable instanceof AbstractNode)
  39. this.queryInternal(searchBounds, childBoundable, visitor)
  40. else if (childBoundable instanceof ItemBoundable)
  41. visitor.visitItem(childBoundable.getItem())
  42. else
  43. Assert.shouldNeverReachHere()
  44. }
  45. } else if (hasInterface(arguments[2], List) && (arguments[0] instanceof Object && arguments[1] instanceof AbstractNode)) {
  46. const searchBounds = arguments[0], node = arguments[1], matches = arguments[2]
  47. const childBoundables = node.getChildBoundables()
  48. for (let i = 0; i < childBoundables.size(); i++) {
  49. const childBoundable = childBoundables.get(i)
  50. if (!this.getIntersectsOp().intersects(childBoundable.getBounds(), searchBounds))
  51. continue
  52. if (childBoundable instanceof AbstractNode)
  53. this.queryInternal(searchBounds, childBoundable, matches)
  54. else if (childBoundable instanceof ItemBoundable)
  55. matches.add(childBoundable.getItem())
  56. else
  57. Assert.shouldNeverReachHere()
  58. }
  59. }
  60. }
  61. getNodeCapacity() {
  62. return this._nodeCapacity
  63. }
  64. lastNode(nodes) {
  65. return nodes.get(nodes.size() - 1)
  66. }
  67. size() {
  68. if (arguments.length === 0) {
  69. if (this.isEmpty())
  70. return 0
  71. this.build()
  72. return this.size(this._root)
  73. } else if (arguments.length === 1) {
  74. const node = arguments[0]
  75. let size = 0
  76. for (let i = node.getChildBoundables().iterator(); i.hasNext(); ) {
  77. const childBoundable = i.next()
  78. if (childBoundable instanceof AbstractNode)
  79. size += this.size(childBoundable)
  80. else if (childBoundable instanceof ItemBoundable)
  81. size += 1
  82. }
  83. return size
  84. }
  85. }
  86. removeItem(node, item) {
  87. let childToRemove = null
  88. for (let i = node.getChildBoundables().iterator(); i.hasNext(); ) {
  89. const childBoundable = i.next()
  90. if (childBoundable instanceof ItemBoundable)
  91. if (childBoundable.getItem() === item) childToRemove = childBoundable
  92. }
  93. if (childToRemove !== null) {
  94. node.getChildBoundables().remove(childToRemove)
  95. return true
  96. }
  97. return false
  98. }
  99. itemsTree() {
  100. if (arguments.length === 0) {
  101. this.build()
  102. const valuesTree = this.itemsTree(this._root)
  103. if (valuesTree === null) return new ArrayList()
  104. return valuesTree
  105. } else if (arguments.length === 1) {
  106. const node = arguments[0]
  107. const valuesTreeForNode = new ArrayList()
  108. for (let i = node.getChildBoundables().iterator(); i.hasNext(); ) {
  109. const childBoundable = i.next()
  110. if (childBoundable instanceof AbstractNode) {
  111. const valuesTreeForChild = this.itemsTree(childBoundable)
  112. if (valuesTreeForChild !== null) valuesTreeForNode.add(valuesTreeForChild)
  113. } else if (childBoundable instanceof ItemBoundable) {
  114. valuesTreeForNode.add(childBoundable.getItem())
  115. } else {
  116. Assert.shouldNeverReachHere()
  117. }
  118. }
  119. if (valuesTreeForNode.size() <= 0) return null
  120. return valuesTreeForNode
  121. }
  122. }
  123. insert(bounds, item) {
  124. Assert.isTrue(!this._built, 'Cannot insert items into an STR packed R-tree after it has been built.')
  125. this._itemBoundables.add(new ItemBoundable(bounds, item))
  126. }
  127. boundablesAtLevel() {
  128. if (arguments.length === 1) {
  129. const level = arguments[0]
  130. const boundables = new ArrayList()
  131. this.boundablesAtLevel(level, this._root, boundables)
  132. return boundables
  133. } else if (arguments.length === 3) {
  134. const level = arguments[0], top = arguments[1], boundables = arguments[2]
  135. Assert.isTrue(level > -2)
  136. if (top.getLevel() === level) {
  137. boundables.add(top)
  138. return null
  139. }
  140. for (let i = top.getChildBoundables().iterator(); i.hasNext(); ) {
  141. const boundable = i.next()
  142. if (boundable instanceof AbstractNode) {
  143. this.boundablesAtLevel(level, boundable, boundables)
  144. } else {
  145. Assert.isTrue(boundable instanceof ItemBoundable)
  146. if (level === -1)
  147. boundables.add(boundable)
  148. }
  149. }
  150. return null
  151. }
  152. }
  153. query() {
  154. if (arguments.length === 1) {
  155. const searchBounds = arguments[0]
  156. this.build()
  157. const matches = new ArrayList()
  158. if (this.isEmpty())
  159. return matches
  160. if (this.getIntersectsOp().intersects(this._root.getBounds(), searchBounds))
  161. this.queryInternal(searchBounds, this._root, matches)
  162. return matches
  163. } else if (arguments.length === 2) {
  164. const searchBounds = arguments[0], visitor = arguments[1]
  165. this.build()
  166. if (this.isEmpty())
  167. return null
  168. if (this.getIntersectsOp().intersects(this._root.getBounds(), searchBounds))
  169. this.queryInternal(searchBounds, this._root, visitor)
  170. }
  171. }
  172. build() {
  173. if (this._built) return null
  174. this._root = this._itemBoundables.isEmpty() ? this.createNode(0) : this.createHigherLevels(this._itemBoundables, -1)
  175. this._itemBoundables = null
  176. this._built = true
  177. }
  178. getRoot() {
  179. this.build()
  180. return this._root
  181. }
  182. remove() {
  183. if (arguments.length === 2) {
  184. const searchBounds = arguments[0], item = arguments[1]
  185. this.build()
  186. if (this.getIntersectsOp().intersects(this._root.getBounds(), searchBounds))
  187. return this.remove(searchBounds, this._root, item)
  188. return false
  189. } else if (arguments.length === 3) {
  190. const searchBounds = arguments[0], node = arguments[1], item = arguments[2]
  191. let found = this.removeItem(node, item)
  192. if (found) return true
  193. let childToPrune = null
  194. for (let i = node.getChildBoundables().iterator(); i.hasNext(); ) {
  195. const childBoundable = i.next()
  196. if (!this.getIntersectsOp().intersects(childBoundable.getBounds(), searchBounds))
  197. continue
  198. if (childBoundable instanceof AbstractNode) {
  199. found = this.remove(searchBounds, childBoundable, item)
  200. if (found) {
  201. childToPrune = childBoundable
  202. break
  203. }
  204. }
  205. }
  206. if (childToPrune !== null)
  207. if (childToPrune.getChildBoundables().isEmpty())
  208. node.getChildBoundables().remove(childToPrune)
  209. return found
  210. }
  211. }
  212. createHigherLevels(boundablesOfALevel, level) {
  213. Assert.isTrue(!boundablesOfALevel.isEmpty())
  214. const parentBoundables = this.createParentBoundables(boundablesOfALevel, level + 1)
  215. if (parentBoundables.size() === 1)
  216. return parentBoundables.get(0)
  217. return this.createHigherLevels(parentBoundables, level + 1)
  218. }
  219. depth() {
  220. if (arguments.length === 0) {
  221. if (this.isEmpty())
  222. return 0
  223. this.build()
  224. return this.depth(this._root)
  225. } else if (arguments.length === 1) {
  226. const node = arguments[0]
  227. let maxChildDepth = 0
  228. for (let i = node.getChildBoundables().iterator(); i.hasNext(); ) {
  229. const childBoundable = i.next()
  230. if (childBoundable instanceof AbstractNode) {
  231. const childDepth = this.depth(childBoundable)
  232. if (childDepth > maxChildDepth) maxChildDepth = childDepth
  233. }
  234. }
  235. return maxChildDepth + 1
  236. }
  237. }
  238. createParentBoundables(childBoundables, newLevel) {
  239. Assert.isTrue(!childBoundables.isEmpty())
  240. const parentBoundables = new ArrayList()
  241. parentBoundables.add(this.createNode(newLevel))
  242. const sortedChildBoundables = new ArrayList(childBoundables)
  243. Collections.sort(sortedChildBoundables, this.getComparator())
  244. for (let i = sortedChildBoundables.iterator(); i.hasNext(); ) {
  245. const childBoundable = i.next()
  246. if (this.lastNode(parentBoundables).getChildBoundables().size() === this.getNodeCapacity())
  247. parentBoundables.add(this.createNode(newLevel))
  248. this.lastNode(parentBoundables).addChildBoundable(childBoundable)
  249. }
  250. return parentBoundables
  251. }
  252. isEmpty() {
  253. if (!this._built) return this._itemBoundables.isEmpty()
  254. return this._root.isEmpty()
  255. }
  256. get interfaces_() {
  257. return [Serializable]
  258. }
  259. }
  260. function IntersectsOp() {}
  261. AbstractSTRtree.IntersectsOp = IntersectsOp
  262. AbstractSTRtree.DEFAULT_NODE_CAPACITY = 10