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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
//! Diving deeper into mathematics, huh? In here you'll find mathematical sets - Sometimes pretty handy!
/// Brings mathematical sets into Rust.
#[derive(Debug, Clone)]
pub struct VecSet<'a, T> {
elements: Vec<T>,
superset: Option<&'a VecSet<'a, T>>,
}
// Main impl
impl<'a, T: Copy + Ord> VecSet<'a, T> {
/// Creates a new `VecSet`.
///
/// # Arguments
/// * `values` - The values for the `VecSet`.
///
/// # Returns
/// A new `VecSet`.
///
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
///
/// let set = VecSet::new(&vec![0, 1, 1, 2, 3, 4, 5]);
///
/// assert_eq!(set.elements(), &vec![0, 1, 2, 3, 4, 5]);
/// ```
#[must_use]
pub fn new(values: &[T]) -> VecSet<'a, T> {
let mut res: VecSet<T> = VecSet { elements: values.to_vec(),
superset: None };
res.elements.sort_unstable();
res.elements.dedup();
res
}
/// Creates a new `VecSet` using a parent-`VecSet` to which it applies a closure.
///
/// # Arguments
/// * `parent` - The `VecSet` from which the new `VecSet` emerges.
/// * `f` - The closure after which the new `VecSet` is created.
///
/// # Returns
/// A child `VecSet`.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// let test1: VecSet<u8> = VecSet::new(&vec![0,1,2,3,4]);
/// let from_parent: VecSet<u8> = VecSet::<u8>::new_subset(&test1, |x| x % 2 == 0);
/// assert_eq!(from_parent, VecSet::new(&vec![0,2,4]));
/// assert_eq!(test1.elements(), &vec![0,1,2,3,4])
/// ```
#[must_use]
pub fn new_subset<F: Fn(T) -> bool>(parent: &'a VecSet<T>, f: F) -> VecSet<'a, T> {
let mut res: VecSet<T> = VecSet { elements: Vec::with_capacity(parent.cardinality()),
superset: Some(parent) };
for elem in &parent.elements {
if f(*elem) {
res.elements.push(*elem);
}
}
res
}
/// Does a mathematical union on two VecSets.
/// `self ∪ other`.
/// # Arguments
/// * `self` - The first set.
/// * `other` - The second set.
///
/// # Returns
/// A new `VecSet<T>`: `self ∪ other`.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// use lib_rapid::math::sets::vec_sets::set;
/// use lib_rapid::compsci::general::BinaryInsert;
/// let s: VecSet<i32> = VecSet::new(&vec![0,1,2,3,4,5,6,7,8,9,10]);
/// let s1: VecSet<i32> = VecSet::new(&vec![11,12,13,13,11,0,0,0]);
///
/// let c: VecSet<i32> = s.union(&s1);
/// assert_eq!(c, set!(0,1,2,3,4,5,6,7,8,9,10,11,12,13));
/// ```
#[must_use]
pub fn union(&self, other: &VecSet<T>) -> VecSet<T> {
let mut res: VecSet<T> = VecSet {elements: Vec::new(),
superset: None };
res.elements.append(&mut self.elements.clone());
res.elements.append(&mut other.elements.clone());
res.elements.sort_unstable();
res.elements.dedup();
res
}
/// Does a mathematical intersection on two sets.
///
/// # Arguments
/// * `self` - The first set.
/// * `other` - The second set.
///
/// # Returns
/// A new `VecSet<T>`: `self ∩ other`.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// use lib_rapid::math::sets::vec_sets::set;
/// use lib_rapid::compsci::general::BinaryInsert; // Used for "set!"
///
/// let s: VecSet<i32> = VecSet::new(&vec![0,1,2,3,4,5,6,7,8,9,10,11]);
/// let s2: VecSet<i32> = VecSet::new(&vec![0,1,2,3,11,0,0,0]);
///
/// let c: VecSet<i32> = s.intersection(&s2);
/// assert_eq!(c, set!(0, 1, 2, 3, 11));
/// ```
#[must_use]
pub fn intersection(&self, other: &VecSet<T>) -> VecSet<T> {
let mut res: VecSet<T> = self.clone();
res.elements.retain(|x| other.elements.binary_search(x).is_ok());
res
}
/// Lets you check for an element in a set.
///
/// # Arguments
/// * `elem` - The element to check for.
///
/// # Returns
/// A boolean value which determines if `elem ∈ self`.
///
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// use lib_rapid::math::sets::vec_sets::set;
///
/// let set = set!(0, 1, 2, 3, 4, 5, 6);
///
/// assert_eq!(false, set.has_element(7));
/// assert_eq!(false, set.has_element(-1));
/// assert_eq!(true, set.has_element(1));
/// ```
#[must_use]
pub fn has_element(&self, elem: T) -> bool {
self.elements.binary_search(&elem).is_ok()
}
/// Lets you insert an element into a set. Does not insert already present values.
///
/// # Arguments
/// * `elem` - The element to insert.
///
/// # Returns
/// Nothing.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// use lib_rapid::math::sets::vec_sets::set;
/// let mut s: VecSet<i32> = VecSet::new(&vec![0,1,2,3,4,5,6,7,8,9,10]);
///
/// s.insert(5);
/// assert_eq!(s.elements(), &vec![0,1,2,3,4,5,6,7,8,9,10]);
/// ```
pub fn insert(&mut self, elem: T) {
self.elements.binary_insert_no_dup(elem)
}
/// Lets you check wether a set has a parent (emerged from another set) or not.
///
/// # Returns
/// A boolean value which determines if the set is a subset of any other set.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// use lib_rapid::math::sets::vec_sets::set;
///
/// let set = set!(0, 1, 2, 3, 4, 5, 6);
/// let subset = VecSet::new_subset(&set, |x| x % 2 == 0);
///
/// assert_eq!(true, subset.has_superset());
/// assert_eq!(false, set.has_superset());
/// ```
#[must_use]
pub fn has_superset(&self) -> bool {
self.superset.is_some()
}
/// Gets you the optional superset.
///
/// # Returns
/// A `Option<&VecSet<T>>`.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// use lib_rapid::math::sets::vec_sets::set;
///
/// let set = set!(0, 1, 2, 3, 4, 5, 6);
/// let subset = VecSet::new_subset(&set, |x| x % 2 == 0);
///
/// assert_eq!(&set, subset.get_superset().unwrap());
/// ```
#[must_use]
pub fn get_superset(&self) -> Option<&VecSet<T>> {
self.superset
}
/// Gets the cardinality of a set.
///
/// # Returns
/// A `usize`: `|self|`.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// use lib_rapid::math::sets::vec_sets::set;
///
/// let set = set!(0, 1, 2, 3, 4, 5, 6);
///
/// assert_eq!(7, set.cardinality());
/// ```
#[must_use]
pub fn cardinality(&self) -> usize {
self.elements.len()
}
/// Lets you set the elements of a set.
///
/// # Arguments
/// * `vals` - The Vec to change the values to.
///
/// # Returns
/// Nothing.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// use lib_rapid::math::sets::vec_sets::set;
///
/// let mut set = set!(0, 1, 2, 3, 4, 5, 6);
/// set.set_elements(&vec![0, 2, 4, 6]);
///
/// assert_eq!(&vec![0, 2, 4, 6], set.elements());
/// ```
pub fn set_elements(&mut self, vals: &[T]) {
self.elements = vals.to_vec();
self.elements.sort_unstable();
}
/// Lets you get the elements of a set.
///
/// # Arguments
/// * none
///
/// # Returns
/// A `&[T]` containing all elements.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// use lib_rapid::math::sets::vec_sets::set;
///
/// let mut set = set!(0, 1, 2, 3, 4, 5, 6);
///
/// assert_eq!(&vec![0, 1, 2, 3, 4, 5, 6], set.elements());
/// ```
#[must_use]
pub fn elements(&self) -> &[T] {
&self.elements
}
}
/// Creates a new `VecSet` more elegantly from values.
///
/// # Returns
/// A new `VecSet`.
/// # Examples
/// ```
/// use lib_rapid::set;
/// use lib_rapid::math::sets::vec_sets::VecSet;
///
/// let set: VecSet<i32> = set!(0,1,2,3,4,5,6,-1);
/// assert_eq!(set.to_string(), "{ -1; 0; 1; 2; 3; 4; 5; 6 }");
/// assert_eq!(set.to_full_string(), "{ -1; 0; 1; 2; 3; 4; 5; 6 }");
#[macro_export]
#[must_use]
macro_rules! set {
( $( $a:expr ),* ) => {
{
use lib_rapid::compsci::general::BinaryInsert;
let mut temp = Vec::new();
$(
temp.binary_insert_no_dup($a);
)*
VecSet::new(&temp)
}
};
}
pub use set;
use crate::compsci::general::BinaryInsert;
impl<T: ToString> VecSet<'_, T> {
/// Lets you print a set with all its parents recursively.
///
/// # Returns
/// Nothing.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// let s: VecSet<i32> = VecSet::new(&vec![0,1,2,3,4,5,6,7,8,9,10]);
/// let s1: VecSet<i32> = VecSet::new_subset(&s, |x| x % 2 == 0);
/// let s2: VecSet<i32> = VecSet::new_subset(&s1, |x| x == 4);
///
/// s2.full_println(); // Prints this set and the superset, see to_full_string.
/// println!("{}", s2); // Only prints this set
/// ```
pub fn full_println(&self) {
println!("{}", self.rec_to_string(&mut String::new()));
}
/// Converts a set with all subsets to a string.
///
/// # Returns
/// A String containing the result.
/// # Examples
/// ```
/// use lib_rapid::math::sets::vec_sets::VecSet;
/// let s: VecSet<i32> = VecSet::new(&vec![0,1,2,3,4,5,6,7,8,9,10]);
/// let s1: VecSet<i32> = VecSet::new_subset(&s, |x| x % 2 == 0);
/// let s2: VecSet<i32> = VecSet::new_subset(&s1, |x| x == 4);
///
/// assert_eq!(s2.to_full_string(), "{ 4 } ⊆ { 0; 2; 4; 6; 8; 10 } ⊆ { 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 }".to_string());
/// ```
#[must_use]
pub fn to_full_string(&self) -> String {
self.rec_to_string(&mut String::new())
}
fn rec_to_string(&self, string: &mut String) -> String {
string.push_str(&self.to_string()); // The child-set at the bottom
if let Some(x) = self.superset { string.push_str(" ⊆ "); // Add subset-character
x.rec_to_string(string); } // Recursively append parent sets
string.to_string()
}
}
// Indexing for Sets
impl<T> std::ops::Index<usize> for VecSet<'_, T> {
type Output = T;
#[inline(always)]
fn index(&self, index: usize) -> &Self::Output {
&self.elements[index]
}
}
// Implement Printing
impl<T: ToString> std::fmt::Display for VecSet<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut res: String = String::from('{');
for elem in &self.elements {
res.push(' ');
res += &elem.to_string();
res.push(';');
}
res.pop();
write!(f, "{} }}", res)
}
}
// Implement Equality
impl<T: PartialEq> PartialEq for VecSet<'_, T> {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
self.elements == other.elements
}
}
impl<T: Copy> Iterator for VecSet<'_, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match self.elements.get(0) {
Some(x) => { Some(*x) }
None => { None }
}
}
}