Truly independent web browser
/*
* Copyright (c) 2026, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Array.h>
#include <AK/StdLibExtras.h>
#include <AK/Types.h>
namespace AK {
template<size_t Size>
class FixedBitmap {
public:
constexpr FixedBitmap(bool default_value)
{
fill(default_value);
}
constexpr void fill(bool value)
{
__builtin_memset(m_data.data(), value ? 0xff : 0x00, size_in_bytes());
}
constexpr void set(size_t index, bool value)
{
VERIFY(index < Size);
if (value)
m_data[index / 8] |= static_cast<u8>(1u << (index % 8));
else
m_data[index / 8] &= static_cast<u8>(~(1u << (index % 8)));
}
[[nodiscard]] constexpr bool get(size_t index) const
{
VERIFY(index < Size);
return 0 != (m_data[index / 8] & (1u << (index % 8)));
}
[[nodiscard]] constexpr size_t size() const { return Size; }
[[nodiscard]] constexpr size_t size_in_bytes() const { return ceil_div(Size, static_cast<size_t>(8)); }
[[nodiscard]] constexpr ReadonlyBytes bytes() const LIFETIME_BOUND { return m_data.span(); }
constexpr void copy_from(ReadonlyBytes bytes)
{
VERIFY(bytes.size() == size_in_bytes());
__builtin_memcpy(m_data.data(), bytes.data(), size_in_bytes());
}
// NB: fill(true) also sets the padding bits past Size, so only compare bitmaps built the same
// way: two false-initialized bitmaps mutated bit by bit compare exactly.
[[nodiscard]] constexpr bool operator==(FixedBitmap const& other) const = default;
private:
Array<u8, ceil_div(Size, static_cast<size_t>(8))> m_data;
};
}