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
/// A representation of a ZF set.
///
/// All elements must belong to a "supertype" `E`. Subsets of the supertype are
/// determined by a given set of conditions (similar to the conditions used in
/// the set construction paradigm of traditional ZF set theory).
///
/// Element existence (ie. whether or not a certain element is a member of a
/// given set) is given through the [`has`](fn@AlgaeSet::has) function. Set
/// unions are given by the [`or`](fn@AlgaeSet::or) function, and set
/// intersections are given by the [`and`](fn@AlgaeSet::and) function.
///
/// # Examples
///
/// ```
/// use algae_rs::algaeset::AlgaeSet;
///
/// let mut pos_floats = AlgaeSet::new(vec![
///     Box::new(|e: f32| e > 0_f32)
/// ]);
///
/// assert!(pos_floats.has(12_f32));
///
/// let neg_floats = AlgaeSet::new(vec![
///     Box::new(|e: f32| e < 0_f32)
/// ]);
///
/// pos_floats.or(neg_floats);
/// let all_floats = pos_floats;
/// assert!(all_floats.has(12_f32));
/// assert!(all_floats.has(-12_f32));
/// ```
pub struct AlgaeSet<E> {
    pos_conditions: Vec<Box<dyn Fn(E) -> bool>>,
    neg_conditions: Vec<Box<dyn Fn(E) -> bool>>,
}

impl<E> AlgaeSet<E> {
    /// Returns an AlgaeSet defined by a `Vec` of conditions
    pub fn new(pos_conditions: Vec<Box<dyn Fn(E) -> bool>>) -> Self {
        Self {
            pos_conditions,
            neg_conditions: vec![],
        }
    }

    /// Returns an AlgaeSet defined by a single condition
    pub fn mono(condition: Box<dyn Fn(E) -> bool>) -> Self {
        Self::new(vec![condition])
    }

    /// Returns an AlgaeSet containing all members of the type `E`
    pub fn all() -> Self {
        Self {
            pos_conditions: vec![Box::new(|_x: E| true)],
            neg_conditions: vec![],
        }
    }
}

impl<E: Copy + Clone> AlgaeSet<E> {
    /// Returns whether or not `element` is in the given set
    pub fn has(&self, element: E) -> bool {
        if self.neg_conditions.iter().any(|c| (c)(element)) {
            return false;
        }
        return self.pos_conditions.iter().any(|c| (c)(element));
    }
}

impl<E: PartialEq + Copy + Clone + 'static> AlgaeSet<E> {
    /// Adds `element` to the given set
    pub fn add(&mut self, element: E) {
        self.neg_conditions.retain(|c| !(c)(element));
        self.pos_conditions.push(Box::new(move |x: E| x == element))
    }

    /// Removes `element` from the given set
    pub fn remove(&mut self, element: E) {
        self.pos_conditions.retain(|c| (c)(element));
        self.neg_conditions.push(Box::new(move |x: E| x == element))
    }

    /// Adds all elements from `other` to `self`
    pub fn or(&mut self, other: Self) {
        self.pos_conditions.push(Box::new(move |x: E| other.has(x)));
    }

    /// Removes all elements from `self` that aren't in `other`
    pub fn and(&mut self, other: Self) {
        self.neg_conditions
            .push(Box::new(move |x: E| !other.has(x)));
    }
}

#[cfg(test)]
#[allow(non_snake_case)]
mod tests {

    use super::*;

    mod infinite_set {

        use super::*;

        #[derive(PartialEq, Clone, Copy)]
        enum Real {
            UInt(u32),
            SInt(i32),
            Float(f32),
        }

        #[test]
        fn has_element() {
            let REALS = AlgaeSet::<Real>::all();
            assert!(REALS.has(Real::UInt(12)));
            assert!(REALS.has(Real::SInt(-42)));
            assert!(REALS.has(Real::Float(-34.2)));
        }

