42 #define P1ATOM_HEADER_SIZE 16
43 #define P1ATOM_LONG_BOND_SIZE 8
44 #define P1ATOM_SHORT_BOND_SIZE 4
50 class P1Atom :
public Atom< CoreConfig< P1Atom<PC>, PC> >
52 public:
enum { ATOM_CATEGORY = 1 };
59 CONFIGURED_BITS_PER_ATOM_IS_INCOMPATIBLE_WITH_P1ATOM = 1/((PC::BITS_PER_ATOM==BITS)?1:0)
71 void SetType(u32 type, u32 width)
80 return GetBitsAllocated() <= BITS;
83 bool HasBeenRepaired()
90 u32 GetBitCount()
const {
95 s32 bitsUsed = GetBitsAllocated();
100 u32 typeBits = BITS-bitsUsed;
101 if (typeBits > 31) typeBits = 31;
102 return this->
m_bits.
Read(BITS-typeBits,typeBits);
105 void WriteStateBits(
ByteSink& ostream)
const
107 s32 bitsUsed = GetBitsAllocated();
109 u32 typeBits = BITS - bitsUsed;
110 if(typeBits > 31) typeBits = 31;
112 u32 leftoverBits = BITS - typeBits;
114 for(u32 i = 0; i < leftoverBits; i++)
120 void ReadStateBits(
const char* stateStr)
122 u32 len = strlen(stateStr);
124 for(u32 i = 0; i < len; i++)
132 s32 bitsUsed = GetBitsAllocated();
134 u32 typeBits = BITS - bitsUsed;
135 if(typeBits > 31) typeBits = 31;
137 u32 leftoverBits = BITS - typeBits;
139 for(u32 i = 0; i < leftoverBits; i++)
151 P1Atom(u32 type, u32 longc, u32 shortc, u32 statec)
153 InitAtom(type,longc,shortc,statec);
156 void InitAtom(u32 type, u32 longc, u32 shortc, u32 statec)
160 SetLongBondCount(longc);
161 SetShortBondCount(shortc);
162 SetStateBitCount(statec);
163 u32 used = GetBitsAllocated();
166 FAIL(ILLEGAL_ARGUMENT);
168 u32 avail = BITS - used;
169 if (avail > 31) avail = 31;
171 if (type >= 1u<<avail)
172 FAIL(ILLEGAL_ARGUMENT);
173 SetType(type, avail);
183 u32 GetBitsAllocated()
const
187 GetLongBondCount()*P1ATOM_LONG_BOND_SIZE
188 + GetShortBondCount()*P1ATOM_SHORT_BOND_SIZE
189 + GetStateBitCount();
192 u32 GetBitsAllocatedExceptState()
const
196 GetLongBondCount()*P1ATOM_LONG_BOND_SIZE
197 + GetShortBondCount()*P1ATOM_SHORT_BOND_SIZE;
200 u32 GetStateBitCount()
const
203 void SetStateBitCount(u32 state)
213 u32 firstState = GetBitsAllocatedExceptState();
214 return this->
m_bits.
Read(firstState+stateIndex, stateWidth);
224 u32 firstState = GetBitsAllocatedExceptState();
225 return this->
m_bits.
Write(firstState+stateIndex, stateWidth, value);
228 u32 GetLongBondCount()
const
231 void SetLongBondCount(u32 count)
234 u32 GetShortBondCount()
const
237 void SetShortBondCount(u32 count)
240 u32 GetLongBond(u32 index)
const {
241 if (index>=GetLongBondCount())
return 0;
244 void SetLongBond(u32 index, u8 value) {
245 if (index>=GetLongBondCount())
return;
246 return this->
m_bits.
Write(16+index*8,8,(u32) value);
249 u32 GetShortBond(u32 index)
const {
250 if (index>=GetShortBondCount())
return 0;
251 u32 start = GetLongBondCount()*8+16;
254 void SetShortBond(u32 index, u8 value) {
255 if (index>=GetLongBondCount())
return;
256 u32 start = GetLongBondCount()*8+16;
257 return this->
m_bits.
Write(start+index*4,4,(u32) value);
260 void ReadVariableBodyInto(u32* arr)
const
266 void WriteVariableBodyFrom(u32* arr)
272 void PrintBits(ByteSink & ostream)
const
275 void Print(ByteSink & ostream)
const
277 u32 type = GetType();
278 u32 lbc = GetLongBondCount();
279 u32 sbc = GetShortBondCount();
280 u32 stc = GetStateBitCount();
282 ostream.Printf(
"P1[%x/%d%d/%d:%x]", type,lbc,sbc,stc,state);
287 u32 AddLongBond(
const SPoint& offset);
289 u32 AddShortBond(
const SPoint& offset);
296 bool GetLongBond(u32 index, SPoint& pt)
const;
302 bool GetShortBond(u32 index, SPoint& pt)
const;
308 bool SetLongBond(u32 index,
const SPoint& pt);
314 bool SetShortBond(u32 index,
const SPoint& pt);
323 void RemoveLongBond(u32 index);
325 void RemoveShortBond(u32 index);
327 P1Atom& operator=(
const P1Atom & rhs);
331 #include "P1Atom.tcc"
void Write(const u32 startIdx, const u32 length, const u32 value)
Definition: BitVector.tcc:99
u32 Read(const u32 startIdx, const u32 length) const
Definition: BitVector.tcc:129
void WriteBit(u32 idx, bool bit)
Definition: BitVector.tcc:41
Definition: CoreConfig.h:40
void Print(ByteSink &ostream) const
Definition: BitVector.tcc:202
void SetStateField(u32 stateIndex, u32 stateWidth, u32 value)
Definition: P1Atom.h:222
u32 GetStateField(u32 stateIndex, u32 stateWidth) const
Definition: P1Atom.h:210
Definition: ByteSink.h:47
bool ReadBit(u32 idx)
Definition: BitVector.tcc:65
Definition: BitVector.h:47
BitVector< BPA > m_bits
Definition: Atom.h:99