27 #ifndef KMATRIX_IMPL_HPP
28 #define KMATRIX_IMPL_HPP
33 #include KSSTREAM_HEADER
58 std::string rowDelim =
"\n",
59 std::string startDelim = std::string(),
60 std::string endDelim = std::string(),
65 std::string ws(
" \t\n");
67 (
elemDelim_.find_first_not_of(ws) != std::string::npos);
69 (
rowDelim_.find_first_not_of(ws) != std::string::npos);
71 (
startDelim_.find_first_not_of(ws) != std::string::npos);
73 (
endDelim_.find_first_not_of(ws) != std::string::npos);
103 template<
typename T, K_UINT_32 BEG,
bool DBG>
106 if (m != 0 && n != 0) {
110 T* ptr = &Mimpl_[0] - BEG;
119 M_ = &vimpl_[0] - BEG;
134 template<
typename T, K_UINT_32 BEG,
bool DBG>
143 template<
typename T, K_UINT_32 BEG,
bool DBG>
154 template<
typename T, K_UINT_32 BEG,
bool DBG>
171 template<
typename T, K_UINT_32 BEG,
bool DBG>
173 : Mimpl_(v, v + m*n) {
179 template<
typename T, K_UINT_32 BEG,
bool DBG>
185 template<
typename T, K_UINT_32 BEG,
bool DBG>
193 template<
typename T, K_UINT_32 BEG,
bool DBG>
209 if (i >= m_ + BEG || j >= n_ + BEG)
212 oss <<
"Trying to access element (" << i <<
", " << j
213 <<
") not included in [" << BEG <<
", " << m_ + BEG - 1 <<
"]["
227 template<
typename T, K_UINT_32 BEG,
bool DBG>
242 if (i >= m_ + BEG || j >= n_ + BEG) {
244 oss <<
"Trying to access element (" << i <<
", " << j
245 <<
") not included in [" << BEG <<
", " << m_ + BEG - 1 <<
"]["
256 template<
typename T, K_UINT_32 BEG,
bool DBG>
263 template<
typename T, K_UINT_32 BEG,
bool DBG>
276 template<
typename T, K_UINT_32 BEG,
bool DBG>
279 if (m == m_ && n == n_) {
289 template<
typename T, K_UINT_32 BEG,
bool DBG>
293 const T* end = ptr + Mimpl_.size();
304 template<
typename T, K_UINT_32 BEG,
bool DBG>
319 template<
typename T, K_UINT_32 BEG,
bool DBG>
331 template<
typename T, K_UINT_32 BEG,
bool DBG>
346 template<
typename T, K_UINT_32 BEG,
bool DBG>
361 for (i = 0; i < m_-1; ++i) {
362 for (j = 0; j < n_; ++j) {
367 for (j = 0; j < n_-1; ++j) {
375 for (i = 0; i < m_-1; ++i) {
376 for (j = 0; j < n_; ++j) {
382 for (j = 0; j < n_; ++j) {
392 for (i = 0; i < m_; ++i) {
393 for (j = 0; j < n_-1; ++j) {
401 for (i = 0; i < m_; ++i) {
402 for (j = 0; j < n_; ++j) {
422 template<
typename T, K_UINT_32 BEG,
bool DBG>
424 if (m_ == 0 || n_ == 0) {
428 const T* ptr = &Mimpl_[0];
431 std::ios::fmtflags f = os.setf(std::ios::scientific, std::ios::floatfield);
432 os.setf(std::ios::showpoint);
437 for (i = 0; i < m_-1; ++i) {
438 for (j = 0; j < n_-1; ++j) {
446 for (j = 0; j < n_-1; ++j) {
462 template<
typename T, K_UINT_32 BEG,
bool DBG>
473 template<
typename T, K_UINT_32 BEG,
bool DBG>
K_UINT_32 nrow() const
Returns m_, the number of rows of the matrix.
void get(std::istream &is)
Reads a matrix from a stream.
std::string endDelim_
Ending string after last matrix element.
void swap(KMatrix &M)
Constant-time swap function between two matrices.
unsigned precision_
Number of significant digits to output.
KMatrix()
Default constructor. Creates an empty matrix.
Contains necessary informations to print a formatted KMatrix.
K_UINT_32 n_
Number of columns of matrix.
void swap(T &a, T &b)
Swaps objects a and b.
KMatrix & operator=(const T &a)
Assigns a copy of a to all elements of the matrix.
bool skipElemDelim_
Must we skip a word between elements ?
void assign(K_UINT_32 m, K_UINT_32 n, const T *v)
Copies a C-style array of instances of T in an m by n matrix.
void init(K_UINT_32 m, K_UINT_32 n)
Helper function to initialize matrix.
std::string startDelim_
Starting string before first matrix element.
std::vector< T * > vimpl_
Array of pointers to rows of Mimpl_.
K_UINT_32 m_
Number of rows of matrix.
unsigned long int K_UINT_32
Unsigned 32-bits integral type.
T & operator()(K_UINT_32 i, K_UINT_32 j)
Returns the element (i,j).
bool skipRowDelim_
Must we skip a word at the end of the row ?
std::string rowDelim_
Delimiter string at the end of each row.
bool skipEndDelim_
Must we skip a word at end of vector ?
std::string elemDelim_
Delimiter string between matrix elements.
void resize(K_UINT_32 m, K_UINT_32 n)
Resizes the matrix. Resulting matrix contents are undefined.
bool skipStartDelim_
Must we skip a word at start of vector ?
Minimalist matrix template class.
std::ostream & operator<<(std::ostream &os, const KMatrix< T, BEG, DBG > &M)
Writes a matrix to a stream.
unsigned width_
Width of output field for nice alignment.
void put(std::ostream &os) const
Writes a matrix to a stream.
K_UINT_32 ncol() const
Returns n_, the number of columns of the matrix.
std::vector< T > Mimpl_
Underlying vector implementation.
Exception class for access to out-of-bound elements.
KMatrixContextImpl * currentMatrixContext
Refers to the currently selected matrix printing context.
std::istream & operator>>(std::istream &is, KMatrix< T, BEG, DBG > &M)
Reads a matrix from a stream.
T ** M_
Pointer to the start of vimpl_.
KMatrixContextImpl(std::string elemDelim=" ", std::string rowDelim="\n", std::string startDelim=std::string(), std::string endDelim=std::string(), unsigned prec=4)
Constructor.