资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

稀疏矩阵的压缩存储-创新互联

    压缩存储值存储极少数的有效数据。使用{row,col,value}三元组存储每一个有效数据,三元组按原矩阵中的位置,以行优先级先后顺序依次存放。

#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include
using namespace std;
//三元组的定义
template
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 SparseMatrix
{
public:
	//无参构造函数
	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(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 Transport()//列转置
	{
		SparseMatrix 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._value = _a[index]._value;
					t._row = _a[index]._col;
					t._col = _a[index]._row;

					tmp._a.push_back(t);
				}

				++index;
			}
		}

		return tmp;
	}

	SparseMatrix FastTransport()//快速转置
	{
		SparseMatrix 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._value = _a[index]._value;
			t._row = _a[index]._col;
			t._col = _a[index]._row;
			tmp._a[start++] = t;

			++index;

		}

		return tmp;
	}

protected:


	vector> _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 sm1((int*)a, 6, 5, 0);
	sm1.Display();

	SparseMatrix sm2 = sm1.Transport();
	sm2.Display();

	SparseMatrix sm3 = sm1.FastTransport();
	sm3.Display();
}

int main()
{
	TestSparseMatrix();
	getchar();
	return 0;
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。

目前成都创新互联公司已为上千家的企业提供了网站建设、域名、网页空间、绵阳服务器托管、企业网站设计、资源网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
当前名称:稀疏矩阵的压缩存储-创新互联
文章来源:http://cdkjz.cn/article/dddsip.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220