pub struct SimpleArray<T, A: Allocator = TESGlobalAlloc> { /* private fields */ }
Expand description
Array whose first pointer is only a pointer to the length.
Memory layout:
┌────────────┬────────────┬────────────┬──────┬────────────┐
│ len: usize │ T[0] │ T[1] │ ... │ T[N - 1] │
└────────────┴────────────┴────────────┴──────┴────────────┘
↑
data: Unique<T> ┘
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::new();
assert_eq!(array.len(), 0);
Implementations§
Source§impl<T> SimpleArray<T>
impl<T> SimpleArray<T>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new empty SimpleArray
.
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::new();
assert_eq!(array.len(), 0);
Sourcepub fn with_capacity(count: usize) -> Self
pub fn with_capacity(count: usize) -> Self
Creates a new SimpleArray
with the specified capacity.
Uninitialized memory leads to undefined operation, so 0 is entered.
§Panics
This function may panic if the allocation fails.
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::with_capacity(5);
assert_eq!(array.len(), 5);
Source§impl<T, A> SimpleArray<T, A>where
A: Allocator,
impl<T, A> SimpleArray<T, A>where
A: Allocator,
Sourcepub const fn new_in(data: Option<Unique<T>>, alloc: A) -> Self
pub const fn new_in(data: Option<Unique<T>>, alloc: A) -> Self
Creates a new empty SimpleArray
with Allocator.
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
use stdx::alloc::Global; // Since TESAllocator is not available for CI, use Rust's.
let array: SimpleArray<i32, Global> = SimpleArray::new_in(None, Global);
assert_eq!(array.len(), 0);
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the length of the array (the number of elements currently stored).
Equivalent C++ size()
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::with_capacity(5);
assert_eq!(array.len(), 5);
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Checks if the array is empty.
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::new();
assert!(array.is_empty());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the array by deallocating its memory.
§Safety
This function performs unsafe operations to deallocate the array memory.
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array: SimpleArray<i32> = SimpleArray::with_capacity(3);
array.clear();
assert!(array.is_empty());
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Gets a reference to the element at the given index.
Return None
if the index is out of bounds.
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array = SimpleArray::with_capacity(3);
array[0] = 1;
assert_eq!(array.get(0), Some(&1));
assert_eq!(array.get(5), None);
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Gets a mutable reference to the element at the given index.
Return None
if the index is out of bounds.
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array = SimpleArray::<i32>::with_capacity(3);
if let Some(value) = array.get_mut(1) {
*value = 42;
};
assert_eq!(array[1], 42);
assert_eq!(array.get_mut(5), None);
Sourcepub const fn as_ptr(&self) -> *mut T
pub const fn as_ptr(&self) -> *mut T
Returns a raw pointer to the data.
Equivalent C++ data()
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::new();
let data = array.as_ptr();
Sourcepub const fn as_mut_slice(&mut self) -> &mut [T]
pub const fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice of the array.
§Safety
This function assumes that the array is properly initialized.
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array = SimpleArray::with_capacity(4);
let slice = array.as_mut_slice();
slice.copy_from_slice(&[1, 2, 3, 4]);
Sourcepub const fn iter(&self) -> SimpleArrayIterator<'_, T, A> ⓘ
pub const fn iter(&self) -> SimpleArrayIterator<'_, T, A> ⓘ
Creates an iterator that borrows the elements of the SimpleArray
.
This method returns an iterator over references to the elements of the array. The iterator allows you to traverse the array without consuming it, meaning the array remains usable after the iteration.
§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array = SimpleArray::with_capacity(3);
let slice = array.as_mut_slice();
slice[0] = 10;
slice[1] = 20;
slice[2] = 30;
let sum: i32 = array.iter().sum();
assert_eq!(sum, 60);