jam_types/
vec_set.rs

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
use super::*;
use alloc::collections::BTreeSet;
use core::fmt;
use scale::Error as DecodeError;

/// A data-structure providing the storage of non-duplicated items and the querying of their
/// inclusion.
pub trait SetLike<T> {
	/// Insert item into the set.
	///
	/// Inserts some item `t` into the set.
	///
	/// Returns `true` iff the item was not already in the set.
	fn insert(&mut self, t: T) -> bool;
	/// Insert multiple items from an iterator.
	fn extend(&mut self, iter: impl Iterator<Item = T>);
	/// Check if a value is in the set.
	///
	/// Returns `true` iff the set contains a value equal to `t`.
	fn contains(&self, t: &T) -> bool;
	/// Remove an item from the set.
	///
	/// Removes the item equal to `t` from the set.
	///
	/// Returns `true` iff the item was previously in the set.
	fn remove(&mut self, t: &T) -> bool;
}

#[cfg(feature = "std")]
impl<T: Eq + std::hash::Hash> SetLike<T> for std::collections::HashSet<T> {
	fn insert(&mut self, t: T) -> bool {
		std::collections::HashSet::<T>::insert(self, t)
	}
	fn extend(&mut self, iter: impl Iterator<Item = T>) {
		<std::collections::HashSet<T> as Extend<T>>::extend(self, iter)
	}
	fn contains(&self, t: &T) -> bool {
		std::collections::HashSet::<T>::contains(self, t)
	}
	fn remove(&mut self, t: &T) -> bool {
		std::collections::HashSet::<T>::remove(self, t)
	}
}

impl<T: Eq + PartialEq + Ord + PartialOrd> SetLike<T> for BTreeSet<T> {
	fn insert(&mut self, t: T) -> bool {
		BTreeSet::<T>::insert(self, t)
	}
	fn extend(&mut self, iter: impl Iterator<Item = T>) {
		<BTreeSet<T> as Extend<T>>::extend(self, iter)
	}
	fn contains(&self, t: &T) -> bool {
		BTreeSet::<T>::contains(self, t)
	}
	fn remove(&mut self, t: &T) -> bool {
		BTreeSet::<T>::remove(self, t)
	}
}

/// An set of items stored in order in a [Vec].
///
/// This is always efficient for small sizes of sets, and efficient for large sizes when
/// insertion is not needed (i.e. items are placed into the set in bulk).
#[derive(Clone, Encode, Eq, PartialEq, Ord, PartialOrd)]
pub struct VecSet<T>(Vec<T>);
impl<T: Eq + PartialEq + Ord + PartialOrd> VecSet<T> {
	/// Create a new, empty instance.
	pub fn new() -> Self {
		Self(Vec::new())
	}
	/// Create a new instance from a sorted [Vec].
	///
	/// Returns [Ok] with an instance containing the same items as `v`, or [Err] if `v` is unsorted.
	pub fn from_sorted(v: Vec<T>) -> Result<Self, ()> {
		let Some(first) = v.first() else { return Ok(Self::new()) };
		v.iter()
			.skip(1)
			.try_fold(first, |a, e| if a < e { Some(e) } else { None })
			.ok_or(())?;
		Ok(Self(v))
	}
	/// Return the number of items this set contains.
	pub fn len(&self) -> usize {
		self.0.len()
	}
	/// Return `true` if this set is empty.
	pub fn is_empty(&self) -> bool {
		self.0.is_empty()
	}
	/// Compares the items in two sets.
	///
	/// Returns `true` iff every item in the set is not found in `other`.
	pub fn is_disjoint(&self, other: &Self) -> bool {
		is_disjoint(&self.0, &other.0)
	}
	/// Return an iterator over the items in this set in order.
	pub fn iter(&self) -> core::slice::Iter<T> {
		self.0.iter()
	}
	/// Consume this set and return a [Vec] of transformed items.
	///
	/// Returns the [Vec] of the resultant values of applying `f` to each item in the set in
	/// order.
	pub fn map<U>(self, f: impl FnMut(T) -> U) -> Vec<U> {
		self.0.into_iter().map(f).collect::<Vec<_>>()
	}
	/// Transform all items by reference and return a [Vec] with the results.
	///
	/// Returns the [Vec] of the resultant values of applying `f` to each item by reference in the
	/// set in order.
	pub fn map_ref<U>(&self, f: impl FnMut(&T) -> U) -> Vec<U> {
		self.0.iter().map(f).collect::<Vec<_>>()
	}
	/// Return a [Vec] of sorted items by cloning this set.
	pub fn to_vec(&self) -> Vec<T>
	where
		T: Clone,
	{
		self.0.clone()
	}
	/// Return a [Vec] of sorted items by consuming this set.
	pub fn into_vec(self) -> Vec<T> {
		self.0
	}
	/// Insert item into the set.
	///
	/// Inserts some item `t` into the set.
	///
	/// Returns `true` iff the item was not already in the set.
	pub fn insert(&mut self, t: T) -> bool {
		match self.0.binary_search(&t) {
			Ok(_) => false,
			Err(i) => {
				self.0.insert(i, t);
				true
			},
		}
	}
	/// Insert multiple items from an iterator.
	pub fn extend(&mut self, iter: impl IntoIterator<Item = T>) {
		self.0.extend(iter);
		self.0.sort_unstable();
		self.0.dedup();
	}
	/// Check if a value is in the set.
	///
	/// Returns `true` iff the set contains a value equal to `t`.
	pub fn contains(&self, t: &T) -> bool {
		self.0.binary_search(t).is_ok()
	}
	/// Remove an item from the set.
	///
	/// Removes the item equal to `t` from the set.
	///
	/// Returns [Some] with the removed item if it was previously in the set.
	pub fn remove(&mut self, t: &T) -> Option<T> {
		match self.0.binary_search(t) {
			Ok(i) => Some(self.0.remove(i)),
			Err(_) => None,
		}
	}
	/// Filter items from the set.
	///
	/// Removes all items from the set for which `f` returns `false`.
	pub fn retain(&mut self, f: impl FnMut(&T) -> bool) {
		self.0.retain(f);
	}
}

