1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 struct Mat<T> { data: Vec<T>, cols: uint, }
14 fn new(data: Vec<T>, cols: uint) -> Mat<T> {
15 Mat { data: data, cols: cols }
17 fn row<'a>(&'a self, row: uint) -> Row<&'a Mat<T>> {
18 Row { mat: self, row: row, }
22 impl<T> Index<(uint, uint), T> for Mat<T> {
23 fn index<'a>(&'a self, &(row, col): &(uint, uint)) -> &'a T {
24 &self.data[row * self.cols + col]
28 impl<'a, T> Index<(uint, uint), T> for &'a Mat<T> {
29 fn index<'b>(&'b self, index: &(uint, uint)) -> &'b T {
34 struct Row<M> { mat: M, row: uint, }
36 impl<T, M: Index<(uint, uint), T>> Index<uint, T> for Row<M> {
37 fn index<'a>(&'a self, col: &uint) -> &'a T {
38 &self.mat[(self.row, *col)]
43 let m = Mat::new(vec!(1u, 2, 3, 4, 5, 6), 3);
46 assert!(r.index(&2) == &6);