Skip to content

Commit

Permalink
Merge #115
Browse files Browse the repository at this point in the history
115: Impl Point for [RTreeNum; N] on all N r=michaelkirk a=dominikWin

- [X] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- [X] I added an entry to `rstar/CHANGELOG.md` if knowledge of this change could be valuable to users.
---

Point is now implemented for `[RTreeNum; N]` where `N` is a `usize`. Previously it was only for N values between 2 and 9. Const generics are stable in every supported version of Rust.

Co-authored-by: Dominik Winecki <dominikwinecki@gmail.com>
  • Loading branch information
bors[bot] and dominikWin authored Apr 10, 2023
2 parents 101773a + 8b8564f commit 759b9d4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
1 change: 1 addition & 0 deletions rstar/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Added
- Added method `RTree::drain()`.
- Changed license field to [SPDX 2.1 license expression](https://spdx.dev/spdx-specification-21-web-version/#h.jxpfx0ykyb60)
- `Point` is now implemented as const generic for any length of `RTreeNum` array

## Changed
- fixed all clippy lint issues
Expand Down
67 changes: 32 additions & 35 deletions rstar/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,48 +304,45 @@ where
}
}

macro_rules! count_exprs {
() => (0);
($head:expr) => (1);
($head:expr, $($tail:expr),*) => (1 + count_exprs!($($tail),*));
}
impl<S, const N: usize> Point for [S; N]
where
S: RTreeNum,
{
type Scalar = S;

macro_rules! implement_point_for_array {
($($index:expr),*) => {
impl<S> Point for [S; count_exprs!($($index),*)]
where
S: RTreeNum,
{
type Scalar = S;
const DIMENSIONS: usize = N;

const DIMENSIONS: usize = count_exprs!($($index),*);
fn generate(mut generator: impl FnMut(usize) -> S) -> Self {
assert!(N >= 2, "Point dimension too small - must be at least 2");

fn generate(mut generator: impl FnMut(usize) -> S) -> Self
{
[$(generator($index)),*]
}
// The same implementation used in std::array::from_fn
// Since this is a const generic it gets unrolled
let mut idx = 0;
[(); N].map(|_| {
let res = generator(idx);
idx += 1;
res
})
}

#[inline]
fn nth(&self, index: usize) -> Self::Scalar {
self[index]
}
#[inline]
fn nth(&self, index: usize) -> Self::Scalar {
assert!(N >= 2, "Point dimension too small - must be at least 2");
self[index]
}

#[inline]
fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
&mut self[index]
}
}
};
#[inline]
fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
assert!(N >= 2, "Point dimension too small - must be at least 2");
&mut self[index]
}
}

implement_point_for_array!(0, 1);
implement_point_for_array!(0, 1, 2);
implement_point_for_array!(0, 1, 2, 3);
implement_point_for_array!(0, 1, 2, 3, 4);
implement_point_for_array!(0, 1, 2, 3, 4, 5);
implement_point_for_array!(0, 1, 2, 3, 4, 5, 6);
implement_point_for_array!(0, 1, 2, 3, 4, 5, 6, 7);
implement_point_for_array!(0, 1, 2, 3, 4, 5, 6, 7, 8);
macro_rules! count_exprs {
() => (0);
($head:expr) => (1);
($head:expr, $($tail:expr),*) => (1 + count_exprs!($($tail),*));
}

macro_rules! fixed_type {
($expr:expr, $type:ty) => {
Expand Down

0 comments on commit 759b9d4

Please sign in to comment.