NodeBase.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import ArrayList from '../../../../../java/util/ArrayList'
  2. export default class NodeBase {
  3. constructor() {
  4. NodeBase.constructor_.apply(this, arguments)
  5. }
  6. static constructor_() {
  7. this._items = new ArrayList()
  8. this._subnode = new Array(2).fill(null)
  9. }
  10. static getSubnodeIndex(interval, centre) {
  11. let subnodeIndex = -1
  12. if (interval.min >= centre) subnodeIndex = 1
  13. if (interval.max <= centre) subnodeIndex = 0
  14. return subnodeIndex
  15. }
  16. hasChildren() {
  17. for (let i = 0; i < 2; i++)
  18. if (this._subnode[i] !== null) return true
  19. return false
  20. }
  21. isPrunable() {
  22. return !(this.hasChildren() || this.hasItems())
  23. }
  24. addAllItems(items) {
  25. items.addAll(this._items)
  26. for (let i = 0; i < 2; i++)
  27. if (this._subnode[i] !== null)
  28. this._subnode[i].addAllItems(items)
  29. return items
  30. }
  31. size() {
  32. let subSize = 0
  33. for (let i = 0; i < 2; i++)
  34. if (this._subnode[i] !== null)
  35. subSize += this._subnode[i].size()
  36. return subSize + this._items.size()
  37. }
  38. addAllItemsFromOverlapping(interval, resultItems) {
  39. if (interval !== null && !this.isSearchMatch(interval)) return null
  40. resultItems.addAll(this._items)
  41. if (this._subnode[0] !== null) this._subnode[0].addAllItemsFromOverlapping(interval, resultItems)
  42. if (this._subnode[1] !== null) this._subnode[1].addAllItemsFromOverlapping(interval, resultItems)
  43. }
  44. hasItems() {
  45. return !this._items.isEmpty()
  46. }
  47. remove(itemInterval, item) {
  48. if (!this.isSearchMatch(itemInterval)) return false
  49. let found = false
  50. for (let i = 0; i < 2; i++)
  51. if (this._subnode[i] !== null) {
  52. found = this._subnode[i].remove(itemInterval, item)
  53. if (found) {
  54. if (this._subnode[i].isPrunable()) this._subnode[i] = null
  55. break
  56. }
  57. }
  58. if (found) return found
  59. found = this._items.remove(item)
  60. return found
  61. }
  62. getItems() {
  63. return this._items
  64. }
  65. depth() {
  66. let maxSubDepth = 0
  67. for (let i = 0; i < 2; i++)
  68. if (this._subnode[i] !== null) {
  69. const sqd = this._subnode[i].depth()
  70. if (sqd > maxSubDepth) maxSubDepth = sqd
  71. }
  72. return maxSubDepth + 1
  73. }
  74. nodeSize() {
  75. let subSize = 0
  76. for (let i = 0; i < 2; i++)
  77. if (this._subnode[i] !== null)
  78. subSize += this._subnode[i].nodeSize()
  79. return subSize + 1
  80. }
  81. add(item) {
  82. this._items.add(item)
  83. }
  84. }