File: matrix.hh

package info (click to toggle)
augustus 3.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 289,676 kB
  • sloc: cpp: 48,711; perl: 13,339; ansic: 1,251; makefile: 859; sh: 58
file content (157 lines) | stat: -rw-r--r-- 3,517 bytes parent folder | download | duplicates (2)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*****************************************************************************\
 * Filename : matrix.hh
 * Author   : Emmanouil Stafilarakis
 * Project  : HMM
 * Version  : 0.1
 *
 * Copyright: ©Stafilarakis
 *
 * Description: Base class for all mathematical matrix objects,
 *
 *
 * Date       |   Author              |  Changes
 *------------|-----------------------|----------------------------------------
 * 20.09.2001 | Stafilarakis Emm.     | Creation of the file
 * 19.08.2002 | Mario Stanke          | fixed bug in assignment op and copy 
 *            |                       | constructor: they weren't called at all
 * 01.09.2007 | Oliver Keller         | Complete rewrite; now use vector internally
 * 22.07.2008 | Oliver Keller         | Another rewrite; now we use an array that 
 *            |                       | is reserved on creation
\******************************************************************************/

#ifndef _MATRIX_HH
#define _MATRIX_HH

// standard C/C++ includes
#include <vector>
#include <ostream>  // for operator<<


using namespace std;

/**
 * @memo    A simple matrix class.
 *
 * @doc     This class provides the functionality of a matrix. It is designed
 *          to easily allocate space without the need to free this explicitily.
 *
 * @author  Emmanouil Stafilarakis
 * @version 0.1
 */
template <class T>
class Matrix {
public:
    /// The type of the stored values
    typedef T         value_t;
public:
    /**
     * Constructor
     */
    Matrix(int n = 0, int m = 0) {
	init(n,m);
    }
    /**
     * Destructor
     */
    ~Matrix( ){ 
	delete[] data;
    }
   /**
     * this is a row: just a pointer
     */
    value_t* operator[] ( int i ) {
	return data + i*width;
    }
    /**
     *
     */
    const value_t* operator[] ( int i ) const {
	return data + i*width;
    }
    /**
     *
     */
    value_t& operator() ( int i, int j ) {
	return (*this)[i][j];
    }
    /**
     *
     */
    const value_t& operator() ( int i, int j ) const {
	return (*this)[i][j];
    }
    /**
     * this clones the row
     */
    vector<T> getRow(int i) const {
	return vector<T>(data + i*width, data + (i+1)*width);
    }
    /**
     *
     */
    vector<T> getColumn(int j) const {
	vector<T> result;
	result.reserve(height);
	for (value_t* p = data + j; p < data + size; p+=width)
	    result.push_back(*p);
	return result;
    }
    /**
     *
     */
    int getColSize( ) const {
	return height;
    }
    /**
     *
     */
    int getRowSize( ) const {
	return width;
    }
    /**
     *
     */
    void operator=(const Matrix<value_t>& mat) {
	resize(mat.height, mat.width);
	for (int i=0; i<size; i++) 
	    data[i] = mat.data[i];
    }
    void assign(int n, int m, value_t t = value_t()) {
	resize(n,m);
	for (int i=0; i<size; i++) 
	    data[i] = t;
    }
    friend ostream& operator<<(ostream& out, const Matrix<value_t>& mat) {
	for (int i = 0; i < mat.size; i++) {
	    out << mat.data[i];
	    if (i+1 % mat.width)
		out << "\t";
	    else
		out << endl;
	}
	return out;
    }

private:
    void resize(int n = 0, int m = 0) {
	if (n == height && m == width) 
	    return;
	delete[] data;
	init(n,m);
    }
    void init(int n = 0, int m = 0) {
	if (n>0 && m>0) {
	    height=n; width=m; size=n*m;
	    data  = new value_t[size];
	} else {
	    size=height=width=0; data=0;
	}
    }
    int     height;  // n
    int     width;  // m
    int     size;    // n*m;
    value_t   *data;  // the nxm matrix
};

#endif   //  _MATRIX_HH