Skip to content

Commit

Permalink
Inline geometry trait accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Jan 3, 2025
1 parent b4ecffa commit 9c0dcd5
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 1 deletion.
8 changes: 8 additions & 0 deletions rust/geoarrow/src/scalar/coord/combined/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,31 @@ impl PartialEq<SeparatedCoord<'_>> for Coord<'_> {
impl CoordTrait for Coord<'_> {
type T = f64;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
match self {
Coord::Interleaved(c) => c.dim(),
Coord::Separated(c) => c.dim(),
}
}

#[inline]
fn nth_or_panic(&self, n: usize) -> Self::T {
match self {
Coord::Interleaved(c) => c.nth_or_panic(n),
Coord::Separated(c) => c.nth_or_panic(n),
}
}

#[inline]
fn x(&self) -> Self::T {
match self {
Coord::Interleaved(c) => c.x(),
Coord::Separated(c) => c.x(),
}
}

#[inline]
fn y(&self) -> Self::T {
match self {
Coord::Interleaved(c) => c.y(),
Expand All @@ -135,27 +139,31 @@ impl CoordTrait for Coord<'_> {
impl CoordTrait for &Coord<'_> {
type T = f64;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
match self {
Coord::Interleaved(c) => c.dim(),
Coord::Separated(c) => c.dim(),
}
}

#[inline]
fn nth_or_panic(&self, n: usize) -> Self::T {
match self {
Coord::Interleaved(c) => c.nth_or_panic(n),
Coord::Separated(c) => c.nth_or_panic(n),
}
}

#[inline]
fn x(&self) -> Self::T {
match self {
Coord::Interleaved(c) => c.x(),
Coord::Separated(c) => c.x(),
}
}

#[inline]
fn y(&self) -> Self::T {
match self {
Coord::Interleaved(c) => c.y(),
Expand Down
10 changes: 10 additions & 0 deletions rust/geoarrow/src/scalar/coord/interleaved/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ impl From<&InterleavedCoord<'_>> for geo::Point {
impl RTreeObject for InterleavedCoord<'_> {
type Envelope = AABB<[f64; 2]>;

#[inline]
fn envelope(&self) -> Self::Envelope {
AABB::from_point([self.x(), self.y()])
}
}

impl PartialEq for InterleavedCoord<'_> {
#[inline]
fn eq(&self, other: &Self) -> bool {
coord_eq(self, other)
}
Expand All @@ -91,19 +93,23 @@ impl PartialEq<SeparatedCoord<'_>> for InterleavedCoord<'_> {
impl CoordTrait for InterleavedCoord<'_> {
type T = f64;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
self.dim.into()
}

#[inline]
fn nth_or_panic(&self, n: usize) -> Self::T {
debug_assert!(n < self.dim.size());
*self.coords.get(self.i * self.dim.size() + n).unwrap()
}

#[inline]
fn x(&self) -> Self::T {
*self.coords.get(self.i * self.dim.size()).unwrap()
}

#[inline]
fn y(&self) -> Self::T {
*self.coords.get(self.i * self.dim.size() + 1).unwrap()
}
Expand All @@ -112,19 +118,23 @@ impl CoordTrait for InterleavedCoord<'_> {
impl CoordTrait for &InterleavedCoord<'_> {
type T = f64;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
self.dim.into()
}

#[inline]
fn nth_or_panic(&self, n: usize) -> Self::T {
debug_assert!(n < self.dim.size());
*self.coords.get(self.i * self.dim.size() + n).unwrap()
}

#[inline]
fn x(&self) -> Self::T {
*self.coords.get(self.i * self.dim.size()).unwrap()
}

#[inline]
fn y(&self) -> Self::T {
*self.coords.get(self.i * self.dim.size() + 1).unwrap()
}
Expand Down
8 changes: 8 additions & 0 deletions rust/geoarrow/src/scalar/coord/separated/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,22 @@ impl PartialEq<InterleavedCoord<'_>> for SeparatedCoord<'_> {
impl CoordTrait for SeparatedCoord<'_> {
type T = f64;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
self.dim.into()
}

#[inline]
fn nth_or_panic(&self, n: usize) -> Self::T {
self.buffers[n][self.i]
}

#[inline]
fn x(&self) -> Self::T {
self.buffers[0][self.i]
}

#[inline]
fn y(&self) -> Self::T {
self.buffers[1][self.i]
}
Expand All @@ -109,18 +113,22 @@ impl CoordTrait for SeparatedCoord<'_> {
impl CoordTrait for &SeparatedCoord<'_> {
type T = f64;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
self.dim.into()
}

#[inline]
fn nth_or_panic(&self, n: usize) -> Self::T {
self.buffers[n][self.i]
}

#[inline]
fn x(&self) -> Self::T {
self.buffers[0][self.i]
}

#[inline]
fn y(&self) -> Self::T {
self.buffers[1][self.i]
}
Expand Down
4 changes: 4 additions & 0 deletions rust/geoarrow/src/scalar/geometry/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl GeometryTrait for Geometry<'_> {
where
Self: 'b;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
match self {
Geometry::Point(p) => p.dim(),
Expand All @@ -114,6 +115,7 @@ impl GeometryTrait for Geometry<'_> {
}
}

