compressed storage values store a very small number of valid data. Using {Row,col,value} ternary group to store each valid data, ternary group according to the position in the original matrix, in order to store the priority of the row in succession. #define _CRT_SECURE_NO_WARNINGS 1#include <vector> #include <iostream>using namespace std;//definition of ternary group Template<class t>struct triple{t _value;size_t _row;size_ t _col; Triple (Const t& value = t (), size_t row = 0, size_t col = 0): _value (value), _row (Row), _col (col) {}};template<class t>class sparsematrix{ public://parameterless Constructor Sparsematrix (): _colsize (0), _rowsize (0) {}//matrix compression Sparsematrix (t* a, size_t m , size_t n, const t& invalid): _rowsize (M), _colsize (n), _invalid ( Invalid) {for (size_t i = 0; i < m; ++i)//Line traversal {for (size_t J&NBSP;=&NBSP;0;&NBSP;J&NBSP;<&NBSP;N;&NBSP;++J)//Column Traversal {if (a[i*n + j] != invalid) {_ A.push_back (triple<T> (A[i*n + j], i, j));}}} Void display ()//print {size_t index = 0;for (size_t i = 0; i < _rowsize; ++i) {for (SIZE_T&NBSP;J&NBSP;=&NBSP;0;&NBSP;J&NBSP;<&NBSP;_COLSIZE;&NBSP;++J) {if (Index < _a.size () && _a[index]._row == i&& _a[index] . _col == j) {cout << _a[index]._value << " "; ++index;} else{cout << _invalid << " ";}} Cout << endl;} Cout << endl;} Sparsematrix<t> transport ()//column transpose {sparsematrix<t> tmp;tmp._colsize = _rowsize; tmp._rowsize = _colsize;tmp._invalid = _invalid;for (size_t i = 0; i < _colsize; ++i) {size_t index = 0;while (index < _ A.size ()) {if (_a[index]._col == i) {triple<t> t; t._value = _a[index]._value;t._row = _a[index]._col;t._col = _a[index]._row;tmp . _a.push_back (t);} ++index;}} Return tmp;} Sparsematrix<t> fasttransport ()//fast Transpose {Sparsematrix<t> tmp;tmp._colsize = _ rowsize;tmp._rowsize = _colsize;tmp._invalid = _invalid;int* rowcounts = New int[_colsize];int* rowstart = new int[_colsize];memset (rowCounts, 0, sizeof (int) *_colsize); memset (rowstart, 0, sizeof (int) *_colsize);size_t index = 0; while (Index < _a.size ()) {Rowcounts[_a[index]._col]++;++index;} rowstart[0] = 0;for (size_t i = 1; i < _colsize; ++i) { ROWSTART[I]&NBSP;=&NBSP;ROWSTART[I&NBSP;-&NBSP;1]&NBSP;+&NBSP;ROWCOUNTS[I&NBSP;-&NBSP;1];} Index = 0;tmp._a.resize (_a.size ());while (Index < _a.size ()) {Size_t rowIndex = _a[index]._col;int& start = rowstart[rowindex]; triple<t> t;t._value = _a[index]._value;t._row = _a[index]._col;t._col = _a[index]._row;tmp._a[start++] = t;++index;} Return tmp;} protected:vector<triple<t>> _a;//Order table size_t _rowsize;//row size_t _colsize;//column T of triples type _invalid;//illegal value};void testsparsematrix () {int a[6][5] ={ { 1, 0, 3, 0, 5 },{ 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0 },{ 2, 0, 4, 0, 6 },{ 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0 } }; SPARSEMATRIX<INT>&NBSP;SM1 ((int*) a, 6, 5, 0); Sm1. Display (); Sparsematrix<int> sm2 = sm1. Transport (); sm2. Display (); Sparsematrix<int> sm3 = sm1. Fasttransport (); sm3. Display ();} Int main () {Testsparsematrix (); GetChar (); return 0;}
This article is from the "Homeopathy" blog, please be sure to keep this source http://lk123456.blog.51cto.com/10831443/1765220
Compressed storage of sparse matrices