You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.5 KiB
59 lines
1.5 KiB
#include "../vectors/vector.hpp"
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
using namespace std;
|
|
class matrix {
|
|
private:
|
|
long long num_rows;
|
|
long long num_cols;
|
|
vector *rows;
|
|
|
|
public:
|
|
matrix(const long long num_rows, const long long num_cols) {
|
|
this->num_rows = num_rows;
|
|
this->num_cols = num_cols;
|
|
this->rows = (vector *)calloc(sizeof(vector), num_rows);
|
|
for (long long i = 0; i < num_rows; i++) {
|
|
this->rows[i] = vector(num_cols);
|
|
}
|
|
}
|
|
~matrix() {
|
|
this->num_rows = 0;
|
|
this->num_cols = 0;
|
|
free(this->rows);
|
|
this->rows = NULL;
|
|
}
|
|
friend ostream &operator<<(ostream &os, const matrix &m) {
|
|
char last = '\0';
|
|
for (long long i = 0; i < m.num_rows; i++) {
|
|
for (long long j = 0; j < m.num_cols; j++) {
|
|
string symbols[3];
|
|
symbols[0] = "|";
|
|
ostringstream oss;
|
|
oss << " " << m.rows[i][j] << " ";
|
|
symbols[1] = oss.str();
|
|
symbols[2] = "|";
|
|
for (int i = 0; i < 3; i++) {
|
|
int len = symbols[i].length() - 1;
|
|
char cur = symbols[i][0];
|
|
if (cur != last) {
|
|
os << symbols[i];
|
|
}
|
|
last = symbols[i][len];
|
|
}
|
|
}
|
|
if (i != m.num_rows - 1)
|
|
os << endl << "|";
|
|
}
|
|
return os;
|
|
}
|
|
const vector operator[](const long long index) const {
|
|
return this->rows[index];
|
|
}
|
|
|
|
// FIXME: Figure out how to make sure you do not try to access something
|
|
// outside of the index
|
|
vector &operator[](const long long index) { return this->rows[index]; }
|
|
};
|