#[inline]
fn as_type(
&self,
) -> geo_traits::GeometryType<
Expand Down Expand Up @@ -185,6 +187,7 @@ impl<'a> GeometryTrait for &'a Geometry<'a> {
where
Self: 'b;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
match self {
Geometry::Point(p) => p.dim(),
Expand All @@ -198,6 +201,7 @@ impl<'a> GeometryTrait for &'a Geometry<'a> {
}
}

#[inline]
fn as_type(
&self,
) -> geo_traits::GeometryType<
Expand Down
6 changes: 6 additions & 0 deletions rust/geoarrow/src/scalar/geometrycollection/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,21 @@ impl<'a> GeometryCollectionTrait for GeometryCollection<'a> {
where
Self: 'b;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
match self.array.dimension() {
Dimension::XY => geo_traits::Dimensions::Xy,
Dimension::XYZ => geo_traits::Dimensions::Xyz,
}
}

#[inline]
fn num_geometries(&self) -> usize {
let (start, end) = self.geom_offsets.start_end(self.geom_index);
end - start
}

#[inline]
unsafe fn geometry_unchecked(&self, i: usize) -> Self::GeometryType<'_> {
self.array.value(self.start_offset + i)
}
Expand All @@ -95,18 +98,21 @@ impl<'a> GeometryCollectionTrait for &'a GeometryCollection<'a> {
where
Self: 'b;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
match self.array.dimension() {
Dimension::XY => geo_traits::Dimensions::Xy,
Dimension::XYZ => geo_traits::Dimensions::Xyz,
}
}

#[inline]
fn num_geometries(&self) -> usize {
let (start, end) = self.geom_offsets.start_end(self.geom_index);
end - start
}

#[inline]
unsafe fn geometry_unchecked(&self, i: usize) -> Self::GeometryType<'_> {
self.array.value(self.start_offset + i)
}
Expand Down
6 changes: 6 additions & 0 deletions rust/geoarrow/src/scalar/linestring/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@ impl<'a> LineStringTrait for LineString<'a> {
where
Self: 'b;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
self.coords.dim().into()
}

#[inline]
fn num_coords(&self) -> usize {
let (start, end) = self.geom_offsets.start_end(self.geom_index);
end - start
}

#[inline]
unsafe fn coord_unchecked(&self, i: usize) -> Self::CoordType<'_> {
self.coords.value(self.start_offset + i)
}
Expand All @@ -93,15 +96,18 @@ impl<'a> LineStringTrait for &'a LineString<'a> {
where
Self: 'b;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
self.coords.dim().into()
}

#[inline]
fn num_coords(&self) -> usize {
let (start, end) = self.geom_offsets.start_end(self.geom_index);
end - start
}

#[inline]
unsafe fn coord_unchecked(&self, i: usize) -> Self::CoordType<'_> {
self.coords.value(self.start_offset + i)
}
Expand Down
6 changes: 6 additions & 0 deletions rust/geoarrow/src/scalar/multilinestring/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,18 @@ impl<'a> MultiLineStringTrait for MultiLineString<'a> {
where
Self: 'b;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
self.coords.dim().into()
}

#[inline]
fn num_line_strings(&self) -> usize {
let (start, end) = self.geom_offsets.start_end(self.geom_index);
end - start
}

#[inline]
unsafe fn line_string_unchecked(&self, i: usize) -> Self::LineStringType<'_> {
LineString::new(self.coords, self.ring_offsets, self.start_offset + i)
}
Expand All @@ -101,15 +104,18 @@ impl<'a> MultiLineStringTrait for &'a MultiLineString<'a> {
where
Self: 'b;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
self.coords.dim().into()
}

#[inline]
fn num_line_strings(&self) -> usize {
let (start, end) = self.geom_offsets.start_end(self.geom_index);
end - start
}

#[inline]
unsafe fn line_string_unchecked(&self, i: usize) -> Self::LineStringType<'_> {
LineString::new(self.coords, self.ring_offsets, self.start_offset + i)
}
Expand Down
6 changes: 6 additions & 0 deletions rust/geoarrow/src/scalar/multipoint/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,21 @@ impl<'a> MultiPointTrait for MultiPoint<'a> {
where
Self: 'b;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
match self.coords.dim() {
Dimension::XY => geo_traits::Dimensions::Xy,
Dimension::XYZ => geo_traits::Dimensions::Xyz,
}
}

#[inline]
fn num_points(&self) -> usize {
let (start, end) = self.geom_offsets.start_end(self.geom_index);
end - start
}

#[inline]
unsafe fn point_unchecked(&self, i: usize) -> Self::PointType<'_> {
Point::new(self.coords, self.start_offset + i)
}
Expand All @@ -98,18 +101,21 @@ impl<'a> MultiPointTrait for &'a MultiPoint<'a> {
where
Self: 'b;

#[inline]
fn dim(&self) -> geo_traits::Dimensions {
match self.coords.dim() {
Dimension::XY => geo_traits::Dimensions::Xy,
Dimension::XYZ => geo_traits::Dimensions::Xyz,
}
}

#[inline]
fn num_points(&self) -> usize {
let (start, end) = self.geom_offsets.start_end(self.geom_index);
end - start
}

#[inline]
unsafe fn point_unchecked(&self, i: usize) -> Self::PointType<'_> {
Point::new(self.coords, self.start_offset + i)
}
Expand Down
Loading

0 comments on commit 9c0dcd5

Please sign in to comment.