PackedInt64Array
64 位整数紧缩数组。
描述
专门设计用于保存 64 位整数值的数组。紧密打包数据,因此可为大型数组节省内存。
注意:该类型存储有符号的 64 位整数,这意味着它可以在区间 [-2^63, 2^63 - 1] 内取值,即 [-9223372036854775808, 9223372036854775807]。超出这些界限将出现回绕。如果只需要紧密打包 32 位整数,请参阅 PackedInt32Array 以获得更节省内存的替代方案。
紧缩数组、类型化数组和非类型化数组之间的差异:与同类型的类型化数组相比,紧缩数组的迭代和修改速度通常更快(例如 PackedInt64Array 与 Array[int])。此外,紧缩数组消耗的内存更少。缺点是紧缩数组不太灵活,因为它们不提供类似 Array.map() 的许多便捷方法。不过类型化数组的迭代和修改速度比非类型化数组更快。
注意:紧缩数组始终通过引用传递。要获取可以独立于原始数组进行修改的数组副本,请使用 duplicate()。内置属性和方法并非如此,它们返回的是紧缩数组的副本,对其进行修改不会影响原值。更新此类内置属性请修改返回的数组,然后将其重新赋值给该属性。
备注
通过 C# 使用该 API 时会有显著不同,详见 C# API 与 GDScript 的差异。
构造函数
PackedInt64Array(from: PackedInt64Array) |
|
PackedInt64Array(from: Array) |
方法
void |
append_array(array: PackedInt64Array) |
void |
clear() |
duplicate() const |
|
void |
|
is_empty() const |
|
void |
|
void |
reverse() |
void |
|
size() const |
|
void |
sort() |
to_byte_array() const |
运算符
operator !=(right: PackedInt64Array) |
|
operator +(right: PackedInt64Array) |
|
operator ==(right: PackedInt64Array) |
|
operator [](index: int) |
构造函数说明
PackedInt64Array PackedInt64Array() 🔗
构造空的 PackedInt64Array。
PackedInt64Array PackedInt64Array(from: PackedInt64Array)
构造给定 PackedInt64Array 的副本。
PackedInt64Array PackedInt64Array(from: Array)
构造新 PackedInt64Array。你还可以传入通用 Array 进行转换。
方法说明
向数组末尾追加一个元素(push_back() 的别名)。
void append_array(array: PackedInt64Array) 🔗
在该数组的末尾追加一个 PackedInt64Array。
int bsearch(value: int, before: bool = true) const 🔗
使用二进法查找已有值的索引(如果该值尚未存在于数组中,则为保持排序顺序的插入索引)。传递 before 说明符是可选的。如果该参数为 false,则返回的索引位于数组中该值的所有已有的条目之后。
注意:在未排序的数组上调用 bsearch() 会产生预料之外的行为。
void clear() 🔗
清空数组。相当于调用 resize() 时指定大小为 0。
返回元素在数组中出现的次数。
PackedInt64Array duplicate() const 🔗
创建该数组的副本,并将该副本返回。
在数组中移除首次出现的某个值并返回 true。如果数组中不存在该值,则不会发生任何事情,返回 false。要按照索引移除元素,请改用 remove_at()。
将数组中的所有元素都设为给定的值。通常与 resize() 一起使用,创建给定大小的数组并初始化元素。
int find(value: int, from: int = 0) const 🔗
在数组中搜索值并返回其索引,如果未找到则返回 -1 。可选地,可以传递起始搜索索引。
Returns the 64-bit integer at the given index in the array. If index is out-of-bounds or negative, this method fails and returns 0.
This method is similar (but not identical) to the [] operator. Most notably, when this method fails, it doesn't pause project execution if run from the editor.
如果该数组包含 value,则返回 true。
int insert(at_index: int, value: int) 🔗
在数组中的给定位置插入新的整数。位置必须有效,或者位于数组末尾(idx == size())。
该数组为空时,返回 true。
将一个值添加到数组中。
从数组中删除位于索引的元素。
设置数组的大小。如果数组增大,则预留数组末端的元素。如果数组缩小,则将数组截断到新的大小。调用一次 resize() 然后赋值比逐个添加新元素要快。
成功时返回 @GlobalScope.OK,失败时返回下列 Error 常量:大小为负数则返回 @GlobalScope.ERR_INVALID_PARAMETER,分配失败则返回 @GlobalScope.ERR_OUT_OF_MEMORY。请使用 size() 获取调整后的实际大小。
void reverse() 🔗
将数组中的元素逆序排列。
int rfind(value: int, from: int = -1) const 🔗
逆序搜索数组。还可以传递起始搜索位置索引。如果为负,则起始索引被视为相对于数组的结尾。
void set(index: int, value: int) 🔗
更改给定索引处的整数。
返回数组中元素的个数。
PackedInt64Array slice(begin: int, end: int = 2147483647) const 🔗
返回该 PackedInt64Array 的切片,是从 begin(含)到 end(不含)的全新 PackedInt64Array。
begin 和 end 的绝对值会按数组大小进行限制,所以 end 的默认值会切到数组大小为止(即 arr.slice(1) 是 arr.slice(1, arr.size()) 的简写)。
如果 begin 或 end 为负,则表示相对于数组的末尾(即 arr.slice(0, -2) 是 arr.slice(0, arr.size() - 2) 的简写)。
void sort() 🔗
将该数组中的元素按升序排列。
PackedByteArray to_byte_array() const 🔗
返回数据的副本,将其中的每个元素都编码为 8 个字节,放入 PackedByteArray 中。
新数组的大小为 int64_array.size() * 8。
运算符说明
bool operator !=(right: PackedInt64Array) 🔗
如果数组内容不同,则返回 true。
PackedInt64Array operator +(right: PackedInt64Array) 🔗
返回新的 PackedInt64Array,新数组的内容为此数组在末尾加上 right。为了提高性能,请考虑改用 append_array()。
bool operator ==(right: PackedInt64Array) 🔗
如果两个数组的内容相同,即对应索引号的整数相等,则返回 true。
返回索引 index 处的 int。负数索引可以用来从末尾开始访问元素。使用越界的数组索引会产生错误。