#ifndef _SPARSE_ARRAY_H_ #define _SPARSE_ARRAY_H_ #include #include class SparseArray { public: /* ------- Creation/Deletion -------- */ SparseArray(int m,int n,double URV); /* SparseArray() creates an empty mxn sparse array of doubles with an unrepresented value as specified */ SparseArray(ifstream& infile,int m,int n,double URV); /* This constructor is a "ReadDense()" I/O routine that should create a new mxn sparse array with the specified unrepresented value whose values are read from the specified stream in a dense format (thus, any values read that are equal to URV should not be stored). This constructor should be able to read arrays written by WriteDense(). */ SparseArray(ifstream& infile); /* This constructor is a "ReadSparse()" I/O routine that should create a new sparse array by reading in the format specified by WriteDense(). Note that the normal constructor parameters m, n, and URV are not needed as they are stored in the file format itself. */ ~SparseArray(); /* ~SparseArray() deletes the array, freeing all memory */ /* ------- Storage -------- */ double Read(int row,int col); /* Read() returns the value at (row,col); if it is not represented, it returns the unrepresented value */ void Store(int row,int col,double val); /* Store() stores a new represented value at (row,col) */ double* Access(int row,int col); /* Access() returns a pointer to the value at (row,col) if it is represented; otherwise, it returns NULL */ void SetURV(double URV); /* SetURV() sets the unrepresented value to be a new value */ double GetURV(); /* GetURV() returns the sparse array's unrepresented value */ /* -------- I/O -------- */ void WriteDense(ofstream& outfile); /* WriteDense() should print out the sparse array to the specified output file stream as though it was an mxn dense array (for example, like the 6x6 arrays in the assignment definition). This file format requires O(n^2 disk space). */ void WriteSparse(ofstream& outfile); /* WriteDense() should print out the sparse array in a compact format such that only the defining information is printed: first m and n will be printed out, then the unrepresented value, then the represented values in the format: "(row,col) val" where row and col are the row and column of the values and "val" is the value stored at that position. This format requires O(r) disk space. */ /* -------- Iteration -------- */ int SetCursorRowStart(int row); int SetCursorRowEnd(int row); int SetCursorColStart(int col); int SetCursorColEnd(int col); /* These four calls set the cursor to be at the first/last represented value of a row/column. They return the column/row of this value, or -1 if the row/col contains no represented values. */ int NextInRow(); int PrevInRow(); int NextInCol(); int PrevInCol(); /* These four calls move the cursor along a row/column from its current position to the next represented value. They return the column/row of the value if it exists or -1 if it doesn't. */ void GetCursorPosition(int& row,int& col); /* GetCursorPosition() returns the current position of the cursor */ void SetCursorPosition(int row,int col); /* SetCursorPosition() sets the cursor to the specified position if it is represented. If it is not represented, the cursor should be left unchanged. */ /* -------- Misc -------- */ double Density(); /* returns the array's density */ int NumRepresented(); /* the # of represented elements */ private: }; #endif