2013-06-16 05:34:59 +00:00
|
|
|
// Copyright 2013 The Lmath Developers. For a full listing of the authors,
|
|
|
|
// refer to the AUTHORS file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-07-12 06:32:12 +00:00
|
|
|
#[macro_escape];
|
2013-06-16 05:34:59 +00:00
|
|
|
|
2013-07-14 03:22:45 +00:00
|
|
|
macro_rules! impl_dimensioned(
|
2013-07-12 06:32:12 +00:00
|
|
|
($Self:ident, $T:ty, $n:expr) => (
|
2013-07-14 03:22:45 +00:00
|
|
|
impl<T> Dimensioned<$T,[$T,..$n]> for $Self<T> {
|
2013-07-12 06:32:12 +00:00
|
|
|
#[inline]
|
2013-07-22 04:41:31 +00:00
|
|
|
pub fn i<'a>(&'a self, i: uint) -> &'a $T {
|
2013-07-12 06:32:12 +00:00
|
|
|
&'a self.as_slice()[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-07-22 04:41:31 +00:00
|
|
|
pub fn mut_i<'a>(&'a mut self, i: uint) -> &'a mut $T {
|
2013-07-12 06:32:12 +00:00
|
|
|
&'a mut self.as_mut_slice()[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn as_slice<'a>(&'a self) -> &'a [$T,..$n] {
|
|
|
|
use std::cast::transmute;
|
|
|
|
unsafe { transmute(self) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [$T,..$n] {
|
|
|
|
use std::cast::transmute;
|
|
|
|
unsafe { transmute(self) }
|
|
|
|
}
|
|
|
|
}
|
2013-07-25 02:52:25 +00:00
|
|
|
);
|
|
|
|
($Self:ident) => (
|
|
|
|
impl Dimensioned<$Self,[$Self,..1]> for $Self {
|
|
|
|
#[inline]
|
|
|
|
pub fn i<'a>(&'a self, i: uint) -> &'a $Self {
|
|
|
|
&'a self.as_slice()[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn mut_i<'a>(&'a mut self, i: uint) -> &'a mut $Self {
|
|
|
|
&'a mut self.as_mut_slice()[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn as_slice<'a>(&'a self) -> &'a [$Self,..1] {
|
|
|
|
use std::cast::transmute;
|
|
|
|
unsafe { transmute(self) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [$Self,..1] {
|
|
|
|
use std::cast::transmute;
|
|
|
|
unsafe { transmute(self) }
|
|
|
|
}
|
|
|
|
}
|
2013-07-12 06:32:12 +00:00
|
|
|
)
|
|
|
|
)
|
2013-06-16 05:34:59 +00:00
|
|
|
|
2013-07-12 09:10:39 +00:00
|
|
|
macro_rules! impl_to_vec(
|
|
|
|
($Self:ident, 2) => (impl_to_vec_helper!(ToVec2, $Self, Vec2, to_vec2, as_vec2));
|
|
|
|
($Self:ident, 3) => (impl_to_vec_helper!(ToVec3, $Self, Vec3, to_vec3, as_vec3));
|
|
|
|
($Self:ident, 4) => (impl_to_vec_helper!(ToVec4, $Self, Vec4, to_vec4, as_vec4));
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! impl_to_vec_helper(
|
|
|
|
($ToVec:ident, $Self:ident, $Vec:ident, $to_vec:ident, $as_vec:ident) => (
|
|
|
|
impl<T:Clone> $ToVec<T> for $Self<T> {
|
|
|
|
#[inline]
|
|
|
|
pub fn $to_vec(&self) -> $Vec<T> {
|
|
|
|
self.$as_vec().clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! impl_as_vec(
|
2013-07-25 05:40:10 +00:00
|
|
|
($Self:ident, 2) => (impl_as_vec_helper!(AsVec2, $Self, Vec2, as_vec2, as_mut_vec2, with_vec2));
|
|
|
|
($Self:ident, 3) => (impl_as_vec_helper!(AsVec3, $Self, Vec3, as_vec3, as_mut_vec3, with_vec3));
|
|
|
|
($Self:ident, 4) => (impl_as_vec_helper!(AsVec4, $Self, Vec4, as_vec4, as_mut_vec4, with_vec4));
|
2013-07-12 09:10:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! impl_as_vec_helper(
|
2013-07-25 05:40:10 +00:00
|
|
|
($AsVec:ident, $Self:ident, $Vec:ident, $as_vec:ident, $as_mut_vec:ident, $with_vec:ident) => (
|
2013-07-12 09:10:39 +00:00
|
|
|
impl<T> $AsVec<T> for $Self<T> {
|
2013-07-21 03:20:28 +00:00
|
|
|
/// Safely transmute to a vec.
|
2013-07-12 09:10:39 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn $as_vec<'a>(&'a self) -> &'a $Vec<T> {
|
|
|
|
use std::cast::transmute;
|
|
|
|
unsafe { transmute(self) }
|
|
|
|
}
|
|
|
|
|
2013-07-21 03:20:28 +00:00
|
|
|
/// Safely transmute to a mutable vec.
|
2013-07-12 09:10:39 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn $as_mut_vec<'a>(&'a mut self) -> &'a mut $Vec<T> {
|
|
|
|
use std::cast::transmute;
|
|
|
|
unsafe { transmute(self) }
|
|
|
|
}
|
2013-07-21 03:20:28 +00:00
|
|
|
|
|
|
|
/// Operate on `self` transmuted to a vec, then return the result as
|
|
|
|
/// transmuted back to the `Self` type.
|
|
|
|
#[inline]
|
2013-07-25 05:40:10 +00:00
|
|
|
pub fn $with_vec<'a>(&'a self, f: &fn(&'a $Vec<T>) -> $Vec<T>) -> $Self<T> {
|
2013-07-21 03:20:28 +00:00
|
|
|
use std::cast::transmute;
|
|
|
|
unsafe { transmute(f(self.$as_vec())) }
|
|
|
|
}
|
2013-07-12 09:10:39 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-07-14 03:22:45 +00:00
|
|
|
macro_rules! impl_swap_components(
|
2013-07-07 06:00:35 +00:00
|
|
|
($Self:ident) => (
|
2013-07-14 03:22:45 +00:00
|
|
|
impl<T:Clone> SwapComponents for $Self<T> {
|
2013-06-16 05:34:59 +00:00
|
|
|
#[inline]
|
2013-07-07 06:00:35 +00:00
|
|
|
pub fn swap(&mut self, a: uint, b: uint) {
|
2013-07-22 04:41:31 +00:00
|
|
|
let tmp = self.i(a).clone();
|
|
|
|
*self.mut_i(a) = self.i(b).clone();
|
|
|
|
*self.mut_i(b) = tmp;
|
2013-06-16 05:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-07 06:00:35 +00:00
|
|
|
)
|
|
|
|
)
|