#ifndef _SPARSE_ARRAY_H_ #define _SPARSE_ARRAY_H_ #include typedef double* SparseArray; /* placeholder: you'll want to change this */ /* ------- Creation/Deletion -------- */ SparseArray NewSparseArray(int m,int n,double URV); /* NewSparseArray() creates an empty mxn sparse array of doubles with the unrepresented value as specified */ void DeleteSparseArray(SparseArray SA); /* DeleteSparseArray() deletes the specified array, freeing all memory */ /* ------- Storage -------- */ double Read(SparseArray SA,int row,int col); /* Read() returns the value at (row,col); if it is not represented, it returns the unrepresented value */ void Store(SparseArray SA,int row,int col,double val); /* Store() stores a new represented value at (row,col) */ double* Access(SparseArray,int row,int col); /* Access() returns a pointer to the value at (row,col) if it is represented; otherwise, it returns NULL */ void SetURV(SparseArray SA,double URV); /* SetURV() sets the unrepresented value to be a new value */ double GetURV(SparseArray SA); /* GetURV() returns the sparse array's unrepresented value */ /* -------- I/O -------- */ void WriteDense(SparseArray SA,FILE* outfile); /* WriteDense() should print out the sparse array to the specified file 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). */ SparseArray ReadDense(FILE* infile,int m,int n,double URV); /* ReadDense() should create a new mxn sparse array with the specified unrepresented value whose values are read from the specified file in a dense format (thus, any values read that are equal to URV should not be stored). ReadDense() should be able to read sparse arrays written by WriteDense(). */ void WriteSparse(SparseArray SA,FILE* 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 file format requires O(r) disk space. */ SparseArray ReadSparse(FILE* infile); /* ReadSparse() should create a new sparse array by reading in the file format specified by WriteDense(). Note that the normal creation parameters m, n, and URV are not needed as they are stored in the file format itself. */ /* -------- Iteration -------- */ int SetCursorRowStart(SparseArray SA,int row); int SetCursorRowEnd(SparseArray SA,int row); int SetCursorColStart(SparseArray SA,int col); int SetCursorColEnd(SparseArray SA,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(SparseArray SA); int NextInCol(SparseArray SA); int PrevInRow(SparseArray SA); int PrevInCol(SparseArray SA); /* 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(SparseArray SA,int* row,int* col); /* GetCursorPosition() returns the current position of the cursor */ void SetCursorPosition(SparseArray SA,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(SparseArray SA); /* returns the array's density */ int NumRepresented(SparseArray SA); /* the # of represented elements */ #endif