-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitfield.typ
62 lines (48 loc) · 1.31 KB
/
bitfield.typ
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
57
58
59
60
61
62
#import "bits.typ"
// TODO: This probably should be improved / optimized.
/// Creates a new bitfield of the given dimensions and
/// initializes its values with the given init function.
///
/// A bitfield stores bits in an array of arrays. The value
/// at `bitfield.at(i).at(j)` is the bit in row `i`, column `j`.
///
/// #arg[init] is a function that takes the row and column index of
/// of a bit and returns a #dtype("boolean") to initialize the bit.
#let new(n, m, init:(i,j) => false) = {
return range(n).map(
i => range(m).map(j => init(i,j))
)
}
#let from-str(..str) = {
return str.pos().map(bits.from-str)
}
#let at(field, i, j) = field.at(i).at(j)
#let map(a, func) = {
return a.enumerate().map(((i, c)) => c.enumerate().map(((j, d)) => func(i, j, d)))
}
#let band(a, b) = {
return a.enumerate().map(((i, c)) => bits.band(c, b.at(i)))
}
#let bor(a, b) = {
return a.enumerate().map(((i, c)) => bits.bor(c, b.at(i)))
}
#let bxor(a, b) = {
return a.enumerate().map(((i, c)) => bits.bxor(c, b.at(i)))
}
#let compose(a, b, at:(0,0), center:false) = {
let (m,n) = (
b.len(), b.first().len()
)
let (x,y) = at
if center {
(x, y) = (
x - int(m/2), y - int(n/2)
)
}
for i in range(m) {
for j in range(n) {
a.at(x + i).at(y + j) = b.at(i).at(j)
}
}
return a
}