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