2013-08-26 05:08:25 +00:00
|
|
|
|
// 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.
|
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
use std::mem;
|
|
|
|
|
use std::ptr;
|
2013-08-26 05:08:25 +00:00
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// An array containing elements of type `Element`
|
2014-07-29 18:06:31 +00:00
|
|
|
|
pub trait Array1<Element: Copy>: Index<uint, Element> + IndexMut<uint, Element> {
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// Get the pointer to the first element of the array.
|
2014-07-29 18:06:31 +00:00
|
|
|
|
fn ptr<'a>(&'a self) -> &'a Element {
|
|
|
|
|
&(*self)[0]
|
|
|
|
|
}
|
2014-05-28 01:59:03 +00:00
|
|
|
|
|
|
|
|
|
/// Get a mutable pointer to the first element of the array.
|
2014-07-29 18:06:31 +00:00
|
|
|
|
fn mut_ptr<'a>(&'a mut self) -> &'a mut Element {
|
|
|
|
|
&mut (*self)[0]
|
|
|
|
|
}
|
2013-09-02 04:01:16 +00:00
|
|
|
|
|
2014-08-11 07:02:25 +00:00
|
|
|
|
#[deprecated = "Use `Array1::swap_elems` instead"]
|
2014-05-28 01:59:03 +00:00
|
|
|
|
#[inline]
|
2014-07-29 18:06:31 +00:00
|
|
|
|
/// Swap the elements at indices `i` and `j` in-place.
|
|
|
|
|
fn swap_i(&mut self, i: uint, j: uint) {
|
2014-08-11 07:02:25 +00:00
|
|
|
|
self.swap_i(i, j)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Swap the elements at indices `i` and `j` in-place.
|
|
|
|
|
#[inline]
|
|
|
|
|
fn swap_elems(&mut self, i: uint, j: uint) {
|
2014-05-28 01:59:03 +00:00
|
|
|
|
// Yeah, ok borrow checker – I know what I'm doing here
|
2014-07-29 18:06:31 +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-05-28 01:59:03 +00:00
|
|
|
|
/// Replace an element in the array.
|
2014-08-11 07:02:25 +00:00
|
|
|
|
#[deprecated = "Use `Array1::replace_elem` instead"]
|
2014-05-28 01:59:03 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
fn replace_i(&mut self, i: uint, src: Element) -> Element {
|
2014-08-11 07:02:25 +00:00
|
|
|
|
self.replace_i(i, src)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Replace an element in the array.
|
|
|
|
|
#[inline]
|
|
|
|
|
fn replace_elem(&mut self, i: uint, src: Element) -> Element {
|
2014-07-29 18:06:31 +00:00
|
|
|
|
mem::replace(&mut (*self)[i], src)
|
2014-05-28 01:59:03 +00:00
|
|
|
|
}
|
2014-07-03 04:34:26 +00:00
|
|
|
|
|
|
|
|
|
/// Apply a function to each element.
|
|
|
|
|
fn map(&mut self, op: |Element| -> Element) -> Self;
|
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
|
2014-07-29 18:06:31 +00:00
|
|
|
|
pub trait Array2<Column: Array1<Element>, Row: Array1<Element>, Element: Copy>:
|
|
|
|
|
Index<uint, Column> + IndexMut<uint, Column> {
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// Get the pointer to the first element of the array.
|
2014-07-29 18:06:31 +00:00
|
|
|
|
fn ptr<'a>(&'a self) -> &'a Element {
|
|
|
|
|
&(*self)[0][0]
|
|
|
|
|
}
|
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.
|
2014-07-29 18:06:31 +00:00
|
|
|
|
fn mut_ptr<'a>(&'a mut self) -> &'a mut Element {
|
|
|
|
|
&mut (*self)[0][0]
|
|
|
|
|
}
|
2014-05-25 01:22:23 +00:00
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// Swap two columns of this array.
|
2014-08-11 07:02:25 +00:00
|
|
|
|
#[deprecated = "Use `Array2::swap_cols` instead"]
|
2013-08-26 05:08:25 +00:00
|
|
|
|
#[inline]
|
2014-05-28 01:59:03 +00:00
|
|
|
|
fn swap_c(&mut self, a: uint, b: uint) {
|
2014-08-11 07:02:25 +00:00
|
|
|
|
self.swap_cols(a, b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Swap two columns of this array.
|
|
|
|
|
#[inline]
|
|
|
|
|
fn swap_cols(&mut self, a: uint, b: uint) {
|
2014-07-29 18:06:31 +00:00
|
|
|
|
unsafe { ptr::swap(&mut (*self)[a], &mut (*self)[b]) };
|
2013-09-03 03:54:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// Replace a column in the array.
|
2014-08-11 07:02:25 +00:00
|
|
|
|
#[deprecated = "Use `Array2::replace_col` instead"]
|
2014-05-28 01:59:03 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
fn replace_c(&mut self, c: uint, src: Column) -> Column {
|
2014-08-11 07:02:25 +00:00
|
|
|
|
self.replace_col(c, src)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Replace a column in the array.
|
|
|
|
|
#[inline]
|
|
|
|
|
fn replace_col(&mut self, c: uint, src: Column) -> Column {
|
2014-07-29 18:06:31 +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-05-28 01:59:03 +00:00
|
|
|
|
/// Get a row from this array by-value.
|
2014-08-11 07:02:25 +00:00
|
|
|
|
#[deprecated = "Use `Array2::row` instead"]
|
|
|
|
|
#[inline]
|
|
|
|
|
fn r(&self, r: uint) -> Row {
|
|
|
|
|
self.row(r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a row from this array by-value.
|
|
|
|
|
fn row(&self, r: uint) -> Row;
|
|
|
|
|
|
|
|
|
|
#[deprecated = "Use `Array2::swap_rows` instead"]
|
|
|
|
|
#[inline]
|
|
|
|
|
fn swap_r(&mut self, a: uint, b: uint) {
|
|
|
|
|
self.swap_rows(a, b)
|
|
|
|
|
}
|
2013-09-14 01:58:19 +00:00
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// Swap two rows of this array.
|
2014-08-11 07:02:25 +00:00
|
|
|
|
fn swap_rows(&mut self, a: uint, b: uint);
|
2013-09-07 09:51:01 +00:00
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
|
/// Swap the values at index `a` and `b`
|
2014-08-11 07:02:25 +00:00
|
|
|
|
#[deprecated = "Use `Array2::swap_elems` instead"]
|
2014-05-28 01:59:03 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
fn swap_cr(&mut self, a: (uint, uint), b: (uint, uint)) {
|
2014-08-11 07:02:25 +00:00
|
|
|
|
self.swap_elems(a, b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Swap the values at index `a` and `b`
|
|
|
|
|
#[inline]
|
|
|
|
|
fn swap_elems(&mut self, a: (uint, uint), b: (uint, uint)) {
|
2014-05-28 01:59:03 +00:00
|
|
|
|
let (ac, ar) = a;
|
|
|
|
|
let (bc, br) = b;
|
2014-07-29 18:06:31 +00:00
|
|
|
|
unsafe { ptr::swap(&mut (*self)[ac][ar], &mut (*self)[bc][br]) };
|
2014-05-28 01:59:03 +00:00
|
|
|
|
}
|
2014-07-03 04:34:26 +00:00
|
|
|
|
|
|
|
|
|
/// Apply a function to each column.
|
2014-07-05 00:37:24 +00:00
|
|
|
|
fn map(&mut self, op: |&Column| -> Column) -> Self;
|
2014-05-28 01:59:03 +00:00
|
|
|
|
}
|