Packed

package
v0.0.0-...-01268d7 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package Packed contains specific Array types that are more efficient and convenient to work with.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearch

func BinarySearch[T ~byte | ~float32 | ~float64 | ~int32 | ~int64](array Array[T], value T, before bool) int

BinarySearch returns the index of value in the sorted array. If it cannot be found, returns where value should be inserted to keep the array sorted. The algorithm used is binary search. The returned index comes before all existing elements equal to value in the array.

Note: Calling BinarySearch on an unsorted array will result in unexpected behavior. Use Sort before calling this method.

func Sort

func Sort[T ~byte | ~float32 | ~float64 | ~int32 | ~int64](array Array[T])

Sort sorts the array in ascending order. The final order is dependent on the "less than" (<) comparison between elements.

Types

type Array

type Array[T Type] GenericArray.Contains[T]

Array contains comparable elements of type T Similar to [Array.Contains[T]] but for a supported Type is more efficient and convienient to work with.

func New

func New[T Type](values ...T) Array[T]

New creates a new array with the given values.

func (*Array[T]) Append

func (array *Array[T]) Append(value T)

Append an element to the end of the array.

func (*Array[T]) AppendArray

func (array *Array[T]) AppendArray(other Array[T])

AppendArray appends all elements of another array to the end of this array.

func (Array[T]) Bytes

func (array Array[T]) Bytes() Bytes

Bytes returns the underlying data in the array as Bytes.

func (Array[T]) Clear

func (array Array[T]) Clear()

Clear clears the array. This is equivalent to using resize with a size of 0.

func (Array[T]) Count

func (array Array[T]) Count(value T) int

Count returns the number of times an element is in the array.

func (Array[T]) Duplicate

func (array Array[T]) Duplicate() Array[T]

Duplicate creates a copy of the array, and returns it.

func (Array[T]) Fill

func (array Array[T]) Fill(value T)

Fill assigns the given value to all elements in the array. This can typically be used together with resize to create an array with a given size and initialized elements.

func (Array[T]) Find

func (array Array[T]) Find(value T) int

Find searches the array for a value and returns its index or -1 if not found.

func (Array[T]) FindLast

func (array Array[T]) FindLast(value T) int

FindLast searches the array in reverse order for a value and returns its index or -1 if not found.

func (Array[T]) Has

func (array Array[T]) Has(value T) bool

Has returns true if the array contains the given value.

func (Array[T]) Index

func (array Array[T]) Index(idx int) T

Index returns the element at the given index.

func (*Array[T]) Insert

func (array *Array[T]) Insert(idx int, value T)

Insert inserts a new element at a given position in the array. The position must be valid, or at the end of the array (idx == size()).

func (Array[T]) IsEmpty

func (array Array[T]) IsEmpty() bool

IsEmpty Returns true if the array is empty.

func (Array[T]) Iter

func (array Array[T]) Iter() iter.Seq2[int, T]

Iter returns an iterator for the array.

func (Array[T]) Len

func (array Array[T]) Len() int

Len returns the number of elements in the array.

func (Array[T]) RemoveAt

func (array Array[T]) RemoveAt(idx int)

RemoveAt removes an element from the array by index.

func (*Array[T]) Resize

func (array *Array[T]) Resize(size int)

Resize sets the size of the array. If the array is grown, reserves elements at the end of the array. If the array is shrunk, truncates the array to the new size. Calling resize once and assigning the new values is faster than adding new elements one by one.

func (Array[T]) Reverse

func (array Array[T]) Reverse()

Reverse reverses the order of the elements in the array.

func (*Array[T]) SetIndex

func (array *Array[T]) SetIndex(idx int, value T)

SetIndex sets the value of the element at the given index.

func (Array[T]) Slice

func (array Array[T]) Slice(begin, end int) Array[T]

Slice returns a slice of the array from the given begin index to the given end index.

func (Array[T]) Values

func (array Array[T]) Values() iter.Seq[T]

Values returns an iterator on the values of the array.

