Skip to content

Commit 717878e

Browse files
authored
Merge pull request #40 from rust-cv/updates-ci-gat-etc
Updates ci gat etc
2 parents a310b14 + 1a1fc00 commit 717878e

12 files changed

Lines changed: 712 additions & 320 deletions

File tree

.cargo/config

Lines changed: 0 additions & 2 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: ci
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
8+
jobs:
9+
lints:
10+
name: lints
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout sources
14+
uses: actions/checkout@v4
15+
16+
- name: Install beta toolchain
17+
uses: dtolnay/rust-toolchain@beta
18+
with:
19+
components: rustfmt, clippy
20+
21+
- name: Set up cache
22+
uses: Swatinem/rust-cache@v2
23+
with:
24+
cache-on-failure: true
25+
26+
- name: Run cargo fmt
27+
run: cargo fmt --all -- --check
28+
29+
- name: Run cargo clippy
30+
run: cargo clippy --all-targets --tests -- -D warnings
31+
32+
no_std:
33+
name: no_std
34+
runs-on: ubuntu-latest
35+
steps:
36+
- name: Checkout sources
37+
uses: actions/checkout@v4
38+
39+
- name: Install beta toolchain for ARM
40+
uses: dtolnay/rust-toolchain@beta
41+
with:
42+
targets: armv7a-none-eabi
43+
44+
- name: Set up cache
45+
uses: Swatinem/rust-cache@v2
46+
with:
47+
cache-on-failure: true
48+
49+
- name: Build binary for armv7a-none-eabi
50+
run: cargo rustc --target=armv7a-none-eabi --manifest-path=ensure_no_std/Cargo.toml
51+
52+
tests:
53+
name: tests
54+
runs-on: ubuntu-latest
55+
steps:
56+
- name: Checkout sources
57+
uses: actions/checkout@v4
58+
59+
- name: Install beta toolchain
60+
uses: dtolnay/rust-toolchain@beta
61+
62+
- name: Set up cache
63+
uses: Swatinem/rust-cache@v2
64+
with:
65+
cache-on-failure: true
66+
67+
- name: Run cargo test
68+
#run: cargo test --all-features --no-fail-fast --locked --workspace -- --nocapture
69+
run: cargo test --all-features --no-fail-fast --workspace -- --nocapture

.github/workflows/lints.yml

Lines changed: 0 additions & 55 deletions
This file was deleted.

.github/workflows/no-std.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

.github/workflows/tests.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

Cargo.toml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
[package]
22
name = "space"
3-
version = "0.18.1-alpha.0"
4-
authors = ["Geordon Worley <vadixidav@gmail.com>", "Yuhan Liin <yuhanliin+github@protonmail.com>"]
5-
edition = "2018"
3+
version = "0.19.0"
4+
authors = [
5+
"Geordon Worley <vadixidav@gmail.com>",
6+
"Yuhan Liin <yuhanliin+github@protonmail.com>",
7+
]
8+
edition = "2024"
69
description = "A library providing abstractions for spatial datastructures and search"
710
documentation = "https://docs.rs/space/"
811
repository = "https://github.com/rust-cv/space"
@@ -17,13 +20,16 @@ default = ["alloc"]
1720
alloc = []
1821

1922
[dependencies]
20-
num-traits = { version = "0.2.14", default-features = false }
23+
num-traits = { version = "0.2.19", default-features = false }
2124
doc-comment = "0.3.3"
25+
pgat = "0.3.0"
2226

2327
[dev-dependencies]
24-
criterion = "0.3.4"
25-
rand_core = "0.6.2"
26-
rand_pcg = "0.3.0"
28+
criterion = "0.7.0"
29+
rand_core = "0.9.3"
30+
rand_pcg = "0.9.0"
31+
ndarray = "0.16.1"
32+
decorum = "0.4.0"
2733

2834
[[bench]]
2935
name = "knn"

