Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: remove COW scalar #472

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/scalar/point/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ impl OwnedPoint {
pub fn new(coords: CoordBuffer, geom_index: usize) -> Self {
Self { coords, geom_index }
}

pub fn as_ref(&self) -> Point {
self.into()
}
}

impl<'a> From<OwnedPoint> for Point<'a> {
fn from(value: OwnedPoint) -> Self {
Self::new_owned(value.coords, value.geom_index)
(&value).into()
}
}

impl<'a> From<&'a OwnedPoint> for Point<'a> {
fn from(value: &'a OwnedPoint) -> Self {
Self::new_borrowed(&value.coords, value.geom_index)
Self::new(&value.coords, value.geom_index)
}
}

Expand Down
47 changes: 13 additions & 34 deletions src/scalar/point/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use crate::algorithm::native::eq::point_eq;
use crate::array::CoordBuffer;
use crate::geo_traits::{CoordTrait, PointTrait};
use crate::io::geo::{coord_to_geo, point_to_geo};
use crate::scalar::OwnedPoint;
use crate::trait_::{GeometryArraySelfMethods, GeometryScalarTrait};
use rstar::{RTreeObject, AABB};
use std::borrow::Cow;

/// An Arrow equivalent of a Point
#[derive(Debug, Clone)]
pub struct Point<'a> {
coords: Cow<'a, CoordBuffer>,
coords: &'a CoordBuffer,
geom_index: usize,
}

Expand All @@ -31,42 +31,15 @@ pub struct Point<'a> {
// }

impl<'a> Point<'a> {
pub fn new(coords: Cow<'a, CoordBuffer>, geom_index: usize) -> Self {
pub fn new(coords: &'a CoordBuffer, geom_index: usize) -> Self {
Point { coords, geom_index }
}

pub fn new_borrowed(coords: &'a CoordBuffer, geom_index: usize) -> Self {
Point {
coords: Cow::Borrowed(coords),
geom_index,
}
}

pub fn new_owned(coords: CoordBuffer, geom_index: usize) -> Self {
Point {
coords: Cow::Owned(coords),
geom_index,
}
}

/// Extracts the owned data.
///
/// Clones the data if it is not already owned.
pub fn into_owned(self) -> Self {
match self.coords {
Cow::Owned(cb) => Self::new_owned(cb, self.geom_index),
Cow::Borrowed(cb) => {
// TODO: should this just take the overhead of converting to a point array and slicing that?
// TODO: DRY this with array impl
let coords = cb.owned_slice(self.geom_index, 1);
Self::new_owned(coords, 0)
}
}
}

pub fn into_owned_inner(self) -> (CoordBuffer, usize) {
let owned = self.into_owned();
(owned.coords.into_owned(), owned.geom_index)
// TODO: should this just take the overhead of converting to a point array and slicing that?
// TODO: DRY this with array impl
let coords = self.coords.owned_slice(self.geom_index, 1);
(coords, 0)
}
}

Expand Down Expand Up @@ -164,6 +137,12 @@ impl PartialEq for Point<'_> {
}
}

impl PartialEq<OwnedPoint> for Point<'_> {
fn eq(&self, other: &OwnedPoint) -> bool {
point_eq(self, other, true)
}
}

#[cfg(test)]
mod test {
use crate::array::{CoordBuffer, PointArray};
Expand Down
Loading