数组-稀疏矩阵快速转置

215 阅读1分钟
void fastTransposeTSMatrix(TSMatrix& A, TSMatrix *B) {
	int col,t, p, q;
	int num[1000] = { 0 }, position[1000];
	B->len = A.len;
	B->n = A.m;
	B->m = A.n;
	for (t = 1;t <= A.len;t++) {  //记录原矩阵每一列非零元素个数 
		num[A.data[t].col]++;
	}
	position[1] = 1;    //记录每一列第一个非零元素转置以后在三元组里面的下标位置 
	for (col = 2;col <= A.n;col++)
		position[col] = position[col - 1] + num[col - 1];
	for (p = 1;p <= A.len;p++) {
		col = A.data[p].col;
		q = position[col];
		B->data[q].row = A.data[p].col;
		B->data[q].col = A.data[p].row;
		B->data[q].e = A.data[p].e;
		position[col]++; //对应列下一个非零元素位置 
	}
}