bitmap.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. #include <cstddef>
  3. #include <functional>
  4. namespace types {
  5. class bitmap {
  6. public:
  7. using deleter_type = std::function<void(unsigned char*, std::size_t)>;
  8. private:
  9. deleter_type m_del;
  10. std::size_t m_size;
  11. unsigned char* m_bm;
  12. static constexpr std::size_t SZ = sizeof(unsigned char) * 8;
  13. public:
  14. constexpr bitmap(const deleter_type& del, unsigned char* bm, std::size_t size)
  15. : m_del(del), m_size(size), m_bm(bm) {}
  16. constexpr bitmap(deleter_type&& del, unsigned char* bm, std::size_t size)
  17. : m_del(std::move(del)), m_size(size), m_bm(bm) {}
  18. explicit constexpr bitmap(std::size_t size)
  19. : m_del { [](unsigned char* bm, std::size_t) {
  20. delete[] bm;
  21. } }
  22. , m_size { (size / SZ) + ((size % SZ) ? 1 : 0) }
  23. , m_bm { new unsigned char[m_size] {} }
  24. { }
  25. bitmap(const bitmap&) = delete;
  26. constexpr ~bitmap()
  27. { m_del(m_bm, m_size); }
  28. constexpr bool test(std::size_t n) const
  29. { return (m_bm[n / SZ] & (1 << (n % SZ))) != 0; }
  30. constexpr void set(std::size_t n)
  31. { m_bm[n / SZ] |= (1 << (n % SZ)); }
  32. constexpr void clear(std::size_t n)
  33. { m_bm[n / SZ] &= (~(1 << (n % SZ))); }
  34. constexpr std::size_t size() const noexcept
  35. { return m_size; }
  36. };
  37. } // namespace types