Rehashing the Matrix

Hey guys,
I'm new here so forgive me if I'm doing this wrong.

I have this assignment where I have to create a map based hash table for a sparse matrix. The professor has provided some code and I'm supposed to fill in parts of it to make it work.

He has given us a class sparseMatrix which we need to provide a hash function for. This hash function is to be defined in a Class TupleHash which is inside sparseMatrix. I think I got that part down. What is really confusing me is what he has done with some typedefs.

For one of them I had to declare an unorderd_map that maps a struct Tuple on to the class template argument Object. I did that like so:

typedef unordered_map<Object,Tuple,TupleHash> HashTable;

The next typedef was given to me like so:

typedef typename HashTable::value_type valueType;

This is giving me a world of unintelligible error messages
This is how it starts.

In instantiation of 'struct std::__detail::__is_noexcept_hash<int, sparseMatrix<int>::TupleHash>':|
recursively required from 'struct std::__and_<std::is_default_constructible<sparseMatrix<int>::TupleHash>, std::is_copy_assignable<sparseMatrix<int>::TupleHash>, std::__detail::__is_noexcept_hash<int, sparseMatrix<int>::TupleHash> >'|

Do you guys have any idea what is going on? Any help will be greatly appreciated
Here is the source code for the class

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

Topic archived. No new replies allowed.