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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#ifndef SPARSEMATRIX_H
#define SPARSEMATRIX_H
#include <vector>
#include <unordered_map>
using namespace std;
template <typename Object>
class sparseMatrix
{
struct Tuple {
int i;
int j;
Tuple (int ii, int jj)
: i(ii), j(jj)
{}
bool operator== (const Tuple& right) const
{
return i == right.i
&& j == right.j;
}
void put(std::ostream& out) const
{
out << "(" << i << "," << j << ")";
}
};
class TupleHash {
/** add your code here*/
public:
std::size_t operator() (const Tuple& t)
{
return (std::size_t)t.hash();
}
};
/** add your code here to use unordered_map to create
a hash table mapping Tuples onto Objects */
typedef unordered_map<Object,Tuple,TupleHash> HashTable;
typedef typename HashTable::value_type valueType;
public:
class Row {
sparseMatrix<Object>* m;
int rowNum;
public:
Row (sparseMatrix<Object>* matrix, int row)
: m(matrix), rowNum(row) {}
Object& operator[] (int col) const {
/**
Add your code here to locate the [rowNum][col]
element of the matrix.
*/
}
};
class ConstRow {
const sparseMatrix<Object>* m;
int rowNum;
public:
ConstRow (const sparseMatrix<Object>* matrix, int row)
: m(matrix), rowNum(row) {}
Object operator[] (int col) const {
/**
Add your code here to locate the [rowNum][col]
element of the matrix (or return the default
value if that element does not exist.
*/
}
};
sparseMatrix( int rows, int cols, const Object& defaultv = Object() )
: nRows(rows), nCols(cols), defaultValue(defaultv)
{
}
/* not yet supported
sparseMatrix( initializer_list<vector<Object>> lst ) : array( lst.size( ) )
{
int i = 0;
for( auto & v : lst )
array[ i++ ] = std::move( v );
}
*/
sparseMatrix( const vector<vector<Object>> & v, const Object& defaultv = Object() )
: nRows(v.size()), nCols(v[0].size()), defaultValue(defaultv)
{
for (int i = 0; i < nRows; ++i)
for (int j = 0; j < nCols; ++j)
{
Tuple t(i, j);
data[t] = v[j][i];
}
}
/* not yet supported
sparseMatrix( vector<vector<Object>> && v ) : array{ std::move( v ) }
{ }
*/
const ConstRow operator[]( int row ) const
{ return ConstRow(this, row); }
Row operator[]( int row )
{ return Row(this, row); }
int numrows( ) const
{ return nRows; }
int numcols( ) const
{ return nCols; }
bool operator== (const sparseMatrix<Object>& m) const
{
if (numcols() != m.numcols() || numrows() != m.numrows())
return false;
const sparseMatrix<Object>& self = *this;
for (int j = 0; j < m.numrows(); ++j)
for (int i = 0; i < m.numcols(); ++i)
{
if (self[j][i] != m[j][i])
return false;
}
return true;
}
private:
int nRows;
int nCols;
Object defaultValue;
std::unordered_map <Tuple, Object, TupleHash> data;
};
template <typename Object>
std::ostream& operator<< (std::ostream& out, const sparseMatrix<Object>& m)
{
for (int j = 0; j < m.numrows(); ++j)
{
for (int i = 0; i < m.numcols(); ++i)
{
if (i > 0)
out << ' ';
out << m[j][i];
}
out << "\n";
}
return out;
}
#endif
|