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
#![warn(missing_docs)]
pub mod setimpls;
pub mod mutimpls;
use indxvec::{Printing};
pub fn trivindex(asc:bool,n:usize) -> Vec<usize> {
if asc { Vec::from_iter(0..n) }
else { Vec::from_iter((0..n).rev()) }
}
pub fn ascdesc(asc:bool) -> &'static str {
if asc { "Ascending" } else { "Descending" }
}
#[derive(Default,Clone,Copy)]
pub enum SType {
#[default]
Empty,
Unordered,
Ordered,
Indexed,
Ranked
}
impl std::fmt::Display for SType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
writeln!(f,"{}",self.to_str())
}
}
#[derive(Default,Clone)]
pub struct Set<T> {
pub stype: SType,
pub ascending: bool,
pub data: Vec<T>,
pub index: Vec<usize>
}
impl<T: std::fmt::Display> std::fmt::Display for Set<T> where T:Copy {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match &self.stype {
SType::Empty => writeln!(f,"Empty Set"),
SType::Unordered => writeln!(f, "Unordered Set\nData: {}",self.data.gr()),
SType::Ordered => writeln!(f, "Ordered {} Set\nData: {}",ascdesc(self.ascending),self.data.gr()),
SType::Indexed => writeln!(f, "Indexed {} Set\nData: {}\nIndex: {}",ascdesc(self.ascending),self.data.gr(),self.index.yl()),
SType::Ranked => writeln!(f, "Ranked {} Set\nData: {}\nRanks: {}",ascdesc(self.ascending),self.data.gr(),self.index.yl()),
}
}
}
pub trait MutSetOps<T> {
fn munordered(&mut self);
fn mordered(&mut self, quantify: &mut impl FnMut(&T) -> f64, asc:bool);
fn mindexed(&mut self, quantify: &mut impl FnMut(&T) -> f64, asc:bool);
fn mranked(&mut self,asc:bool);
fn msame(&mut self, s:&mut Self, quantify: &mut impl FnMut(&T) -> f64);
fn mdelete(&mut self, item:T) -> bool;
fn mdeleteall(&mut self, item:T) -> usize;
fn minsert(&mut self, item:T);
fn mreverse(&mut self);
fn mnonrepeat(&mut self);
fn munion(&mut self, s: &Self);
fn mintersection(&mut self, s: &Self);
fn mdifference(&mut self, s: &Self);
}