cgmath/src/array.rs

104 lines
3.4 KiB
Rust
Raw Normal View History

// Copyright 2013 The OMath 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.
use std::mem;
use std::ptr;
2015-01-03 21:29:26 +00:00
use std::ops::*;
/// An array containing elements of type `Element`
2015-01-05 01:56:01 +00:00
pub trait Array1<Element: Copy>: Index<uint, Output=Element> + IndexMut<uint, Output=Element> {
/// Get the pointer to the first element of the array.
fn ptr<'a>(&'a self) -> &'a Element {
&(*self)[0]
}
/// Get a mutable pointer to the first element of the array.
fn mut_ptr<'a>(&'a mut self) -> &'a mut Element {
&mut (*self)[0]
}
2013-09-02 04:01:16 +00:00
/// Swap the elements at indices `i` and `j` in-place.
#[inline]
fn swap_elems(&mut self, i: uint, j: uint) {
// Yeah, ok borrow checker I know what I'm doing here
unsafe { ptr::swap(&mut (*self)[i], &mut (*self)[j]) };
}
2014-05-25 01:22:23 +00:00
/// Replace an element in the array.
#[inline]
fn replace_elem(&mut self, i: uint, src: Element) -> Element {
mem::replace(&mut (*self)[i], src)
}
/// Apply a function to each element.
2015-01-07 22:34:42 +00:00
fn map<F>(&mut self, op: F) -> Self
where F: FnMut(Element) -> Element;
}
2014-05-25 01:22:23 +00:00
/// A column-major array
2014-08-30 00:00:34 +00:00
pub trait Array2<Column: Array1<Element>+'static, Row: Array1<Element>, Element: Copy>:
2015-01-05 01:56:01 +00:00
Index<uint, Output=Column> + IndexMut<uint, Output=Column> {
/// Get the pointer to the first element of the array.
fn ptr<'a>(&'a self) -> &'a Element {
&(*self)[0][0]
}
2014-05-25 01:22:23 +00:00
/// Get a mutable pointer to the first element of the array.
fn mut_ptr<'a>(&'a mut self) -> &'a mut Element {
&mut (*self)[0][0]
}
2014-05-25 01:22:23 +00:00
/// Swap two columns of this array.
#[inline]
fn swap_cols(&mut self, a: uint, b: uint) {
unsafe { ptr::swap(&mut (*self)[a], &mut (*self)[b]) };
}
/// Replace a column in the array.
#[inline]
fn replace_col(&mut self, c: uint, src: Column) -> Column {
mem::replace(&mut (*self)[c], src)
}
/// Get a row from this array by-value.
fn row(&self, r: uint) -> Row;
/// Swap two rows of this array.
fn swap_rows(&mut self, a: uint, b: uint);
/// Swap the values at index `a` and `b`
#[inline]
fn swap_elems(&mut self, a: (uint, uint), b: (uint, uint)) {
let (ac, ar) = a;
let (bc, br) = b;
unsafe { ptr::swap(&mut (*self)[ac][ar], &mut (*self)[bc][br]) };
}
/// Apply a function to each column.
2015-01-07 22:34:42 +00:00
fn map<F>(&mut self, op: F) -> Self
where F: FnMut(&Column) -> Column;
}
/// Homogeneous arrays of elements that can be converted to and from `[T, ..N]`
/// arrays.
pub trait FixedArray<V> {
fn into_fixed(self) -> V;
fn as_fixed<'a>(&'a self) -> &'a V;
fn as_mut_fixed<'a>(&'a mut self) -> &'a mut V;
fn from_fixed(v: V) -> Self;
fn from_fixed_ref<'a>(v: &'a V) -> &'a Self;
fn from_fixed_mut<'a>(v: &'a mut V) -> &'a mut Self;
}