1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::ops::{Index,IndexMut};
#[derive(Clone)]
pub struct Vec2d<T> {
	pub items: Vec<T>,
	x_len: usize,
	y_len: usize,
}
impl <T>Vec2d<T> {
	pub fn new(x_len:usize,y_len:usize) -> Self {
		Vec2d {
			items : Vec::with_capacity(x_len * y_len),
			x_len: x_len,
			y_len: y_len,
		}
	}
	fn calculate_index(&self,index:(usize,usize)) -> Option<usize> {
		let (y_index,x_index) = index;
		let result;
		if (x_index < self.x_len) && (y_index < self.y_len) {
			result = Some(y_index * self.x_len + x_index);				
		} else {
			result = None;
		}
		return result;
	}
}
impl<T> Index<(usize,usize)> for Vec2d<T> {
    type Output = T;
    #[inline]
    fn index(&self, index: (usize,usize)) -> &T {
    	let index = self.calculate_index(index);
    	match index {
    		Some(index) => {
		        return self.items.index(index);    			
    		},
    		None => {
    			panic!("Error while indexing Vec2d. Index out of bound.");
    		}
    	}
    }
}

impl<T> IndexMut<(usize,usize)> for Vec2d<T> {
    #[inline]
    fn index_mut(&mut self, index: (usize,usize)) -> &mut T {        
        let index = self.calculate_index(index);
    	match index {
    		Some(index) => {
		        return self.items.index_mut(index);    			
    		},
    		None => {
    			panic!("Error while indexing Vec2d. Index out of bound.");
    		}
    	}
    }
}

#[test]
fn it_works() {
}