type Bytes

type Bytes Array[byte]

Bytes provides additional methods for working with arrays of bytes.

func (*Bytes) Append

func (array *Bytes) Append(value byte)

Append an element to the end of the array.

func (*Bytes) AppendArray

func (array *Bytes) AppendArray(other Bytes)

AppendArray appends all elements of another array to the end of this array.

func (Bytes) BinarySearch

func (array Bytes) BinarySearch(value byte, before bool) int

BinarySearch returns the index of value in the sorted array. If it cannot be found, returns where value should be inserted to keep the array sorted. The algorithm used is binary search. The returned index comes before all existing elements equal to value in the array.

Note: Calling BinarySearch on an unsorted array will result in unexpected behavior. Use Sort before calling this method.

func (Bytes) Bytes

func (array Bytes) Bytes() []byte

Bytes returns the underlying data in the array as a slice of bytes.

func (Bytes) Clear

func (array Bytes) Clear()

Clear clears the array. bytehis is equivalent to using resize with a size of 0.

func (Bytes) Compress

func (array Bytes) Compress(mode CompressionMode) Bytes

Compress returns a new PackedByteArray with the data compressed. Set the compression mode using one of CompressionMode's constants.

func (Bytes) Count

func (array Bytes) Count(value byte) int

Count returns the number of times an element is in the array.

func (Bytes) Decode

func (array Bytes) Decode(offset uintptr) any

Decode a variant-encoded value from the bytes starting at offset. Returns null if a valid variant can't be decoded.

func (Bytes) DecodeFloat32

func (array Bytes) DecodeFloat32(offset uintptr) float32

DecodeFloat32 decodes a 32-bit floating-point number from the bytes starting at offset. Fails if the byte count is insufficient. Returns 0.0 if a valid number can't be decoded.

func (Bytes) DecodeFloat64

func (array Bytes) DecodeFloat64(offset uintptr) float64

DecodeFloat64 decodes a 64-bit floating-point number from the bytes starting at offset. Fails if the byte count is insufficient. Returns 0.0 if a valid number can't be decoded.

func (Bytes) DecodeInt16

func (array Bytes) DecodeInt16(offset uintptr) int16

DecodeInt16 decodes a 16-bit signed integer number from the bytes starting at offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func (Bytes) DecodeInt32

func (array Bytes) DecodeInt32(offset uintptr) int32

DecodeInt32 decodes a 32-bit signed integer number from the bytes starting at offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func (Bytes) DecodeInt64

func (array Bytes) DecodeInt64(offset uintptr) int64

DecodeInt64 decodes a 64-bit signed integer number from the bytes starting at offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func (Bytes) DecodeInt8

func (array Bytes) DecodeInt8(offset uintptr) int8

DecodeInt8 decodes a 8-bit signed integer number from the bytes starting at offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func (Bytes) DecodeSize

func (array Bytes) DecodeSize(offset uintptr) uintptr

Decodes a size of a Variant from the bytes starting at offset. Requires at least 4 bytes of data starting at the offset, otherwise fails.

func (Bytes) DecodeUint16

func (array Bytes) DecodeUint16(offset uintptr) uint16

DecodeUint16 decodes a 16-bit unsigned integer number from the bytes starting at offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func (Bytes) DecodeUint32

func (array Bytes) DecodeUint32(offset uintptr) uint32

DecodeUint32 decodes a 32-bit unsigned integer number from the bytes starting at offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func (Bytes) DecodeUint64

func (array Bytes) DecodeUint64(offset uintptr) uint64

DecodeUint64 decodes a 64-bit unsigned integer number from the bytes starting at offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func (Bytes) DecodeUint8

func (array Bytes) DecodeUint8(offset uintptr) uint8

DecodeUint8 decodes a 8-bit unsigned integer number from the bytes starting at offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func (Bytes) DecompressSize

func (array Bytes) DecompressSize(buffer_size int, mode CompressionMode) Bytes

