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
#![allow(clippy::needless_range_loop)]
use crate::card::*;
use self::Iteration::*;
#[derive(PartialEq, Eq)]
enum Iteration { Continue, Break }
trait ForEach<T> {
fn foreach<F>(&self, f: F) where F: FnMut(T) -> Iteration;
fn find_first(&self) -> Option<T> {
let mut first = None;
self.foreach(|x| { first = Some(x); Break });
first
}
fn find_all(&self) -> Vec<T> {
let mut all = Vec::new();
self.foreach(|x| { all.push(x); Continue });
all
}
fn count(&self) -> usize {
let mut num = 0;
self.foreach(|_| { num += 1; Continue });
num
}
fn contains_any(&self) -> bool {
let mut contains = false;
self.foreach(|_| { contains = true; Break });
contains
}
}
impl ForEach<Set> for [Card] {
fn foreach<F>(&self, mut f: F) where F: FnMut(Set) -> Iteration {
for a in 2..self.len() {
for b in 1..a {
for c in 0..b {
let triple = (self[a], self[b], self[c]);
if let Some(set) = triple.to_set() {
if f(set) == Break {
return;
}
}
}
}
}
}
}
impl ForEach<SuperSet> for [Card] {
fn foreach<F>(&self, mut f: F) where F: FnMut(SuperSet) -> Iteration {
for a in 3..self.len() {
for b in 2..a {
for c in 1..b {
for d in 0..c {
let quad = (self[a], self[b], self[c], self[d]);
if let Some(superset) = quad.to_superset() {
if f(superset) == Break {
return;
}
}
}
}
}
}
}
}
pub trait FindSets {
fn find_first_set(&self) -> Option<Set>;
fn find_all_sets(&self) -> Vec<Set>;
fn count_sets(&self) -> usize;
fn contains_set(&self) -> bool;
}
impl FindSets for [Card] {
fn find_first_set(&self) -> Option<Set> {
self.find_first()
}
fn find_all_sets(&self) -> Vec<Set> {
self.find_all()
}
fn count_sets(&self) -> usize {
ForEach::<Set>::count(self)
}
fn contains_set(&self) -> bool {
ForEach::<Set>::contains_any(self)
}
}
pub trait FindSuperSets {
fn find_first_superset(&self) -> Option<SuperSet>;
fn find_all_supersets(&self) -> Vec<SuperSet>;
fn count_supersets(&self) -> usize;
fn contains_superset(&self) -> bool;
}
impl FindSuperSets for [Card] {
fn find_first_superset(&self) -> Option<SuperSet> {
self.find_first()
}
fn find_all_supersets(&self) -> Vec<SuperSet> {
self.find_all()
}
fn count_supersets(&self) -> usize {
ForEach::<SuperSet>::count(self)
}
fn contains_superset(&self) -> bool {
ForEach::<SuperSet>::contains_any(self)
}
}