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
//! Vectors containing Boolean values are essentially sets
//! represented by their characteristic functions.
//! They are useful since they allow many computations
//! to be implemented as simple linear algebra over the two-element semi-ring.

use core::util::indexer::Indexer;
use std::ops::{BitAnd, BitOr, Index, IndexMut, Not};

/// A vector with values in `bool`, the two-element semi-ring.
/// For convenience and a poor emulation of type-checking
/// we do not index these over integers directly
/// but use an `Indexer`.
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct BoolVec<I: Indexer> {
	data: Vec<bool>,
	indexer: I,
}

impl<I: Indexer> Index<I::Index> for BoolVec<I> {
	type Output = bool;

	fn index(&self, i: I::Index) -> &Self::Output {
		&self.data[self.indexer.to_num(i)]
	}
}

impl<I: Indexer> IndexMut<I::Index> for BoolVec<I> {
	fn index_mut(&mut self, i: I::Index) -> &mut Self::Output {
		&mut self.data[self.indexer.to_num(i)]
	}
}

// TODO: Allow iteration over set / true positions?
impl<I> BoolVec<I>
where
	I: Indexer,
{
	/// Wrap an existing Vec<bool> into a Boolen vector.
	pub fn from_data(data: Vec<bool>, indexer: I) -> BoolVec<I> {
		assert_eq!(data.len(), indexer.range());
		BoolVec { data, indexer }
	}

	/// Create a new Boolean vector with all positions being unset.
	///
	/// # Examples
	///
	/// ```
	/// # use gorrosion::core::util::bool_vec::BoolVec;
	/// let indexer = 17;
	/// let falses = BoolVec::falses(indexer);
	/// for i in 0..17 {
	///	assert_eq!(falses[i], false);
	/// }
	/// ```
	pub fn falses(indexer: I) -> Self {
		let size = indexer.range();
		let data = vec![false; size];
		BoolVec { data, indexer }
	}

	/// Create a new Boolean vector with all positions being set.
	///
	/// # Examples
	///
	/// ```
	/// # use gorrosion::core::util::bool_vec::BoolVec;
	/// let indexer = 23;
	/// let trues = BoolVec::trues(indexer);
	/// for i in 0..23 {
	///	assert_eq!(trues[i], true);
	/// }
	/// ```
	pub fn trues(indexer: I) -> Self {
		let size = indexer.range();
		let data = vec![true; size];
		BoolVec { data, indexer }
	}

	/// Consider a vector as a map into the Booleans
	/// and apply a unary operation pointwise.
	// TODO: Find common abstraction of bit_map_unary and bit_map_binary.
	// TODO: Implement consuming version.
	fn bit_map_unary<F: Fn(bool) -> bool>(&self, op: F) -> Self {
		let indexer = self.indexer.clone();
		let size = self.data.len();
		let mut data = Vec::with_capacity(size);
		for i in 0..size {
			data.push(op(self.data[i]));
		}
		BoolVec { data, indexer }
	}

	/// Consider two vectors as maps into the Booleans
	/// and apply a binary operation pointwise.
	fn bit_map_binary<F: Fn(bool, bool) -> bool>(
		&self,
		other: &Self,
		op: F,
	) -> Self {
		assert_eq!(self.indexer, other.indexer);
		let indexer = self.indexer.clone();
		let size = self.data.len();
		let mut data = Vec::with_capacity(size);
		for i in 0..size {
			data.push(op(self.data[i], other.data[i]));
		}
		BoolVec { data, indexer }
	}

	/// Intersect two vectors considered as sets.
	///
	/// # Examples
	///
	/// ```
	/// # use gorrosion::core::util::bool_vec::BoolVec;
	/// let indexer = 3;
	/// let a = BoolVec::from_data(vec![true, true, false], indexer);
	/// let b = BoolVec::from_data(vec![false, true, true], indexer);
	/// let c = BoolVec::from_data(vec![false, true, false], indexer);
	/// assert_eq!(a.intersection(&b), c);
	/// ```
	pub fn intersection(&self, other: &Self) -> Self {
		self & other
	}

	/// Unite two vectors considered as sets.
	///
	/// # Examples
	///
	/// ```
	/// # use gorrosion::core::util::bool_vec::BoolVec;
	/// let indexer = 3;
	/// let a = BoolVec::from_data(vec![false, true, false], indexer);
	/// let b = BoolVec::from_data(vec![false, false, true], indexer);
	/// let c = BoolVec::from_data(vec![false, true, true], indexer);
	/// assert_eq!(a.union(&b), c);
	/// ```
	pub fn union(&self, other: &Self) -> Self {
		self | other
	}

	/// Take the complement of a vector
	/// considered as subset of the all-true vector of the same length.
	///
	/// # Examples
	///
	/// ```
	/// # use gorrosion::core::util::bool_vec::BoolVec;
	/// let indexer = 3;
	/// let a = BoolVec::from_data(vec![true, true, false], indexer);
	/// let b = BoolVec::from_data(vec![false, false, true], indexer);
	/// assert_eq!(a.complement(), b);
	/// ```
	pub fn complement(&self) -> Self {
		!self
	}

	/// Change the way the vector is indexed.
	///
	/// # Examples
	///
	/// ```
	/// # use gorrosion::core::util::bool_vec::BoolVec;
	/// # use gorrosion::core::util::indexer;
	/// let rect = indexer::Rect::new(2, 3);
	/// let num = 6;
	/// let vec_rect = BoolVec::falses(rect);
	/// let vec_num = BoolVec::falses(num);
	/// assert_eq!(vec_num, vec_rect.reindex(num));
	/// ```
	pub fn reindex<J>(self, indexer: J) -> BoolVec<J>
	where
		J: Indexer,
	{
		assert_eq!(self.indexer.range(), indexer.range());
		let data = self.data;
		BoolVec { indexer, data }
	}

	/// Get a reference to the indexer.
	pub fn indexer(&self) -> &I {
		&self.indexer
	}
}

