壓縮存儲值存儲極少數(shù)的有效數(shù)據(jù)。使用{row,col,value}三元組存儲每一個有效數(shù)據(jù),三元組按原矩陣中的位置,以行優(yōu)先級先后順序依次存放。 #define _CRT_SECURE_NO_WARNINGS 1 #include <vector> #include<iostream> using namespace std; //三元組的定義 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: //無參構造函數(shù) SparseMatrix() :_colSize(0) , _rowSize(0) {} //矩陣的壓縮 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)//行遍歷 { for (size_t j = 0; j < n; ++j)//列遍歷 { if (a[i*n + j] != invalid) { _a.push_back(Triple<T>(a[i*n + j], i, j)); } } } } void Display()//打印 { size_t index = 0; for (size_t i = 0; i < _rowSize; ++i) { for (size_t j = 0; j < _colSize; ++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()//列轉置 { 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()//快速轉置 { 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] = rowStart[i - 1] + rowCounts[i - 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;//三元組類型的順序表 size_t _rowSize;//行 size_t _colSize;//列 T _invalid;//非法值 }; 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> 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; }
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調度,確保服務器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務器買多久送多久。
目前成都創(chuàng)新互聯(lián)公司已為上千家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)頁空間、綿陽服務器托管、企業(yè)網(wǎng)站設計、資源網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
當前標題:稀疏矩陣的壓縮存儲-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://m.rwnh.cn/article8/dddsip.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、軟件開發(fā)、動態(tài)網(wǎng)站、網(wǎng)站改版、網(wǎng)站設計、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)