struct Array<Element>A growable, copy-on-write sequence with value semantics.
storage[0..<count] is initialized and count <= capacity. Copies share storage until mutation; every mutating operation first makes the buffer unique. Indexing traps rather than returning none.
Properties
Methods
func get(_ index: Int) -> &Element
Borrows the element at index, panicking when it is out of bounds.
View Source
pub func get(_ index: Int) -> &Element {
self._check_index(index: index)
self._get_unchecked(index: index)
}mut func push(consume _ value: Element) -> ()
Appends value, doubling capacity when the initialized prefix is full.
The buffer is made unique before the write. Amortized time is O(1), with an O(count) copy when growth or copy-on-write detachment is required.
View Source
pub mut func push(consume _ value: Element) -> () {
let new_capacity = if self.capacity == 0 {
1
} else {
self.capacity * 2
}
if self.count >= self.capacity {
let fresh = self.allocate_with_capacity(capacity: new_capacity)
self.capacity = new_capacity
self.storage = fresh
} else {
self.uniqued_storage()
}
self._init_slot(index: self.count, value: value)
self.count = self.count + 1
}mut func set(index: Int, consume value: Element) -> Void
Overwrite: the replaced element is loaded out and dropped at scope exit (deep, per its own deinit) after the new value is written.
View Source
pub mut func set(index: Int, consume value: Element) -> Void {
self._check_index(index: index)
self.uniqued_storage()
let ptr: RawPtr = #_ir(self.storage.base, index) { %? = gep Element $0 $1 }
let old: Element = _load<Element>(ptr: ptr)
_store<Element>(ptr: ptr, value: value)
}mut func swap(i: Int, j: Int) -> Void
Exchanges two initialized elements after detaching shared storage.
Both indices are checked before mutation; either invalid index performs 'panic and leaves the array unchanged.
View Source
pub mut func swap(i: Int, j: Int) -> Void {
self._check_index(index: i)
self._check_index(index: j)
if i == j { return () }
let storage = self.uniqued_storage()
let a: RawPtr = #_ir(storage.base, i) { %? = gep Element $0 $1 }
let b: RawPtr = #_ir(storage.base, j) { %? = gep Element $0 $1 }
_swap<Element>(a: a, b: b)
}consuming func map<T>(consume fn: (Element) -> T) -> Map<ArrayIntoIterator<Element>, T>
Consumes the array into a lazy mapping iterator.
Elements move out in index order and fn runs only when the returned iterator advances. Call to_array on that iterator for eager collection.
View Source
pub consuming func map<T>(consume fn: (Element) -> T) -> Map<ArrayIntoIterator<Element>, T> {
self.into_iter().map(fn: fn)
}consuming func skip(count: Int) -> ArrayIntoIterator<Element>
Consumes the array and eagerly discards its first count elements.
Discarded elements are dropped as they are read. Counts at or below zero preserve every element; counts beyond the length return an exhausted iterator.
View Source
pub consuming func skip(count: Int) -> ArrayIntoIterator<Element> {
self.into_iter().skip(count: count)
}func last() -> Optional<&Element>
Returns the final value, or none when the collection is empty.
View Source
pub func last() -> Optional<&Element> {
if self.count == 0 { return Optional.none }
Optional.some(self.get(self.count - 1))
}mut func pop() -> Element?
The popped slot becomes unreachable before the loaded element's owner escapes, so CoW sharers keep their copy: unique the buffer first, exactly like into_iter.
View Source
pub mut func pop() -> Element? {
if self.count == 0 { return Optional.none }
self.uniqued_storage()
let index = self.count - 1
let ptr: RawPtr = #_ir(self.storage.base, index) { %? = gep Element $0 $1 }
let element: Element = _load<Element>(ptr: ptr)
self.count = index
Optional.some(element)
}func is_empty() -> Bool
Returns whether the collection contains no values.
View Source
pub func is_empty() -> Bool {
self.count == 0
}