impl<'now, I> BitAnd for &'now BoolVec<I>
where
	I: Indexer,
{
	type Output = BoolVec<I>;

	fn bitand(self, other: Self) -> Self::Output {
		BoolVec::bit_map_binary(self, other, BitAnd::bitand)
	}
}

impl<'now, I> BitOr for &'now BoolVec<I>
where
	I: Indexer,
{
	type Output = BoolVec<I>;

	fn bitor(self, other: Self) -> Self::Output {
		BoolVec::bit_map_binary(self, other, BitOr::bitor)
	}
}

impl<'now, I> Not for &'now BoolVec<I>
where
	I: Indexer,
{
	type Output = BoolVec<I>;

	fn not(self) -> Self::Output {
		BoolVec::bit_map_unary(self, Not::not)
	}
}

// TODO: Only keep until code coverage tools can handle doc tests.
#[cfg(test)]
mod tests {
	use super::BoolVec;
	use core::util::indexer;

	#[test]
	fn doc_tests() {
		let indexer = 17;
		let falses = BoolVec::falses(indexer);
		for i in 0..17 {
			assert_eq!(falses[i], false);
		}
		let indexer = 23;
		let trues = BoolVec::trues(indexer);
		for i in 0..23 {
			assert_eq!(trues[i], true);
		}
		let indexer = 3;
		let a = BoolVec::from_data(vec![true, true, false], indexer);
		let b = BoolVec::from_data(vec![false, true, true], indexer);
		let c = BoolVec::from_data(vec![false, true, false], indexer);
		assert_eq!(a.intersection(&b), c);
		let indexer = 3;
		let a = BoolVec::from_data(vec![false, true, false], indexer);
		let b = BoolVec::from_data(vec![false, false, true], indexer);
		let c = BoolVec::from_data(vec![false, true, true], indexer);
		assert_eq!(a.union(&b), c);
		let indexer = 3;
		let a = BoolVec::from_data(vec![true, true, false], indexer);
		let b = BoolVec::from_data(vec![false, false, true], indexer);
		assert_eq!(a.complement(), b);
		let rect = indexer::Rect::new(2, 3);
		let num = 6;
		let vec_rect = BoolVec::falses(rect);
		let vec_num = BoolVec::falses(num);
		assert_eq!(vec_num, vec_rect.reindex(num));
	}
}