2013-08-26 05:08:25 +00:00
|
|
|
|
// Copyright 2013 The OMath Developers. For a full listing of the authors,
|
2015-03-14 02:49:46 +00:00
|
|
|
|
// refer to the Cargo.toml file at the top-level directory of this distribution.
|
2013-08-26 05:08:25 +00:00
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
use std::mem;
|
|
|
|
|
use std::ptr;
|
2015-01-03 21:29:26 +00:00
|
|
|
|
use std::ops::*;
|
2013-08-26 05:08:25 +00:00
|
|
|
|
|
2015-11-14 01:03:12 +00:00
|
|
|
|
use num::PartialOrd;
|
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// An array containing elements of type `Element`
|
2015-11-03 03:15:11 +00:00
|
|
|
|
pub trait Array1 where
|
2015-11-03 04:40:52 +00:00
|
|
|
|
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
|
2015-11-03 03:15:11 +00:00
|
|
|
|
Self: Index<usize, Output = <Self as Array1>::Element>,
|
|
|
|
|
Self: IndexMut<usize, Output = <Self as Array1>::Element>,
|
|
|
|
|
{
|
|
|
|
|
type Element: Copy;
|
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// Get the pointer to the first element of the array.
|
2015-11-03 03:15:11 +00:00
|
|
|
|
fn ptr<'a>(&'a self) -> &'a Self::Element {
|
2015-10-04 01:58:53 +00:00
|
|
|
|
&self[0]
|
2014-07-29 18:06:31 +00:00
|
|
|
|
}
|
2014-05-28 01:59:03 +00:00
|
|
|
|
|
|
|
|
|
/// Get a mutable pointer to the first element of the array.
|
2015-11-03 03:15:11 +00:00
|
|
|
|
fn mut_ptr<'a>(&'a mut self) -> &'a mut Self::Element {
|
2015-10-04 01:58:53 +00:00
|
|
|
|
&mut self[0]
|
2014-07-29 18:06:31 +00:00
|
|
|
|
}
|
2013-09-02 04:01:16 +00:00
|
|
|
|
|
2014-08-11 07:02:25 +00:00
|
|
|
|
/// Swap the elements at indices `i` and `j` in-place.
|
|
|
|
|
#[inline]
|
2015-01-09 23:16:39 +00:00
|
|
|
|
fn swap_elems(&mut self, i: usize, j: usize) {
|
2014-05-28 01:59:03 +00:00
|
|
|
|
// Yeah, ok borrow checker – I know what I'm doing here
|
2015-10-04 01:58:53 +00:00
|
|
|
|
unsafe { ptr::swap(&mut self[i], &mut self[j]) };
|
2014-05-28 01:59:03 +00:00
|
|
|
|
}
|
2014-05-25 01:22:23 +00:00
|
|
|
|
|
2014-08-11 07:02:25 +00:00
|
|
|
|
/// Replace an element in the array.
|
|
|
|
|
#[inline]
|
2015-11-03 03:15:11 +00:00
|
|
|
|
fn replace_elem(&mut self, i: usize, src: Self::Element) -> Self::Element {
|
2015-10-04 01:58:53 +00:00
|
|
|
|
mem::replace(&mut self[i], src)
|
2014-05-28 01:59:03 +00:00
|
|
|
|
}
|
2015-11-14 01:03:12 +00:00
|
|
|
|
|
|
|
|
|
/// The sum of the elements of the array.
|
|
|
|
|
fn sum(self) -> Self::Element where Self::Element: Add<Output = <Self as Array1>::Element>;
|
|
|
|
|
|
|
|
|
|
/// The product of the elements of the array.
|
|
|
|
|
fn product(self) -> Self::Element where Self::Element: Mul<Output = <Self as Array1>::Element>;
|
|
|
|
|
|
|
|
|
|
/// The minimum element of the array.
|
|
|
|
|
fn min(self) -> Self::Element where Self::Element: PartialOrd;
|
|
|
|
|
|
|
|
|
|
/// The maximum element of the array.
|
|
|
|
|
fn max(self) -> Self::Element where Self::Element: PartialOrd;
|
2014-05-28 01:59:03 +00:00
|
|
|
|
}
|
2014-05-25 01:22:23 +00:00
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// A column-major array
|
2015-11-03 03:15:11 +00:00
|
|
|
|
pub trait Array2 where
|
2015-11-03 04:40:52 +00:00
|
|
|
|
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
|
2015-11-03 03:15:11 +00:00
|
|
|
|
Self: Index<usize, Output = <Self as Array2>::Column>,
|
|
|
|
|
Self: IndexMut<usize, Output = <Self as Array2>::Column>,
|
|
|
|
|
{
|
|
|
|
|
type Element: Copy;
|
|
|
|
|
type Column: Array1<Element = Self::Element>;
|
|
|
|
|
type Row: Array1<Element = Self::Element>;
|
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// Get the pointer to the first element of the array.
|
2015-11-03 03:15:11 +00:00
|
|
|
|
fn ptr<'a>(&'a self) -> &'a Self::Element {
|
2015-10-04 01:58:53 +00:00
|
|
|
|
&self[0][0]
|
2014-07-29 18:06:31 +00:00
|
|
|
|
}
|
2014-05-25 01:22:23 +00:00
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// Get a mutable pointer to the first element of the array.
|
2015-11-03 03:15:11 +00:00
|
|
|
|
fn mut_ptr<'a>(&'a mut self) -> &'a mut Self::Element {
|
2015-10-04 01:58:53 +00:00
|
|
|
|
&mut self[0][0]
|
2014-07-29 18:06:31 +00:00
|
|
|
|
}
|
2014-05-25 01:22:23 +00:00
|
|
|
|
|
2014-08-11 07:02:25 +00:00
|
|
|
|
/// Swap two columns of this array.
|
|
|
|
|
#[inline]
|
2015-01-09 23:16:39 +00:00
|
|
|
|
fn swap_cols(&mut self, a: usize, b: usize) {
|
2015-10-04 01:58:53 +00:00
|
|
|
|
unsafe { ptr::swap(&mut self[a], &mut self[b]) };
|
2013-09-03 03:54:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-11 07:02:25 +00:00
|
|
|
|
/// Replace a column in the array.
|
|
|
|
|
#[inline]
|
2015-11-03 03:15:11 +00:00
|
|
|
|
fn replace_col(&mut self, c: usize, src: Self::Column) -> Self::Column {
|
2015-10-04 01:58:53 +00:00
|
|
|
|
mem::replace(&mut self[c], src)
|
2014-05-28 01:59:03 +00:00
|
|
|
|
}
|
2013-08-26 05:08:25 +00:00
|
|
|
|
|
2014-08-11 07:02:25 +00:00
|
|
|
|
/// Get a row from this array by-value.
|
2015-11-03 03:15:11 +00:00
|
|
|
|
fn row(&self, r: usize) -> Self::Row;
|
2014-08-11 07:02:25 +00:00
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// Swap two rows of this array.
|
2015-01-09 23:16:39 +00:00
|
|
|
|
fn swap_rows(&mut self, a: usize, b: usize);
|
2013-09-07 09:51:01 +00:00
|
|
|
|
|
2014-08-11 07:02:25 +00:00
|
|
|
|
/// Swap the values at index `a` and `b`
|
|
|
|
|
#[inline]
|
2015-01-09 23:16:39 +00:00
|
|
|
|
fn swap_elems(&mut self, a: (usize, usize), b: (usize, usize)) {
|
2014-05-28 01:59:03 +00:00
|
|
|
|
let (ac, ar) = a;
|
|
|
|
|
let (bc, br) = b;
|
2015-10-04 01:58:53 +00:00
|
|
|
|
unsafe { ptr::swap(&mut self[ac][ar], &mut self[bc][br]) };
|
2014-05-28 01:59:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|