Skip to content

Commit d6f1959

Browse files
authored
Merge pull request #15 from nyurik/lints
2 parents 706fe8d + 58b6860 commit d6f1959

File tree

5 files changed

+60
-49
lines changed

5 files changed

+60
-49
lines changed

.github/workflows/rust.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ jobs:
1313
check-links:
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@v3
16+
- uses: actions/checkout@v4
1717
- name: Markup Link Checker (mlc)
1818
uses: becheran/[email protected]
1919

2020
build:
2121
runs-on: ubuntu-latest
2222
steps:
23-
- uses: actions/checkout@v2
24-
- name: Build
25-
run: cargo build --verbose
26-
- name: Run tests
27-
run: cargo test --verbose
28-
- name: Markup Link Checker (mlc)
29-
uses: becheran/[email protected]
23+
- uses: actions/checkout@v4
24+
- name: Build
25+
run: cargo build --verbose
26+
- name: Run tests
27+
run: cargo test --verbose
28+
- name: Markup Link Checker (mlc)
29+
uses: becheran/[email protected]

Cargo.toml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,32 @@
22
name = "fast_hilbert"
33
version = "2.0.1"
44
authors = ["Armin <[email protected]>"]
5-
edition = "2018"
5+
edition = "2021"
66
description = "Fast Hilbert 2D curve computation using an efficient Lookup Table (LUT)."
77
keywords = [ "space-filling-curve", "hilbert", "peano-curves", "mapping", "algorithm" ]
88
readme = "README.md"
99
license = "MIT"
10-
categories = ["algorithms"]
10+
categories = ["algorithms", "science::geo"]
1111
repository = "https://github.com/becheran/fast-hilbert"
12+
rust-version = "1.80"
1213

1314
[dev-dependencies]
1415
image = "0.25.6"
15-
criterion = "0.5.1"
16+
criterion = "0.6.0"
1617
hilbert = "0.1.2"
1718
hilbert_2d = "1.1.0"
1819
hilbert_curve = "0.2.0"
1920

2021
[[bench]]
2122
name = "benchmark"
2223
harness = false
24+
25+
[lints.rust]
26+
unsafe_code = "forbid"
27+
unused_qualifications = "warn"
28+
29+
[lints.clippy]
30+
pedantic = { level = "warn", priority = -1 }
31+
cast_possible_truncation = "allow"
32+
cast_possible_wrap = "allow"
33+
cast_sign_loss = "allow"

benches/benchmark.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::hint::black_box;
22
use criterion::{criterion_group, criterion_main, Criterion};
33

4+
#[allow(clippy::too_many_lines)]
45
fn criterion_benchmark(c: &mut Criterion) {
56
let bits: usize = 8;
67
let n: usize = 2usize.pow(bits as u32);
@@ -16,7 +17,7 @@ fn criterion_benchmark(c: &mut Criterion) {
1617
));
1718
}
1819
}
19-
})
20+
});
2021
});
2122

2223
c.bench_function("hilbert_2d", |b| {
@@ -31,7 +32,7 @@ fn criterion_benchmark(c: &mut Criterion) {
3132
));
3233
}
3334
}
34-
})
35+
});
3536
});
3637

3738
c.bench_function("hilbert", |b| {
@@ -42,7 +43,7 @@ fn criterion_benchmark(c: &mut Criterion) {
4243
black_box(p.hilbert_transform(black_box(bits)));
4344
}
4445
}
45-
})
46+
});
4647
});
4748

4849
c.bench_function("fast_hilbert", |b| {
@@ -56,21 +57,21 @@ fn criterion_benchmark(c: &mut Criterion) {
5657
));
5758
}
5859
}
59-
})
60+
});
6061
});
6162

