Gistrec icon

BitMap

Gistrec | PRO | 04/21/18 06:53:56 PM UTC | 0 ⭐ | 676 👁️ | Never ⏰ | []
C++ |

1.03 KB

|

None

|

0 👍

/

0 👎

#include <iostream>
#include <vector>
#include <cstdint>
 
using std::size_t;
 
template <typename Block>
class BitMap {
  static const size_t BitsPerBlock = sizeof(Block) * 8;
 
public:
  BitMap(size_t height, size_t width)
    : m_width(width)
    , m_height(height)
    , m_blocks((width * height) / BitsPerBlock + 1, 0)
  {}
 
  bool get(size_t x, size_t y) const {
    const size_t index = x * m_width + y;
    const size_t nbit = index % BitsPerBlock;
    const size_t nblock = index / BitsPerBlock;
 
    return m_blocks[nblock] & (1 << nbit);
  }
 
  void set(size_t x, size_t y) {
    const size_t index = x * m_width + y;
    const size_t nbit = index % BitsPerBlock;
    const size_t nblock = index / BitsPerBlock;
 
    m_blocks[nblock] |= (1 << nbit);
  }
 
  size_t width() const {
    return m_width;
  }
 
  size_t height() const {
    return m_height;
  }
 
private:
  size_t m_width;
  size_t m_height;
  std::vector<Block> m_blocks;
};
 
int main() {
    // your code goes here
    return 0;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