blob: 5caec8eb729dbd6869022763e9afeaa91152c6b9 (
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
161
162
163
164
165
166
167
168
169
|
(.module:
[lux (#- not and or)
[control
[equivalence (#+ Equivalence)]
pipe]
[data
["." maybe]
[number
["." i64]]
[text
format]
[collection
["." array ("array/." Fold<Array>)]]]])
(type: #export Chunk I64)
(def: #export chunk-size i64.width)
(type: #export Bits
(Array Chunk))
(def: empty-chunk Chunk (.i64 |0))
(def: #export empty
Bits
(array.new |0))
(def: #export (size bits)
(-> Bits Nat)
(array/fold (function (_ chunk total)
(|> chunk i64.count (n/+ total)))
|0
bits))
(def: #export (capacity bits)
(-> Bits Nat)
(|> bits array.size (n/* chunk-size)))
(def: #export empty?
(-> Bits Bit)
(|>> size (n/= |0)))
(def: #export (get index bits)
(-> Nat Bits Bit)
(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)
(i64.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
[])
.nat)
|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 (.i64 chunk))
(recur (dec size|output))))
output)))))]
[set i64.set]
[clear i64.clear]
[flip i64.flip]
)
(def: #export (intersects? reference sample)
(-> Bits Bits Bit)
(let [chunks (n/min (array.size reference)
(array.size sample))]
(loop [idx |0]
(if (n/< chunks idx)
(.or (|> (..chunk idx sample)
(i64.and (..chunk idx reference))
("lux i64 =" empty-chunk)
.not)
(recur (inc idx)))
#0))))
(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 (|> input (..chunk idx) i64.not .nat)
|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 (.i64 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 (|> (..chunk idx subject)
(<op> (..chunk idx param))
.nat)
|0
(recur (dec size|output) output)
chunk
(|> (if (is? ..empty output)
(: Bits (array.new size|output))
output)
(array.write idx (.i64 chunk))
(recur (dec size|output))))
output)))))]
[and i64.and]
[or i64.or]
[xor i64.xor]
)
(structure: #export _ (Equivalence Bits)
(def: (= reference sample)
(let [size (n/max (array.size reference)
(array.size sample))]
(loop [idx |0]
(if (n/< size idx)
(.and ("lux i64 ="
(..chunk idx reference)
(..chunk idx sample))
(recur (inc idx)))
#1)))))
|