Skip to content

Commit 6aa9ad2

Browse files
authored
Merge pull request #8 from naomijub/minor-improvements_and_CI
Small improvements improvements
2 parents 24758e3 + 4edb6fd commit 6aa9ad2

File tree

8 files changed

+651
-589
lines changed

8 files changed

+651
-589
lines changed

.github/workflows/rust.yml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Rust
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "*" ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
- name: Install alsa and udev
19+
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev libxkbcommon-dev
20+
- name: Build
21+
run: cargo build --all-features --release --verbose
22+
23+
test:
24+
runs-on: ubuntu-latest
25+
permissions: write-all
26+
steps:
27+
- uses: actions/checkout@v2
28+
- name: Create folder
29+
run: mkdir -p assets/voxel_data/
30+
- name: Install alsa and udev
31+
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev libxkbcommon-dev
32+
- name: tests
33+
run: cargo test
34+
35+
fmt:
36+
runs-on: ubuntu-latest
37+
38+
steps:
39+
- uses: actions/checkout@v2
40+
- name: FMT
41+
run: cargo fmt -- --check
42+
43+
clippy:
44+
runs-on: ubuntu-latest
45+
46+
steps:
47+
- uses: actions/checkout@v2
48+
- name: Install alsa and udev
49+
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev libxkbcommon-dev
50+
- name: install-clippy
51+
run: rustup component add clippy
52+
- name: clippy
53+
run: cargo clippy --all-features -- -W clippy::all -W clippy::nursery --deny "warnings"

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
/Cargo.lock

Cargo.toml

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lindel"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
authors = ["froderick <[email protected]>"]
55
edition = "2018"
66
description = "A crate for Hilbert and Morton encoding and decoding; in a word, linearising and delinearising."
@@ -18,12 +18,13 @@ repository = "https://github.com/DoubleHyphen/lindel"
1818
all-features = true
1919

2020
[dependencies]
21-
num = {version = "0.2"}
22-
num-traits = {version = "0.2"}
21+
num = "0.4"
22+
num-traits = "0.2"
2323
nalgebra = {version = "0.25", optional = true}
2424

2525
[dev-dependencies]
2626
rand = "0.8"
27+
criterion = "0.4"
2728

2829
[dependencies.morton-encoding]
2930
version = "^2"
@@ -33,3 +34,8 @@ default-features = false
3334
default = ["std"]
3435
std = []
3536
nalg = ["nalgebra"]
37+
38+
39+
[[bench]]
40+
name = "metrics"
41+
harness = false

benches/metrics.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use criterion::{criterion_group, criterion_main, Criterion};
2+
use lindel::*;
3+
4+
pub fn criterion_benchmark(c: &mut Criterion) {
5+
c.bench_function("hilbert_decode", |b| {
6+
b.iter(|| {
7+
let _: [u32; 3] = hilbert_decode(34589430);
8+
})
9+
});
10+
c.bench_function("hilbert_encode", |b| {
11+
b.iter(|| {
12+
let _: u128 = hilbert_encode([43u32, 12, 32]);
13+
})
14+
});
15+
16+
c.bench_function("morton_decode", |b| {
17+
b.iter(|| {
18+
let _: [u32; 3] = morton_decode(34589430);
19+
})
20+
});
21+
c.bench_function("morton_encode", |b| {
22+
b.iter(|| {
23+
let _: u128 = morton_encode([43u32, 12, 32]);
24+
})
25+
});
26+
}
27+
28+
criterion_group!(benches, criterion_benchmark);
29+
criterion_main!(benches);

src/compact_encoding.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
//! Chris Hamilton, and (in the near future) of Compact Z-indices by the crate maintainer.
33
44
/// Right rotation of x by b bits out of n.
5-
fn rotate_right(x: usize, b: u32, n: u32) -> usize {
5+
const fn rotate_right(x: usize, b: u32, n: u32) -> usize {
66
let l = x & ((1 << b) - 1);
77
let r = x >> b;
88
(l << (n - b)) | r
99
}
1010

1111
/// Left rotation of x by b bits out of n.
12-
fn rotate_left(x: usize, b: u32, n: u32) -> usize {
12+
const fn rotate_left(x: usize, b: u32, n: u32) -> usize {
1313
rotate_right(x, n - b, n)
1414
}
1515

1616
/// Binary reflected Gray code.
17-
fn gray_code(i: usize) -> usize {
17+
const fn gray_code(i: usize) -> usize {
1818
i ^ (i >> 1)
1919
}
2020

2121
/// e(i), the entry point for the ith sub-hypercube.
22-
fn entry_point(i: usize) -> usize {
22+
const fn entry_point(i: usize) -> usize {
2323
if i == 0 {
2424
0
2525
} else {
@@ -28,13 +28,13 @@ fn entry_point(i: usize) -> usize {
2828
}
2929

3030
/// g(i), the inter sub-hypercube direction.
31-
fn inter_direction(i: usize) -> u32 {
31+
const fn inter_direction(i: usize) -> u32 {
3232
// g(i) counts the trailing set bits in i
3333
(!i).trailing_zeros()
3434
}
3535

3636
/// d(i), the intra sub-hypercube direction.
37-
fn intra_direction(i: usize) -> u32 {
37+
const fn intra_direction(i: usize) -> u32 {
3838
if i & 1 != 0 {
3939
inter_direction(i)
4040
} else if i > 0 {
@@ -45,7 +45,7 @@ fn intra_direction(i: usize) -> u32 {
4545
}
4646

4747
/// T transformation inverse
48-
fn t_inverse(dims: u32, e: usize, d: u32, a: usize) -> usize {
48+
const fn t_inverse(dims: u32, e: usize, d: u32, a: usize) -> usize {
4949
rotate_left(a, d, dims) ^ e
5050
}
5151

@@ -136,12 +136,12 @@ pub fn from_compact_hilbert_index(index: usize, bits: &[u32], point: &mut [usize
136136
}
137137

138138
/// T transformation
139-
fn t(dims: u32, e: usize, d: u32, b: usize) -> usize {
139+
const fn t(dims: u32, e: usize, d: u32, b: usize) -> usize {
140140
rotate_right(b ^ e, d, dims)
141141
}
142142

143143
/// GrayCodeInverse
144-
fn gray_code_inverse(mut g: usize) -> usize {
144+
const fn gray_code_inverse(mut g: usize) -> usize {
145145
let mut i = 0;
146146

147147
while g != 0 {
@@ -199,8 +199,6 @@ pub fn compact_hilbert_index(bits: &[u32], point: &[usize]) -> usize {
199199
h
200200
}
201201

202-
203-
204202
/*fn returns_closure (xs: &[u8]) -> impl Fn(u32) -> u32 {
205203
let sum: u32 = xs.iter().map(|&x| x as u32).sum();
206204
move |a| a*sum

0 commit comments

Comments
 (0)