blob: 84e536676a5cfc90c53f418b7f766c0bebb0020c (
plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
(.module:
[lux #- not and or]
(lux (control [equality #+ Eq]
pipe)
(data [maybe]
[bit]
(coll [array "array/" Fold<Array>])
text/format)))
(type: #export Chunk Nat)
(def: #export chunk-size bit.width)
(type: #export Bits
(Array Chunk))
(def: empty-chunk Chunk +0)
(def: #export empty
Bits
(array.new +0))
(def: #export (size bits)
(-> Bits Nat)
(array/fold (function (_ chunk total)
(n/+ total (bit.count chunk)))
+0
bits))
(def: #export (capacity bits)
(-> Bits Nat)
(|> bits array.size (n/* chunk-size)))
(def: #export (empty? bits)
(-> Bits Bool)
(n/= +0 (size bits)))
(def: #export (get index bits)
(-> Nat Bits Bool)
(let [[chunk-index bit-index] (n//% chunk-size index)]
(.and (n/< (array.size bits) chunk-index)
(|> (array.read chunk-index bits)
(maybe.default empty-chunk)
(bit.set? bit-index)))))
(def: (chunk idx bits)
(-> Nat Bits Chunk)
(if (n/< (array.size bits) idx)
(|> bits (array.read idx) (maybe.default empty-chunk))
empty-chunk))
(do-template [<name> <op>]
[(def: #export (<name> index input)
(-> Nat Bits Bits)
(let [[chunk-index bit-index] (n//% chunk-size index)]
(loop [size|output (n/max (inc chunk-index)
(array.size input))
output ..empty]
(let [idx|output (dec size|output)]
(if (n/> +0 size|output)
(case (|> (chunk idx|output input)
(cond> [(new> (n/= chunk-index idx|output))]
[(<op> bit-index)]
## else
[]))
+0
## TODO: Remove 'no-op' once new-luxc is the official compiler.
(let [no-op (recur (dec size|output) output)]
no-op)
chunk
(|> (if (is? ..empty output)
(: Bits (array.new size|output))
output)
(array.write idx|output chunk)
(recur (dec size|output))))
output)))))]
[set bit.set]
[clear bit.clear]
[flip bit.flip]
)
(def: #export (intersects? reference sample)
(-> Bits Bits Bool)
(let [chunks (n/min (array.size reference)
(array.size sample))]
(loop [idx +0]
(if (n/< chunks idx)
(.or (|> (chunk idx sample)
(bit.and (chunk idx reference))
(n/= empty-chunk)
.not)
(recur (inc idx)))
false))))
(def: #export (not input)
(-> Bits Bits)
(case (array.size input)
+0
..empty
size|output
(loop [size|output size|output
output ..empty]
(let [idx (dec size|output)]
(case (bit.not (chunk idx input))
+0
(recur (dec size|output) output)
chunk
(if (n/> +0 size|output)
(|> (if (is? ..empty output)
(: Bits (array.new size|output))
output)
(array.write idx chunk)
(recur (dec size|output)))
output))))))
(do-template [<name> <op>]
[(def: #export (<name> param subject)
(-> Bits Bits Bits)
(case (n/max (array.size param)
(array.size subject))
+0
..empty
size|output
(loop [size|output size|output
output ..empty]
(let [idx (dec size|output)]
(if (n/> +0 size|output)
(case (<op> (chunk idx param) (chunk idx subject))
+0
(recur (dec size|output) output)
chunk
(|> (if (is? ..empty output)
(: Bits (array.new size|output))
output)
(array.write idx chunk)
(recur (dec size|output))))
output)))))]
[and bit.and]
[or bit.or]
[xor bit.xor]
)
(struct: #export _ (Eq Bits)
(def: (= reference sample)
(let [size|= (n/max (array.size reference)
(array.size sample))]
(loop [idx +0]
(if (n/< size|= idx)
(.and (n/= (chunk idx reference)
(chunk idx sample))
(recur (inc idx)))
true)))))
|