impl<T: Eq + PartialEq + Ord + PartialOrd> SetLike<T> for VecSet<T> {
	fn insert(&mut self, t: T) -> bool {
		VecSet::<T>::insert(self, t)
	}
	fn extend(&mut self, iter: impl Iterator<Item = T>) {
		VecSet::<T>::extend(self, iter)
	}
	fn contains(&self, t: &T) -> bool {
		VecSet::<T>::contains(self, t)
	}
	fn remove(&mut self, t: &T) -> bool {
		VecSet::<T>::remove(self, t).is_some()
	}
}

impl<T: fmt::Debug> fmt::Debug for VecSet<T> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self.0.len() {
			0 => write!(f, "[]"),
			_ => {
				write!(f, "[{:?}", self.0[0])?;
				for i in 1..self.0.len() {
					write!(f, ", {:?}", self.0[i])?;
				}
				write!(f, "]")
			},
		}
	}
}

impl<T: fmt::Display> fmt::Display for VecSet<T> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self.0.len() {
			0 => write!(f, "[]"),
			_ => {
				write!(f, "[{}", self.0[0])?;
				for i in 1..self.0.len() {
					write!(f, ", {}", self.0[i])?;
				}
				write!(f, "]")
			},
		}
	}
}

impl<T: Decode + Eq + PartialEq + Ord + PartialOrd> Decode for VecSet<T> {
	fn decode<I: scale::Input>(input: &mut I) -> Result<Self, DecodeError> {
		Ok(Self::from_sorted(Vec::<T>::decode(input)?).map_err(|()| "set out-of-order")?)
	}
}

impl<T: Default + Eq + PartialEq + Ord + PartialOrd> Default for VecSet<T> {
	fn default() -> Self {
		Self::new()
	}
}
impl<T> AsRef<[T]> for VecSet<T> {
	fn as_ref(&self) -> &[T] {
		&self.0[..]
	}
}
impl<T> AsMut<[T]> for VecSet<T> {
	fn as_mut(&mut self) -> &mut [T] {
		&mut self.0[..]
	}
}

impl<T> From<VecSet<T>> for Vec<T> {
	fn from(s: VecSet<T>) -> Vec<T> {
		s.0
	}
}
impl<T: Eq + PartialEq + Ord + PartialOrd> From<Vec<T>> for VecSet<T> {
	fn from(mut v: Vec<T>) -> Self {
		v.sort_unstable();
		Self(v)
	}
}
impl<T: Eq + PartialEq + Ord + PartialOrd> FromIterator<T> for VecSet<T> {
	fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
		Vec::<T>::from_iter(iter).into()
	}
}
impl<T: Eq + PartialEq + Ord + PartialOrd> Extend<T> for VecSet<T> {
	fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
		VecSet::<T>::extend(self, iter)
	}
}
impl<T: Eq + PartialEq + Ord + PartialOrd> IntoIterator for VecSet<T> {
	type Item = T;
	type IntoIter = <Vec<T> as IntoIterator>::IntoIter;

	fn into_iter(self) -> Self::IntoIter {
		self.0.into_iter()
	}
}

pub(crate) fn is_disjoint<X: Ord + PartialOrd>(
	a: impl IntoIterator<Item = X>,
	b: impl IntoIterator<Item = X>,
) -> bool {
	let mut ordered_a = a.into_iter();
	let mut ordered_b = b.into_iter();
	let mut a_next = ordered_a.next();
	let mut b_next = ordered_b.next();
	while let (Some(a), Some(b)) = (a_next.as_ref(), b_next.as_ref()) {
		if a == b {
			return false
		}
		if a < b {
			a_next = ordered_a.next();
		} else {
			b_next = ordered_b.next();
		}
	}
	true
}