Skip to content

Commit

Permalink
Generic mapping function for vectors (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceeri authored Aug 11, 2024
1 parent a4ee21a commit fe3365b
Show file tree
Hide file tree
Showing 37 changed files with 362 additions and 0 deletions.
14 changes: 14 additions & 0 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,20 @@ impl {{ self_t }} {
{% endif %}
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn({{ scalar_t }}) -> {{ scalar_t }},
{
Self::new(
{% for c in components %}
f(self.{{ c }}),
{%- endfor %}
)
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/coresimd/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ impl Vec3A {
Self(Simd::from_array([v; 4]))
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/coresimd/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ impl Vec4 {
Self(Simd::from_array([v; 4]))
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/neon/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ impl Vec3A {
unsafe { UnionCast { a: [v; 4] }.v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/neon/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ impl Vec4 {
unsafe { UnionCast { a: [v; 4] }.v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/scalar/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ impl Vec3A {
Self { x: v, y: v, z: v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/scalar/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ impl Vec4 {
}
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/sse2/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ impl Vec3A {
unsafe { UnionCast { a: [v; 4] }.v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/sse2/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ impl Vec4 {
unsafe { UnionCast { a: [v; 4] }.v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ impl Vec2 {
Self { x: v, y: v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ impl Vec3 {
Self { x: v, y: v, z: v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/wasm32/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ impl Vec3A {
Self(f32x4(v, v, v, v))
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f32/wasm32/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ impl Vec4 {
Self(f32x4(v, v, v, v))
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f32) -> f32,
{
Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f64/dvec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ impl DVec2 {
Self { x: v, y: v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f64) -> f64,
{
Self::new(f(self.x), f(self.y))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f64/dvec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ impl DVec3 {
Self { x: v, y: v, z: v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f64) -> f64,
{
Self::new(f(self.x), f(self.y), f(self.z))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/f64/dvec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ impl DVec4 {
}
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(f64) -> f64,
{
Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/i16/i16vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ impl I16Vec2 {
Self { x: v, y: v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(i16) -> i16,
{
Self::new(f(self.x), f(self.y))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/i16/i16vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ impl I16Vec3 {
Self { x: v, y: v, z: v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(i16) -> i16,
{
Self::new(f(self.x), f(self.y), f(self.z))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/i16/i16vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ impl I16Vec4 {
}
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(i16) -> i16,
{
Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/i32/ivec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ impl IVec2 {
Self { x: v, y: v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(i32) -> i32,
{
Self::new(f(self.x), f(self.y))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/i32/ivec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ impl IVec3 {
Self { x: v, y: v, z: v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(i32) -> i32,
{
Self::new(f(self.x), f(self.y), f(self.z))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/i32/ivec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ impl IVec4 {
}
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(i32) -> i32,
{
Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
10 changes: 10 additions & 0 deletions src/i64/i64vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ impl I64Vec2 {
Self { x: v, y: v }
}

/// Returns a vector containing each element of `self` modified by a mapping function `f`.
#[inline]
#[must_use]
pub fn map<F>(self, f: F) -> Self
where
F: Fn(i64) -> i64,
{
Self::new(f(self.x), f(self.y))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
/// for each element of `self`.
///
Expand Down
Loading

0 comments on commit fe3365b

Please sign in to comment.