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
mod range;
use core::fmt::Debug;
use core::hash::Hash;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use crate::location::Location;
use crate::vector::{Columns, Component as VecComponent, Rows};
pub use range::{ColumnRange, ColumnRangeError, Range, RangeError, RowRange, RowRangeError};
pub trait Component: Sized + From<isize> + Copy + Debug + Ord + Eq + Hash {
type Converse: Component<Converse = Self>;
type Distance: VecComponent;
fn from_location(location: &Location) -> Self;
fn combine(self, other: Self::Converse) -> Location;
fn name() -> &'static str;
fn value(self) -> isize;
fn add(self, amount: Self::Distance) -> Self;
fn distance_to(self, target: Self) -> Self::Distance {
target.distance_from(self)
}
fn distance_from(self, origin: Self) -> Self::Distance;
fn transpose(self) -> Self::Converse {
self.value().into()
}
}
macro_rules! make_component {
(
$Name:ident,
$Converse:ident,
$Distance:ident,
$from_loc:ident,
($self:ident, $other:ident) =>
($first:ident, $second:ident),
$name:expr
) => {
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $Name(pub isize);
impl Add<$Converse> for $Name {
type Output = Location;
fn add(self, rhs: $Converse) -> Location {
self.combine(rhs)
}
}
impl Add<$Distance> for $Name {
type Output = Self;
fn add(self, rhs: $Distance) -> Self {
$Name(self.0 + rhs.0)
}
}
impl AddAssign<$Distance> for $Name {
fn add_assign(&mut self, rhs: $Distance) {
self.0 += rhs.0
}
}
impl Sub<$Distance> for $Name {
type Output = Self;
fn sub(self, rhs: $Distance) -> Self {
$Name(self.0 - rhs.0)
}
}
impl SubAssign<$Distance> for $Name {
fn sub_assign(&mut self, rhs: $Distance) {
self.0 -= rhs.0
}
}
impl Sub<$Name> for $Name {
type Output = $Distance;
fn sub(self, rhs: Self) -> $Distance {
$Distance(self.0 - rhs.0)
}
}
impl From<isize> for $Name {
fn from(value: isize) -> Self {
$Name(value)
}
}
impl Component for $Name {
type Converse = $Converse;
type Distance = $Distance;
#[inline]
fn from_location(loc: &Location) -> Self {
loc.$from_loc
}
fn combine($self, $other: Self::Converse) -> Location {
Location::new($first, $second)
}
#[inline(always)]
fn name() -> &'static str {
$name
}
fn add(self, distance: Self::Distance) -> Self {
self + distance
}
fn distance_from(self, origin: Self) -> Self::Distance {
self - origin
}
fn value(self) -> isize {
self.0
}
}
}
}
make_component! {Row, Column, Rows, row, (self, other) => (self, other), "row"}
make_component! {Column, Row, Columns, column, (self, other) => (other, self), "column"}