        #[test]
        fn remove_element() {
            let mut REALS = AlgaeSet::<Real>::all();
            REALS.remove(Real::Float(23.1));
            assert!(REALS.has(Real::Float(23.2)));
            assert!(!REALS.has(Real::Float(23.1)));
        }

        #[test]
        fn add_after_remove() {
            let mut REALS = AlgaeSet::<Real>::all();
            REALS.remove(Real::Float(32.1));
            assert!(!REALS.has(Real::Float(32.1)));
            REALS.add(Real::Float(32.1));
            assert!(REALS.has(Real::Float(32.1)));
        }

        #[test]
        fn remove_after_add_after_remove() {
            let mut REALS = AlgaeSet::<Real>::all();
            assert!(REALS.has(Real::Float(32.1)));
            REALS.remove(Real::Float(32.1));
            assert!(!REALS.has(Real::Float(32.1)));
            REALS.add(Real::Float(32.1));
            assert!(REALS.has(Real::Float(32.1)));
            REALS.remove(Real::Float(32.1));
            assert!(!REALS.has(Real::Float(32.1)));
        }

        #[test]
        fn overlapping_union() {
            let REALS = AlgaeSet::<Real>::all();
            let mut FLOATS = AlgaeSet::<Real>::mono(Box::new(|x: Real| match x {
                Real::UInt(_) => false,
                Real::SInt(_) => false,
                Real::Float(_) => true,
            }));
            assert!(!FLOATS.has(Real::UInt(12)));
            FLOATS.or(REALS);
            assert!(FLOATS.has(Real::UInt(12)));
        }

        #[test]
        fn encompassing_union() {
            let mut REALS = AlgaeSet::<Real>::all();
            let FLOATS = AlgaeSet::<Real>::mono(Box::new(|x: Real| match x {
                Real::UInt(_) => false,
                Real::SInt(_) => false,
                Real::Float(_) => true,
            }));
            REALS.or(FLOATS);
            assert!(REALS.has(Real::Float(12.0)));
            assert!(REALS.has(Real::UInt(12)));
            assert!(REALS.has(Real::SInt(-12)));
        }

        #[test]
        fn disjoint_union() {
            let UINTS = AlgaeSet::<Real>::mono(Box::new(|x: Real| match x {
                Real::UInt(_) => true,
                Real::SInt(_) => false,
                Real::Float(_) => false,
            }));
            let mut FLOATS = AlgaeSet::<Real>::mono(Box::new(|x: Real| match x {
                Real::UInt(_) => false,
                Real::SInt(_) => false,
                Real::Float(_) => true,
            }));
            assert!(FLOATS.has(Real::Float(12.0)));
            assert!(!FLOATS.has(Real::UInt(12)));
            FLOATS.or(UINTS);
            assert!(FLOATS.has(Real::Float(12.0)));
            assert!(FLOATS.has(Real::UInt(12)));
        }

        #[test]
        fn overlapping_intersection() {
            let REALS = AlgaeSet::<Real>::all();
            let mut FLOATS = AlgaeSet::<Real>::mono(Box::new(|x: Real| match x {
                Real::UInt(_) => false,
                Real::SInt(_) => false,
                Real::Float(_) => true,
            }));
            assert!(!FLOATS.has(Real::UInt(12)));
            FLOATS.and(REALS);
            assert!(!FLOATS.has(Real::UInt(12)));
        }

        #[test]
        fn encompassing_intersection() {
            let mut REALS = AlgaeSet::<Real>::all();
            let FLOATS = AlgaeSet::<Real>::mono(Box::new(|x: Real| match x {
                Real::UInt(_) => false,
                Real::SInt(_) => false,
                Real::Float(_) => true,
            }));
            assert!(REALS.has(Real::UInt(12)));
            assert!(REALS.has(Real::SInt(-12)));
            assert!(REALS.has(Real::Float(12.0)));
            REALS.and(FLOATS);
            assert!(REALS.has(Real::Float(12.0)));
            assert!(!REALS.has(Real::UInt(12)));
            assert!(!REALS.has(Real::SInt(-12)));
        }

