package adb_pool

Pool :: struct($T: typeid) {
	pool: []T,
	usage, capacity: int,
}

init :: proc(pool: ^Pool($T), capacity: int, allocator := context.allocator) {
	pool.pool = make([]T, capacity, allocator)
	pool.usage = 0
	pool.capacity = capacity
}

elems_available :: proc(pool: ^Pool($T), capacity: int) -> bool {
	return pool.usage + count <= pool.capacity
}

get :: proc(pool: ^Pool($T), count: int) -> []T {
	elements := pool.pool[pool.usage:pool.usage + count]
	pool.usage += count
	return elements
}

usage :: proc(pool: Pool($T)) -> int {
	return pool.usage
}

get_allocated_elements :: proc(pool: Pool($T)) -> []T {
	return pool.pool[:pool.usage]
}

clear :: proc(pool: ^Pool($T)) {
	pool.usage = 0
}

free_range :: proc(pool: ^Pool($T), from, count: int) {
	assert(count > 0, "Cannot free zero or negative count")
	to := from + count
	assert(to < pool.usage, "Cannot free beyond usage")
	mem.copy(&pool.pool[from], &pool.pool[to], (pool.usage - to) * size_of(T))
}