README.md

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,66 @@ See the [bitarray](https://crates.io/crates/bitarray) crate for an implementatio
3030
of `MetricPoint` using hamming distance (with optional, though unstable, 512-bit
3131
SIMD support, and always-on 64-bit popcnt instruction support).
3232

33-
## Usage
33+
## Usage Examples
3434

35-
```rust
36-
use space::Metric;
37-
38-
struct Hamming;
39-
40-
impl Metric<u8> for Hamming {
41-
type Unit = u8;
42-
43-
fn distance(&self, &a: &u8, &b: &u8) -> Self::Unit {
44-
(a ^ b).count_ones() as u8
45-
}
46-
}
47-
```
35+
This example shows how to use the LinearSearch and LinearContainer that come built-in by default. You would use third party containers similarly and can abstract over them using the traits like SpatialContainer and Knn.
4836

4937
```rust
50-
use space::{Knn, KnnFromBatch, LinearKnn, Metric};
38+
use pgat::ReferenceProxy;
39+
use space::{Knn, LinearContainer, LinearSearch, Metric, SpatialContainer};
5140

52-
#[derive(Default)]
41+
#[derive(Copy, Clone, Default)]
5342
struct Hamming;
5443

55-
impl Metric<u8> for Hamming {
44+
impl Metric<ReferenceProxy<u8>> for Hamming {
5645
type Unit = u8;
5746

5847
fn distance(&self, &a: &u8, &b: &u8) -> Self::Unit {
5948
(a ^ b).count_ones() as u8
6049
}
6150
}
6251

63-
let data = vec![
52+
// Use type aliases like below to get default proxy types (ReferenceView) on the container.
53+
type Container = LinearContainer<Hamming, u8, u8>;
54+
type Search<'a> = LinearSearch<'a, Hamming, u8, u8>;
55+
56+
let data = [
6457
(0b1010_1010, 12),
6558
(0b1111_1111, 13),
6659
(0b0000_0000, 14),
6760
(0b1111_0000, 16),
6861
(0b0000_1111, 10),
6962
];
7063

71-
let search: LinearKnn<Hamming, _> = KnnFromBatch::from_batch(data.iter());
64+
let search = Search::new(Hamming, &data);
7265

7366
assert_eq!(
74-
&search.knn(&0b0101_0000, 3),
67+
search.knn(&0b0101_0000, 3).as_slice(),
7568
&[
7669
(2, &data[2].0, &data[2].1),
7770
(2, &data[3].0, &data[3].1),
7871
(6, &data[0].0, &data[0].1)
7972
]
8073
);
74+
75+
let mut search = Container::from_metric_and_iterator(Hamming, data);
76+
77+
assert_eq!(
78+
search.knn(&0b0101_0000, 3).as_slice(),
79+
&[
80+
(2, &data[2].0, &data[2].1),
81+
(2, &data[3].0, &data[3].1),
82+
(6, &data[0].0, &data[0].1)
83+
]
84+
);
85+
86+
search.insert(0b0101_0001, 8);
87+
88+
assert_eq!(search.nn(&0b0101_0000), Some((1, &0b0101_0001, &8)));
8189
```
8290

91+
For an example on how to create a container, a great reference may be found in tests/ndarray.rs in the repository. For brevity it is omitted here, but it shows how to create a specialized structure that uses an Array2 as the storage mechanism for Array1 points.
92+
8393
## Benchmarks
8494

8595
To run the benchmarks, use the following command:

benches/knn.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
use criterion::{criterion_group, criterion_main, Criterion};
1+
use criterion::{Criterion, criterion_group, criterion_main};
22
use rand_core::{RngCore, SeedableRng};
33
use rand_pcg::Pcg64;
44
use space::{Bits512, Knn, MetricPoint};
55

66
fn criterion_benchmark(c: &mut Criterion) {
77
let mut rng = Pcg64::from_seed([1; 32]);
8-
let mut gen = || {
8+
let mut generator = || {
99
let mut feature = Bits512([0; 64]);
1010
rng.fill_bytes(&mut *feature);
1111
feature
1212
};
13-
let search = gen();
14-
let data = (0..16384).map(|_| gen()).collect::<Vec<_>>();
13+
let search = generator();
14+
let data = (0..16384).map(|_| generator()).collect::<Vec<_>>();
1515
c.bench_function("space: 4-nn in 16384", |b| {
1616
b.iter(|| space::LinearKnn(data.iter()).knn(&search, 4).len())
1717
})

0 commit comments

Comments
 (0)