        #[test]
        fn disjoint_intersection() {
            let UINTS = AlgaeSet::<Real>::mono(Box::new(|x: Real| match x {
                Real::UInt(_) => true,
                Real::SInt(_) => false,
                Real::Float(_) => false,
            }));
            let mut FLOATS = AlgaeSet::<Real>::mono(Box::new(|x: Real| match x {
                Real::UInt(_) => false,
                Real::SInt(_) => false,
                Real::Float(_) => true,
            }));
            assert!(FLOATS.has(Real::Float(12.0)));
            assert!(!FLOATS.has(Real::UInt(12)));
            FLOATS.and(UINTS);
            assert!(!FLOATS.has(Real::Float(12.0)));
            assert!(!FLOATS.has(Real::UInt(12)));
        }
    }

    mod finite_set {

        use super::*;

        #[test]
        fn has_element() {
            let Z2 = AlgaeSet::<i32>::mono(Box::new(|x: i32| x % 2 == x));
            assert!(Z2.has(1));
            assert!(Z2.has(0));
            assert!(!Z2.has(2));
            assert!(!Z2.has(-2));
        }

        #[test]
        fn add_element() {
            let mut Z2 = AlgaeSet::<i32>::mono(Box::new(|x: i32| x % 2 == x));
            assert!(!Z2.has(2));
            Z2.add(2);
            assert!(Z2.has(2));
        }

        #[test]
        fn remove_element() {
            let mut Z2 = AlgaeSet::<i32>::mono(Box::new(|x: i32| x % 2 == x));
            assert!(Z2.has(1));
            Z2.remove(1);
            assert!(!Z2.has(1));
        }

        #[test]
        fn overlapping_union() {
            let mut Z2 = AlgaeSet::<i32>::mono(Box::new(|x: i32| x % 2 == x));
            let Z3 = AlgaeSet::<i32>::mono(Box::new(|x: i32| x % 3 == x));
            Z2.or(Z3);
            assert!(Z2.has(0));
            assert!(Z2.has(1));
            assert!(Z2.has(2));
        }

        #[test]
        fn encompassing_union() {
            let Z2 = AlgaeSet::<i32>::mono(Box::new(|x: i32| x % 2 == x));
            let mut Z3 = AlgaeSet::<i32>::mono(Box::new(|x: i32| x % 3 == x));
            Z3.or(Z2);
            assert!(Z3.has(0));
            assert!(Z3.has(1));
            assert!(Z3.has(2));
        }

        #[test]
        fn disjoint_union() {
            let mut one = AlgaeSet::<i32>::mono(Box::new(|x: i32| x == 1));
            let two = AlgaeSet::<i32>::mono(Box::new(|x: i32| x == 2));
            one.or(two);
            assert!(one.has(1));
            assert!(one.has(2));
        }

        #[test]
        fn overlapping_intersection() {
            let mut Z2 = AlgaeSet::<i32>::mono(Box::new(|x: i32| x % 2 == x));
            let one = AlgaeSet::<i32>::mono(Box::new(|x: i32| x == 1));
            Z2.and(one);
            assert!(Z2.has(1));
            assert!(!Z2.has(0));
        }

        #[test]
        fn encompassing_intersection() {
            let Z2 = AlgaeSet::<i32>::mono(Box::new(|x: i32| x % 2 == x));
            let mut one = AlgaeSet::<i32>::mono(Box::new(|x: i32| x == 1));
            one.and(Z2);
            assert!(one.has(1));
            assert!(!one.has(0));
        }

        #[test]
        fn disjoint_intersection() {
            let mut one = AlgaeSet::<i32>::mono(Box::new(|x: i32| x == 1));
            let two = AlgaeSet::<i32>::mono(Box::new(|x: i32| x == 2));
            one.and(two);
            assert!(!one.has(1));
            assert!(!one.has(2));
        }
    }
}