Home | History | Annotate | Download | only in ustl-1.0
      1 // This file is part of the ustl library, an STL implementation.
      2 //
      3 // Copyright (C) 2005 by Mike Sharov <msharov (at) users.sourceforge.net>
      4 // This file is free software, distributed under the MIT License.
      5 //
      6 // umatrix.h
      7 //
      8 
      9 #ifndef UMATRIX_H_740EBFEF554E833645E0FD72419A8185
     10 #define UMATRIX_H_740EBFEF554E833645E0FD72419A8185
     11 
     12 #include "utuple.h"
     13 
     14 namespace ustl {
     15 
     16 /// \class matrix umatrix.h ustl.h
     17 /// \ingroup Sequences
     18 ///
     19 /// \brief A two-dimensional array of NX*NY elements of type T.
     20 ///
     21 template <size_t NX, size_t NY, typename T>
     22 class matrix : public tuple<NX*NY,T> {
     23 public:
     24     typedef tuple<NX,T>					row_type;
     25     typedef tuple<NY,T>					column_type;
     26     typedef tuple<NX*NY,T>				tuple_type;
     27     typedef typename tuple_type::value_type		value_type;
     28     typedef typename tuple_type::size_type		size_type;
     29     typedef typename tuple_type::pointer		pointer;
     30     typedef typename tuple_type::const_pointer		const_pointer;
     31     typedef typename tuple_type::reference		reference;
     32     typedef typename tuple_type::const_reference	const_reference;
     33     typedef typename tuple_type::iterator		iterator;
     34     typedef typename tuple_type::const_iterator		const_iterator;
     35     typedef typename tuple_type::range_t		range_t;
     36     typedef typename tuple_type::const_range_t		const_range_t;
     37     typedef typename tuple_type::reverse_iterator	reverse_iterator;
     38     typedef typename tuple_type::const_reverse_iterator	const_reverse_iterator;
     39 public:
     40     inline			matrix (void)			{ fill_n (matrix::begin(), NX*NY, T()); }
     41     inline size_type		columns (void) const		{ return (NX); }
     42     inline size_type		rows (void) const		{ return (NY); }
     43     inline const_iterator	at (size_type i) const		{ return (matrix::begin() + i * NX); }
     44     inline iterator		at (size_type i)		{ return (matrix::begin() + i * NX); }
     45     inline const_iterator	operator[] (size_type i) const	{ return (at (i)); }
     46     inline iterator		operator[] (size_type i)	{ return (at (i)); }
     47     inline row_type		row (size_type r) const		{ return (row_type (at (r))); }
     48     inline column_type		column (size_type c) const;
     49     template <typename T2>
     50     inline const matrix&	operator= (const matrix<NX,NY,T2>& src)	{ tuple_type::operator= (src); return (*this); }
     51     inline const matrix&	operator= (const matrix<NX,NY,T>& src)	{ tuple_type::operator= (src); return (*this); }
     52     inline const matrix&	operator+= (const_reference v)		{ tuple_type::operator+= (v); return (*this); }
     53     inline const matrix&	operator-= (const_reference v)		{ tuple_type::operator-= (v); return (*this); }
     54     inline const matrix&	operator*= (const_reference v)		{ tuple_type::operator*= (v); return (*this); }
     55     inline const matrix&	operator/= (const_reference v)		{ tuple_type::operator/= (v); return (*this); }
     56     inline const matrix		operator+ (const_reference v) const
     57 				    { matrix result (*this); result += v; return (result); }
     58     inline const matrix		operator- (const_reference v) const
     59 				    { matrix result (*this); result -= v; return (result); }
     60     inline const matrix		operator* (const_reference v) const
     61 				    { matrix result (*this); result *= v; return (result); }
     62     inline const matrix		operator/ (const_reference v) const
     63 				    { matrix result (*this); result /= v; return (result); }
     64 };
     65 
     66 template <size_t NX, size_t NY, typename T>
     67 inline typename matrix<NX,NY,T>::column_type matrix<NX,NY,T>::column (size_type c) const
     68 {
     69     column_type result;
     70     const_iterator src (matrix::begin() + c);
     71     iterator dest (result.begin());
     72     for (uoff_t i = 0; i < NY; ++ i, ++ dest, src += NX)
     73 	*dest = *src;
     74     return (result);
     75 }
     76 
     77 } // namespace ustl
     78 
     79 #endif
     80 
     81