【代码】稀疏矩阵的压缩存储与转置算法
2016-05-11 14:41
471 查看
#include<iostream> #include<vector> template <class T> struct Triple { T _value; size_t _row; size_t _col; Triple(const T& t =T(),size_t row = 0 ,size_t col = 0) :_value(t) , _row(row) , _col(col) {} }; template <class T> class SpareMatrix { public: SpareMatrix(T* a,size_t col,size_t row,const T invalid ) :_row(row) , _col(col) , _invalid(invalid) { for (size_t i = 0; i < row; ++i) { for (size_t j = 0; j < col; ++j) { if (invalid != a[i*col + j]) _array.push_back(Triple<T>(a[i*col + j], i, j)); } } } SpareMatrix() :_row(0) , _col(0) , _invalid(0) {} SpareMatrix<T> Transport() { SpareMatrix<T> ret; for (size_t i = 0; i < _col; ++i) { size_t Index = 0; while (Index != _array.size()) { if (_array[Index]._col == i) { Triple<T> temp(_array[Index]._value, _array[Index]._col, _array[Index]._row); ret._array.push_back(temp); } Index++; } } ret._col = _row; ret._row = _col; return ret; } SpareMatrix<T> QuickTransport() { int *RowCounts = new int [_col]; int *RowStarts = new int [_col]; memset(RowCounts, 0, sizeof(int)*_col); memset(RowStarts, 0, sizeof(int)*_col); SpareMatrix<T>ret; ret._array.resize(_array.size()); size_t index = 0; while (index < _array.size()) { RowCounts[_array[index++]._col]++; } index = 1; RowStarts[0] = 0; while (index < _col) { RowStarts[index] = RowCounts[index - 1] + RowStarts[index++ - 1]; } index = 0; while (index < _array.size()) { int &RowStart = RowStarts[_array[index]._col]; Triple<T>tmp(_array[index]._value, _array[index]._col, _array[index]._row); ret._array[RowStart++] = tmp; index++; } ret._col = _row; ret._row = _col; ret._invalid = _invalid; return ret; } void Print() { size_t index = 0; for (size_t i = 0; i < _row; ++i) { for (size_t j = 0; j < _col; ++j) { if (index < _array.size() && _array[index]._col == j) { cout << _array[index++]._value << " "; } else cout << _invalid << " "; } cout << endl; } cout << endl; } protected: size_t _row; size_t _col; T _invalid; std::vector<Triple<T>> _array; };
相关文章推荐
- Python使用稀疏矩阵节省内存实例
- 数据结构(C语言描述)读书笔记之稀疏矩阵
- 稀疏矩阵的变带宽存储方法。
- 稀疏矩阵-----三元组顺序表
- 稀疏矩阵的加法(用十字链表实现A=A+B)
- 稀疏矩阵求最小或最大的K个特征值及其特征向量
- Spark中稀疏矩阵的存储方式
- 数据结构Java实现——④数组——>稀疏矩阵十字链表存储法
- 数据结构Java实现——④数组——>稀疏矩阵三元组顺序存储
- 各种特殊矩阵的压缩存储
- 数据结构之自建算法库——稀疏矩阵的三元组表示
- 数据结构实践——稀疏矩阵相加
- 数据结构例程——稀疏矩阵的十字链表表示
- 数组和广义表
- 数据结构学习五(稀疏矩阵的实现,三元组)
- ubuntu 14.04 下安装Armadillo库 进行稀疏矩阵特征分解
- 看数据结构写代码(21) 稀疏矩阵(十字链表方式)
- 稀疏矩阵-三元组
- 第九周项目3 建立稀疏矩阵三元组算法库
- 第九周项目3 稀疏矩阵加法运算