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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::convert::TryFrom;
use crate::error::{i32_to_usize};
use crate::error::Result;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub struct Vec2<T> (pub T, pub T);
impl<T> Vec2<T> {
pub fn map<B>(self, map: impl Fn(T) -> B) -> Vec2<B> {
Vec2(map(self.0), map(self.1))
}
pub fn try_from<S>(value: Vec2<S>) -> std::result::Result<Self, T::Error> where T: TryFrom<S> {
let x = T::try_from(value.0)?;
let y = T::try_from(value.1)?;
Ok(Vec2(x, y))
}
#[inline] pub fn area(self) -> T where T: std::ops::Mul<T, Output = T> {
self.0 * self.1
}
#[inline] pub fn x(self) -> T { self.0 }
#[inline] pub fn y(self) -> T { self.1 }
#[inline] pub fn width(self) -> T { self.0 }
#[inline] pub fn height(self) -> T { self.1 }
}
impl Vec2<i32> {
pub fn to_usize(self, error_message: &'static str) -> Result<Vec2<usize>> {
let x = i32_to_usize(self.0, error_message)?;
let y = i32_to_usize(self.1, error_message)?;
Ok(Vec2(x, y))
}
}
impl Vec2<usize> {
pub fn to_i32(self) -> Vec2<i32> {
let x = i32::try_from(self.0).expect("vector x coordinate too large");
let y = i32::try_from(self.1).expect("vector y coordinate too large");
Vec2(x, y)
}
}
impl<T: std::ops::Add<T>> std::ops::Add<Vec2<T>> for Vec2<T> {
type Output = Vec2<T::Output>;
fn add(self, other: Vec2<T>) -> Self::Output {
Vec2(self.0 + other.0, self.1 + other.1)
}
}
impl<T: std::ops::Sub<T>> std::ops::Sub<Vec2<T>> for Vec2<T> {
type Output = Vec2<T::Output>;
fn sub(self, other: Vec2<T>) -> Self::Output {
Vec2(self.0 - other.0, self.1 - other.1)
}
}
impl<T: std::ops::Div<T>> std::ops::Div<Vec2<T>> for Vec2<T> {
type Output = Vec2<T::Output>;
fn div(self, other: Vec2<T>) -> Self::Output {
Vec2(self.0 / other.0, self.1 / other.1)
}
}
impl<T: std::ops::Mul<T>> std::ops::Mul<Vec2<T>> for Vec2<T> {
type Output = Vec2<T::Output>;
fn mul(self, other: Vec2<T>) -> Self::Output {
Vec2(self.0 * other.0, self.1 * other.1)
}
}
impl<T> From<(T, T)> for Vec2<T> {
fn from((x, y): (T, T)) -> Self { Vec2(x, y) }
}
impl<T> From<Vec2<T>> for (T, T) {
fn from(vec2: Vec2<T>) -> Self { (vec2.0, vec2.1) }
}
pub(crate) fn floor_log_2(mut number: u32) -> u32 {
let mut log = 0;
while number > 1 {
log += 1;
number >>= 1;
}
log
}
pub(crate) fn ceil_log_2(mut number: u32) -> u32 {
let mut log = 0;
let mut round_up = 0;
while number > 1 {
if number & 1 != 0 {
round_up = 1;
}
log += 1;
number >>= 1;
}
log + round_up
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum RoundingMode {
Down,
Up,
}
impl RoundingMode {
pub(crate) fn log2(self, number: usize) -> usize {
match self {
RoundingMode::Down => self::floor_log_2(number as u32) as usize,
RoundingMode::Up => self::ceil_log_2(number as u32) as usize,
}
}
pub(crate) fn divide(self, dividend: usize, divisor: usize) -> usize {
match self {
RoundingMode::Up => (dividend + divisor - 1) / divisor,
RoundingMode::Down => dividend / divisor,
}
}
}