registry

package
v0.19.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GenerationShiftBits = 64 - 8
	FlagsShiftBits      = 64 - 16
	IndexBitMask        = 0x00_00_FFFF_FFFF_FFFF
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Handle

type Handle uint64

Byte 1: Generation; Byte 2: Flags; Bytes 3-8: Index

func NewHandle

func NewHandle(generation byte, flags HandleFlag, index uint64) Handle

func (Handle) Flags

func (h Handle) Flags() HandleFlag

func (Handle) Generation

func (h Handle) Generation() byte

func (Handle) HasFlag

func (h Handle) HasFlag(ef HandleFlag) bool

func (Handle) Index

func (h Handle) Index() uint64

func (Handle) IsZero added in v0.19.6

func (h Handle) IsZero() bool

IsZero reports whether the handle is in its default 'zero' state. A zero handle is an invalid handle that does NOT point to any entity

type HandleFlag

type HandleFlag byte
const (
	HandleFlag_None  HandleFlag = 0
	HandleFlag_Alive HandleFlag = 1 << (iota - 1)
)

type Iterator added in v0.18.1

type Iterator[T any] struct {
	// contains filtered or unexported fields
}

Iterator goes through the entire registry it was created from and returns all alive items, and nil after its done.

The iterator will still work if items are added/removed to the registry after it was created, but the following conditions apply:

  • If items are removed, iterator will not show the removed items (assuming it didn't return them before their removal)
  • If items are added, the iterator will either only return older items (i.e. is not affected), or only return newer items (i.e. items that were going to be returned before will now not get returned in favor of newly inserted items), or a mix of old and new items. However, in all cases the iterator will *never* returns more items than were alive at the time of the iterator's creation.
  • If items were both added and removed, the iterator might follow either of the previous 2 cases or a combination of them

To summarize: The iterator will *never* return more items than were alive at the time of its creation, and will *never* return freed items

Example usage:

for item, handle := it.Next(); !it.IsDone(); item, handle = it.Next() {
	// Do stuff
}

func (*Iterator[T]) IsDone added in v0.18.1

func (it *Iterator[T]) IsDone() bool

func (*Iterator[T]) Next added in v0.18.1

func (it *Iterator[T]) Next() (*T, Handle)

type Registry

type Registry[T any] struct {
	ItemCount uint
	Handles   []Handle
	Items     []T

	FreeList     *freeListitem
	FreeListSize uint32

	// The number of slots required to be in the free list before the free list
	// is used for creating new entries
	FreeListUsageThreshold uint32
}

Registry is a storage data structure that can efficiently create/get/free items using generational indices. Each item stored in the registry is associated with a 'handle' object that is used to get and free objects

The registry 'owns' all items it stores and returns pointers to items in its array. All items are allocated upfront.

It is NOT safe to concurrently create or free items. However, it is SAFE to concurrently get items

func NewRegistry

func NewRegistry[T any](size uint32) *Registry[T]

func (*Registry[T]) Free

func (r *Registry[T]) Free(id Handle)

Free resets the entity flags then adds this entity to the free list

func (*Registry[T]) Get

func (r *Registry[T]) Get(id Handle) *T

func (*Registry[T]) New

func (r *Registry[T]) New() (*T, Handle)

func (*Registry[T]) NewIterator added in v0.18.1

func (r *Registry[T]) NewIterator() Iterator[T]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL