#include "bitmap.h"
#include <stdlib.h>
#include <inttypes.h>

long long bitmap_find_first(uint8_t* map, size_t length)
{
    int offset = 0;
    for (unsigned i = 0; i < length; i++, offset += 8) {
        if (map[i] == 0xFF) continue;

        return offset + __builtin_ctz(~map[i]);
    }

    return -1;
}

char bitmap_get(uint8_t* map, size_t pos)
{
    return map[pos / 8] & (1 << (pos % 8));
}

void bitmap_set(uint8_t* map, size_t pos)
{
    map[pos / 8] |= (1 << (pos % 8));
}

void bitmap_unset(uint8_t* map, size_t pos)
{
    map[pos / 8] &= ~(1 << (pos % 8));
}