Skip to content

Commit 5ca6f8d

Browse files
committed
More unsafe
1 parent c097a12 commit 5ca6f8d

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/day23.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,48 +108,48 @@ pub fn part2(s: &str) -> &'static str {
108108

109109
i += 6;
110110
}
111-
}
112111

113-
vertecies.sort_unstable_by(|a, b| b.1.cmp(&a.1));
114-
let mut i = vertecies.len() - 1;
115-
while vertecies[i].1 == 0 {
116-
i -= 1;
117-
}
118-
let vertecies = &mut vertecies[..i + 1];
112+
vertecies.sort_unstable_by(|a, b| b.1.cmp(&a.1));
113+
let mut i = vertecies.len() - 1;
114+
while vertecies.get_unchecked(i).1 == 0 {
115+
i -= 1;
116+
}
117+
let vertecies = vertecies.get_unchecked_mut(..i + 1);
119118

120-
let max_degree = vertecies[0].1 as usize;
119+
let max_degree = vertecies.get_unchecked(0).1 as usize;
121120

122-
for i in 0..max_degree {
123-
vertecies[i].1 = i as u16;
124-
}
125-
for i in max_degree..vertecies.len() {
126-
vertecies[i].1 = max_degree as u16;
127-
}
121+
for i in 0..max_degree {
122+
vertecies.get_unchecked_mut(i).1 = i as u16;
123+
}
124+
for i in max_degree..vertecies.len() {
125+
vertecies.get_unchecked_mut(i).1 = max_degree as u16;
126+
}
128127

129-
let mut cs = [const { heapless::Vec::<u16, MAX_C>::new() }; MAX_C];
128+
let mut cs = [const { heapless::Vec::<u16, MAX_C>::new() }; MAX_C];
130129

131-
let mut q = heapless::Vec::<u16, MAX_C>::new();
132-
let mut q_max = heapless::Vec::<u16, MAX_C>::new();
130+
let mut q = heapless::Vec::<u16, MAX_C>::new();
131+
let mut q_max = heapless::Vec::<u16, MAX_C>::new();
133132

134-
expand_first(vertecies, &g, &connections, &mut q, &mut q_max, &mut cs);
133+
expand_first(vertecies, &g, &connections, &mut q, &mut q_max, &mut cs);
135134

136-
q_max.sort_unstable();
135+
q_max.sort_unstable();
137136

138-
unsafe {
139137
let mut i = 0;
140138
for p in q_max {
139+
std::hint::assert_unchecked(i + 2 < 13 * 3);
141140
SCRATCH[i + 0] = (p / 26) as u8 + b'a';
142141
SCRATCH[i + 1] = (p % 26) as u8 + b'a';
143142
SCRATCH[i + 2] = b',';
144143
i += 3;
145144
}
146145

146+
std::hint::assert_unchecked(i - 1 < 13 * 3);
147147
str::from_utf8_unchecked(&SCRATCH[..i - 1])
148148
}
149149
}
150150

151151
// Using a modified version of this algorithm: https://web.archive.org/web/20160911054636/http://www.dcs.gla.ac.uk/~pat/jchoco/clique/indSetMachrahanish/papers/tomita2003.pdf
152-
fn expand_first(
152+
unsafe fn expand_first(
153153
mut r: &mut [(u16, u16)],
154154
g: &BitArray<[u64; BAL]>,
155155
cons: &[heapless::Vec<u16, MAX_C>; MAX],
@@ -161,12 +161,12 @@ fn expand_first(
161161
while let Some(((p, color), rest)) = r.split_last_mut() {
162162
let p = *p as usize;
163163
if q.len() + *color as usize + 1 > q_max.len() {
164-
q.push(p as u16).unwrap();
164+
q.push_unchecked(p as u16);
165165

166166
let mut new_r = heapless::Vec::<(u16, u16), MAX_C>::new();
167167
for i in cons[p].iter() {
168168
if unsafe { *r_map.get_unchecked(*i as usize) } {
169-
new_r.push((*i, 0)).unwrap();
169+
new_r.push_unchecked((*i, 0));
170170
}
171171
}
172172

@@ -186,7 +186,7 @@ fn expand_first(
186186
}
187187

188188
// Using this algorithm: https://web.archive.org/web/20160911054636/http://www.dcs.gla.ac.uk/~pat/jchoco/clique/indSetMachrahanish/papers/tomita2003.pdf
189-
fn expand(
189+
unsafe fn expand(
190190
mut r: &mut [(u16, u16)],
191191
g: &BitArray<[u64; BAL]>,
192192
q: &mut heapless::Vec<u16, MAX_C>,
@@ -196,12 +196,12 @@ fn expand(
196196
while let Some(((p, color), rest)) = r.split_last_mut() {
197197
let p = *p as usize;
198198
if q.len() + *color as usize + 1 > q_max.len() {
199-
q.push(p as u16).unwrap();
199+
q.push_unchecked(p as u16);
200200

201201
let mut new_r = heapless::Vec::<(u16, u16), MAX_C>::new();
202202
for (i, _) in rest.iter() {
203203
if unsafe { *g.get_unchecked(*i as usize * MAX + p) } {
204-
new_r.push((*i, 0)).unwrap();
204+
new_r.push_unchecked((*i, 0));
205205
}
206206
}
207207

@@ -248,7 +248,7 @@ unsafe fn number_sort(
248248
maxno = k;
249249
cs.get_unchecked_mut(maxno + 1).clear();
250250
}
251-
cs.get_unchecked_mut(k).push(p as u16).unwrap();
251+
cs.get_unchecked_mut(k).push_unchecked(p as u16);
252252

253253
r = rest;
254254
}

0 commit comments

Comments
 (0)