Storage and fast transpose of matrices

Source: Internet
Author: User


#include <iostream> #include <vector> #include <string>using namespace std;// Ternary Group template<class t>struct triple{size_t _row;size_t _col; t _value;}; Template<class t>class sparsematrix{public:sparsematrix (); Sparsematrix (T* a,size_t row,size_t col,const t& invalid); Sparsematrix (CONST&NBSP;SPARSEMATRIX&LT;T&GT;&AMP;&NBSP;SM); Sparsematrix<t>& operator= (CONST&NBSP;SPARSEMATRIX&LT;T&GT;&AMP;&NBSP;SM); ~SparseMatrix (); void  display (); Sparsematrix& transpose (); Sparsematrix& fasttranspose ();p rivate:vector<triple<t>> _array;size_t _rowsize; size_t _colsize; t _invalid;}; Template<class t>sparsematrix<t>::sparsematrix (): _rowsize (0), _colsize (0), _invalid (0) {} Template<class t>sparsematrix<t>::sparsematrix (T* a,size_t row,size_t col, Const t& invalid): _rowsize (Row), _colsize (col), _Invalid (invalid) {for (size_t i=0;i<_rowsize;i++) {for (size_t j=0;j<_colsize;j++) {if (a[i*_colsize +j]!=_invalid) {triple<t> t;t._row=i;t._col=j;t._value=a[i*_colsize+j];_array.push_back (T);}}}} Template<class t>sparsematrix<t>::sparsematrix (CONST&NBSP;SPARSEMATRIX&LT;T&GT;&AMP;&NBSP;SM) : _rowsize (Sm._rowsize), _colsize (sm._colsize), _invalid (Sm._invalid) {for (size_t i=0;i<sm._array.size (); I + +) {_array.push_back (sm._array[i]);}} Template<class t>sparsematrix<t>& sparsematrix<t>::operator= (const  SPARSEMATRIX&LT;T&GT;&AMP;&NBSP;SM) {_rowsize=sm._rowsize;_colsize=sm._colsize;_invalid=sm._invalid;for (size_t  i=0;i<_sm.array.size (; i++)) {_array.push_back (sm._array[i]);} Return *this;} Template<class t>sparsematrix<t>::~sparsematrix () {}template<class t>void  Sparsematrix<t>::D isplay () {size_t index=0;for (size_t i=0;i<_rowsize;i++) {for (size_t j= 0;j<_colsize;j++) {if (Index<_array.size () && (_array[index]._row==i) && (_array[index]._col==j)) {cout <<_array[index]._value;index++;} Else{cout<<_invalid;} cout<< " ";} Cout<<endl;} Cout<<endl;} General Transpose Template<class t>sparsematrix<t>& sparsematrix<t>::transpose () { Sparsematrix<t> sm; for (size_t i=0;i<_colsize;i++)  {for (size_t index=0;index <_array.size (); index++) {if (_array[index]._col==i) {triple<t> t;t._row=_array[index]._col;t._col=_ Array[index]._row;t._value=_array[index]._value;sm._array.push_back (t);}}  }swap (Sm._array,_array); swap (_rowsize,_colsize); return *this;} Quick Transpose Template<class t>sparsematrix<t>& sparsematrix<t>:: fasttranspose () { int* rowcounts=new int[_colsize];//counts the number of data per row after transpose  //2 0 2 0 2 int*  rowstart=new int[_colsize];//statistics the size of the starting position of each row of data after the transpose//  new  whyIt's _colsize, must it be him?   //Yes, explain  0 2 2 4 4 memset in the following example (Rowcounts,0,sizeof ( int) *_colsize);  //Initialize  rowcounts memset (rowstart,0,sizeof (int) *_colsize);  for (size_t  Index=0;index<_array.size (); index++)  { rowcounts[_array[index]._col]++; }  //initialization  rowstart rowstart[0]=0; for (size_t index=1;index<_colsize;index++)  {  Rowstart[index]= rowstart[index-1]+rowcounts[index-1]; } rowstart[0]=0; for (Int index =0;index<_array.size (); index++)  { triple<t> t; t._row=_array[index]._col; t. _col=_array[index]._row; t._value=_array[index]._value; }return *this;} Void test1 () {int a[]={1,0,3,0,5,0,0,0,0,0,0,0,0,0,0,1,0,3,0,5,0,0,0,0,0,0,0,0,0,0,0,}; SPARSEMATRIX&LT;INT&GT;&NBSP;SM1 (a,6,5,0); sm1. Display (); SPARSEMATRIX&LT;INT&GT;SM2 (SM1); sm2. Display (); SPARSEMATRIX&LT;INT&GT;SM3=SM1;SM3. Display ();} Void test2 () {int a[6][5]={{1,0,3,0,5},{0,0,0,0,0},{0,0,0,0,0},{1,0,3,0,5},{0,0,0,0,0},{0,0,0,0,0}}; SPARSEMATRIX&LT;INT&GT;&NBSP;SM1 (a[0],6,5,0); sm1. Display (); Sparsematrix<int> sm2=sm1. Transpose (); sm2. Display (); Sparsematrix<int> sm3=sm1. Fasttranspose (); sm3. Display ();} Int main () {//test1 (); Test2 (); System ("pause"); return 0;}


This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1758809

Storage and fast transpose of matrices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.