#ifndef CTRIPLE_SPARSE_MATRIX_H
#define CTRIPLE_SPARSE_MATRIX_H
/************************************************************************/
/* 以下是C++ 矩阵 类, 三元组;
/************************************************************************/
//三元组类
template<class Elemplent>
class CTriple
{
public:
int rows,cols;
CTriple(int r,int c,Elemplent tempelemplent);
CTriple();
Elemplent value;
};
//构造函数
template <class Elemplent>
CTriple<Elemplent>::CTriple()
{
}
//构造函数
template<class Elemplent>
CTriple<Elemplent>::CTriple(int r,int c,Elemplent tempelemplent)
{
rows =r;
cols =c;
value = tempelemplent;
}
//三元矩阵类定义
template <class Elemplent>
class CTripleSparseMatrix
{
protected:
int row,col,num;
int maxSize;
CTriple<Elemplent> *triplePtr;
public:
CTripleSparseMatrix(int temprow = 22,int tempcol=22,int tempmaxsize=111);
CTripleSparseMatrix(const CTripleSparseMatrix<Elemplent> & tempclass);
CTripleSparseMatrix<Elemplent> operator = (const CTripleSparseMatrix<Elemplent> & tempclass);
int GetRow()const;
int GetCol() const;
int GetNum() const;
bool SetElemplent(int temprow,int tempcol,const Elemplent & tempelemplent);
bool GetElemplent(int temprow,int tempcol,Elemplent & tempelemplent);
void SimpleTranspose(const CTripleSparseMatrix<Elemplent> & tempsourse,CTripleSparseMatrix<Elemplent> &dest);
void FastTranspose(const CTripleSparseMatrix<Elemplent> & tempsourse,CTripleSparseMatrix<Elemplent> &dest);
};
//构造函数
template <class Elemplent>
CTripleSparseMatrix<Elemplent>::CTripleSparseMatrix(int temprow /* = 22 */,int tempcol/* =22 */,int tempmaxsize/* =111 */)
{
row = temprow;
col = tempcol;
maxSize = tempmaxsize;
num = 0;
triplePtr = new CTriple<Elemplent>[tempmaxsize];
}
//得到列数
template <class Elemplent>
int CTripleSparseMatrix<Elemplent>::GetCol()const
{
return col;
}
//得到总数
template <class Elemplent>
int CTripleSparseMatrix<Elemplent>::GetNum()const
{
return num;
}
//得到行数
template <class Elemplent>
int CTripleSparseMatrix<Elemplent>::GetRow()const
{
return row;
}
//设置元素
template<class Elemplent>
bool CTripleSparseMatrix<Elemplent>::SetElemplent(int temprow,int tempcol,const Elemplent & tempelemplent)
{
if ( temprow > row || col >col || col <1 || row <1)
{
return false;
}
int i,j;
//查找元素
for (j = num -1 ;(j>0)&&(temprow<triplePtr[j].rows||temprow ==triplePtr[j].rows)&&
(tempcol<triplePtr[j].cols||tempcol ==triplePtr[j].cols);j--)
{
}
if (j>=0&& temprow == triplePtr[j].rows&& tempcol == triplePtr[j].cols)
{
//如果=0,就是说删除咯!
if(tempelemplent == 0)
{
for (i = j+1;i<num;i++)
{
triplePtr[i-1]= triplePtr[i];
}
num --;
}
//赋值
else triplePtr[j].value = tempelemplent;
}
else if (tempelemplent !=0)
{
//添加一个新元素
if (num<maxSize)
{
for (i = num -1;i>j;i--)
{
triplePtr[i+1] = triplePtr[i];
}
triplePtr[j+1].rows = temprow;
triplePtr[j+1].cols = tempcol;
triplePtr[j+1].value = tempelemplent;
num++;
return true;
}
else
{
return false;
}
}
return false;
}
//得到元素值,赋值给参数
template <class Elemplent>
bool CTripleSparseMatrix<Elemplent>::GetElemplent(int temprow,int tempcol,Elemplent & tempelemplent)
{
if (temprow >row||tempcol>col||tempcol<1||temprow<1)
{
return false;
}
int j;
//查找元素
for (j = num -1 ;(j>0)&&(temprow<triplePtr[j].rows||temprow ==triplePtr[j].rows)&&
(tempcol<triplePtr[j].cols||tempcol ==triplePtr[j].cols);j--)
{
}
//如果找到赋值
if (j>=0 && tempcol == triplePtr[j].cols && temprow == triplePtr[j].rows)
{
tempelemplent = triplePtr[j].value;
return true;
}
//设置为空
tempelemplent = 0;
return false;
}
//复制构造函数
template <class Elemplent>
CTripleSparseMatrix<Elemplent>::CTripleSparseMatrix(const CTripleSparseMatrix<Elemplent> & tempclass)
{
maxSize = tempclass.maxSize;
triplePtr = new CTriple<Elemplent>[maxSize];
row = tempclass.row;
col = tempclass.col;
num = tempclass.num;
for (int i = 0;i<num ;i++)
{
triplePtr[i] = tempclass.triplePtr[i];
}
}
//重载=
template <class Elemplent>
CTripleSparseMatrix<Elemplent>& CTripleSparseMatrix<Elemplent>::operator=(const CTripleSparseMatrix<Elemplent> & tempclass)
{
if (this == &tempclass)
{
return *this;
}
maxSize = tempclass.maxSize;
triplePtr = new CTriple<Elemplent>[maxSize];
row = tempclass.row;
col = tempclass.col;
num = tempclass.num;
for (int i = 0;i<num ;i++)
{
triplePtr[i] = tempclass.triplePtr[i];
}
return *this;
}
//简单转换,复杂度n*N
template <class Elemplent>
void CTripleSparseMatrix<Elemplent>::SimpleTranspose(const CTripleSparseMatrix<Elemplent> & sourse,CTripleSparseMatrix<Elemplent> &dest)
{
dest.col = sourse.GetCol();
dest.row = sourse.GetRow();
dest.num = sourse.GetNum();
dest.maxSize = sourse.maxSize;
delete []triplePtr;
triplePtr = new CTriple<Elemplent>[maxSize];
if (num >0)
{
int destpos = 0;
for (int col = 0;col<sourse.col;col++)
{
for (int soursepos = 0;soursepos <num;soursepos++)
{
if (sourse.triplePtr[soursepos].cols = col;)
{
//行列交换
dest.triplePtr[destpos].rows = sourse.triplePtr[soursepos].cols;
dest.triplePtr[destpos].cols = sourse.triplePtr[soursepos].rows;
dest.triplePtr[destpos].value = sourse.triplePtr[soursepos].value;
destpos++;
}
}
}
}
}
//快速转化
template <class Elemplent>
void CTripleSparseMatrix<Elemplent>::FastTranspose(const CTripleSparseMatrix<Elemplent> & sourse,CTripleSparseMatrix<Elemplent> &dest)
{
dest.col = sourse.GetCol();
dest.row = sourse.GetRow();
dest.num = sourse.GetNum();
dest.maxSize = sourse.maxSize;
delete []triplePtr;
triplePtr = new CTriple<Elemplent>[maxSize];
//用来存储每一列的第一元素的数组
int *cNum = new int[sourse.GetCol() +1];
int *cPos = new int[sourse.GetCol() +1];
int col;
int sourcePos;
if (dest.num >0)
{
//初始化他
for (col = 1;col<sourse.col;col++;)
{
cNum[i] = 0;
}
//记录每一列有几个元素
for (sourcePos = 0 ;sourcePos <sourse.num;sourcePos++)
{
++cNum[sourse.triplePtr[sourcePos].cols];
}
cPos [1] = 0;
//每一列的元素第一个元素之前有几个,简单说第一个元素的在新的里面的位置
for (col = 2;col<sourse.col;col++)
{
cPos[col] = cPos[col-1]+ cNum[col-1];
}
//赋值
for (sourcePos=0;sourcePos<sourse.num;sourcePos++)
{
//得到每一个元素的第一个元素的位置
int destpos = cNum[sourse.triplePtr[sourcePos].cols];
dest.triplePtr[destpos].cols = sourse.triplePtr[sourcePos].rows;
dest.triplePtr[destpos].rows = sourse.triplePtr[sourcePos].cols;
dest.triplePtr[destpos].value = sourse.triplePtr[sourcePos].value;
//下一列
++cNum[sourse.triplePtr[sourcePos].cols];
}
}
delete []cNum;
delete []cPos;
}
//时间长,难眠有错误
#endif