Skip to content

Commit

Permalink
Remove lifetime from scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Dec 16, 2024
1 parent 5b69dac commit 13c1d9f
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 495 deletions.
10 changes: 6 additions & 4 deletions rust/geoarrow/src/array/coord/interleaved/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,17 @@ impl InterleavedCoordBuffer {
self.len() == 0
}

pub fn value(&self, index: usize) -> InterleavedCoord<'_> {
pub fn value(&self, index: usize) -> InterleavedCoord {
assert!(index <= self.len());
self.value_unchecked(index)
}

pub fn value_unchecked(&self, index: usize) -> InterleavedCoord<'_> {
pub fn value_unchecked(&self, index: usize) -> InterleavedCoord {
InterleavedCoord {
coords: &self.coords,
i: index,
coords: self
.coords
.clone()
.slice(index * self.dim.size(), self.dim.size()),
dim: self.dim,
}
}
Expand Down
7 changes: 3 additions & 4 deletions rust/geoarrow/src/array/coord/separated/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,14 @@ impl SeparatedCoordBuffer {
self.len() == 0
}

pub fn value(&self, index: usize) -> SeparatedCoord<'_> {
pub fn value(&self, index: usize) -> SeparatedCoord {
assert!(index <= self.len());
self.value_unchecked(index)
}

pub fn value_unchecked(&self, index: usize) -> SeparatedCoord<'_> {
pub fn value_unchecked(&self, index: usize) -> SeparatedCoord {
SeparatedCoord {
buffers: &self.buffers,
i: index,
buffers: self.buffers.clone().map(|buffer| buffer.slice(index, 1)),
dim: self.dim,
}
}
Expand Down
12 changes: 8 additions & 4 deletions rust/geoarrow/src/array/linestring/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ impl LineStringArray {
self.metadata,
)
}

fn value(&self, index: usize) -> LineString {
LineString::new(self.slice(index, 1))
}
}

impl ArrayBase for LineStringArray {
Expand Down Expand Up @@ -284,7 +288,7 @@ impl GeometryArraySelfMethods for LineStringArray {

impl NativeGeometryAccessor for LineStringArray {
unsafe fn value_as_geometry_unchecked(&self, index: usize) -> crate::scalar::Geometry {
Geometry::LineString(LineString::new(&self.coords, &self.geom_offsets, index))
Geometry::LineString(self.value(index))
}
}

Expand All @@ -294,17 +298,17 @@ impl<'a> crate::trait_::NativeGEOSGeometryAccessor<'a> for LineStringArray {
&'a self,
index: usize,
) -> std::result::Result<geos::Geometry, geos::Error> {
let geom = LineString::new(&self.coords, &self.geom_offsets, index);
let geom = self.value(index);
(&geom).try_into()
}
}

impl<'a> ArrayAccessor<'a> for LineStringArray {
type Item = LineString<'a>;
type Item = LineString;
type ItemGeo = geo::LineString;

unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item {
LineString::new(&self.coords, &self.geom_offsets, index)
self.value(index)
}
}

Expand Down
8 changes: 4 additions & 4 deletions rust/geoarrow/src/array/point/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl GeometryArraySelfMethods for PointArray {

impl NativeGeometryAccessor for PointArray {
unsafe fn value_as_geometry_unchecked(&self, index: usize) -> crate::scalar::Geometry {
Geometry::Point(Point::new(&self.coords, index))
Geometry::Point(Point::new(self.slice(index, 1)))
}
}

Expand All @@ -226,17 +226,17 @@ impl<'a> crate::trait_::NativeGEOSGeometryAccessor<'a> for PointArray {
&'a self,
index: usize,
) -> std::result::Result<geos::Geometry, geos::Error> {
let geom = Point::new(&self.coords, index);
let geom = Point::new(self.slice(index, 1));
(&geom).try_into()
}
}

impl<'a> ArrayAccessor<'a> for PointArray {
type Item = Point<'a>;
type Item = Point;
type ItemGeo = geo::Point;

unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item {
Point::new(&self.coords, index)
Point::new(self.slice(index, 1))
}
}

Expand Down
15 changes: 7 additions & 8 deletions rust/geoarrow/src/array/polygon/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ impl PolygonArray {
self.metadata,
)
}

