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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2018-2019 Parity Technologies (UK) Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Dynamically sized, write-once, lazily allocating bitfields,
//! e.g. for compact accumulation of votes cast on a block while
//! retaining information on the type of vote and identity of the
//! voter within a voter set.

use crate::std::{cmp::Ordering, iter, ops::BitOr, vec::Vec};
use either::Either;

/// A dynamically sized, write-once (per bit), lazily allocating bitfield.
#[derive(Eq, PartialEq, Clone, Debug, Default)]
pub struct Bitfield {
	bits: Vec<u64>,
}

impl From<Vec<u64>> for Bitfield {
	fn from(bits: Vec<u64>) -> Bitfield {
		Bitfield { bits }
	}
}

impl Bitfield {
	/// Create a new empty bitfield.
	///
	/// Does not allocate.
	pub fn new() -> Bitfield {
		Bitfield { bits: Vec::new() }
	}

	/// Whether the bitfield is blank / empty.
	pub fn is_blank(&self) -> bool {
		self.bits.is_empty()
	}

	/// Merge another bitfield into this bitfield.
	///
	/// As a result, this bitfield has all bits set that are set in either bitfield.
	///
	/// This function only allocates if this bitfield is shorter than the other
	/// bitfield, in which case it is resized accordingly to accomodate for all
	/// bits of the other bitfield.
	pub fn merge(&mut self, other: &Self) -> &mut Self {
		if self.bits.len() < other.bits.len() {
			let new_len = other.bits.len();
			self.bits.resize(new_len, 0);
		}

		for (i, word) in other.bits.iter().enumerate() {
			self.bits[i] |= word;
		}

		self
	}

	/// Set a bit in the bitfield at the specified position.
	///
	/// If the bitfield is not large enough to accomodate for a bit set
	/// at the specified position, it is resized accordingly.
	pub fn set_bit(&mut self, position: usize) -> &mut Self {
		let word_off = position / 64;
		let bit_off = position % 64;

		if word_off >= self.bits.len() {
			let new_len = word_off + 1;
			self.bits.resize(new_len, 0);
		}

		self.bits[word_off] |= 1 << (63 - bit_off);
		self
	}

	/// Test if the bit at the specified position is set.
	#[cfg(test)]
	pub fn test_bit(&self, position: usize) -> bool {
		let word_off = position / 64;

		if word_off >= self.bits.len() {
			return false
		}

		test_bit(self.bits[word_off], position % 64)
	}

	/// Get an iterator over all bits that are set (i.e. 1) at even bit positions.
	pub fn iter1s_even(&self) -> impl Iterator<Item = Bit1> + '_ {
		self.iter1s(0, 1)
	}

	/// Get an iterator over all bits that are set (i.e. 1) at odd bit positions.
	pub fn iter1s_odd(&self) -> impl Iterator<Item = Bit1> + '_ {
		self.iter1s(1, 1)
	}

	/// Get an iterator over all bits that are set (i.e. 1) at even bit positions
	/// when merging this bitfield with another bitfield, without modifying
	/// either bitfield.
	pub fn iter1s_merged_even<'a>(&'a self, other: &'a Self) -> impl Iterator<Item = Bit1> + 'a {
		self.iter1s_merged(other, 0, 1)
	}

	/// Get an iterator over all bits that are set (i.e. 1) at odd bit positions
	/// when merging this bitfield with another bitfield, without modifying
	/// either bitfield.
	pub fn iter1s_merged_odd<'a>(&'a self, other: &'a Self) -> impl Iterator<Item = Bit1> + 'a {
		self.iter1s_merged(other, 1, 1)
	}

	/// Get an iterator over all bits that are set (i.e. 1) in the bitfield,
	/// starting at bit position `start` and moving in steps of size `2^step`
	/// per word.
	fn iter1s(&self, start: usize, step: usize) -> impl Iterator<Item = Bit1> + '_ {
		iter1s(self.bits.iter().cloned(), start, step)
	}

	/// Get an iterator over all bits that are set (i.e. 1) when merging
	/// this bitfield with another bitfield, without modifying either
	/// bitfield, starting at bit position `start` and moving in steps
	/// of size `2^step` per word.
	fn iter1s_merged<'a>(
		&'a self,
		other: &'a Self,
		start: usize,
		step: usize,
	) -> impl Iterator<Item = Bit1> + 'a {
		match self.bits.len().cmp(&other.bits.len()) {
			Ordering::Equal => Either::Left(iter1s(
				self.bits.iter().zip(&other.bits).map(|(a, b)| a | b),
				start,
				step,
			)),
			Ordering::Less => Either::Right(Either::Left(iter1s(
				self.bits.iter().chain(iter::repeat(&0)).zip(&other.bits).map(|(a, b)| a | b),
				start,
				step,
			))),
			Ordering::Greater => Either::Right(Either::Right(iter1s(
				self.bits
					.iter()
					.zip(other.bits.iter().chain(iter::repeat(&0)))
					.map(|(a, b)| a | b),
				start,
				step,
			))),
		}
	}
}

