moab
BitPage.hpp
Go to the documentation of this file.
00001 #ifndef BIT_PAGE_HPP
00002 #define BIT_PAGE_HPP
00003 
00004 #include <assert.h>
00005 #include "BitTag.hpp"
00006 
00007 namespace moab {
00008 
00009 class Range;
00010 
00016 class BitPage
00017 {
00018 public:
00026   BitPage( int bits_per_ent, unsigned char init_val );
00027   
00042   void get_bits( int offset, int count, int bits_per_ent, unsigned char* data ) const;
00043   
00059   void set_bits( int offset, int count, int bits_per_ent, const unsigned char* data );
00060   
00075   void set_bits( int offset, int count, int bits_per_ent, unsigned char value );
00076 
00089   unsigned char get_bits( int offset, int bits_per_ent ) const;
00090   
00104   void set_bits( int offset, int bits_per_ent, unsigned char data );
00105   
00118   void search( unsigned char value, int offset, int count, 
00119                int bits_per_ent, Range& results, EntityHandle start ) const;
00120 
00121 private:
00122 
00124   char byteArray[BitTag::PageSize];
00125 };
00126 
00127 inline unsigned char BitPage::get_bits( int offset, int per_ent ) const
00128 {
00129     // Assume per_ent is a power of two, which should be guaranteed
00130     // by higher-level code.
00131   unsigned char mask = (unsigned char)(1<<per_ent)-1; // 2^per_ent - 1
00132   int byte = (offset * per_ent) >> 3; // shifting 3 is dividing by eight
00133   int bit =  (offset * per_ent) & 7;  // masking with 7 is modulo eight
00134   assert(byte < BitTag::PageSize);
00135   return (unsigned char)(byteArray[byte] >> bit) & mask;
00136 } 
00137 
00138 inline void BitPage::set_bits( int offset, int per_ent, unsigned char bits )
00139 {
00140   int byte = (offset * per_ent) >> 3; // shifting 3 is dividing by eight
00141   int bit =  (offset * per_ent) & 7;  // masking with 7 is modulo eight
00142   assert(byte < BitTag::PageSize);
00143     // Assume per_ent is a power of two, which should be guaranteed
00144     // by higher-level code.
00145   unsigned char mask = (unsigned char)((1<<per_ent)-1) << bit;
00146   byteArray[byte] = (char)((byteArray[byte] & ~mask) | ((bits << bit) & mask));
00147 } 
00148 
00149 inline void BitPage::get_bits( int offset, int count, int per_ent, unsigned char* data ) const
00150 {
00151   unsigned char* end = data+count;
00152   while (data != end)
00153     *(data++) = get_bits( offset++, per_ent );
00154 }
00155 
00156 inline void BitPage::set_bits( int offset, int count, int per_ent, const unsigned char* data )
00157 {
00158   const unsigned char* end = data+count;
00159   while (data != end)
00160     set_bits( offset++, per_ent, *(data++) );
00161 }
00162 
00163 inline void BitPage::set_bits( int offset, int count, int per_ent, unsigned char value )
00164 {
00165   int end = offset + count;
00166   while (offset < end)
00167     set_bits( offset++, per_ent, value );
00168 }
00169   
00170 } // namespace moab
00171 
00172 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines