titeration_doesnt_copy.nim 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. discard """
  2. output: "true"
  3. """
  4. type
  5. Idx = object
  6. i: int
  7. Node = object
  8. n: int
  9. next: seq[Idx]
  10. FooBar = object
  11. s: seq[Node]
  12. proc `=copy`(dest: var Idx; source: Idx) {.error.}
  13. proc `=copy`(dest: var Node; source: Node) {.error.}
  14. proc `=copy`(dest: var FooBar; source: FooBar) {.error.}
  15. proc doSomething(ss: var seq[int], s: FooBar) =
  16. for i in 0 .. s.s.len-1:
  17. for elm in items s.s[i].next:
  18. ss.add s.s[elm.i].n
  19. when isMainModule:
  20. const foo = FooBar(s: @[Node(n: 1, next: @[Idx(i: 0)])])
  21. var ss: seq[int]
  22. doSomething(ss, foo)
  23. echo ss == @[1]
  24. from sequtils import mapIt
  25. from strutils import join
  26. proc toBinSeq*(b: uint8): seq[uint8] =
  27. ## Return binary sequence from each bits of uint8.
  28. runnableExamples:
  29. from sequtils import repeat
  30. doAssert 0'u8.toBinSeq == 0'u8.repeat(8)
  31. doAssert 0b1010_1010.toBinSeq == @[1'u8, 0, 1, 0, 1, 0, 1, 0]
  32. result = @[]
  33. var c = b
  34. for i in 1..8:
  35. result.add (uint8(c and 0b1000_0000) shr 7)
  36. c = c shl 1
  37. proc toBinString*(data: openArray[uint8], col: int): string =
  38. ## Return binary string from each bits of uint8.
  39. runnableExamples:
  40. doAssert @[0b0000_1111'u8, 0b1010_1010].toBinString(8) == "0000111110101010"
  41. doAssert @[0b1000_0000'u8, 0b0000_0000].toBinString(1) == "10"
  42. result = ""
  43. for b in items data.mapIt(it.toBinSeq.mapIt(it.`$`[0].char)):
  44. for i, c in b:
  45. if i < col:
  46. result.add c
  47. doAssert @[0b0000_1111'u8, 0b1010_1010].toBinString(8) == "0000111110101010"
  48. doAssert @[0b1000_0000'u8, 0b0000_0000].toBinString(1) == "10"
  49. block: # bug #23982
  50. iterator `..`(a, b: ptr int16): ptr int16 = discard
  51. var a: seq[int16] #; let p = a[0].addr
  52. var b: seq[ptr int16]
  53. try:
  54. for x in a[0].addr .. b[1]: # `p .. b[1]` works
  55. discard
  56. except IndexDefect:
  57. discard