矩陣(Matrix)

#include <iostream>
#include <vector>
using namespace std;

template<class T>
//對稱矩陣
class SymmeticMatrix
{
public:
	//構造函數,實現對稱矩陣初始化
	SymmeticMatrix(T* Array,size_t sz)
		:_sz(sz*(sz+1)/2-1)
		,_array(new T[_sz])
		,_n(sz)
	{
		size_t index=0;
		for(int i=0;i<sz;i++)
		{
			for(int j=0;j<=i;j++)
			{
				_array[index]=Array[i*sz+j];
				index++;
			}
		}
	}

	~SymmeticMatrix()
	{
		if(NULL == _array)
		{
			delete[] _array;
			_sz=0;
			_n=0;
		}
	}

	//獲取對稱矩陣元素
	T& Access(size_t i,size_t j)
	{
		if(i<j)
		{
			swap(i,j);
		}
		return _array[i*(i+1)/2+j];
	}

	//打印對稱矩陣
	void Print()
	{
		for(int i=0;i<_n;i++)
		{
			for(int j=0;j<_n;j++)
			{
				if(i>=j)
				{
					cout<<_array[i*(i+1)/2+j]<<" ";
				}
				else
				{
					cout<<_array[j*(j+1)/2+i]<<" ";
					//break;
				}
			}
			cout<<endl;
		}
	}
private:
	size_t _sz;
	T* _array;
	size_t _n;
};

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:
	SparseMatrix()
		:_colSize(0)
		,_rowSize(0)
	{}

	//構造函數
	SparseMatrix(T* a, size_t m, size_t n, const T& invalid) //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;
	}

	//稀疏矩陣一般轉置,時間複雜度O(colSize*有效數據的個數)
	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.reserve(_a.size());
		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;
};

發佈了49 篇原創文章 · 獲贊 10 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章