List:Internals« Previous MessageNext Message »
From:Mats Kindahl Date:March 17 2005 12:35pm
Subject:bk commit into 5.1 tree (mats:1.1790)
View as plain text  
Below is the list of changes that have just been committed into a local
5.1 repository of mats. When mats does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet
  1.1790 05/03/17 13:35:13 mats@stripped +1 -0
  Extended bitvector with operator== and operator!=.

  sql/bitvector.h
    1.2 05/03/17 13:35:09 mats@stripped +38 -0
    Added operator== and operator!=.
    Added compile-time check of assumptions for implementation.

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	mats
# Host:	romeo.kindahl.net
# Root:	/home/bk/bv-mysql-5.1

--- 1.1/sql/bitvector.h	2005-03-17 10:08:41 +01:00
+++ 1.2/sql/bitvector.h	2005-03-17 13:35:09 +01:00
@@ -22,6 +22,10 @@
 #include <cstring>
 #include <climits>
 
+#if CHAR_BIT != 8
+#  error "This implementation is designed for 8-bit bytes!"
+#endif
+
 namespace {
     template <class T> 
     inline void my_swap(T& x, T& y) { 
@@ -70,9 +74,27 @@
     return (bits + (byte_bits-1)) / byte_bits; 
   }
 
+  // Tidy the last byte (by clearing the unused bits) of the bitvector to make
+  // comparison easy.
+  // Here I'm assuming that we're working with 8-bit bytes.
+  void tidy_last_byte() {
+    byte* const last_byte = m_data + bytes() - 1;
+
+    // Get the number of unused bits (0..7) in the last byte
+    unsigned int const unused = ((8U - (size() & 0x7U)) & 0x7U);
+
+    // Create a mask with the lower 'unused' bits clear and the upper bits
+    // set.
+    unsigned int const mask = ~((1 << unused) - 1);
+
+    // Mask the last byte
+    *last_byte &= mask;
+  }
+
   template <class ReturnType, class Func>
   inline ReturnType apply_to_byte(size_t const pos, Func op) const
   {
+    // Here I'm assuming that we're working with 8-bit bytes.
     ptrdiff_t const byte_pos = pos >> 3;
     byte const mask = (1 << (pos & 0x7U));
     return op(&m_data[byte_pos], mask);
@@ -100,6 +122,7 @@
   {
     // std::copy(data, data + byte_size(size), m_data);
     memcpy(m_data, data, byte_size(size));
+    tidy_last_byte();
   }
 
   bitvector(bitvector const& other) 
@@ -107,6 +130,7 @@
   {
     // std::copy(other.m_data, other.m_data + other.bytes(), m_data);
     memcpy(m_data, other.data(), other.bytes());
+    tidy_last_byte();		// Just a precaution
   }
 
   // Assignment operator
@@ -141,6 +165,7 @@
     // allowed.
     // std::fill(m_data, m_data + bytes(), 255);
     memset(m_data, 255, bytes()); 
+    tidy_last_byte();
   }
 
   // Set a bit to a value
@@ -152,6 +177,7 @@
   void clear_all() { 
     // std::fill(m_data, m_data + bytes(), 0);
     memset(m_data, 0, bytes()); 
+    tidy_last_byte();
   }
 
   // Reset one bit in the vector
@@ -166,6 +192,18 @@
   bool get_bit(size_t pos) const {
     return apply_to_byte<bool>(pos, test_bit_op());
   };
+
+  bool operator==(bitvector const& rhs) const {
+    if (size() != rhs.size())
+      return false;
+    if (memcmp(data(), rhs.data(), bytes()) != 0)
+      return false;
+    return true;
+  }
+
+  bool operator!=(bitvector const& rhs) const {
+    return !(*this == rhs);
+  }
 
 private:
   size_t m_size;
Thread
bk commit into 5.1 tree (mats:1.1790)Mats Kindahl17 Mar