package ecs

import (
	
	
	
)

type number interface {
	int | int8 | uint8 | int16 | uint16 | int32 | uint32 | cacheID | observerID
}

// entityPool is an implementation using implicit linked lists.
// Implements https://skypjack.github.io/2019-05-06-ecs-baf-part-3/
type entityPool struct {
	pointer   unsafe.Pointer
	entities  []Entity
	next      entityID
	available uint32
	reserved  entityID
}

// newEntityPool creates a new, initialized Entity pool.
func newEntityPool( uint32,  uint32) entityPool {
	 := make([]Entity, , +)
	// Reserved zero and wildcard entities.
	for  := range  {
		[] = Entity{entityID(), math.MaxUint32}
	}
	return entityPool{
		entities:  ,
		next:      0,
		available: 0,
		pointer:   unsafe.Pointer(&[0]),
		reserved:  entityID(),
	}
}

// Get returns a fresh or recycled entity.
func ( *entityPool) () Entity {
	if .available == 0 {
		return .getNew()
	}
	 := .next
	.next, .entities[.next].id = .entities[.next].id, .next
	.available--
	return .entities[]
}

// Allocates and returns a new entity. For internal use.
func ( *entityPool) () Entity {
	 := Entity{id: entityID(len(.entities)), gen: 0}
	.entities = append(.entities, )
	.pointer = unsafe.Pointer(&.entities[0])
	return 
}

// Recycle hands an entity back for recycling.
func ( *entityPool) ( Entity) {
	if .id < .reserved {
		panic("can't recycle reserved zero or wildcard entity")
	}
	.entities[.id].gen++
	.next, .entities[.id].id = .id, .next
	.available++
}

// Reset recycles all entities. Does NOT free the reserved memory.
func ( *entityPool) () {
	.entities = .entities[:.reserved]
	.next = 0
	.available = 0
}

// Alive returns whether an entity is still alive, based on the entity's generations.
func ( *entityPool) ( Entity) bool {
	return .gen == (*Entity)(unsafe.Add(.pointer, entitySize*uintptr(.id))).gen
}

// Len returns the current number of used entities.
func ( *entityPool) () int {
	return len(.entities) - int(.reserved) - int(.available)
}

// Cap returns the current capacity (used and recycled entities).
func ( *entityPool) () int {
	return len(.entities) - int(.reserved)
}

// TotalCap returns the current capacity in terms of reserved memory.
func ( *entityPool) () int {
	return cap(.entities)
}

// Available returns the current number of available/recycled entities.
func ( *entityPool) () int {
	return int(.available)
}

// bitPool is a pool of bits that makes it possible to obtain an un-set bit,
// and to recycle that bit for later use.
// This implementation uses an implicit list.
type bitPool struct {
	bits      []uint8
	length    uint8
	next      uint8
	available uint8
}

// newBitPool creates a new empty bitPool.
func newBitPool() bitPool {
	return bitPool{
		bits: make([]uint8, mask64TotalBits),
	}
}

// Get returns a fresh or recycled bit.
func ( *bitPool) () uint8 {
	if .available == 0 {
		return .getNew()
	}
	 := .next
	.next, .bits[.next] = .bits[.next], .next
	.available--
	return .bits[]
}

// Allocates and returns a new bit. For internal use.
func ( *bitPool) () uint8 {
	if .length >= mask64TotalBits {
		panic(fmt.Sprintf("run out of the maximum of %d bits. "+
			"This is likely caused by unclosed queries that lock the world. "+
			"Make sure that all queries finish their iteration or are closed manually", mask64TotalBits))
	}
	 := .length
	.bits[.length] = 
	.length++
	return 
}

// Recycle hands a bit back for recycling.
func ( *bitPool) ( uint8) {
	.next, .bits[] = , .next
	.available++
}

// Reset recycles all bits.
func ( *bitPool) () {
	.next = 0
	.length = 0
	.available = 0
}

// entityPool is an implementation using implicit linked lists.
// Implements https://skypjack.github.io/2019-05-06-ecs-baf-part-3/
type intPool[ number] struct {
	pool              []
	next              
	available         uint32
	capacityIncrement uint32
}

// newEntityPool creates a new, initialized Entity pool.
func newIntPool[ number]( uint32) intPool[] {
	return intPool[]{
		pool:              make([], 0, ),
		next:              0,
		available:         0,
		capacityIncrement: ,
	}
}

// Get returns a fresh or recycled entity.
func ( *intPool[]) ()  {
	if .available == 0 {
		return .getNew()
	}
	 := .next
	.next, .pool[.next] = .pool[.next], .next
	.available--
	return .pool[]
}

// Allocates and returns a new entity. For internal use.
func ( *intPool[]) ()  {
	 := (len(.pool))
	if len(.pool) == cap(.pool) {
		 := .pool
		.pool = make([], len(.pool), len(.pool)+int(.capacityIncrement))
		copy(.pool, )
	}
	.pool = append(.pool, )
	return 
}

// Recycle hands an entity back for recycling.
func ( *intPool[]) ( ) {
	.next, .pool[] = , .next
	.available++
}

// Reset recycles all entities. Does NOT free the reserved memory.
func ( *intPool[]) () {
	.pool = .pool[:0]
	.next = 0
	.available = 0
}