devela/data/collections/array/d1/methods/general.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
// devela::data::collections::array::d1::methods::general
//
//! 1-dimensional array general methods (Storage-independent).
//
// TOC
// - constructors
// - methods
// - T: PartialEq methods
// - Option<T> methods
use crate::{
array_from_fn, iif, Array, ElementNotFound, IndexOutOfBounds, MismatchedBounds, Storage,
};
/// # Constructors
impl<T, const CAP: usize, S: Storage> Array<T, CAP, S> {
/// Returns a new `Array` from the given primitive `array`.
pub fn new(array: [T; CAP]) -> Self {
Self { data: array.into() }
}
/// Returns a new `Array`, where each element `T` is the returned value from `f`.
///
/// See: `array::`[`from_fn`][core::array::from_fn]
pub fn from_fn<F: FnMut(usize) -> T>(f: F) -> Self {
Self { data: array_from_fn(f).into() }
}
// WAIT [array_try_from_fn](https://github.com/rust-lang/rust/issues/89379)
}
/// # Methods
impl<T, const CAP: usize, S: Storage> Array<T, CAP, S> {
/// Returns the total capacity of the array, equals `CAP`.
#[must_use]
pub const fn capacity(&self) -> usize {
CAP
}
/// Returns a shared slice containing the entire array.
#[must_use]
pub fn as_slice(&self) -> &[T] {
self.data.as_slice()
}
/// Returns an exclusive slice containing the entire array.
#[must_use]
pub fn as_mut_slice(&mut self) -> &mut [T] {
self.data.as_mut_slice()
}
/// Fills all elements of the array with the given `element`.
pub fn fill(&mut self, element: T)
where
T: Clone,
{
self.iter_mut().for_each(|i| *i = element.clone());
}
/// Fills all elements of the array with the default value.
pub fn fill_default(&mut self)
where
T: Default,
{
self.iter_mut().for_each(|i| *i = T::default());
}
}
/// # `T: PartialEq` methods
impl<T: PartialEq, const CAP: usize, S: Storage> Array<T, CAP, S> {
/// Returns `true` if the array contains `element`.
///
/// # Example
/// ```
/// # use devela::Array;
/// let a = Array::<_, 5>::new([5, 78, 42, 33, 9]);
/// assert![a.contains(&9)];
/// assert![!a.contains(&8)];
/// ```
#[must_use]
pub fn contains(&self, element: &T) -> bool {
self.iter().any(|n| n == element)
}
/// Returns `true` if the array contains `element`,
/// from the `start` index (inclusive).
///
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= CAP`.
pub fn contains_from(&self, element: &T, start: usize) -> Result<bool, IndexOutOfBounds> {
iif![start >= CAP; return Err(IndexOutOfBounds(Some(start)))];
Ok(self.iter().skip(start).any(|n| n == element))
}
/// Returns `true` if the array contains `element`,
/// from the `start` index (inclusive).
///
/// # Errors
/// Returns [`IndexOutOfBounds`] if `end >= CAP`.
pub fn contains_until(&self, element: &T, end: usize) -> Result<bool, IndexOutOfBounds> {
iif![end >= CAP; return Err(IndexOutOfBounds(Some(end)))];
Ok(self.iter().take(end + 1).any(|n| n == element))
}
/// Returns `true` if the array contains `element`,
/// between the range `start..=end` (inclusive).
///
/// # Errors
/// Returns [`IndexOutOfBounds`][MismatchedBounds:IndexOutOfBounds]
/// if either `start` or `end` `>= CAP`,
/// or [`MismatchedIndices`][MismatchedBounds::MismatchedIndices] if `start > end`.
pub fn contains_between(
&self,
element: &T,
start: usize,
end: usize,
) -> Result<bool, MismatchedBounds> {
iif![start >= CAP; return Err(MismatchedBounds::IndexOutOfBounds(Some(start)))];
iif![end >= CAP; return Err(MismatchedBounds::IndexOutOfBounds(Some(end)))];
iif![start > end; return Err(MismatchedBounds::MismatchedIndices)];
Ok(self.iter().skip(start).take(end - start + 1).any(|n| n == element))
}
/// Finds the index of the first occurrence of `element` in the array.
///
/// # Example
/// ```
/// # use devela::{Array, ElementNotFound};
/// let a = Array::<_, 5>::new([5, 78, 42, 33, 9]);
/// assert_eq![a.find_index(&9), Ok(4)];
/// assert_eq![a.find_index(&8), Err(ElementNotFound)];
/// ```
/// # Errors
/// Returns [`ElementNotFound`] if the `element` can't be found.
pub fn find_index(&self, element: &T) -> Result<usize, ElementNotFound> {
self.iter()
.enumerate()
.find_map(|(i, n)| (n == element).then_some(i))
.ok_or(ElementNotFound)
}
}
/// # `Option<T>` methods
impl<T, const CAP: usize, S: Storage> Array<Option<T>, CAP, S> {
/// Takes out some element at `index`, leaving `None` in its place.
#[must_use]
pub fn take(&mut self, index: usize) -> Option<T> {
self.get_mut(index)?.take()
}
/// Replaces some element at `index` with `value`, returning the old one.
#[must_use]
pub fn replace(&mut self, index: usize, value: T) -> Option<T> {
self.get_mut(index)?.replace(value)
}
/// Sets the element at `index` to `None`.
pub fn unset(&mut self, index: usize) {
self[index] = None;
}
/// Clears the array by setting all elements to `None`.
pub fn clear(&mut self) {
self.iter_mut().for_each(|i| *i = None);
}
/// Returns the number of `Some` elements in the array.
#[must_use]
pub fn count_some(&self) -> usize {
self.iter().filter(|opt| opt.is_some()).count()
}
/// Returns the number of `None` elements in the array.
#[must_use]
pub fn count_none(&self) -> usize {
self.iter().filter(|opt| opt.is_none()).count()
}
/// Returns the number of `None` elements in the array.
#[must_use]
pub fn is_empty(&self) -> bool {
self.iter().all(|opt| opt.is_some())
}
/// Returns the number of `None` elements in the array.
#[must_use]
pub fn is_full(&self) -> bool {
self.iter().all(|opt| opt.is_some())
}
/// Fills all `None` elements of the array with the given cloned `value`.
pub fn fill_none(&mut self, value: T)
where
T: Clone,
{
self.iter_mut().filter(|opt| opt.is_none()).for_each(|opt| *opt = Some(value.clone()));
}
/// Returns the index of the first `None` element.
///
/// # Errors
/// Returns [`ElementNotFound`] if the array is full.
pub fn first_none(&self) -> Result<usize, ElementNotFound> {
self.iter().position(|opt| opt.is_none()).ok_or(ElementNotFound)
}
/// Returns some reference to the first `None` element.
///
/// # Errors
/// Returns [`ElementNotFound`] if the array is full.
pub fn first_none_ref(&self) -> Result<&Option<T>, ElementNotFound> {
self.iter().find(|opt| opt.is_none()).ok_or(ElementNotFound)
}
/// Returns some exclusive reference to the first `None` element.
///
/// # Errors
/// Returns [`ElementNotFound`] if the array is full.
pub fn first_none_mut(&mut self) -> Result<&mut Option<T>, ElementNotFound> {
self.iter_mut().find(|opt| opt.is_none()).ok_or(ElementNotFound)
}
/// Returns the index of the first `Some` element.
///
/// # Errors
/// Returns [`ElementNotFound`] if the array is full.
pub fn first_some(&self) -> Result<usize, ElementNotFound> {
self.iter().position(|opt| opt.is_some()).ok_or(ElementNotFound)
}
/// Returns some reference to the first `Some` element
///
/// # Errors
/// Returns [`ElementNotFound`] if the array is full.
pub fn first_some_ref(&self) -> Result<&Option<T>, ElementNotFound> {
self.iter().find(|opt| opt.is_some()).ok_or(ElementNotFound)
}
/// Returns some exclusive reference to the first `Some` element.
///
/// # Errors
/// Returns [`ElementNotFound`] if the array is full.
pub fn first_some_mut(&mut self) -> Result<&mut Option<T>, ElementNotFound> {
self.iter_mut().find(|opt| opt.is_some()).ok_or(ElementNotFound)
}
}