summaryrefslogtreecommitdiff
path: root/lib/std/bitset.myr
blob: afc66b404d29c61e6bf9ee3cebc1d1e4a7556133 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use "alloc"
use "die"
use "extremum"
use "mk"
use "slcp"
use "sldup"
use "slfill"
use "types"
use "hashfuncs"

pkg std =
	type bitset = struct
		bits : size[:]
	;;

	const mkbs	: (-> bitset#)
	const bsdup	: (bs : bitset# -> bitset#)
	const bsfree	: (bs : bitset# -> void)
	const bsclear	: (bs : bitset# -> bitset#)

	const bsmax	: (a : bitset# -> size)
	const bscount	: (a : bitset# -> size)

	generic bsput	: (bs : bitset#, v : @a::(integral,numeric) -> bool)
	generic bsdel	: (bs : bitset#, v : @a::(integral,numeric) -> bool)
	generic bshas	: (bs : bitset#, v : @a::(integral,numeric) -> bool)

	const bsdiff	: (a : bitset#, b : bitset# -> void)
	const bsintersect	: (a : bitset#, b : bitset# -> void)
	const bsunion	: (a : bitset#, b : bitset# -> void)
	const bseq	: (a : bitset#, b : bitset# -> bool)
	const bsissubset	: (a : bitset#, b : bitset# -> bool)
	const bshash	: (a : bitset# -> uint64)

	type bsiter = struct
		idx	: size
		bs	: bitset#
	;;

	impl iterable bsiter -> size
	const  bybsvalue	: (bs : bitset# -> bsiter)
;;

const mkbs = {
	-> zalloc()
}

const bsdup = {bs
	-> mk([.bits=sldup(bs.bits)])
}

const bsfree = {bs
	slfree(bs.bits)
	free(bs)
}

const bsclear = {bs
	slfill(bs.bits, 0)
	-> bs
}

const bsmax = {bs
	-> bs.bits.len * sizeof(size) * 8
}

const bscount = {bs
	var n

	n = 0
	for v : bybsvalue(bs)
		n++
	;;
	-> n
}

generic bsput = {bs, v
	var idx, off, changed

	idx = (v : size) / (8*sizeof(size))
	off = (v : size) % (8*sizeof(size))
	ensurelen(bs, idx)

	changed = (bs.bits[idx] & (1 << off)) == 0
	bs.bits[idx] |= (1 << off)
	-> changed
}

generic bsdel = {bs, v
	var idx, off, changed

	changed = false
	idx = (v : size) / (8*sizeof(size))
	off = (v : size) % (8*sizeof(size))
	if idx < bs.bits.len
		changed = (bs.bits[idx] & (1 << off)) != 0
		bs.bits[idx] &= ~(1 << off)
	;;
	-> changed
}

generic bshas = {bs, v
	var idx
	var off

	idx = (v : size) / (8*sizeof(size))
	off = (v : size) % (8*sizeof(size))
	if idx >= bs.bits.len
		-> false
	;;
	-> (bs.bits[idx] & (1 << off)) != 0
}

const bsunion = {a, b
	eqsz(a, b)
	for var i = 0; i < b.bits.len; i++
		a.bits[i] |= b.bits[i]
	;;
}

const bsintersect = {a, b
	var n

	n = min(a.bits.len, b.bits.len)
	for var i = 0; i < n; i++
		a.bits[i] &= b.bits[i]
	;;
}

const bsdiff = {a, b
	var n

	n = min(b.bits.len, a.bits.len)
	for var i = 0; i < n; i++
		a.bits[i] &= ~b.bits[i]
	;;
}

const bsissubset = {a, b
	eqsz(a, b);
	for var i = 0; i < a.bits.len; i++
		if (b.bits[i] & a.bits[i]) != b.bits[i]
			-> false
		;;
	;;
	-> true
}

const bseq = {a, b
	eqsz(a, b)
	for var i = 0; i < a.bits.len; i++
		if a.bits[i] != b.bits[i]
			-> false
		;;
	;;
	-> true
}

const bshash = {a
	var end : size

	end = a.bits.len
	while end > 0 && a.bits[end - 1] == 0
		end--
	;;
	-> std.slhash(a.bits[:end])
}

const ensurelen = {bs, len
	if bs.bits.len <= len
		slzgrow(&bs.bits, len + 1)
	;;
}

const eqsz = {a, b
	var sz

	sz = max(a.bits.len, b.bits.len)
	ensurelen(a, sz)
	ensurelen(b, sz)
}

const bybsvalue = {bs
	-> [.idx=0, .bs=bs]
}

impl iterable bsiter -> size =
	__iternext__ = {itp, valp
		var bs = itp.bs
		var n = bsmax(bs)
		for var i = itp.idx; i < n; i++
			/* fast forward by whole chunks */
			while i < n && bs.bits[i / (8*sizeof(size))] == 0
				i = (i + 8*sizeof(size)) & ~(8*sizeof(size) - 1)
			;;
			if bshas(bs, i)
				itp.idx = i + 1
				valp# = i
				-> true
			;;
		;;
		itp.idx = n
		-> false
	}

	__iterfin__ = {itp, valp
	}
;;