-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsemigroup.rs
82 lines (65 loc) · 1.77 KB
/
semigroup.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
use lift::SemiGroup;
use std::hash::Hash;
use std::collections::linked_list::LinkedList;
use std::collections::vec_deque::VecDeque;
use std::collections::{BinaryHeap, BTreeSet, HashSet};
//implementation for numerics
semigroup_num!(i8);
semigroup_num!(i16);
semigroup_num!(i32);
semigroup_num!(i64);
semigroup_num!(u8);
semigroup_num!(u16);
semigroup_num!(u32);
semigroup_num!(u64);
semigroup_num!(isize);
semigroup_num!(usize);
semigroup_num!(f32);
semigroup_num!(f64);
//Implementataion of SemiGroup for String
impl SemiGroup for String {
type A = String;
fn add(&self, b: &Self::A) -> Self::A {
let mut ret = String::from("");
ret.push_str(self);
ret.push_str(b);
ret
}
}
//Implementation for SemiGroup for HashSet
impl<T: Clone + Hash + Eq> SemiGroup for HashSet<T> {
type A = HashSet<T>;
fn add(&self, b: &Self::A) -> Self::A {
let mut ret = HashSet::new();
ret.extend(self.iter().cloned());
ret.extend(b.iter().cloned());
ret
}
}
//Implementation of SemiGroup for Vec<T>
semigroup!(Vec);
//Implementation of SemiGroup for LinkedList<T>
semigroup!(LinkedList);
//Implementation of SemiGroup for VecDeque<T>
semigroup!(VecDeque);
//Implementation of SemiGroup for BinaryHeap<T>
semigroup_ord!(BinaryHeap);
//Implemenatation of SemiGroup for BTreeSet<T>
semigroup_ord!(BTreeSet);
#[cfg(test)]
mod test {
use lift::{SemiGroup};
#[test]
fn test_vec() {
let one = vec!(1,2);
let two = vec!(3,4);
assert_eq!(one.add(&two), vec!(1,2,3,4));
assert_eq!(one, vec!(1,2));
}
#[test]
fn test_string() {
let one = String::from("one");
let two = String::from("two");
assert_eq!(one.add(&two), String::from("onetwo"));
}
}