Decompress returns a new PackedByteArray with the data decompressed. Set buffer_size to the size of the uncompressed data. Set the compression mode using one of CompressionMode's constants.

func (Bytes) DecompressUpto

func (array Bytes) DecompressUpto(max_output_size int, mode CompressionMode) Bytes

DecompressUpto returns a new PackedByteArray with the data decompressed. Set the compression mode using one of CompressionMode's constants. This method only accepts brotli, gzip, and deflate compression modes.

This method is potentially slower than decompress, as it may have to re-allocate its output buffer multiple times while decompressing, whereas decompress knows it's output buffer size from the beginning.

func (Bytes) Duplicate

func (array Bytes) Duplicate() Bytes

Duplicate creates a copy of the array, and returns it.

func (Bytes) Encode

func (array Bytes) Encode(offset uintptr, value any)

Encode a variant-encoded value at the index of offset bytes. Sufficient space must be allocated, depending on the encoded variant's size

func (Bytes) EncodeFloat32

func (array Bytes) EncodeFloat32(offset uintptr, value float32)

EncodeFloat32 encodes a 32-bit floating-point number as bytes at the index of offset bytes. The array must have at least 4 bytes of allocated space, starting at the offset.

func (Bytes) EncodeFloat64

func (array Bytes) EncodeFloat64(offset uintptr, value float64)

EncodeFloat64 encodes a 64-bit floating-point number as bytes at the index of offset bytes. The array must have at least 8 bytes of allocated space, starting at the offset.

func (Bytes) EncodeInt16

func (array Bytes) EncodeInt16(offset uintptr, value int16)

EncodeInt16 encodes a 16-bit signed integer number as bytes at the index of offset bytes. The array must have at least 2 bytes of allocated space, starting at the offset.

func (Bytes) EncodeInt32

func (array Bytes) EncodeInt32(offset uintptr, value int32)

EncodeInt32 encodes a 32-bit signed integer number as bytes at the index of offset bytes. The array must have at least 4 bytes of allocated space, starting at the offset.

func (Bytes) EncodeInt64

func (array Bytes) EncodeInt64(offset uintptr, value int64)

EncodeInt64 encodes a 64-bit signed integer number as bytes at the index of offset bytes. The array must have at least 8 bytes of allocated space, starting at the offset.

func (Bytes) EncodeInt8

func (array Bytes) EncodeInt8(offset uintptr, value int8)

EncodeInt8 encodes a 8-bit signed integer number as bytes at the index of offset bytes. The array must have at least 1 byte of allocated space, starting at the offset.

func (Bytes) EncodeUint16

func (array Bytes) EncodeUint16(offset uintptr, value uint16)

EncodeUint16 encodes a 16-bit unsigned integer number as bytes at the index of offset bytes. The array must have at least 2 bytes of allocated space, starting at the offset.

func (Bytes) EncodeUint32

func (array Bytes) EncodeUint32(offset uintptr, value uint32)

EncodeUint32 encodes a 32-bit unsigned integer number as bytes at the index of offset bytes. The array must have at least 4 bytes of allocated space, starting at the offset.

func (Bytes) EncodeUint64

func (array Bytes) EncodeUint64(offset uintptr, value uint64)

EncodeUint64 encodes a 64-bit unsigned integer number as bytes at the index of offset bytes. The array must have at least 8 bytes of allocated space, starting at the offset.

func (Bytes) EncodeUint8

func (array Bytes) EncodeUint8(offset uintptr, value uint8)

EncodeUint8 encodes a 8-bit unsigned integer number as bytes at the index of offset bytes. The array must have at least 1 byte of allocated space, starting at the offset.

func (Bytes) Fill

func (array Bytes) Fill(value byte)

Fill assigns the given value to all elements in the array. bytehis can typically be used together with resize to create an array with a given size and initialized elements.

func (Bytes) Find

func (array Bytes) Find(value byte) int

Find searches the array for a value and returns its index or -1 if not found.

func (Bytes) FindLast

