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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::fmt;

use traits::{MatrixRawGet, MatrixRawSet, MatrixShape, MatrixVStack, SameShape};
use matrix::write_mat;

impl<T: MatrixShape,
     B: MatrixShape>
MatrixVStack<B> for
T
{
	unsafe fn unsafe_vstack(self, bot: B) -> VStack<T, B>
	{
		VStack::unsafe_new(self, bot)
	}

	fn vstack(self, bot: B) -> VStack<T, B>
	{
		VStack::new(self, bot)
	}
}

#[derive(Copy)]
pub struct VStack<T, B>
{
	top: T,
	bot: B,
}

impl<T: MatrixShape,
     B: MatrixShape>
VStack<T, B>
{
	unsafe fn unsafe_new(top: T, bot: B) -> VStack<T, B>
	{
		VStack{ top: top, bot: bot }
	}

	fn new(top: T, bot: B) -> VStack<T, B>
	{
		assert_eq!(top.ncol(), bot.ncol());
		VStack{ top: top, bot: bot }
	}
}

impl<T: MatrixRawGet + MatrixShape,
     B: MatrixRawGet>
MatrixRawGet for
VStack<T, B>
{
	unsafe fn raw_get(&self, r: usize, c: usize) -> f64
	{
		if r < self.top.nrow()
		{
			self.top.raw_get(r, c)
		}
		else
		{
			self.bot.raw_get(r - self.top.nrow(), c)
		}
	}
}

impl<T: MatrixRawSet + MatrixShape,
     B: MatrixRawSet>
MatrixRawSet for
VStack<T, B>
{
	unsafe fn raw_set(&self, r: usize, c: usize, val: f64)
	{
		if r < self.top.nrow()
		{
			self.top.raw_set(r, c, val)
		}
		else
		{
			self.bot.raw_set(r - self.top.nrow(), c, val)
		}
	}
}

impl<T: MatrixShape,
     B: MatrixShape>
MatrixShape for
VStack<T, B>
{
	fn nrow(&self) -> usize
	{
		self.top.nrow() + self.bot.nrow()
	}

	fn ncol(&self) -> usize
	{
		self.top.ncol()
	}
}

impl<T: MatrixShape,
     B: MatrixShape>
SameShape for
VStack<T, B>
{
	fn same_shape(&self, nrow: usize, ncol: usize) -> bool
	{
		self.nrow() == nrow && self.ncol() == ncol
	}
}

impl<T: Clone,
     B: Clone>
Clone for
VStack<T, B>
{
	fn clone(&self) -> VStack<T, B>
	{
		VStack{ top: self.top.clone(), bot: self.bot.clone() }
	}
}

impl<T: MatrixRawGet + MatrixShape,
     B: MatrixRawGet + MatrixShape>
fmt::Display for
VStack<T, B>
{
	fn fmt(&self, buf: &mut fmt::Formatter) -> fmt::Result
	{
		write_mat(buf, self)
	}
}