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
use collection::{Collection, MutableCollection, Sliceable};
use card::{StandardCard};
use deck::deck_creator;
use std::cmp::PartialEq;
#[derive(Debug)]
pub struct NoDuplicatesDeck<T: PartialEq> {
_cards: Vec<T>
}
impl<T> NoDuplicatesDeck<T>
where T: PartialEq
{
pub fn new() -> NoDuplicatesDeck<T>
{
NoDuplicatesDeck{_cards:Vec::new()}
}
pub fn new_standard_deck() -> NoDuplicatesDeck<StandardCard>
{
NoDuplicatesDeck{_cards: deck_creator::create_standard_deck()}
}
fn contains(&self, item: &T) -> bool
{
self._cards.contains(item)
}
}
impl<T> Collection for NoDuplicatesDeck<T>
where T: PartialEq
{
type Item=T;
fn size(&self) -> usize
{
self._cards.len()
}
}
impl<T> MutableCollection for NoDuplicatesDeck<T>
where T: PartialEq
{
fn push(&mut self, item: Self::Item) -> ()
{
if !self.contains(&item)
{self._cards.push(item)}
}
fn pop(&mut self) -> Option<Self::Item>
{
self._cards.pop()
}
fn insert_at(&mut self, index: usize, item: Self::Item) -> ()
{
if !self.contains(&item)
{self._cards.insert(index, item)}
}
fn remove_at(&mut self, index:usize) -> Option<Self::Item>
{
self._cards.remove_at(index)
}
}
impl<T> Sliceable for NoDuplicatesDeck<T>
where T: PartialEq
{
type Item = T;
fn as_mut_slice(&mut self) -> &mut [T]
{
self._cards.as_mut_slice()
}
}
#[cfg(test)]
mod non_duplicate_tests
{
use super::NoDuplicatesDeck;
use deck::{Deck};
#[test]
fn cannot_add_duplicates() -> ()
{
let item = 1;
let mut deck = NoDuplicatesDeck::new();
deck.add_card_to_bottom(item);
assert_eq!(deck.size(), 1);
deck.add_card_to_bottom(item);
assert_eq!(deck.size(), 1);
}
}