BBB » 10 дек 2007, 09:47
PAS_SET.CPP (часть 1)
Код: Выделить всё
/*************************************************************************/
/* Эмуляция паскалевских множеств Set of byte/tChar для C++ */
/*************************************************************************/
/*************************************************************************/
/* Interface */
/*************************************************************************/
#include <MEM.H>
#include <STRING.H>
#include "Pas_Set.H"
#define div /
#define mod %
#define FillChar(dest,size,symb) \
( memset( &(dest), (symb), (size) ) )
//--------------------------------------------
const tByteSet sZERO; /* "пустое" множество, т.е. sZERO==[] */
const tByteSet& scZERO = sZERO; /* "пустое" множество, alias */
const tByteSet& sbZERO = sZERO; /* "пустое" множество, alias */
SetPas::SetPas() /* constructor */
{
FillChar(abSet, sizeof(abSet), 0 ); /* обнулили */
}; /* of "SetPas::SetPas()" */
SetPas::SetPas (const SetPas& sSource) /* constructor */
{
*this=sSource;
}; /* of "SetPas::SetPas(const SetPas& sSource) */
SetPas::SetPas(tPtrChar acSource) /* constructor */
{
word wLen;
//---------------------------------
FillChar(abSet, sizeof(abSet), 0 ); /* обнулили */
for (wLen=0; wLen<strlen((char*)acSource); wLen++)
{
*this = *this+acSource[wLen];
};
}; /* of "SetPas::SetPas(tPtrChar acSource)" */
SetPas::SetPas(tPtrSChar acSource) /* constructor */
{
word wLen;
//---------------------------------
FillChar(abSet, sizeof(abSet), 0 ); /* обнулили */
for (wLen=0; wLen<strlen((char*)acSource); wLen++)
{
*this = *this+acSource[wLen];
};
}; /* of "SetPas::SetPas(tPtrSChar acSource)" */
byte SetPas::Power()
/* возвращает мощность множества */
{
byte bPower = 0;
byte bOutLoop, bInLoop, bCurrByte;
//------------------
for (bOutLoop=0; bOutLoop<sizeof(abSet); bOutLoop++)
{
bCurrByte=abSet[bOutLoop];
for (bInLoop=0; bInLoop<BITS_PER_BYTE; bInLoop++)
{
bPower+=(bCurrByte & 1);
bCurrByte>>=1;
}; /* of "for (bInLoop=0; bInLoop<sizeof(abSet); bInLoop++)" */
}; /* of "for (bOutLoop=0; bOutLoop<sizeof(abSet); bOutLoop++)" */
return (bPower);
}; /* SetPas::Power() */
/* FRI}S of class "StringPas": */
SetPas operator + (const SetPas& sSet, byte bItem)
/* операция '+' реализует объединение множеств */
{
byte bByte, bBit; /* номера смещения(байта) и бита в множестве */
SetPas sTmp;
//----------------
sTmp=sSet;
bByte=bItem div BITS_PER_BYTE;
bBit =bItem mod BITS_PER_BYTE;
/* устанавливаем соответствующий бит */
sTmp.abSet[bByte]=(sTmp.abSet[bByte] | (1<<bBit) );
/* возвращаем результат */
return (sTmp);
}; /* of " + (const SetPas& sSet, byte bItem)" */
SetPas operator + (const SetPas& sSet1, const SetPas& sSet2)
/* операция '+' реализует объединение множеств */
{
word w;
SetPas sTmp;
//----------------
/* идем по байтам множества */
for (w=0; w<sizeof(sTmp.abSet); w++)
{
/* устанавливаем соответствующий байт */
sTmp.abSet[w]=(sSet1.abSet[w] | sSet2.abSet[w] );
}; /* for */
/* возвращаем результат */
return (sTmp);
}; /* of " + (const SetPas& sSet1, const SetPas& sSet2)" */
SetPas operator - (const SetPas& sSet, byte bItem)
/* операция '-' реализует разность множеств */
{
byte bByte, bBit; /* номера смещения(байта) и бита в множестве */
SetPas sTmp;
//----------------
sTmp=sSet;
bByte=bItem div BITS_PER_BYTE;
bBit =bItem mod BITS_PER_BYTE;
/* сбрасываем соответствующий бит */
sTmp.abSet[bByte]=(sTmp.abSet[bByte] & (~(1<<bBit)) );
/* возвращаем результат */
return (sTmp);
}; /* of " - (const SetPas& sSet, byte bItem)" */
SetPas operator - (const SetPas& sSet1, const SetPas& sSet2)
/* операция '-' реализует разность множеств */
{
word w;
SetPas sTmp;
//----------------
/* идем по байтам множества */
for (w=0; w<sizeof(sTmp.abSet); w++)
{
/* устанавливаем соответствующий байт нужным образом */
sTmp.abSet[w]=(sSet1.abSet[w] & (~(sSet2.abSet[w])) );
}; /* for */
/* возвращаем результат */
return (sTmp);
}; /* of " - (const SetPas& sSet1, const SetPas& sSet2)" */
SetPas operator * (const SetPas& sSet1, const SetPas& sSet2)
/* операция '*' реализует пересечение множеств */
{
word w;
SetPas sTmp;
//----------------
/* идем по байтам множества */
for (w=0; w<sizeof(sTmp.abSet); w++)
{
/* устанавливаем соответствующий байт нужным образом */
sTmp.abSet[w]=(sSet1.abSet[w] & sSet2.abSet[w] );
}; /* for */
/* возвращаем результат */
return (sTmp);
}; /* of " * (const SetPas& sSet1, const SetPas& sSet2)" */
PAS_SET.CPP (часть 1)
[code]
/*************************************************************************/
/* Эмуляция паскалевских множеств Set of byte/tChar для C++ */
/*************************************************************************/
/*************************************************************************/
/* Interface */
/*************************************************************************/
#include <MEM.H>
#include <STRING.H>
#include "Pas_Set.H"
#define div /
#define mod %
#define FillChar(dest,size,symb) \
( memset( &(dest), (symb), (size) ) )
//--------------------------------------------
const tByteSet sZERO; /* "пустое" множество, т.е. sZERO==[] */
const tByteSet& scZERO = sZERO; /* "пустое" множество, alias */
const tByteSet& sbZERO = sZERO; /* "пустое" множество, alias */
SetPas::SetPas() /* constructor */
{
FillChar(abSet, sizeof(abSet), 0 ); /* обнулили */
}; /* of "SetPas::SetPas()" */
SetPas::SetPas (const SetPas& sSource) /* constructor */
{
*this=sSource;
}; /* of "SetPas::SetPas(const SetPas& sSource) */
SetPas::SetPas(tPtrChar acSource) /* constructor */
{
word wLen;
//---------------------------------
FillChar(abSet, sizeof(abSet), 0 ); /* обнулили */
for (wLen=0; wLen<strlen((char*)acSource); wLen++)
{
*this = *this+acSource[wLen];
};
}; /* of "SetPas::SetPas(tPtrChar acSource)" */
SetPas::SetPas(tPtrSChar acSource) /* constructor */
{
word wLen;
//---------------------------------
FillChar(abSet, sizeof(abSet), 0 ); /* обнулили */
for (wLen=0; wLen<strlen((char*)acSource); wLen++)
{
*this = *this+acSource[wLen];
};
}; /* of "SetPas::SetPas(tPtrSChar acSource)" */
byte SetPas::Power()
/* возвращает мощность множества */
{
byte bPower = 0;
byte bOutLoop, bInLoop, bCurrByte;
//------------------
for (bOutLoop=0; bOutLoop<sizeof(abSet); bOutLoop++)
{
bCurrByte=abSet[bOutLoop];
for (bInLoop=0; bInLoop<BITS_PER_BYTE; bInLoop++)
{
bPower+=(bCurrByte & 1);
bCurrByte>>=1;
}; /* of "for (bInLoop=0; bInLoop<sizeof(abSet); bInLoop++)" */
}; /* of "for (bOutLoop=0; bOutLoop<sizeof(abSet); bOutLoop++)" */
return (bPower);
}; /* SetPas::Power() */
/* FRI}S of class "StringPas": */
SetPas operator + (const SetPas& sSet, byte bItem)
/* операция '+' реализует объединение множеств */
{
byte bByte, bBit; /* номера смещения(байта) и бита в множестве */
SetPas sTmp;
//----------------
sTmp=sSet;
bByte=bItem div BITS_PER_BYTE;
bBit =bItem mod BITS_PER_BYTE;
/* устанавливаем соответствующий бит */
sTmp.abSet[bByte]=(sTmp.abSet[bByte] | (1<<bBit) );
/* возвращаем результат */
return (sTmp);
}; /* of " + (const SetPas& sSet, byte bItem)" */
SetPas operator + (const SetPas& sSet1, const SetPas& sSet2)
/* операция '+' реализует объединение множеств */
{
word w;
SetPas sTmp;
//----------------
/* идем по байтам множества */
for (w=0; w<sizeof(sTmp.abSet); w++)
{
/* устанавливаем соответствующий байт */
sTmp.abSet[w]=(sSet1.abSet[w] | sSet2.abSet[w] );
}; /* for */
/* возвращаем результат */
return (sTmp);
}; /* of " + (const SetPas& sSet1, const SetPas& sSet2)" */
SetPas operator - (const SetPas& sSet, byte bItem)
/* операция '-' реализует разность множеств */
{
byte bByte, bBit; /* номера смещения(байта) и бита в множестве */
SetPas sTmp;
//----------------
sTmp=sSet;
bByte=bItem div BITS_PER_BYTE;
bBit =bItem mod BITS_PER_BYTE;
/* сбрасываем соответствующий бит */
sTmp.abSet[bByte]=(sTmp.abSet[bByte] & (~(1<<bBit)) );
/* возвращаем результат */
return (sTmp);
}; /* of " - (const SetPas& sSet, byte bItem)" */
SetPas operator - (const SetPas& sSet1, const SetPas& sSet2)
/* операция '-' реализует разность множеств */
{
word w;
SetPas sTmp;
//----------------
/* идем по байтам множества */
for (w=0; w<sizeof(sTmp.abSet); w++)
{
/* устанавливаем соответствующий байт нужным образом */
sTmp.abSet[w]=(sSet1.abSet[w] & (~(sSet2.abSet[w])) );
}; /* for */
/* возвращаем результат */
return (sTmp);
}; /* of " - (const SetPas& sSet1, const SetPas& sSet2)" */
SetPas operator * (const SetPas& sSet1, const SetPas& sSet2)
/* операция '*' реализует пересечение множеств */
{
word w;
SetPas sTmp;
//----------------
/* идем по байтам множества */
for (w=0; w<sizeof(sTmp.abSet); w++)
{
/* устанавливаем соответствующий байт нужным образом */
sTmp.abSet[w]=(sSet1.abSet[w] & sSet2.abSet[w] );
}; /* for */
/* возвращаем результат */
return (sTmp);
}; /* of " * (const SetPas& sSet1, const SetPas& sSet2)" */
[/code]