Bintree.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import Root from './Root'
  2. import Interval from './Interval'
  3. import ArrayList from '../../../../../java/util/ArrayList'
  4. export default class Bintree {
  5. constructor() {
  6. Bintree.constructor_.apply(this, arguments)
  7. }
  8. static constructor_() {
  9. this._root = null
  10. this._minExtent = 1.0
  11. this._root = new Root()
  12. }
  13. static ensureExtent(itemInterval, minExtent) {
  14. let min = itemInterval.getMin()
  15. let max = itemInterval.getMax()
  16. if (min !== max) return itemInterval
  17. if (min === max) {
  18. min = min - minExtent / 2.0
  19. max = min + minExtent / 2.0
  20. }
  21. return new Interval(min, max)
  22. }
  23. size() {
  24. if (this._root !== null) return this._root.size()
  25. return 0
  26. }
  27. insert(itemInterval, item) {
  28. this.collectStats(itemInterval)
  29. const insertInterval = Bintree.ensureExtent(itemInterval, this._minExtent)
  30. this._root.insert(insertInterval, item)
  31. }
  32. query() {
  33. if (arguments.length === 1) {
  34. if (typeof arguments[0] === 'number') {
  35. const x = arguments[0]
  36. return this.query(new Interval(x, x))
  37. } else if (arguments[0] instanceof Interval) {
  38. const interval = arguments[0]
  39. const foundItems = new ArrayList()
  40. this.query(interval, foundItems)
  41. return foundItems
  42. }
  43. } else if (arguments.length === 2) {
  44. const interval = arguments[0], foundItems = arguments[1]
  45. this._root.addAllItemsFromOverlapping(interval, foundItems)
  46. }
  47. }
  48. iterator() {
  49. const foundItems = new ArrayList()
  50. this._root.addAllItems(foundItems)
  51. return foundItems.iterator()
  52. }
  53. remove(itemInterval, item) {
  54. const insertInterval = Bintree.ensureExtent(itemInterval, this._minExtent)
  55. return this._root.remove(insertInterval, item)
  56. }
  57. collectStats(interval) {
  58. const del = interval.getWidth()
  59. if (del < this._minExtent && del > 0.0) this._minExtent = del
  60. }
  61. depth() {
  62. if (this._root !== null) return this._root.depth()
  63. return 0
  64. }
  65. nodeSize() {
  66. if (this._root !== null) return this._root.nodeSize()
  67. return 0
  68. }
  69. }