为可压缩存储,我真是煞费苦心,能不能帮我看看还有没有其他方法实现双下标?
能不能帮我看看还有没有其他方法实现双下标
#include <assert.h>
#include <iostream>
#define HALF(X) ((X+1)/2)
class CBit4Matrix
{
typedef struct
{
unsigned char High:4;
unsigned char Low:4;
}DFOURBITS;
class CBit4Value
{
DFOURBITS& _Value;
bool _bLow;
public:
CBit4Value(DFOURBITS& value,bool bLow):_Value(value) {};
virtual ~CBit4Value() {};
unsigned char operator= (unsigned char n)
{assert(n<16);_bLow?_Value.Low=n:_Value.High=n;return n;}
operator unsigned char () {return _bLow?_Value.Low:_Value.High;}
};
class CBit4Line
{
DFOURBITS* _pLine;
CBit4Matrix* _pMat;
public:
CBit4Line(DFOURBITS* p,CBit4Matrix* pMat):_pLine(p),_pMat(pMat)
{ASSERT(nLn>0 && nCol>0);
_pData = new DFOURBITS[_sLn*HALF(_sCol)];}
virtual ~CBit4Line()
if (_pData)
{delete []_pData;_pData = NULL; }
CBit4Value operator [] (long nCol)
{assert(nCol<_pMat->getCol() && nCol>=0);
CBit4Value rMV(_pLine[nCol/2],nCol%2);
return rMV;}
};
DFOURBITS *_pData;
long _sLn,_sCol;
///< 逻辑上的总行列数 \n\e不是物理上的 \n\e 使用时要加HALF宏转变成物理上的
public:
CBit4Matrix();
CBit4Matrix(long nLn,long nCol);
virtual ~CBit4Matrix();
long getLn() const {return _sLn;}
long getCol() const {return _sCol;}
CBit4Line operator [] (long nLn)
{assert(nLn<_sLn && nLn>=0);
CBit4Line r(_pData+nLn*HALF(_sCol),this);
return r;}
friend std::ostream& operator<<(std::ostream& os,CBit4Matrix& m)
{
long i,j;
for (i=0;i<m._sLn;i++)
{
for (j=0;j<HALF(m._sCol);j++)
{
os<<(int)m._pData[i*HALF(m._sCol)+j].High<<" ";
os<<(int)m._pData[i*HALF(m._sCol)+j].Low<<" ";
}
os<<std::endl;
}
return os;
}
};
就是为了实现双下标,好麻烦,呵呵,
void test_run()
{
CBit4Matrix m(3,3);
int i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
m[i][j] = i*3+j;
cerr<<i*3+j;
}cerr<<endl;
}
cerr<<m;//这块还有点问题,莫名奇妙,再研究研究
//error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CBit4Matrix' (or there is no acceptable conversion)
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cerr<<(int)m[i][j]<<' ';
}cerr<<endl;
}
}