6263
let xy_low: (u32, u32) = (1, 2);
6364
let xy_high: (u32, u32) = (u32::MAX - 1, u32::MAX - 2);
6465
let order: u8 = 32;
65-
let n: usize = 2usize.pow(order as u32);
66+
let n: usize = 2usize.pow(u32::from(order));
6667
c.bench_function("fast_hilbert_low", |b| {
6768
b.iter(|| {
6869
black_box(fast_hilbert::xy2h(
6970
black_box(xy_low.0),
7071
black_box(xy_low.1),
7172
black_box(order),
7273
));
73-
})
74+
});
7475
});
7576
c.bench_function("fast_hilbert_high", |b| {
7677
b.iter(|| {
@@ -79,7 +80,7 @@ fn criterion_benchmark(c: &mut Criterion) {
7980
black_box(xy_high.1),
8081
black_box(order),
8182
));
82-
})
83+
});
8384
});
8485
c.bench_function("hilbert_curve_low", |b| {
8586
b.iter(|| {
@@ -88,7 +89,7 @@ fn criterion_benchmark(c: &mut Criterion) {
8889
xy_low.1 as usize,
8990
n,
9091
));
91-
})
92+
});
9293
});
9394
c.bench_function("hilbert_curve_high", |b| {
9495
b.iter(|| {
@@ -97,7 +98,7 @@ fn criterion_benchmark(c: &mut Criterion) {
9798
xy_high.1 as usize,
9899
n,
99100
));
100-
})
101+
});
101102
});
102103
c.bench_function("hilbert_2d_low", |b| {
103104
b.iter(|| {
@@ -107,7 +108,7 @@ fn criterion_benchmark(c: &mut Criterion) {
107108
order as usize,
108109
hilbert_2d::Variant::Hilbert,
109110
));
110-
})
111+
});
111112
});
112113
c.bench_function("hilbert_2d_high", |b| {
113114
b.iter(|| {
@@ -117,19 +118,19 @@ fn criterion_benchmark(c: &mut Criterion) {
117118
order as usize,
118119
hilbert_2d::Variant::Hilbert,
119120
));
120-
})
121+
});
121122
});
122123
c.bench_function("hilbert_low", |b| {
123124
b.iter(|| {
124125
let p = hilbert::Point::new(0, &[xy_low.0, xy_low.1]);
125126
black_box(p.hilbert_transform(order as usize));
126-
})
127+
});
127128
});
128129
c.bench_function("hilbert_high", |b| {
129130
b.iter(|| {
130131
let p = hilbert::Point::new(0, &[xy_high.0, xy_high.1]);
131132
black_box(p.hilbert_transform(order as usize));
132-
})
133+
});
133134
});
134135
}
135136
criterion_group!(

clippy.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
allow-unwrap-in-tests = true
2+
avoid-breaking-exported-api = false

src/lib.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub fn xy2h<T: Unsigned>(x: T, y: T, order: u8) -> <T as Unsigned>::Key {
158158
200, 7, 196, 214, 87, 146, 145, 76, 13, 194, 67, 213, 148, 19, 208, 143, 14, 193, 128,
159159
];
160160

161-
let coor_bits = (core::mem::size_of::<T>() << 3) as u32;
161+
let coor_bits = (size_of::<T>() << 3) as u32;
162162
let useless_bits = (x | y).leading_zeros() & !1;
163163
let lowest_order = (coor_bits - useless_bits) as u8 + (order & 1);
164164

@@ -173,7 +173,7 @@ pub fn xy2h<T: Unsigned>(x: T, y: T, order: u8) -> <T as Unsigned>::Key {
173173
let index = (x_in | y_in | state.into()).as_usize();
174174

175175
let r = LUT_3[index];
176-
state = r & 0b11000000;
176+
state = r & 0b1100_0000;
177177
let r: T::Key = r.into();
178178

179179
let mut hhh: T::Key = r & T::SIXTY_THREE;
@@ -229,7 +229,7 @@ pub fn h2xy<T: Unsigned>(h: <T as Unsigned>::Key, order: u8) -> (T, T) {
229229
169, 232, 224, 97, 34, 106, 107, 227, 219, 147, 146, 26, 153, 216, 208, 81, 137, 200, 192,
230230
65, 2, 74, 75, 195, 68, 5, 13, 140, 20, 92, 93, 213, 22, 94, 95, 215, 143, 206, 198, 71,
231231
];
232-
let coor_bits = (core::mem::size_of::<T>() << 3) as u8;
232+
let coor_bits = (size_of::<T>() << 3) as u8;
233233
let useless_bits = (h.leading_zeros() >> 1) as u8 & !1;
234234
let lowest_order = coor_bits - useless_bits + (order & 1);
235235

@@ -245,7 +245,7 @@ pub fn h2xy<T: Unsigned>(h: <T as Unsigned>::Key, order: u8) -> (T, T) {
245245
let h_in: u8 = h_in.as_u8();
246246

247247
let r: u8 = LUT_3_REV[state as usize | h_in as usize];
248-
state = r & 0b11000000;
248+
state = r & 0b1100_0000;
249249

250250
let xxx: T = r.into();
251251
let xxx: T = xxx >> 3i8;
@@ -303,24 +303,24 @@ mod tests {
303303
let mut lut_3: [u8; 256] = [0; 256];
304304
for input in 0..=255 {
305305
//for input in 4..=4 {
306-
let mut state: u8 = (input & 0b11000000) >> 4;
306+
let mut state: u8 = (input & 0b1100_0000) >> 4;
307307
let mut result: u8 = 0;
308-
let mut x_mask: u8 = 0b00100000;
309-
let mut y_mask: u8 = 0b00000100;
308+
let mut x_mask: u8 = 0b0010_0000;
309+
let mut y_mask: u8 = 0b0000_0100;
310310
for i in 0..3 {
311311
let idx = state | (input & x_mask) >> (4 - i) | (input & y_mask) >> (2 - i);
312312
let r = LUT_SXY2SH[idx as usize];
313313
// Override State
314314
state = r & 0b1100;
315-
result = (result & 0b00111111) | (state << 4);
315+
result = (result & 0b0011_1111) | (state << 4);
316316
// Dx Dy
317-
result = (result & !(0b00110000 >> (i * 2))) | ((r & 0b0011) << ((2 - i) * 2));
317+
result = (result & !(0b0011_0000 >> (i * 2))) | ((r & 0b0011) << ((2 - i) * 2));
318318
x_mask >>= 1;
319319
y_mask >>= 1;
320320
}
321321
lut_3[input as usize] = result;
322322
}
323-
println!("{:?}", lut_3);
323+
println!("{lut_3:?}");
324324
}
325325

326326
#[test]
@@ -329,9 +329,9 @@ mod tests {
329329
let mut lut_3: [u8; 256] = [0; 256];
330330
for input in 0..=255 {
331331
//for input in 4..=4 {
332-
let mut state: u8 = (input & 0b11000000) >> 6;
332+
let mut state: u8 = (input & 0b1100_0000) >> 6;
333333
let mut result: u8 = 0;
334-
let mut h_mask: u8 = 0b00110000;
334+
let mut h_mask: u8 = 0b0011_0000;
335335
for i in 0..3 {
336336
let idx = (state << 2) | (input & h_mask) >> (4 - (i * 2));
337337
let r = LUT_SH2SXY[idx as usize];
@@ -340,14 +340,14 @@ mod tests {
340340
let x = (r & 0b10) >> 1;
341341
let y = r & 0b1;
342342
// Set state
343-
result = (result & 0b00111111) | (state << 6);
344-
result = (result & !(0b00100000 >> i)) | (x << (5 - i));
345-
result = (result & !(0b00000100 >> i)) | (y << (2 - i));
343+
result = (result & 0b0011_1111) | (state << 6);
344+
result = (result & !(0b0010_0000 >> i)) | (x << (5 - i));
345+
result = (result & !(0b0000_0100 >> i)) | (y << (2 - i));
346346
h_mask >>= 2;
347347
}
348348
lut_3[input as usize] = result;
349349
}
350-
println!("{:?}", lut_3);
350+
println!("{lut_3:?}");
351351
}
352352

353353
#[test]
@@ -430,6 +430,7 @@ mod tests {
430430
}
431431
}
432432

433+
#[allow(clippy::cast_precision_loss, clippy::needless_range_loop)]
433434
fn draw_hilbert_curve(iteration: u32) -> image::ImageBuffer<image::Rgb<u8>, Vec<u8>> {
434435
let size: usize = 256;
435436
let border = 32 / iteration;
@@ -439,10 +440,10 @@ mod tests {
439440
let mut points: Vec<(u32, u32)> = vec![(0, 0); 2usize.pow(iteration * 2)];
440441
for i in 0..2usize.pow(iteration * 2) {
441442
let (mut x, mut y) = h2xy(i as u64, iteration as u8);
442-
let step = (size as u32 - border * 2) as f64 / (2usize.pow(iteration) as f64 - 1.0);
443-
x = (x as f64 * step) as u32 + border;
444-
y = (y as f64 * step) as u32 + border;
445-
points[i] = (x, y)
443+
let step = f64::from(size as u32 - border * 2) / (2usize.pow(iteration) as f64 - 1.0);
444+
x = (f64::from(x) * step) as u32 + border;
445+
y = (f64::from(y) * step) as u32 + border;
446+
points[i] = (x, y);
446447
}
447448

448449
let mut prev = (0, 0);
@@ -457,25 +458,21 @@ mod tests {
457458
let pixel = imgbuf.get_pixel_mut(prev.0, prev.1);
458459
*pixel = white;
459460
prev.0 += 1;
460-
continue;
461461
}
462462
while prev.0 > *x {
463463
let pixel = imgbuf.get_pixel_mut(prev.0, prev.1);
464464
*pixel = white;
465465
prev.0 -= 1;
466-
continue;
467466
}
468467
while prev.1 < *y {
469468
let pixel = imgbuf.get_pixel_mut(prev.0, prev.1);
470469
*pixel = white;
471470
prev.1 += 1;
472-
continue;
473471
}
474472
while prev.1 > *y {
475473
let pixel = imgbuf.get_pixel_mut(prev.0, prev.1);
476474
*pixel = white;
477475
prev.1 -= 1;
478-
continue;
479476
}
480477
}
481478
imgbuf
@@ -486,7 +483,7 @@ mod tests {
486483
fn write_image() {
487484
for i in 1..7 {
488485
let imgbuf = draw_hilbert_curve(i);
489-
imgbuf.save(format!("doc/h{}.png", i)).unwrap();
486+
imgbuf.save(format!("doc/h{i}.png")).unwrap();
490487
}
491488
}
492489
}

0 commit comments

Comments
 (0)