func (array Bytes) FindLast(value byte) int

FindLast searches the array in reverse order for a value and returns its index or -1 if not found.

func (Bytes) Has

func (array Bytes) Has(value byte) bool

Has returns true if the array contains the given value.

func (Bytes) HasVariantAt

func (array Bytes) HasVariantAt(offset int) bool

HasVariantAt returns true if a valid Variant value can be decoded at the byte_offset. Returns false otherwise

func (Bytes) Index

func (array Bytes) Index(idx int) byte

Index returns the element at the given index.

func (*Bytes) Insert

func (array *Bytes) Insert(idx int, value byte)

Insert inserts a new element at a given position in the array. bytehe position must be valid, or at the end of the array (idx == size()).

func (Bytes) IsEmpty

func (array Bytes) IsEmpty() bool

IsEmpty Returns true if the array is empty.

func (Bytes) Iter

func (array Bytes) Iter() iter.Seq2[int, byte]

Iter returns an iterator for the array.

func (Bytes) Len

func (array Bytes) Len() int

Len returns the number of elements in the array.

func (Bytes) RemoveAt

func (array Bytes) RemoveAt(idx int)

RemoveAt removes an element from the array by index.

func (*Bytes) Resize

func (array *Bytes) Resize(size int)

Resize sets the size of the array. If the array is grown, reserves elements at the end of the array. If the array is shrunk, truncates the array to the new size. Calling resize once and assigning the new values is faster than adding new elements one by one.

func (Bytes) Reverse

func (array Bytes) Reverse()

Reverse reverses the order of the elements in the array.

func (*Bytes) SetIndex

func (array *Bytes) SetIndex(idx int, value byte)

SetIndex sets the value of the element at the given index.

func (Bytes) Slice

func (array Bytes) Slice(begin, end int) Bytes

Slice returns a slice of the array from the given begin index to the given end index.

func (Bytes) Sort

func (array Bytes) Sort()

Sort sorts the array in ascending order. The final order is dependent on the "less than" (<) comparison between elements.

func (Bytes) ToFloat32Array

func (array Bytes) ToFloat32Array() Array[float32]

ToFloat32Array returns a copy of the data converted to a PackedFloat32Array, where each block of 4 bytes has been converted to a 32-bit floating point number. The size of the input array must be a multiple of 4 (size of 32-bit float). The size of the new array will be byte_array.size() / 4. If the original data can't be converted to 32-bit floats, the resulting data is undefined.

func (Bytes) ToFloat64Array

func (array Bytes) ToFloat64Array() Array[float64]

ToFloat64Array returns a copy of the data converted to a PackedFloat64Array, where each block of 8 bytes has been converted to a 64-bit floating point number. The size of the input array must be a multiple of 8 (size of 64-bit float). The size of the new array will be byte_array.size() / 8. If the original data can't be converted to 64-bit floats, the resulting data is undefined.

func (Bytes) ToHex

func (array Bytes) ToHex() string

ToHex returns a hexadecimal representation of this array as a String.

func (Bytes) ToInt32Array

func (array Bytes) ToInt32Array() Array[int32]

ToInt32Array returns a copy of the data converted to a PackedInt32Array, where each block of 4 bytes has been converted to a signed 32-bit integer.

The size of the input array must be a multiple of 4 (size of 32-bit integer). The size of the new array will be byte_array.size() / 4.

If the original data can't be converted to signed 32-bit integers, the resulting data is undefined.

func (Bytes) ToInt64Array

func (array Bytes) ToInt64Array() Array[int64]

ToInt64Array returns a copy of the data converted to a PackedInt64Array, where each block of 8 bytes has been converted to a signed 64-bit integer. The size of the input array must be a multiple of 8 (size of 64-bit integer). The size of the new array will be byte_array.size() / 8. If the original data can't be converted to signed 64-bit integers, the resulting data is undefined.

type CompressionMode

