Advertisement
Gistrec

BitMap

Apr 21st, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdint>
  4.  
  5. using std::size_t;
  6.  
  7. template <typename Block>
  8. class BitMap {
  9.   static const size_t BitsPerBlock = sizeof(Block) * 8;
  10.  
  11. public:
  12.   BitMap(size_t height, size_t width)
  13.     : m_width(width)
  14.     , m_height(height)
  15.     , m_blocks((width * height) / BitsPerBlock + 1, 0)
  16.   {}
  17.  
  18.   bool get(size_t x, size_t y) const {
  19.     const size_t index = x * m_width + y;
  20.     const size_t nbit = index % BitsPerBlock;
  21.     const size_t nblock = index / BitsPerBlock;
  22.  
  23.     return m_blocks[nblock] & (1 << nbit);
  24.   }
  25.  
  26.   void set(size_t x, size_t y) {
  27.     const size_t index = x * m_width + y;
  28.     const size_t nbit = index % BitsPerBlock;
  29.     const size_t nblock = index / BitsPerBlock;
  30.  
  31.     m_blocks[nblock] |= (1 << nbit);
  32.   }
  33.  
  34.   size_t width() const {
  35.     return m_width;
  36.   }
  37.  
  38.   size_t height() const {
  39.     return m_height;
  40.   }
  41.  
  42. private:
  43.   size_t m_width;
  44.   size_t m_height;
  45.   std::vector<Block> m_blocks;
  46. };
  47.  
  48. int main() {
  49.     // your code goes here
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement