pub struct Array<X: Extension = Never> { /* private fields */ }Expand description
A PHP array: an ordered map from ArrayKey to Value.
Packed (a Vec, no stored keys) while it is a list 0, 1, 2, ... whose
next free index is its length; an ordered hash otherwise. The switch is
invisible. Semantics follow the engine: assigning an existing key keeps
its position, a removed key assigned again goes to the end, and push
uses the next free integer key (nNextFreeElement): one past the largest
integer key inserted, except that the engine’s packed arrays (integer keys
in ascending order, possibly with holes) set it to one past the key they
append, which lowers it after trailing elements were removed
([0, 1, 2], unset 1 and 2, $a[1] = x, then $a[] uses 2). That
state is tracked as the engine does (nNumUsed, nTableSize), with the
table size an array of n elements is created with (max(8, 2^k >= n)).
Copy-on-write, like the engine’s arrays: a clone shares the elements (an atomic reference count, so values can cross threads) and the first mutation of a shared array copies it. Mutating a clone never changes the original. A new empty array allocates nothing.
Implementations§
Source§impl<X: Extension> Array<X>
impl<X: Extension> Array<X>
Sourcepub fn with_capacity(n: usize) -> Self
pub fn with_capacity(n: usize) -> Self
An empty array with room for n list elements.
Sourcepub fn from_entries(
entries: impl IntoIterator<Item = (ArrayKey, Value<X>)>,
next: Option<i64>,
) -> Self
pub fn from_entries( entries: impl IntoIterator<Item = (ArrayKey, Value<X>)>, next: Option<i64>, ) -> Self
An array with these entries (in order, keys as given) and this next free index.
Sourcepub fn id(&self) -> Option<usize>
pub fn id(&self) -> Option<usize>
An identity for the elements: the same for clones that share them
and never reused while one of them is alive (a write to a shared
array copies it first). None for a new empty array.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves room for additional more elements (nothing observable changes).
Sourcepub fn next_index(&self) -> Option<i64>
pub fn next_index(&self) -> Option<i64>
The key push would use, as PHP tracks it (None: no integer key yet, so 0).
Sourcepub fn get(&self, k: &ArrayKey) -> Option<&Value<X>>
pub fn get(&self, k: &ArrayKey) -> Option<&Value<X>>
The value at k, if any (the key as given, not normalised).
Sourcepub fn get_ref(&self, k: KeyRef<'_>) -> Option<&Value<X>>
pub fn get_ref(&self, k: KeyRef<'_>) -> Option<&Value<X>>
The value at a borrowed key, if any (the key as given, not normalised).
Sourcepub fn get_str(&self, k: &str) -> Option<&Value<X>>
pub fn get_str(&self, k: &str) -> Option<&Value<X>>
$a["..."]: the key is normalised ("7" finds 7).
Sourcepub fn get_mut(&mut self, k: &ArrayKey) -> Option<&mut Value<X>>
pub fn get_mut(&mut self, k: &ArrayKey) -> Option<&mut Value<X>>
The value at k, mutably, if any (a shared array is copied first).
Sourcepub fn contains_key(&self, k: &ArrayKey) -> bool
pub fn contains_key(&self, k: &ArrayKey) -> bool
Whether k is set.
Sourcepub fn set(&mut self, k: ArrayKey, v: Value<X>) -> Option<Value<X>>
pub fn set(&mut self, k: ArrayKey, v: Value<X>) -> Option<Value<X>>
$a[$k] = $v (the key as given, see ArrayKey::normalize): an
existing key keeps its position. Returns the previous value.
Sourcepub fn remove(&mut self, k: &ArrayKey) -> Option<Value<X>>
pub fn remove(&mut self, k: &ArrayKey) -> Option<Value<X>>
unset($a[$k]): the key, if set again, goes to the end.
Sourcepub fn map_values(
&self,
f: impl FnMut(KeyRef<'_>, &Value<X>) -> Value<X>,
) -> Array<X>
pub fn map_values( &self, f: impl FnMut(KeyRef<'_>, &Value<X>) -> Value<X>, ) -> Array<X>
A new array with the same keys, in the same order, and the value f
makes of each entry (array_map() with one array, which keeps the
keys): the same array in every other respect, next free index
included. No key is copied into a new table or looked up again.
Sourcepub fn values(
&self,
) -> impl ExactSizeIterator<Item = &Value<X>> + DoubleEndedIterator
pub fn values( &self, ) -> impl ExactSizeIterator<Item = &Value<X>> + DoubleEndedIterator
The values in order.
Sourcepub fn keys(&self) -> impl ExactSizeIterator<Item = KeyRef<'_>>
pub fn keys(&self) -> impl ExactSizeIterator<Item = KeyRef<'_>>
The keys in order.
Trait Implementations§
Source§impl<X: Extension> Drop for Array<X>
impl<X: Extension> Drop for Array<X>
Source§impl<X: Extension> FromIterator<Value<X>> for Array<X>
A list.
impl<X: Extension> FromIterator<Value<X>> for Array<X>
A list.
Source§fn from_iter<T: IntoIterator<Item = Value<X>>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = Value<X>>>(iter: T) -> Self
A list of the values.