[llvm-commits] [llvm] r102709 - in /llvm/trunk: include/llvm/ADT/SmallBitVector.h unittests/ADT/SmallBitVectorTest.cpp

Benjamin Kramer benny.kra at googlemail.com
Fri Apr 30 05:29:39 PDT 2010


Author: d0k
Date: Fri Apr 30 07:29:39 2010
New Revision: 102709

URL: http://llvm.org/viewvc/llvm-project?rev=102709&view=rev
Log:
Implement a read/write operator[] for SmallBitVector with a proxy class.

Modified:
    llvm/trunk/include/llvm/ADT/SmallBitVector.h
    llvm/trunk/unittests/ADT/SmallBitVectorTest.cpp

Modified: llvm/trunk/include/llvm/ADT/SmallBitVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallBitVector.h?rev=102709&r1=102708&r2=102709&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallBitVector.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallBitVector.h Fri Apr 30 07:29:39 2010
@@ -52,6 +52,34 @@
     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 {
     return X & uintptr_t(1);
   }
@@ -81,7 +109,7 @@
 
   void setSmallRawBits(uintptr_t NewRawBits) {
     assert(isSmall());
-    X = NewRawBits << 1 | uintptr_t(1);
+    X = (NewRawBits << 1) | uintptr_t(1);
   }
 
   // Return the size.
@@ -99,7 +127,7 @@
   }
 
   void setSmallBits(uintptr_t NewBits) {
-    setSmallRawBits(NewBits & ~(~uintptr_t(0) << getSmallSize()) |
+    setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
                     (getSmallSize() << SmallNumDataBits));
   }
 
@@ -298,7 +326,11 @@
   }
 
   // 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 {
     assert(Idx < size() && "Out-of-bounds Bit access.");
     if (isSmall())

Modified: llvm/trunk/unittests/ADT/SmallBitVectorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallBitVectorTest.cpp?rev=102709&r1=102708&r2=102709&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/SmallBitVectorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/SmallBitVectorTest.cpp Fri Apr 30 07:29:39 2010
@@ -176,4 +176,12 @@
   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());
+}
 }





More information about the llvm-commits mailing list