Off the top of my head:
template <class element_type>
class matrix
{
public:
matrix(size_t width, size_t height): m_stride(width), m_height(height), m_storage(width*height) { }
element_type &operator()(size_t row, size_t column)
{
// column major
return m_storage[std::slice(column, m_height, m_stride)][row];
// row major
return m_storage[std::slice(row, m_stride, m_height)][column];
}
private:
std::valarray<element_type> m_storage;
size_t m_stride;
size_t m_height;
};
std::valarray
provides many interesting ways to access elements, via slices, masks, multidimentional slices, or an indirection table. See std::slice_array
, std::gslice_array
, std::mask_array
, and std::indirect_array
for more details.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…