torc_basic_test.nim 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. discard """
  2. output: "MEM 0"
  3. cmd: "nim c --gc:orc $file"
  4. """
  5. type
  6. Node = ref object
  7. name: char
  8. sccId: int
  9. #a: array[3, Node]
  10. a0, a1, a2: Node
  11. rc: int
  12. proc edge(a, b: Node) =
  13. inc b.rc
  14. if a.a0 == nil: a.a0 = b
  15. elif a.a1 == nil: a.a1 = b
  16. else: a.a2 = b
  17. when false:
  18. var i = 0
  19. while a.a[i] != nil: inc i
  20. a.a[i] = b
  21. proc createNode(name: char): Node =
  22. new result
  23. result.name = name
  24. #[
  25. +--------------------------------+
  26. v |
  27. +---------+ +------+ |
  28. | | | | |
  29. | A +----->+ | +------+------+
  30. +--+------+ | | | |
  31. | | | | C ------------> G <--|
  32. | | R | | |
  33. +--v------+ | | +-------------+
  34. | | | | ^
  35. | B <------+ | |
  36. | | | +--------+
  37. +---------+ | |
  38. +------+
  39. ]#
  40. proc use(x: Node) = discard
  41. proc main =
  42. let a = createNode('A')
  43. let b = createNode('B')
  44. let r = createNode('R')
  45. let c = createNode('C')
  46. a.edge b
  47. a.edge r
  48. r.edge b
  49. r.edge c
  50. let g = createNode('G')
  51. g.edge g
  52. g.edge g
  53. c.edge g
  54. c.edge a
  55. use g
  56. use b
  57. proc buildComplexGraph: Node =
  58. # see https://en.wikipedia.org/wiki/Strongly_connected_component for the
  59. # graph:
  60. let a = createNode('a')
  61. let b = createNode('b')
  62. let c = createNode('c')
  63. let d = createNode('d')
  64. let e = createNode('e')
  65. a.edge c
  66. c.edge b
  67. c.edge e
  68. b.edge a
  69. d.edge c
  70. e.edge d
  71. let f = createNode('f')
  72. b.edge f
  73. e.edge f
  74. let g = createNode('g')
  75. let h = createNode('h')
  76. let i = createNode('i')
  77. f.edge g
  78. f.edge i
  79. g.edge h
  80. h.edge i
  81. i.edge g
  82. let j = createNode('j')
  83. h.edge j
  84. i.edge j
  85. let k = createNode('k')
  86. let l = createNode('l')
  87. f.edge k
  88. k.edge l
  89. l.edge k
  90. k.edge j
  91. let m = createNode('m')
  92. let n = createNode('n')
  93. let p = createNode('p')
  94. let q = createNode('q')
  95. m.edge n
  96. n.edge p
  97. n.edge q
  98. q.edge p
  99. p.edge m
  100. q.edge k
  101. d.edge m
  102. e.edge n
  103. result = a
  104. proc main2 =
  105. let g = buildComplexGraph()
  106. main()
  107. main2()
  108. GC_fullCollect()
  109. echo "MEM ", getOccupiedMem()