tgenericcomputedrange.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import math
  2. block: # issue #19916
  3. type
  4. Test[S: static[Natural]] = object
  5. a: array[ceilDiv(S, 8), uint8]
  6. let a = Test[32]()
  7. doAssert a.a.len == 4
  8. block: # issue #20514
  9. type Foo[S:static[array[2, int]]] = object
  10. values: array[prod(S), float]
  11. doAssert Foo[[4,5]]().values.len == 20
  12. block: # issue #20937
  13. type
  14. Vec3[T: SomeNumber] {.bycopy.} = tuple[x, y, z: T]
  15. func volume[T](v: Vec3[T]): T =
  16. when T is SomeUnsignedInt:
  17. v.x * v.y * v.z
  18. else:
  19. abs (v.x * v.y * v.z)
  20. type
  21. Matrix3[C: static Vec3[uint], T] = object
  22. cells: array[C.volume, T]
  23. let m = Matrix3[(1.uint, 1.uint, 1.uint), uint](cells: [0.uint])
  24. doAssert m.cells.len == 1
  25. let m2 = Matrix3[(4.uint, 3.uint, 5.uint), uint]()
  26. doAssert m2.cells.len == 60
  27. block: # issue #19284
  28. type Board[N, M: static Slice[int]] = array[len(N)*len(M), int8]
  29. var t: Board[0..4, 0..4]
  30. doAssert t.len == 25
  31. block: # minimal issue #19284
  32. proc foo[T](x: T): int =
  33. result = 0
  34. type Foo[N: static int] = array[0..foo(N), int]
  35. var t: Foo[5]
  36. doAssert t.len == 1
  37. block:
  38. type Foo[T; U: static T] = range[T(0) .. U]
  39. block:
  40. var x: array[Foo[int, 1], int]
  41. x[0] = 1
  42. x[1] = 2
  43. doAssert x == [0: 1, 1: 2]
  44. doAssert x is array[0 .. 1, int]
  45. block:
  46. type Bar = enum a, b, c
  47. var x: array[Foo[Bar, c], int]
  48. x[a] = 1
  49. x[b] = 2
  50. x[c] = 3
  51. doAssert x == [a: 1, b: 2, c: 3]
  52. doAssert x is array[a .. c, int]
  53. block:
  54. type Foo[T; U: static T] = array[T(0) .. U, int]
  55. block:
  56. var x: Foo[int, 1]
  57. x[0] = 1
  58. x[1] = 2
  59. doAssert x == [0: 1, 1: 2]
  60. doAssert x is array[0 .. 1, int]
  61. block:
  62. type Bar = enum a, b, c
  63. var x: Foo[Bar, c]
  64. x[a] = 1
  65. x[b] = 2
  66. x[c] = 3
  67. doAssert x == [a: 1, b: 2, c: 3]
  68. doAssert x is array[a .. c, int]
  69. block:
  70. type Foo[T; U: static T] = array[T(0) .. U + 1, int]
  71. block:
  72. var x: Foo[int, 1]
  73. x[0] = 1
  74. x[1] = 2
  75. x[2] = 3
  76. doAssert x == [0: 1, 1: 2, 2: 3]
  77. doAssert x is array[0 .. 2, int]
  78. block:
  79. type Foo[T; U: static T] = array[T(0) .. (U * 2) + 1, int]
  80. block:
  81. var x: Foo[int, 1]
  82. x[0] = 1
  83. x[1] = 2
  84. x[2] = 3
  85. x[3] = 4
  86. doAssert x == [0: 1, 1: 2, 2: 3, 3: 4]
  87. doAssert x is array[0 .. 3, int]
  88. block: # issue #22187
  89. template m(T: type, s: int64): int64 = s
  90. func p(n: int64): int = int(n)
  91. type F[T; s: static int64] = object
  92. k: array[p(m(T, s)), int64]
  93. var x: F[int, 3]
  94. doAssert x.k is array[3, int64]
  95. block: # issue #22490
  96. proc log2trunc(x: uint64): int =
  97. if x == 0: int(0) else: int(0)
  98. template maxChunkIdx(T: typedesc): int64 = 0'i64
  99. template layer(vIdx: int64): int = log2trunc(0'u64)
  100. type HashList[T] = object
  101. indices: array[int(layer(maxChunkIdx(T))), int]