/**
 * Find a range of unset bits in the {@link BitSet}
 * useful for allocation algorithms
 * @param {BitSet} bitset
 * @param {number} bit_count
 * @param {number} start_index
 * @returns {number}
 */
export function bitset_find_clear_gap(bitset, bit_count, start_index = 0) {
    let slot_index = start_index;

    main_loop:while (true) {

        slot_index = bitset.nextClearBit(slot_index)

        const search_range_end = slot_index + bit_count;

        for (let i = slot_index; i < search_range_end; i++) {
            if (bitset.get(i)) {
                // slot is occupied
                slot_index = i + 1;

                continue main_loop;
            }
        }

        // found a hole big enough
        return slot_index;
    }
}