NodeBase.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import ArrayList from '../../../../../java/util/ArrayList'
  2. import Serializable from '../../../../../java/io/Serializable'
  3. export default class NodeBase {
  4. constructor() {
  5. NodeBase.constructor_.apply(this, arguments)
  6. }
  7. static constructor_() {
  8. this._items = new ArrayList()
  9. this._subnode = new Array(4).fill(null)
  10. }
  11. static getSubnodeIndex(env, centrex, centrey) {
  12. let subnodeIndex = -1
  13. if (env.getMinX() >= centrex) {
  14. if (env.getMinY() >= centrey) subnodeIndex = 3
  15. if (env.getMaxY() <= centrey) subnodeIndex = 1
  16. }
  17. if (env.getMaxX() <= centrex) {
  18. if (env.getMinY() >= centrey) subnodeIndex = 2
  19. if (env.getMaxY() <= centrey) subnodeIndex = 0
  20. }
  21. return subnodeIndex
  22. }
  23. hasChildren() {
  24. for (let i = 0; i < 4; i++)
  25. if (this._subnode[i] !== null) return true
  26. return false
  27. }
  28. isPrunable() {
  29. return !(this.hasChildren() || this.hasItems())
  30. }
  31. addAllItems(resultItems) {
  32. resultItems.addAll(this._items)
  33. for (let i = 0; i < 4; i++)
  34. if (this._subnode[i] !== null)
  35. this._subnode[i].addAllItems(resultItems)
  36. return resultItems
  37. }
  38. getNodeCount() {
  39. let subSize = 0
  40. for (let i = 0; i < 4; i++)
  41. if (this._subnode[i] !== null)
  42. subSize += this._subnode[i].size()
  43. return subSize + 1
  44. }
  45. size() {
  46. let subSize = 0
  47. for (let i = 0; i < 4; i++)
  48. if (this._subnode[i] !== null)
  49. subSize += this._subnode[i].size()
  50. return subSize + this._items.size()
  51. }
  52. addAllItemsFromOverlapping(searchEnv, resultItems) {
  53. if (!this.isSearchMatch(searchEnv)) return null
  54. resultItems.addAll(this._items)
  55. for (let i = 0; i < 4; i++)
  56. if (this._subnode[i] !== null)
  57. this._subnode[i].addAllItemsFromOverlapping(searchEnv, resultItems)
  58. }
  59. visitItems(searchEnv, visitor) {
  60. for (let i = this._items.iterator(); i.hasNext(); )
  61. visitor.visitItem(i.next())
  62. }
  63. hasItems() {
  64. return !this._items.isEmpty()
  65. }
  66. remove(itemEnv, item) {
  67. if (!this.isSearchMatch(itemEnv)) return false
  68. let found = false
  69. for (let i = 0; i < 4; i++)
  70. if (this._subnode[i] !== null) {
  71. found = this._subnode[i].remove(itemEnv, item)
  72. if (found) {
  73. if (this._subnode[i].isPrunable()) this._subnode[i] = null
  74. break
  75. }
  76. }
  77. if (found) return found
  78. found = this._items.remove(item)
  79. return found
  80. }
  81. visit(searchEnv, visitor) {
  82. if (!this.isSearchMatch(searchEnv)) return null
  83. this.visitItems(searchEnv, visitor)
  84. for (let i = 0; i < 4; i++)
  85. if (this._subnode[i] !== null)
  86. this._subnode[i].visit(searchEnv, visitor)
  87. }
  88. getItems() {
  89. return this._items
  90. }
  91. depth() {
  92. let maxSubDepth = 0
  93. for (let i = 0; i < 4; i++)
  94. if (this._subnode[i] !== null) {
  95. const sqd = this._subnode[i].depth()
  96. if (sqd > maxSubDepth) maxSubDepth = sqd
  97. }
  98. return maxSubDepth + 1
  99. }
  100. isEmpty() {
  101. let isEmpty = true
  102. if (!this._items.isEmpty()) isEmpty = false; else
  103. for (let i = 0; i < 4; i++)
  104. if (this._subnode[i] !== null)
  105. if (!this._subnode[i].isEmpty()) {
  106. isEmpty = false
  107. break
  108. }
  109. return isEmpty
  110. }
  111. add(item) {
  112. this._items.add(item)
  113. }
  114. get interfaces_() {
  115. return [Serializable]
  116. }
  117. }