/// Turn an iterator over u64 words into an iterator over bits that
/// are set (i.e. `1`) in these words, starting at bit position `start`
/// and moving in steps of size `2^step` per word.
fn iter1s<'a, I>(iter: I, start: usize, step: usize) -> impl Iterator<Item = Bit1> + 'a
where
	I: Iterator<Item = u64> + 'a,
{
	debug_assert!(start < 64 && step < 7);
	let steps = (64 >> step) - (start >> step);
	iter.enumerate().flat_map(move |(i, word)| {
		let n = if word == 0 { 0 } else { steps };
		(0..n).filter_map(move |j| {
			let bit_pos = start + (j << step);
			if test_bit(word, bit_pos) {
				Some(Bit1 { position: i * 64 + bit_pos })
			} else {
				None
			}
		})
	})
}

fn test_bit(word: u64, position: usize) -> bool {
	let mask = 1 << (63 - position);
	word & mask == mask
}

impl BitOr<&Bitfield> for Bitfield {
	type Output = Bitfield;

	fn bitor(mut self, rhs: &Bitfield) -> Self::Output {
		self.merge(rhs);
		self
	}
}

/// A bit that is set (i.e. 1) in a `Bitfield`.
#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
pub struct Bit1 {
	/// The position of the bit in the bitfield.
	pub position: usize,
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::std::iter;
	use quickcheck::*;

	impl Arbitrary for Bitfield {
		fn arbitrary(g: &mut Gen) -> Bitfield {
			let n = usize::arbitrary(g) % g.size();
			let mut b = iter::from_fn(|| Some(u64::arbitrary(g))).take(n).collect::<Vec<_>>();

			// we need to make sure we don't add empty words at the end of the
			// bitfield otherwise it would break equality on some of the tests
			// below.
			while let Some(0) = b.last() {
				b.pop();
			}

			Bitfield::from(b)
		}
	}

	#[test]
	fn set_bit() {
		fn prop(mut a: Bitfield, idx: usize) -> bool {
			// let's bound the max bitfield index at 2^24. this is needed because when calling
			// `set_bit` we will extend the backing vec to accomodate the given bitfield size, this
			// way we restrict the maximum allocation size to 16MB.
			let idx = idx.min(1 << 24);

			a.set_bit(idx).test_bit(idx)
		}

		quickcheck(prop as fn(_, _) -> _)
	}

	#[test]
	fn bitor() {
		fn prop(a: Bitfield, b: Bitfield) -> bool {
			let c = a.clone() | &b;
			let mut c_bits = c.iter1s(0, 0);
			c_bits.all(|bit| a.test_bit(bit.position) || b.test_bit(bit.position))
		}

		quickcheck(prop as fn(_, _) -> _)
	}

	#[test]
	fn bitor_commutative() {
		fn prop(a: Bitfield, b: Bitfield) -> bool {
			a.clone() | &b == b | &a
		}

		quickcheck(prop as fn(_, _) -> _)
	}

	#[test]
	fn bitor_associative() {
		fn prop(a: Bitfield, b: Bitfield, c: Bitfield) -> bool {
			(a.clone() | &b) | &c == a | &(b | &c)
		}

		quickcheck(prop as fn(_, _, _) -> _)
	}

	#[test]
	fn iter1s() {
		fn all(a: Bitfield) {
			let mut b = Bitfield::new();
			for Bit1 { position } in a.iter1s(0, 0) {
				b.set_bit(position);
			}
			assert_eq!(a, b);
		}

		fn even_odd(a: Bitfield) {
			let mut b = Bitfield::new();
			for Bit1 { position } in a.iter1s_even() {
				assert!(!b.test_bit(position));
				assert!(position % 2 == 0);
				b.set_bit(position);
			}
			for Bit1 { position } in a.iter1s_odd() {
				assert!(!b.test_bit(position));
				assert!(position % 2 == 1);
				b.set_bit(position);
			}
			assert_eq!(a, b);
		}

		quickcheck(all as fn(_));
		quickcheck(even_odd as fn(_));
	}

	#[test]
	fn iter1s_merged() {
		fn all(mut a: Bitfield, b: Bitfield) {
			let mut c = Bitfield::new();
			for bit1 in a.iter1s_merged(&b, 0, 0) {
				c.set_bit(bit1.position);
			}
			assert_eq!(&c, a.merge(&b))
		}

		fn even_odd(mut a: Bitfield, b: Bitfield) {
			let mut c = Bitfield::new();
			for Bit1 { position } in a.iter1s_merged_even(&b) {
				assert!(!c.test_bit(position));
				assert!(position % 2 == 0);
				c.set_bit(position);
			}
			for Bit1 { position } in a.iter1s_merged_odd(&b) {
				assert!(!c.test_bit(position));
				assert!(position % 2 == 1);
				c.set_bit(position);
			}
			assert_eq!(&c, a.merge(&b));
		}

		quickcheck(all as fn(_, _));
		quickcheck(even_odd as fn(_, _));
	}
}