-
Notifications
You must be signed in to change notification settings - Fork 200
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
Map coords inplace #170
Map coords inplace #170
Conversation
src/algorithm/translate.rs
Outdated
@@ -23,15 +23,22 @@ pub trait Translate<T> { | |||
/// assert_eq!(translated, correct_ls); | |||
/// ``` | |||
fn translate(&self, xoff: T, yoff: T) -> Self where T: Float; | |||
|
|||
/// Translate a Geometry along its axes, but in place. | |||
fn translate_inplace(&mut self, xoff: T, yoff: T) where T: Float; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about this last weekend. Seems like most of our "algorithms" (like Translate
or Orient
) should mutate self
by default and if the user needs a new copy, they can just clone
the object beforehand. As opposed to having _inplace
variants of all our algorithms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of mutating in-place by default. It also means you can use the various algorithms as trait objects, which makes things more flexible.
This seems related to a comment I made on #119:
One way you could make this easier (using the
Translate
algorithm as an example) is to add a requiredtranslate_inplace()
and then add atranslate() -> Self where Self: Sized + Clone
which has a default impl that returns a mutated clone.
what if we combined both of our PRs and unify the coordinate iterators into a single trait like this? trait CoordsIterator<T: Float> {
fn iter_coords(&self) -> Box<Iterator<Item = &Coordinate<T>>>;
fn iter_coords_mut(&mut self) -> Box<Iterator<Item = &mut Coordinate<T>>>;
fn map_coords<NT: Float>(&self, f: &Fn(&Coordinate<T> -> Coordinate<NT>)) -> Self::Output;
fn map_coords_inplace(&mut self, f: &Fn(&Coordinate<T> -> Coordinate<T>));
} does some of this feel redundant? i'm undecided right now. even if they are a little redundant, they all do slightly different things and at this point i'm not too concerned. thoughts? |
My main nagging bit with I'm personally in favour of keeping an in-place & clone versions. I know you can replace the clone with the inplace easily, but I personally just like having an API which makes easy things easy. But it does increase the API and amount of code. You don't want to wide up like PHP with a mishmash of everything. I'm not wedded to the idea though. We should definitely have a way to do it inplace, so there's always an allocation-free option. I tried to merge |
2e493be
to
31e66e2
Compare
I've rebased this. Any thoughts? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
after looking over it again, seems good to me!
whenever you're ready, you should be able to merge by writing a github comment with r<equals>frewsxcv
as seen here: https://bors.tech/documentation/
where T: Float, NT: Float; | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lots of whitespace here
For my application at work I used the The idea is you have a basic This then lets you build nice things on top of Our Example trait definitionspub trait ForEach {
fn for_each<F>(&self, func: F)
where
F: FnMut(&Vector);
fn for_each_mut<F>(&mut self, func: F)
where
F: FnMut(&mut Vector);
}
pub trait Translate {
fn translate(&mut self, by: Vector);
fn translated(&self, by: Vector) -> Self
where
Self: Sized + Clone,
{ ... }
fn move_to(&mut self, new_centre: Vector)
where
Self: Centroid,
{ ... }
fn moved(&self, new_centre: Vector) -> Self
where
Self: Centroid + Sized + Clone,
{ ... }
}
pub trait Rotate {
fn rotate_about(&mut self, angle: f64, point: Vector);
fn rotate(&mut self, angle: f64)
where
Self: Centroid,
{ ... }
fn rotated(&self, angle: f64) -> Self
where
Self: Sized + Clone + Centroid,
{ ... }
} |
bors r=frewsxcv |
1 similar comment
bors r=frewsxcv |
170: Map coords inplace r=frewsxcv a=rory To compliment the `.map_coords()` feature, I've added a `.map_coords_inplace` which will apply a provided function to all coordinatines in place, i.e. not doing any allocations. Some of this has a lot of overlap with #164. I've used that to add a `.translate_inplace()` feature.
Build succeeded |
To compliment the
.map_coords()
feature, I've added a.map_coords_inplace
which will apply a provided function to all coordinatines in place, i.e. not doing any allocations.Some of this has a lot of overlap with #164.
I've used that to add a
.translate_inplace()
feature.