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 std::cmp::PartialEq;
use std::convert::From;
use std::fmt::{self, Debug};
use std::ops::{Deref, DerefMut};
pub type Axis = usize;
pub const VERTICAL: Axis = 0;
pub const DIAGONAL_UP: Axis = 1;
pub const HORIZONTAL: Axis = 2;
pub const DIAGONAL_DOWN: Axis = 3;
pub struct Axes<T>([T; 4]);
impl<T> Axes<T> {
pub fn new(vert: T, diag_up: T, hori: T, diag_down: T) -> Axes<T> {
Axes([vert, diag_up, hori, diag_down])
}
pub fn vertical(&self) -> &T { &self.0[VERTICAL] }
pub fn diagonal_up(&self) -> &T { &self.0[DIAGONAL_UP] }
pub fn horizontal(&self) -> &T { &self.0[HORIZONTAL] }
pub fn diagonal_down(&self) -> &T { &self.0[DIAGONAL_DOWN] }
pub fn count<P>(&self, mut predicate: P) -> usize where P: FnMut(&T) -> bool {
let mut count = 0;
for x in &self.0 {
if predicate(x) { count += 1; }
}
count
}
pub fn any<P>(&self, predicate: P) -> bool where P: FnMut(&T) -> bool {
self.0.iter().any(predicate)
}
}
impl<T: Default> Axes<T> {
pub fn new_horizontal(axis: T) -> Axes<T> {
Axes([T::default(), T::default(), axis, T::default()])
}
pub fn new_diagonal_up(axis: T) -> Axes<T> {
Axes([T::default(), axis, T::default(), T::default()])
}
pub fn new_vertical(axis: T) -> Axes<T> {
Axes([axis, T::default(), T::default(), T::default()])
}
pub fn new_diagonal_down(axis: T) -> Axes<T> {
Axes([T::default(), T::default(), T::default(), axis])
}
}
impl<T: Debug> Debug for Axes<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Axes({:?})", self.0)
}
}
impl<T: PartialEq> PartialEq for Axes<T> {
fn eq(&self, other: &Axes<T>) -> bool {
self.0 == other.0
}
}
impl<T> From<[T; 4]> for Axes<T> {
fn from(array: [T; 4]) -> Self {
Axes(array)
}
}
impl<T> Deref for Axes<T> {
type Target = [T; 4];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Axes<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}