mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-10-10 16:24:04 +00:00
Implement a read/write operator[] for SmallBitVector with a proxy class.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@102709 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
1e44aa0412
commit
b252fbd179
@ -52,6 +52,34 @@ class SmallBitVector {
|
|||||||
SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
|
SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Encapsulation of a single bit.
|
||||||
|
class reference {
|
||||||
|
SmallBitVector &TheVector;
|
||||||
|
unsigned BitPos;
|
||||||
|
|
||||||
|
public:
|
||||||
|
reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
|
||||||
|
|
||||||
|
reference& operator=(reference t) {
|
||||||
|
*this = bool(t);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
reference& operator=(bool t) {
|
||||||
|
if (t)
|
||||||
|
TheVector.set(BitPos);
|
||||||
|
else
|
||||||
|
TheVector.reset(BitPos);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator bool() const {
|
||||||
|
return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
bool isSmall() const {
|
bool isSmall() const {
|
||||||
return X & uintptr_t(1);
|
return X & uintptr_t(1);
|
||||||
}
|
}
|
||||||
@ -81,7 +109,7 @@ class SmallBitVector {
|
|||||||
|
|
||||||
void setSmallRawBits(uintptr_t NewRawBits) {
|
void setSmallRawBits(uintptr_t NewRawBits) {
|
||||||
assert(isSmall());
|
assert(isSmall());
|
||||||
X = NewRawBits << 1 | uintptr_t(1);
|
X = (NewRawBits << 1) | uintptr_t(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the size.
|
// Return the size.
|
||||||
@ -99,7 +127,7 @@ class SmallBitVector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setSmallBits(uintptr_t NewBits) {
|
void setSmallBits(uintptr_t NewBits) {
|
||||||
setSmallRawBits(NewBits & ~(~uintptr_t(0) << getSmallSize()) |
|
setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
|
||||||
(getSmallSize() << SmallNumDataBits));
|
(getSmallSize() << SmallNumDataBits));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,7 +326,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Indexing.
|
// Indexing.
|
||||||
// TODO: Add an index operator which returns a "reference" (proxy class).
|
reference operator[](unsigned Idx) {
|
||||||
|
assert(Idx < size() && "Out-of-bounds Bit access.");
|
||||||
|
return reference(*this, Idx);
|
||||||
|
}
|
||||||
|
|
||||||
bool operator[](unsigned Idx) const {
|
bool operator[](unsigned Idx) const {
|
||||||
assert(Idx < size() && "Out-of-bounds Bit access.");
|
assert(Idx < size() && "Out-of-bounds Bit access.");
|
||||||
if (isSmall())
|
if (isSmall())
|
||||||
|
@ -176,4 +176,12 @@ TEST(SmallBitVectorTest, CompoundAssignment) {
|
|||||||
EXPECT_EQ(100U, A.size());
|
EXPECT_EQ(100U, A.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(SmallBitVectorTest, ProxyIndex) {
|
||||||
|
SmallBitVector Vec(3);
|
||||||
|
EXPECT_TRUE(Vec.none());
|
||||||
|
Vec[0] = Vec[1] = Vec[2] = true;
|
||||||
|
EXPECT_EQ(Vec.size(), Vec.count());
|
||||||
|
Vec[2] = Vec[1] = Vec[0] = false;
|
||||||
|
EXPECT_TRUE(Vec.none());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user