trtree.nim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. discard """
  2. output: '''1 [2, 3, 4, 7]
  3. [0, 0]'''
  4. targets: "c"
  5. joinable: false
  6. disabled: 32bit
  7. cmd: "nim c --gc:arc $file"
  8. """
  9. # bug #13110: This test failed with --gc:arc.
  10. # this test wasn't written for 32 bit
  11. # don't join because the code is too messy.
  12. # Nim RTree and R*Tree implementation
  13. # S. Salewski, 06-JAN-2018
  14. # http://www-db.deis.unibo.it/courses/SI-LS/papers/Gut84.pdf
  15. # http://dbs.mathematik.uni-marburg.de/publications/myPapers/1990/BKSS90.pdf
  16. # RT: range type like float, int
  17. # D: Dimension
  18. # M: Max entries in one node
  19. # LT: leaf type
  20. type
  21. Dim* = static[int]
  22. Ext[RT] = tuple[a, b: RT] # extend (range)
  23. Box*[D: Dim; RT] = array[D, Ext[RT]] # Rectangle for 2D
  24. BoxCenter*[D: Dim; RT] = array[D, RT]
  25. L*[D: Dim; RT, LT] = tuple[b: Box[D, RT]; l: LT] # called Index Entry or index record in the Guttman paper
  26. H[M, D: Dim; RT, LT] = ref object of RootRef
  27. parent: H[M, D, RT, LT]
  28. numEntries: int
  29. level: int
  30. N[M, D: Dim; RT, LT] = tuple[b: Box[D, RT]; n: H[M, D, RT, LT]]
  31. LA[M, D: Dim; RT, LT] = array[M, L[D, RT, LT]]
  32. NA[M, D: Dim; RT, LT] = array[M, N[M, D, RT, LT]]
  33. Leaf[M, D: Dim; RT, LT] = ref object of H[M, D, RT, LT]
  34. a: LA[M, D, RT, LT]
  35. Node[M, D: Dim; RT, LT] = ref object of H[M, D, RT, LT]
  36. a: NA[M, D, RT, LT]
  37. RTree*[M, D: Dim; RT, LT] = ref object of RootRef
  38. root: H[M, D, RT, LT]
  39. bigM: int
  40. m: int
  41. RStarTree*[M, D: Dim; RT, LT] = ref object of RTree[M, D, RT, LT]
  42. firstOverflow: array[32, bool]
  43. p: int
  44. proc newLeaf[M, D: Dim; RT, LT](): Leaf[M, D, RT, LT] =
  45. new result
  46. proc newNode[M, D: Dim; RT, LT](): Node[M, D, RT, LT] =
  47. new result
  48. proc newRTree*[M, D: Dim; RT, LT](minFill: range[30 .. 50] = 40): RTree[M, D, RT, LT] =
  49. assert(M > 1 and M < 101)
  50. new result
  51. result.bigM = M
  52. result.m = M * minFill div 100
  53. result.root = newLeaf[M, D, RT, LT]()
  54. proc newRStarTree*[M, D: Dim; RT, LT](minFill: range[30 .. 50] = 40): RStarTree[M, D, RT, LT] =
  55. assert(M > 1 and M < 101)
  56. new result
  57. result.bigM = M
  58. result.m = M * minFill div 100
  59. result.p = M * 30 div 100
  60. result.root = newLeaf[M, D, RT, LT]()
  61. proc center(r: Box): auto =#BoxCenter[r.len, typeof(r[0].a)] =
  62. var res: BoxCenter[r.len, typeof(r[0].a)]
  63. for i in 0 .. r.high:
  64. when r[0].a is SomeInteger:
  65. res[i] = (r[i].a + r[i].b) div 2
  66. elif r[0].a is SomeFloat:
  67. res[i] = (r[i].a + r[i].b) / 2
  68. else: assert false
  69. return res
  70. proc distance(c1, c2: BoxCenter): auto =
  71. var res: typeof(c1[0])
  72. for i in 0 .. c1.high:
  73. res += (c1[i] - c2[i]) * (c1[i] - c2[i])
  74. return res
  75. proc overlap(r1, r2: Box): auto =
  76. result = typeof(r1[0].a)(1)
  77. for i in 0 .. r1.high:
  78. result *= (min(r1[i].b, r2[i].b) - max(r1[i].a, r2[i].a))
  79. if result <= 0: return 0
  80. proc union(r1, r2: Box): Box =
  81. for i in 0 .. r1.high:
  82. result[i].a = min(r1[i].a, r2[i].a)
  83. result[i].b = max(r1[i].b, r2[i].b)
  84. proc intersect(r1, r2: Box): bool =
  85. for i in 0 .. r1.high:
  86. if r1[i].b < r2[i].a or r1[i].a > r2[i].b:
  87. return false
  88. return true
  89. proc area(r: Box): auto = #typeof(r[0].a) =
  90. result = typeof(r[0].a)(1)
  91. for i in 0 .. r.high:
  92. result *= r[i].b - r[i].a
  93. proc margin(r: Box): auto = #typeof(r[0].a) =
  94. result = typeof(r[0].a)(0)
  95. for i in 0 .. r.high:
  96. result += r[i].b - r[i].a
  97. # how much enlargement does r1 need to include r2
  98. proc enlargement(r1, r2: Box): auto =
  99. area(union(r1, r2)) - area(r1)
  100. proc search*[M, D: Dim; RT, LT](t: RTree[M, D, RT, LT]; b: Box[D, RT]): seq[LT] =
  101. proc s[M, D: Dim; RT, LT](n: H[M, D, RT, LT]; b: Box[D, RT]; res: var seq[LT]) =
  102. if n of Node[M, D, RT, LT]:
  103. let h = Node[M, D, RT, LT](n)
  104. for i in 0 ..< n.numEntries:
  105. if intersect(h.a[i].b, b):
  106. s(h.a[i].n, b, res)
  107. elif n of Leaf[M, D, RT, LT]:
  108. let h = Leaf[M, D, RT, LT](n)
  109. for i in 0 ..< n.numEntries:
  110. if intersect(h.a[i].b, b):
  111. res.add(h.a[i].l)
  112. else: assert false
  113. result = newSeq[LT]()
  114. s(t.root, b, result)
  115. # Insertion
  116. # a R*TREE proc
  117. proc chooseSubtree[M, D: Dim; RT, LT](t: RTree[M, D, RT, LT]; b: Box[D, RT]; level: int): H[M, D, RT, LT] =
  118. assert level >= 0
  119. var it = t.root
  120. while it.level > level:
  121. let nn = Node[M, D, RT, LT](it)
  122. var i0 = 0 # selected index
  123. var minLoss = typeof(b[0].a).high
  124. if it.level == 1: # childreen are leaves -- determine the minimum overlap costs
  125. for i in 0 ..< it.numEntries:
  126. let nx = union(nn.a[i].b, b)
  127. var loss = 0
  128. for j in 0 ..< it.numEntries:
  129. if i == j: continue
  130. loss += (overlap(nx, nn.a[j].b) - overlap(nn.a[i].b, nn.a[j].b)) # overlap (i, j) == (j, i), so maybe cache that?
  131. var rep = loss < minLoss
  132. if loss == minLoss:
  133. let l2 = enlargement(nn.a[i].b, b) - enlargement(nn.a[i0].b, b)
  134. rep = l2 < 0
  135. if l2 == 0:
  136. let l3 = area(nn.a[i].b) - area(nn.a[i0].b)
  137. rep = l3 < 0
  138. if l3 == 0:
  139. rep = nn.a[i].n.numEntries < nn.a[i0].n.numEntries
  140. if rep:
  141. i0 = i
  142. minLoss = loss
  143. else:
  144. for i in 0 ..< it.numEntries:
  145. let loss = enlargement(nn.a[i].b, b)
  146. var rep = loss < minLoss
  147. if loss == minLoss:
  148. let l3 = area(nn.a[i].b) - area(nn.a[i0].b)
  149. rep = l3 < 0
  150. if l3 == 0:
  151. rep = nn.a[i].n.numEntries < nn.a[i0].n.numEntries
  152. if rep:
  153. i0 = i
  154. minLoss = loss
  155. it = nn.a[i0].n
  156. return it
  157. proc pickSeeds[M, D: Dim; RT, LT](t: RTree[M, D, RT, LT]; n: Node[M, D, RT, LT] | Leaf[M, D, RT, LT]; bx: Box[D, RT]): (int, int) =
  158. var i0, j0: int
  159. var bi, bj: typeof(bx)
  160. var largestWaste = typeof(bx[0].a).low
  161. for i in -1 .. n.a.high:
  162. for j in 0 .. n.a.high:
  163. if unlikely(i == j): continue
  164. if unlikely(i < 0):
  165. bi = bx
  166. else:
  167. bi = n.a[i].b
  168. bj = n.a[j].b
  169. let b = union(bi, bj)
  170. let h = area(b) - area(bi) - area(bj)
  171. if h > largestWaste:
  172. largestWaste = h
  173. i0 = i
  174. j0 = j
  175. return (i0, j0)
  176. proc pickNext[M, D: Dim; RT, LT](t: RTree[M, D, RT, LT]; n0, n1, n2: Node[M, D, RT, LT] | Leaf[M, D, RT, LT]; b1, b2: Box[D, RT]): int =
  177. let a1 = area(b1)
  178. let a2 = area(b2)
  179. var d = typeof(a1).low
  180. for i in 0 ..< n0.numEntries:
  181. let d1 = area(union(b1, n0.a[i].b)) - a1
  182. let d2 = area(union(b2, n0.a[i].b)) - a2
  183. if (d1 - d2) * (d1 - d2) > d:
  184. result = i
  185. d = (d1 - d2) * (d1 - d2)
  186. from algorithm import SortOrder, sort
  187. proc sortPlus[T](a: var openArray[T], ax: var T, cmp: proc (x, y: T): int {.closure.}, order = algorithm.SortOrder.Ascending) =
  188. var j = 0
  189. let sign = if order == algorithm.SortOrder.Ascending: 1 else: -1
  190. for i in 1 .. a.high:
  191. if cmp(a[i], a[j]) * sign < 0:
  192. j = i
  193. if cmp(a[j], ax) * sign < 0:
  194. swap(ax, a[j])
  195. a.sort(cmp, order)
  196. # R*TREE procs
  197. proc rstarSplit[M, D: Dim; RT, LT](t: RStarTree[M, D, RT, LT]; n: var Node[M, D, RT, LT] | var Leaf[M, D, RT, LT]; lx: L[D, RT, LT] | N[M, D, RT, LT]): typeof(n) =
  198. type NL = typeof(lx)
  199. var nBest: typeof(n)
  200. new nBest
  201. var lx = lx
  202. when n is Node[M, D, RT, LT]:
  203. lx.n.parent = n
  204. var lxbest: typeof(lx)
  205. var m0 = lx.b[0].a.typeof.high
  206. for d2 in 0 ..< 2 * D:
  207. let d = d2 div 2
  208. if d2 mod 2 == 0:
  209. sortPlus(n.a, lx, proc (x, y: NL): int = cmp(x.b[d].a, y.b[d].a))
  210. else:
  211. sortPlus(n.a, lx, proc (x, y: NL): int = cmp(x.b[d].b, y.b[d].b))
  212. for i in t.m - 1 .. n.a.high - t.m + 1:
  213. var b = lx.b
  214. for j in 0 ..< i: # we can precalculate union() for range 0 .. t.m - 1, but that seems to give no real benefit.Maybe for very large M?
  215. #echo "x",j
  216. b = union(n.a[j].b, b)
  217. var m = margin(b)
  218. b = n.a[^1].b
  219. for j in i ..< n.a.high: # again, precalculation of tail would be possible
  220. #echo "y",j
  221. b = union(n.a[j].b, b)
  222. m += margin(b)
  223. if m < m0:
  224. nbest[] = n[]
  225. lxbest = lx
  226. m0 = m
  227. var i0 = -1
  228. var o0 = lx.b[0].a.typeof.high
  229. for i in t.m - 1 .. n.a.typeof.high - t.m + 1:
  230. var b1 = lxbest.b
  231. for j in 0 ..< i:
  232. b1 = union(nbest.a[j].b, b1)
  233. var b2 = nbest.a[^1].b
  234. for j in i ..< n.a.high:
  235. b2 = union(nbest.a[j].b, b2)
  236. let o = overlap(b1, b2)
  237. if o < o0:
  238. i0 = i
  239. o0 = o
  240. n.a[0] = lxbest
  241. for i in 0 ..< i0:
  242. n.a[i + 1] = nbest.a[i]
  243. new result
  244. result.level = n.level
  245. result.parent = n.parent
  246. for i in i0 .. n.a.high:
  247. result.a[i - i0] = nbest.a[i]
  248. n.numEntries = i0 + 1
  249. result.numEntries = M - i0
  250. when n is Node[M, D, RT, LT]:
  251. for i in 0 ..< result.numEntries:
  252. result.a[i].n.parent = result
  253. proc quadraticSplit[M, D: Dim; RT, LT](t: RTree[M, D, RT, LT]; n: var Node[M, D, RT, LT] | var Leaf[M, D, RT, LT]; lx: L[D, RT, LT] | N[M, D, RT, LT]): typeof(n) =
  254. var n1, n2: typeof(n)
  255. var s1, s2: int
  256. new n1
  257. new n2
  258. n1.parent = n.parent
  259. n2.parent = n.parent
  260. n1.level = n.level
  261. n2.level = n.level
  262. var lx = lx
  263. when n is Node[M, D, RT, LT]:
  264. lx.n.parent = n
  265. (s1, s2) = pickSeeds(t, n, lx.b)
  266. assert s1 >= -1 and s2 >= 0
  267. if unlikely(s1 < 0):
  268. n1.a[0] = lx
  269. else:
  270. n1.a[0] = n.a[s1]
  271. dec(n.numEntries)
  272. if s2 == n.numEntries: # important fix
  273. s2 = s1
  274. n.a[s1] = n.a[n.numEntries]
  275. inc(n1.numEntries)
  276. var b1 = n1.a[0].b
  277. n2.a[0] = n.a[s2]
  278. dec(n.numEntries)
  279. n.a[s2] = n.a[n.numEntries]
  280. inc(n2.numEntries)
  281. var b2 = n2.a[0].b
  282. if s1 >= 0:
  283. n.a[n.numEntries] = lx
  284. inc(n.numEntries)
  285. while n.numEntries > 0 and n1.numEntries < (t.bigM + 1 - t.m) and n2.numEntries < (t.bigM + 1 - t.m):
  286. let next = pickNext(t, n, n1, n2, b1, b2)
  287. let d1 = area(union(b1, n.a[next].b)) - area(b1)
  288. let d2 = area(union(b2, n.a[next].b)) - area(b2)
  289. if (d1 < d2) or (d1 == d2 and ((area(b1) < area(b2)) or (area(b1) == area(b2) and n1.numEntries < n2.numEntries))):
  290. n1.a[n1.numEntries] = n.a[next]
  291. b1 = union(b1, n.a[next].b)
  292. inc(n1.numEntries)
  293. else:
  294. n2.a[n2.numEntries] = n.a[next]
  295. b2 = union(b2, n.a[next].b)
  296. inc(n2.numEntries)
  297. dec(n.numEntries)
  298. n.a[next] = n.a[n.numEntries]
  299. if n.numEntries == 0:
  300. discard
  301. elif n1.numEntries == (t.bigM + 1 - t.m):
  302. while n.numEntries > 0:
  303. dec(n.numEntries)
  304. n2.a[n2.numEntries] = n.a[n.numEntries]
  305. inc(n2.numEntries)
  306. elif n2.numEntries == (t.bigM + 1 - t.m):
  307. while n.numEntries > 0:
  308. dec(n.numEntries)
  309. n1.a[n1.numEntries] = n.a[n.numEntries]
  310. inc(n1.numEntries)
  311. when n is Node[M, D, RT, LT]:
  312. for i in 0 ..< n2.numEntries:
  313. n2.a[i].n.parent = n2
  314. n[] = n1[]
  315. return n2
  316. proc overflowTreatment[M, D: Dim; RT, LT](t: RStarTree[M, D, RT, LT]; n: var Node[M, D, RT, LT] | var Leaf[M, D, RT, LT]; lx: L[D, RT, LT] | N[M, D, RT, LT]): typeof(n)
  317. proc adjustTree[M, D: Dim; RT, LT](t: RTree[M, D, RT, LT]; l, ll: H[M, D, RT, LT]; hb: Box[D, RT]) =
  318. var n = l
  319. var nn = ll
  320. assert n != nil
  321. while true:
  322. if n == t.root:
  323. if nn == nil:
  324. break
  325. t.root = newNode[M, D, RT, LT]()
  326. t.root.level = n.level + 1
  327. Node[M, D, RT, LT](t.root).a[0].n = n
  328. n.parent = t.root
  329. nn.parent = t.root
  330. t.root.numEntries = 1
  331. let p = Node[M, D, RT, LT](n.parent)
  332. var i = 0
  333. while p.a[i].n != n:
  334. inc(i)
  335. var b: typeof(p.a[0].b)
  336. if n of Leaf[M, D, RT, LT]:
  337. when false:#if likely(nn.isNil): # no performance gain
  338. b = union(p.a[i].b, Leaf[M, D, RT, LT](n).a[n.numEntries - 1].b)
  339. else:
  340. b = Leaf[M, D, RT, LT](n).a[0].b
  341. for j in 1 ..< n.numEntries:
  342. b = trtree.union(b, Leaf[M, D, RT, LT](n).a[j].b)
  343. elif n of Node[M, D, RT, LT]:
  344. b = Node[M, D, RT, LT](n).a[0].b
  345. for j in 1 ..< n.numEntries:
  346. b = union(b, Node[M, D, RT, LT](n).a[j].b)
  347. else:
  348. assert false
  349. #if nn.isNil and p.a[i].b == b: break # no performance gain
  350. p.a[i].b = b
  351. n = H[M, D, RT, LT](p)
  352. if unlikely(nn != nil):
  353. if nn of Leaf[M, D, RT, LT]:
  354. b = Leaf[M, D, RT, LT](nn).a[0].b
  355. for j in 1 ..< nn.numEntries:
  356. b = union(b, Leaf[M, D, RT, LT](nn).a[j].b)
  357. elif nn of Node[M, D, RT, LT]:
  358. b = Node[M, D, RT, LT](nn).a[0].b
  359. for j in 1 ..< nn.numEntries:
  360. b = union(b, Node[M, D, RT, LT](nn).a[j].b)
  361. else:
  362. assert false
  363. if p.numEntries < p.a.len:
  364. p.a[p.numEntries].b = b
  365. p.a[p.numEntries].n = nn
  366. inc(p.numEntries)
  367. assert n != nil
  368. nn = nil
  369. else:
  370. let h: N[M, D, RT, LT] = (b, nn)
  371. nn = quadraticSplit(t, p, h)
  372. assert n == H[M, D, RT, LT](p)
  373. assert n != nil
  374. assert t.root != nil
  375. proc insert*[M, D: Dim; RT, LT](t: RTree[M, D, RT, LT]; leaf: N[M, D, RT, LT] | L[D, RT, LT]; level: int = 0) =
  376. when leaf is N[M, D, RT, LT]:
  377. assert level > 0
  378. type NodeLeaf = Node[M, D, RT, LT]
  379. else:
  380. assert level == 0
  381. type NodeLeaf = Leaf[M, D, RT, LT]
  382. for d in leaf.b:
  383. assert d.a <= d.b
  384. let l = NodeLeaf(chooseSubtree(t, leaf.b, level))
  385. if l.numEntries < l.a.len:
  386. l.a[l.numEntries] = leaf
  387. inc(l.numEntries)
  388. when leaf is N[M, D, RT, LT]:
  389. leaf.n.parent = l
  390. adjustTree(t, l, nil, leaf.b)
  391. else:
  392. let l2 = quadraticSplit(t, l, leaf)
  393. assert l2.level == l.level
  394. adjustTree(t, l, l2, leaf.b)
  395. # R*Tree insert procs
  396. proc rsinsert[M, D: Dim; RT, LT](t: RStarTree[M, D, RT, LT]; leaf: N[M, D, RT, LT] | L[D, RT, LT]; level: int)
  397. proc reInsert[M, D: Dim; RT, LT](t: RStarTree[M, D, RT, LT]; n: var Node[M, D, RT, LT] | var Leaf[M, D, RT, LT]; lx: L[D, RT, LT] | N[M, D, RT, LT]) =
  398. type NL = typeof(lx)
  399. var lx = lx
  400. var buf: typeof(n.a)
  401. let p = Node[M, D, RT, LT](n.parent)
  402. var i = 0
  403. while p.a[i].n != n:
  404. inc(i)
  405. let c = center(p.a[i].b)
  406. sortPlus(n.a, lx, proc (x, y: NL): int = cmp(distance(center(x.b), c), distance(center(y.b), c)))
  407. n.numEntries = M - t.p
  408. swap(n.a[n.numEntries], lx)
  409. inc n.numEntries
  410. var b = n.a[0].b
  411. for i in 1 ..< n.numEntries:
  412. b = union(b, n.a[i].b)
  413. p.a[i].b = b
  414. for i in M - t.p + 1 .. n.a.high:
  415. buf[i] = n.a[i]
  416. rsinsert(t, lx, n.level)
  417. for i in M - t.p + 1 .. n.a.high:
  418. rsinsert(t, buf[i], n.level)
  419. proc overflowTreatment[M, D: Dim; RT, LT](t: RStarTree[M, D, RT, LT]; n: var Node[M, D, RT, LT] | var Leaf[M, D, RT, LT]; lx: L[D, RT, LT] | N[M, D, RT, LT]): typeof(n) =
  420. if n.level != t.root.level and t.firstOverflow[n.level]:
  421. t.firstOverflow[n.level] = false
  422. reInsert(t, n, lx)
  423. return nil
  424. else:
  425. let l2 = rstarSplit(t, n, lx)
  426. assert l2.level == n.level
  427. return l2
  428. proc rsinsert[M, D: Dim; RT, LT](t: RStarTree[M, D, RT, LT]; leaf: N[M, D, RT, LT] | L[D, RT, LT]; level: int) =
  429. when leaf is N[M, D, RT, LT]:
  430. assert level > 0
  431. type NodeLeaf = Node[M, D, RT, LT]
  432. else:
  433. assert level == 0
  434. type NodeLeaf = Leaf[M, D, RT, LT]
  435. let l = NodeLeaf(chooseSubtree(t, leaf.b, level))
  436. if l.numEntries < l.a.len:
  437. l.a[l.numEntries] = leaf
  438. inc(l.numEntries)
  439. when leaf is N[M, D, RT, LT]:
  440. leaf.n.parent = l
  441. adjustTree(t, l, nil, leaf.b)
  442. else:
  443. when leaf is N[M, D, RT, LT]: # TODO do we need this?
  444. leaf.n.parent = l
  445. let l2 = overflowTreatment(t, l, leaf)
  446. if l2 != nil:
  447. assert l2.level == l.level
  448. adjustTree(t, l, l2, leaf.b)
  449. proc insert*[M, D: Dim; RT, LT](t: RStarTree[M, D, RT, LT]; leaf: L[D, RT, LT]) =
  450. for d in leaf.b:
  451. assert d.a <= d.b
  452. for i in mitems(t.firstOverflow):
  453. i = true
  454. rsinsert(t, leaf, 0)
  455. # delete
  456. proc findLeaf[M, D: Dim; RT, LT](t: RTree[M, D, RT, LT]; leaf: L[D, RT, LT]): Leaf[M, D, RT, LT] =
  457. proc fl[M, D: Dim; RT, LT](h: H[M, D, RT, LT]; leaf: L[D, RT, LT]): Leaf[M, D, RT, LT] =
  458. var n = h
  459. if n of Node[M, D, RT, LT]:
  460. for i in 0 ..< n.numEntries:
  461. if intersect(Node[M, D, RT, LT](n).a[i].b, leaf.b):
  462. let l = fl(Node[M, D, RT, LT](n).a[i].n, leaf)
  463. if l != nil:
  464. return l
  465. elif n of Leaf[M, D, RT, LT]:
  466. for i in 0 ..< n.numEntries:
  467. if Leaf[M, D, RT, LT](n).a[i] == leaf:
  468. return Leaf[M, D, RT, LT](n)
  469. else:
  470. assert false
  471. return nil
  472. fl(t.root, leaf)
  473. proc condenseTree[M, D: Dim; RT, LT](t: RTree[M, D, RT, LT]; leaf: Leaf[M, D, RT, LT]) =
  474. var n: H[M, D, RT, LT] = leaf
  475. var q = newSeq[H[M, D, RT, LT]]()
  476. var b: typeof(leaf.a[0].b)
  477. while n != t.root:
  478. let p = Node[M, D, RT, LT](n.parent)
  479. var i = 0
  480. while p.a[i].n != n:
  481. inc(i)
  482. if n.numEntries < t.m:
  483. dec(p.numEntries)
  484. p.a[i] = p.a[p.numEntries]
  485. q.add(n)
  486. else:
  487. if n of Leaf[M, D, RT, LT]:
  488. b = Leaf[M, D, RT, LT](n).a[0].b
  489. for j in 1 ..< n.numEntries:
  490. b = union(b, Leaf[M, D, RT, LT](n).a[j].b)
  491. elif n of Node[M, D, RT, LT]:
  492. b = Node[M, D, RT, LT](n).a[0].b
  493. for j in 1 ..< n.numEntries:
  494. b = union(b, Node[M, D, RT, LT](n).a[j].b)
  495. else:
  496. assert false
  497. p.a[i].b = b
  498. n = n.parent
  499. if t of RStarTree[M, D, RT, LT]:
  500. for n in q:
  501. if n of Leaf[M, D, RT, LT]:
  502. for i in 0 ..< n.numEntries:
  503. for i in mitems(RStarTree[M, D, RT, LT](t).firstOverflow):
  504. i = true
  505. rsinsert(RStarTree[M, D, RT, LT](t), Leaf[M, D, RT, LT](n).a[i], 0)
  506. elif n of Node[M, D, RT, LT]:
  507. for i in 0 ..< n.numEntries:
  508. for i in mitems(RStarTree[M, D, RT, LT](t).firstOverflow):
  509. i = true
  510. rsinsert(RStarTree[M, D, RT, LT](t), Node[M, D, RT, LT](n).a[i], n.level)
  511. else:
  512. assert false
  513. else:
  514. for n in q:
  515. if n of Leaf[M, D, RT, LT]:
  516. for i in 0 ..< n.numEntries:
  517. insert(t, Leaf[M, D, RT, LT](n).a[i])
  518. elif n of Node[M, D, RT, LT]:
  519. for i in 0 ..< n.numEntries:
  520. insert(t, Node[M, D, RT, LT](n).a[i], n.level)
  521. else:
  522. assert false
  523. proc delete*[M, D: Dim; RT, LT](t: RTree[M, D, RT, LT]; leaf: L[D, RT, LT]): bool {.discardable.} =
  524. let l = findLeaf(t, leaf)
  525. if l.isNil:
  526. return false
  527. else:
  528. var i = 0
  529. while l.a[i] != leaf:
  530. inc(i)
  531. dec(l.numEntries)
  532. l.a[i] = l.a[l.numEntries]
  533. condenseTree(t, l)
  534. if t.root.numEntries == 1:
  535. if t.root of Node[M, D, RT, LT]:
  536. t.root = Node[M, D, RT, LT](t.root).a[0].n
  537. t.root.parent = nil
  538. return true
  539. var t = [4, 1, 3, 2]
  540. var xt = 7
  541. sortPlus(t, xt, system.cmp, SortOrder.Ascending)
  542. echo xt, " ", t
  543. type
  544. RSE = L[2, int, int]
  545. RSeq = seq[RSE]
  546. proc rseq_search(rs: RSeq; rse: RSE): seq[int] =
  547. result = newSeq[int]()
  548. for i in rs:
  549. if intersect(i.b, rse.b):
  550. result.add(i.l)
  551. proc rseq_delete(rs: var RSeq; rse: RSE): bool =
  552. for i in 0 .. rs.high:
  553. if rs[i] == rse:
  554. #rs.delete(i)
  555. rs[i] = rs[rs.high]
  556. rs.setLen(rs.len - 1)
  557. return true
  558. import random, algorithm
  559. proc test(n: int) =
  560. var b: Box[2, int]
  561. echo center(b)
  562. var x1, x2, y1, y2: int
  563. var t = newRStarTree[8, 2, int, int]()
  564. #var t = newRTree[8, 2, int, int]()
  565. var rs = newSeq[RSE]()
  566. for i in 0 .. 5:
  567. for i in 0 .. n - 1:
  568. x1 = rand(1000)
  569. y1 = rand(1000)
  570. x2 = x1 + rand(25)
  571. y2 = y1 + rand(25)
  572. b = [(x1, x2), (y1, y2)]
  573. let el: L[2, int, int] = (b, i + 7)
  574. t.insert(el)
  575. rs.add(el)
  576. for i in 0 .. (n div 4):
  577. let j = rand(rs.high)
  578. var el = rs[j]
  579. assert t.delete(el)
  580. assert rs.rseq_delete(el)
  581. for i in 0 .. n - 1:
  582. x1 = rand(1000)
  583. y1 = rand(1000)
  584. x2 = x1 + rand(100)
  585. y2 = y1 + rand(100)
  586. b = [(x1, x2), (y1, y2)]
  587. let el: L[2, int, int] = (b, i)
  588. let r = search(t, b)
  589. let r2 = rseq_search(rs, el)
  590. assert r.len == r2.len
  591. assert r.sorted(system.cmp) == r2.sorted(system.cmp)
  592. test(500)