type CompressionMode int
const (
	CompressionFastLZ CompressionMode = iota
	CompressionDeflate
	CompressionZstandard // not implemented
	CompressionGzip
	CompressionBrotli // not implemented
)

type Strings

Strings is a packed array of readable strings.

func MakeStrings

func MakeStrings(values ...string) Strings

MakeStrings creates a new array of strings with the given values.

func (*Strings) Append

func (array *Strings) Append(value String.Readable)

Append an element to the end of the array.

func (*Strings) AppendArray

func (array *Strings) AppendArray(other Strings)

AppendArray appends all elements of another array to the end of this array.

func (Strings) BinarySearch

func (array Strings) BinarySearch(value String.Readable, before bool) int

BinarySearch returns the index of value in the sorted array. If it cannot be found, returns where value should be inserted to keep the array sorted. The algorithm used is binary search. The returned index comes before all existing elements equal to value in the array.

Note: Calling BinarySearch on an unsorted array will result in unexpected behavior. Use Sort before calling this method.

func (Strings) Bytes

func (array Strings) Bytes() Bytes

Bytes returns a Bytes array containing zero-byte seperated strings.

func (Strings) Clear

func (array Strings) Clear()

Clear clears the array. String.Readablehis is equivalent to using resize with a size of 0.

func (Strings) Count

func (array Strings) Count(value String.Readable) int

Count returns the number of times an element is in the array.

func (Strings) Duplicate

func (array Strings) Duplicate() Strings

Duplicate creates a copy of the array, and returns it.

func (Strings) Fill

func (array Strings) Fill(value String.Readable)

Fill assigns the given value to all elements in the array. String.Readablehis can typically be used together with resize to create an array with a given size and initialized elements.

func (Strings) Find

func (array Strings) Find(value String.Readable) int

Find searches the array for a value and returns its index or -1 if not found.

func (Strings) FindLast

func (array Strings) FindLast(value String.Readable) int

FindLast searches the array in reverse order for a value and returns its index or -1 if not found.

func (Strings) Has

func (array Strings) Has(value String.Readable) bool

Has returns true if the array contains the given value.

func (Strings) Index

func (array Strings) Index(idx int) String.Readable

Index returns the element at the given index.

func (*Strings) Insert

func (array *Strings) Insert(idx int, value String.Readable)

Insert inserts a new element at a given position in the array. String.Readablehe position must be valid, or at the end of the array (idx == size()).

func (Strings) IsEmpty

func (array Strings) IsEmpty() bool

IsEmpty Returns true if the array is empty.

func (Strings) Iter

func (array Strings) Iter() iter.Seq2[int, String.Readable]

Iter returns an iterator for the array.

func (Strings) Len

func (array Strings) Len() int

Len returns the number of elements in the array.

func (Strings) RemoveAt

func (array Strings) RemoveAt(idx int)

RemoveAt removes an element from the array by index.

func (*Strings) Resize

func (array *Strings) Resize(size int)

Resize sets the size of the array. If the array is grown, reserves elements at the end of the array. If the array is shrunk, truncates the array to the new size. Calling resize once and assigning the new values is faster than adding new elements one by one.

func (Strings) Reverse

func (array Strings) Reverse()

Reverse reverses the order of the elements in the array.

func (*Strings) SetIndex

func (array *Strings) SetIndex(idx int, value String.Readable)

SetIndex sets the value of the element at the given index.

func (Strings) Slice

func (array Strings) Slice(begin, end int) Strings

Slice returns a slice of the array from the given begin index to the given end index.

func (Strings) Sort

func (array Strings) Sort()

Sort sorts the array in ascending order. The final order is dependent on the "less than" (<) comparison between elements.

func (Strings) Strings

func (array Strings) Strings() []string

Strings returns a slice of strings from the array.

type Type

type Type interface {
	~byte | ~Color.RGBA | ~float32 | ~float64 | ~int32 | ~int64 | ~Vector2.XY | ~Vector3.XYZ | ~Vector4.XYZW
}

Type supported by Array.

Jump to

Keyboard shortcuts

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