tconvexhull.nim 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. discard """
  2. output: '''true
  3. true
  4. true
  5. true
  6. true
  7. true'''
  8. ccodeCheck: "\\i ! @'deepCopy(' .*"
  9. """
  10. # parallel convex hull for Nim bigbreak
  11. # nim c --threads:on -d:release pconvex_hull.nim
  12. import algorithm, sequtils, threadpool
  13. type Point = tuple[x, y: float]
  14. proc cmpPoint(a, b: Point): int =
  15. result = cmp(a.x, b.x)
  16. if result == 0:
  17. result = cmp(a.y, b.y)
  18. template cross[T](o, a, b: T): untyped =
  19. (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x)
  20. template pro(): untyped =
  21. while lr1 > 0 and cross(result[lr1 - 1], result[lr1], p[i]) <= 0:
  22. discard result.pop
  23. lr1 -= 1
  24. result.add(p[i])
  25. lr1 += 1
  26. proc half[T](p: seq[T]; upper: bool): seq[T] =
  27. var i, lr1: int
  28. result = @[]
  29. lr1 = -1
  30. if upper:
  31. i = 0
  32. while i <= high(p):
  33. pro()
  34. i += 1
  35. else:
  36. i = high(p)
  37. while i >= low(p):
  38. pro()
  39. i -= 1
  40. discard result.pop
  41. proc convex_hull[T](points: var seq[T], cmp: proc(x, y: T): int {.closure.}) : seq[T] =
  42. if len(points) < 2: return points
  43. points.sort(cmp)
  44. var ul: array[2, FlowVar[seq[T]]]
  45. parallel:
  46. for k in 0..ul.high:
  47. ul[k] = spawn half[T](points, k == 0)
  48. result = concat(^ul[0], ^ul[1])
  49. var s = map(toSeq(0..999999), proc(x: int): Point = (float(x div 1000), float(x mod 1000)))
  50. setMaxPoolSize 2
  51. #echo convex_hull[Point](s, cmpPoint)
  52. for i in 0..5:
  53. echo convex_hull[Point](s, cmpPoint) ==
  54. @[(0.0, 0.0), (999.0, 0.0), (999.0, 999.0), (0.0, 999.0)]