稀疏矩阵的压缩存储和逆置,以及快速逆置,以及稀疏矩阵的加法

////稀疏矩阵的压缩存储、逆置、快速逆置、加法
//#include<vector>
//#include<iostream>
//#include<assert.h>
//using namespace std;
//
//
//template<typename T>
//struct message
//{
//  message(int row, int col, T value)
//  :_row(row)
//  , _col(col)
//  , _value(value)
//  {}
//  int _row;
//  int _col;
//  T _value;
//};
//
////稀疏矩阵的压缩存储
//template<typename T>
//class SparseMatrix
//{
//public:
//  SparseMatrix(T*_arr, T invaild, int row, int col)
//      :  arr(_arr)
//      , _invaild(invaild)
//      , _row(row)
//      , _col(col)
//  {
//      assert(arr);
//      for (int i = 0; i < row; ++i)
//      {
//          for (int j = 0; j < col; ++j)
//          {
//              if (arr[i*col + j] != invaild)
//              {
//                  message<T>m(i, j, arr[i*col + j]);
//                  s.push_back(m);
//              }
//          }
//      }
//  }
//  SparseMatrix()
//  {
//
//  }
//
//  vector<message<T>>& Sort(vector<message<T>>&vect)
//  {
//      for (size_t idx1 = 0; idx1 < vect.size(); ++idx1)
//      {
//          for (size_t idx2 = 0; idx2 < (vect.size() - idx1) - 1; ++idx2)
//          {
//              if ((vect[idx2]._row > vect[idx2 + 1]._row)
//                  || ((vect[idx2]._row == vect[idx2 + 1]._row) && (vect[idx2]._col > vect[idx2 + 1]._col))
//                  )
//              {
//                  swap(vect[idx2]._row, vect[idx2 + 1]._row);
//                  swap(vect[idx2]._col, vect[idx2 + 1]._col);
//                  swap(vect[idx2]._value, vect[idx2 + 1]._value);
//              }
//          }
//      }
//      return vect;
//  }
//
//  template<class T>
//  SparseMatrix<T> operator+(SparseMatrix<T>&sm)
//  {
//      SparseMatrix<T> TMP;
//      TMP.arr = arr;
//      TMP._col = _col;
//      TMP._row = _row;
//      TMP._invaild = _invaild;
//      vector<message<T> > vect;
//      size_t i = 0;
//      size_t j = 0;
//      while (i < s.size() && j < s.size())
//      {
//          if (sm.s[i]._row < s[j]._row)
//          {
//              vect.push_back(sm.s[i]);
//              i++;
//          }
//          else if (sm.s[i]._row > s[j]._row)
//          {
//              vect.push_back(s[j]);
//              j++;
//          }
//          else if (sm.s[i]._col<s[j]._col)
//          {
//              vect.push_back(sm.s[i]);
//              i++;
//          }
//          else if (sm.s[i]._col==s[j]._col)
//          {
//              T tmp = sm.s[i]._value + s[j]._value;
//              if (_invaild!=tmp)
//                  vect.push_back(message<T>(s[j]._row, s[j]._col,tmp));
//              j++;
//              i++;
//          }
//          else
//          {
//              vect.push_back(s[j]);
//              j++;
//          }
//      }
//
//      while (i<s.size())
//      {
//          vect.push_back(sm.s[i]);
//          i++;
//      }
//
//      while (j<s.size())
//      {
//          vect.push_back(s[j]);
//          j++;
//      }
//      TMP.s = vect;
//      return TMP;
//  }
//
//  template<typename T>
//  friend ostream&operator<<(ostream&_cout, SparseMatrix<T>&sm)
//  {
//      size_t index = 0;
//  for (int i = 0; i < sm._row; ++i)
//  {
//      for (int j = 0; j < sm._col; ++j)
//      {
//          if ((index < sm.s.size())
//              && (sm.s[index]._row == i)
//              &&(sm.s[index]._col == j))
//          {
//              _cout << sm.s[index++]._value << " ";
//          }
//          else
//              _cout << sm._invaild << " ";
//      }
//      _cout << endl;
//  }
//  return _cout;
//  }
//
//  //稀疏矩阵的逆置
//  SparseMatrix<T> Translate()
//  {
//      SparseMatrix<T> TMP;
//      TMP.arr = arr;
//      TMP._col = _row;
//      TMP._row = _col;
//      TMP._invaild = _invaild;
//      for (int i = 0; i <_col; ++i)
//      {
//          for (int j = 0; j < _row; ++j)
//          {
//              if (arr[j*_col + i] != _invaild)
//              {
//                  message<T> m(i, j, arr[j*_col + i]);
//                  TMP.s.push_back(m);
//              }
//          }
//      }
//      return TMP;
//  }
//
//
//  //稀疏矩阵的快速逆置
//  SparseMatrix<T> FastTranslate()
//  {
//      SparseMatrix<T> TMP;
//      TMP.arr = arr;
//      TMP._col = _row;
//      TMP._row = _col;
//      TMP._invaild = _invaild;
//      vector<message<T> > vect(s);
//      for (size_t idx = 0; idx < vect.size(); ++idx)
//      {
//          swap(vect[idx]._row, vect[idx]._col);
//      }
//      vect=Sort(vect);
//      TMP.s = vect;
//      return TMP;
//  }
//
//private:
//  vector<message<T> >s;
//  T* arr;
//  T _invaild;
//  int _row;
//  int _col;
//};
//
//
//
//void test()
//{
//  int array[6][5] = {
//      { 1, 0, 3, 0, 5 },
//      { 0, 0, 0, 0, 0 },
//      { 0, 0, 0, 0, 0 },
//      { 1, 3, 0, 1, 0 },
//      { 0, 0, 0, 0, 0 },
//      { 0, 0, 0, 0, 0 } };
//  SparseMatrix<int> s(array[0], 0, 6, 5);
//  cout << s << endl;
//  cout << s.Translate() << endl;
//  cout << s.FastTranslate() << endl;
//
//  int array1[6][5] = {
//      { 0, 0, 0, 0, 1 },
//      { 0, 0, 1, 0, 0 },
//      { 0, 0, 0, 0, 0 },
//      { 1, 3, 0, 1, 0 },
//      { 0, 0, 0, 0, 0 },
//      { 0, 0, 0, 0, 0 } };
//  SparseMatrix<int> s1(array1[0], 0, 6, 5);
//  cout << s1 << endl;
//  cout << (s1 + s);
//}
//
//
//int main()
//{
//  test();
//  system("pause");
//  return 0;
//}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值