fn value(&self, index: usize) -> Polygon {
Polygon::new(self.slice(index, 1))
}
}

impl ArrayBase for PolygonArray {
Expand Down Expand Up @@ -300,12 +304,7 @@ impl GeometryArraySelfMethods for PolygonArray {

impl NativeGeometryAccessor for PolygonArray {
unsafe fn value_as_geometry_unchecked(&self, index: usize) -> crate::scalar::Geometry {
Geometry::Polygon(Polygon::new(
&self.coords,
&self.geom_offsets,
&self.ring_offsets,
index,
))
Geometry::Polygon(self.value(index))
}
}

Expand All @@ -315,7 +314,7 @@ impl<'a> crate::trait_::NativeGEOSGeometryAccessor<'a> for PolygonArray {
&'a self,
index: usize,
) -> std::result::Result<geos::Geometry, geos::Error> {
let geom = Polygon::new(&self.coords, &self.geom_offsets, &self.ring_offsets, index);
let geom = self.value(index);
(&geom).try_into()
}
}
Expand All @@ -325,7 +324,7 @@ impl<'a> ArrayAccessor<'a> for PolygonArray {
type ItemGeo = geo::Polygon;

unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item {
Polygon::new(&self.coords, &self.geom_offsets, &self.ring_offsets, index)
self.value(index)
}
}

Expand Down
34 changes: 17 additions & 17 deletions rust/geoarrow/src/scalar/coord/combined/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use geo_traits::to_geo::ToGeoCoord;
use geo_traits::CoordTrait;

#[derive(Debug, Clone)]
pub enum Coord<'a> {
Separated(SeparatedCoord<'a>),
Interleaved(InterleavedCoord<'a>),
pub enum Coord {
Separated(SeparatedCoord),
Interleaved(InterleavedCoord),
}

impl Coord<'_> {
impl Coord {
/// Return `true` if all values in the coordinate are f64::NAN
pub(crate) fn is_nan(&self) -> bool {
match self {
Expand All @@ -21,7 +21,7 @@ impl Coord<'_> {
}
}

impl NativeScalar for Coord<'_> {
impl NativeScalar for Coord {
type ScalarGeo = geo::Coord;

fn to_geo(&self) -> Self::ScalarGeo {
Expand All @@ -39,25 +39,25 @@ impl NativeScalar for Coord<'_> {
}
}

impl From<Coord<'_>> for geo::Coord {
impl From<Coord> for geo::Coord {
fn from(value: Coord) -> Self {
(&value).into()
}
}

impl From<&Coord<'_>> for geo::Coord {
impl From<&Coord> for geo::Coord {
fn from(value: &Coord) -> Self {
value.to_coord()
}
}

impl From<Coord<'_>> for geo::Point {
impl From<Coord> for geo::Point {
fn from(value: Coord) -> Self {
(&value).into()
}
}

impl From<&Coord<'_>> for geo::Point {
impl From<&Coord> for geo::Point {
fn from(value: &Coord) -> Self {
match value {
Coord::Separated(c) => c.into(),
Expand All @@ -66,7 +66,7 @@ impl From<&Coord<'_>> for geo::Point {
}
}

impl RTreeObject for Coord<'_> {
impl RTreeObject for Coord {
type Envelope = AABB<[f64; 2]>;

fn envelope(&self) -> Self::Envelope {
Expand All @@ -77,25 +77,25 @@ impl RTreeObject for Coord<'_> {
}
}

impl PartialEq for Coord<'_> {
impl PartialEq for Coord {
fn eq(&self, other: &Self) -> bool {
self.x_y() == other.x_y()
}
}

impl PartialEq<InterleavedCoord<'_>> for Coord<'_> {
fn eq(&self, other: &InterleavedCoord<'_>) -> bool {
impl PartialEq<InterleavedCoord> for Coord {
fn eq(&self, other: &InterleavedCoord) -> bool {
self.x_y() == other.x_y()
}
}

impl PartialEq<SeparatedCoord<'_>> for Coord<'_> {
fn eq(&self, other: &SeparatedCoord<'_>) -> bool {
impl PartialEq<SeparatedCoord> for Coord {
fn eq(&self, other: &SeparatedCoord) -> bool {
self.x_y() == other.x_y()
}
}

impl CoordTrait for Coord<'_> {
impl CoordTrait for Coord {
type T = f64;

fn dim(&self) -> geo_traits::Dimensions {
Expand Down Expand Up @@ -127,7 +127,7 @@ impl CoordTrait for Coord<'_> {
}
}

impl CoordTrait for &Coord<'_> {
impl CoordTrait for &Coord {
type T = f64;

fn dim(&self) -> geo_traits::Dimensions {
Expand Down
45 changes: 22 additions & 23 deletions rust/geoarrow/src/scalar/coord/interleaved/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ use geo_traits::to_geo::ToGeoCoord;
use geo_traits::CoordTrait;

#[derive(Debug, Clone)]
pub struct InterleavedCoord<'a> {
pub(crate) coords: &'a ScalarBuffer<f64>,
pub(crate) i: usize,
pub struct InterleavedCoord {
pub(crate) coords: ScalarBuffer<f64>,
pub(crate) dim: Dimension,
}

impl InterleavedCoord<'_> {
impl InterleavedCoord {
/// Return `true` if all values in the coordinate are f64::NAN
pub(crate) fn is_nan(&self) -> bool {
(0..self.dim.size()).all(|coord_dim| self.nth_or_panic(coord_dim).is_nan())
}
}

impl NativeScalar for InterleavedCoord<'_> {
impl NativeScalar for InterleavedCoord {
type ScalarGeo = geo::Coord;

fn to_geo(&self) -> Self::ScalarGeo {
Expand All @@ -40,52 +39,52 @@ impl NativeScalar for InterleavedCoord<'_> {
}
}

impl From<InterleavedCoord<'_>> for geo::Coord {
impl From<InterleavedCoord> for geo::Coord {
fn from(value: InterleavedCoord) -> Self {
(&value).into()
}
}

impl From<&InterleavedCoord<'_>> for geo::Coord {
impl From<&InterleavedCoord> for geo::Coord {
fn from(value: &InterleavedCoord) -> Self {
value.to_coord()
}
}

impl From<InterleavedCoord<'_>> for geo::Point {
fn from(value: InterleavedCoord<'_>) -> Self {
impl From<InterleavedCoord> for geo::Point {
fn from(value: InterleavedCoord) -> Self {
(&value).into()
}
}

impl From<&InterleavedCoord<'_>> for geo::Point {
fn from(value: &InterleavedCoord<'_>) -> Self {
impl From<&InterleavedCoord> for geo::Point {
fn from(value: &InterleavedCoord) -> Self {
let coord: geo::Coord = value.into();
coord.into()
}
}

impl RTreeObject for InterleavedCoord<'_> {
impl RTreeObject for InterleavedCoord {
type Envelope = AABB<[f64; 2]>;

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

impl PartialEq for InterleavedCoord<'_> {
impl PartialEq for InterleavedCoord {
fn eq(&self, other: &Self) -> bool {
coord_eq(self, other)
}
}

impl PartialEq<SeparatedCoord<'_>> for InterleavedCoord<'_> {
fn eq(&self, other: &SeparatedCoord<'_>) -> bool {
impl PartialEq<SeparatedCoord> for InterleavedCoord {
fn eq(&self, other: &SeparatedCoord) -> bool {
coord_eq(self, other)
}
}

impl CoordTrait for InterleavedCoord<'_> {
impl CoordTrait for InterleavedCoord {
type T = f64;

fn dim(&self) -> geo_traits::Dimensions {
Expand All @@ -94,19 +93,19 @@ impl CoordTrait for InterleavedCoord<'_> {

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()
*self.coords.get(n).unwrap()
}

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

fn y(&self) -> Self::T {
*self.coords.get(self.i * self.dim.size() + 1).unwrap()
*self.coords.get(1).unwrap()
}
}

impl CoordTrait for &InterleavedCoord<'_> {
impl CoordTrait for &InterleavedCoord {
type T = f64;

fn dim(&self) -> geo_traits::Dimensions {
Expand All @@ -115,15 +114,15 @@ impl CoordTrait for &InterleavedCoord<'_> {

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()
*self.coords.get(n).unwrap()
}

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

fn y(&self) -> Self::T {
*self.coords.get(self.i * self.dim.size() + 1).unwrap()
*self.coords.get(1).unwrap()
}
}

Expand Down
Loading

0 comments on commit 13c1d9f

Please sign in to comment.