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
use crate::{Growth, SplitVec};
use core::ops::{Index, IndexMut};
impl<T, G> Index<usize> for SplitVec<T, G>
where
G: Growth,
{
type Output = T;
/// Returns a reference to the `index`-th item of the vector.
///
/// # Panics
///
/// Panics if `index` is out of bounds.
///
/// # Examples
///
/// ```
/// use orx_split_vec::*;
///
/// let mut vec = SplitVec::with_linear_growth(4);
///
/// vec.extend_from_slice(&[0, 1, 2, 3]);
///
/// assert_eq!(&1, &vec[1]);
/// assert_eq!(&3, &vec[3]);
/// // let x = &vec[4]; // panics!
/// ```
fn index(&self, index: usize) -> &Self::Output {
let (f, i) = self
.get_fragment_and_inner_indices(index)
.expect("index is out of bounds");
&self.fragments[f][i]
}
}
impl<T, G> IndexMut<usize> for SplitVec<T, G>
where
G: Growth,
{
/// Returns a mutable reference to the `index`-th item of the vector.
///
/// # Panics
///
/// Panics if `index` is out of bounds.
///
/// # Examples
///
/// ```
/// use orx_split_vec::*;
///
/// let mut vec = SplitVec::with_linear_growth(2);
///
/// vec.extend_from_slice(&[0, 1, 2, 3]);
///
/// let item2 = &mut vec[2];
/// *item2 = 42;
/// assert_eq!(vec, &[0, 1, 42, 3]);
///
/// // let x = &mut vec[4]; // panics!
/// ```
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
let (f, i) = self
.get_fragment_and_inner_indices(index)
.expect("index is out of bounds");
&mut self.fragments[f][i]
}
}
impl<T, G> Index<(usize, usize)> for SplitVec<T, G>
where
G: Growth,
{
type Output = T;
/// One can treat the split vector as a jagged array
/// and access an item with (fragment_index, inner_fragment_index)
/// if these numbers are known.
///
/// # Panics
///
/// Panics if:
///
/// * `fragment_and_inner_index.0` is not a valid fragment index; i.e., not within `0..self.fragments().len()`, or
/// * `fragment_and_inner_index.1` is not a valid index for the corresponding fragment; i.e., not within `0..self.fragments()[fragment_and_inner_index.0].len()`.
///
/// # Examples
///
/// Assume that we create a split vector with a constant growth of N elements.
/// This means that each fraction will have a capacity and max-length of N.
///
/// Then, the fragment and inner index of the element with index `i` can be computed as
/// `(i / N, i % N)`.
///
/// ```
/// use orx_split_vec::*;
///
/// let mut vec = SplitVec::with_linear_growth(2);
///
/// for i in 0..10 {
/// vec.push(i);
/// }
///
/// // layout of the data will be as follows:
/// // fragment-0: [0, 1, 2, 3]
/// // fragment-1: [4, 5, 6, 7]
/// // fragment-2: [8, 9]
///
/// assert_eq!(1, vec[(0, 1)]);
/// assert_eq!(7, vec[(1, 3)]);
/// assert_eq!(8, vec[(2, 0)]);
///
/// // since we know the layout, we can define the index transformer for direct access
/// fn fragment_and_inner_idx(index: usize) -> (usize, usize) {
/// (index / 4, index % 4)
/// }
///
/// for index in 0..vec.len() {
/// let split_access = &vec[index];
/// let direct_access = &vec[fragment_and_inner_idx(index)];
/// assert_eq!(split_access, direct_access);
/// }
///
/// ```
fn index(&self, fragment_and_inner_index: (usize, usize)) -> &Self::Output {
&self.fragments[fragment_and_inner_index.0][fragment_and_inner_index.1]
}
}
impl<T, G> IndexMut<(usize, usize)> for SplitVec<T, G>
where
G: Growth,
{
/// One can treat the split vector as a jagged array
/// and access an item with (fragment_index, inner_fragment_index)
/// if these numbers are known.
///
/// # Panics
///
/// Panics if:
///
/// * `fragment_and_inner_index.0` is not a valid fragment index; i.e., not within `0..self.fragments().len()`, or
/// * `fragment_and_inner_index.1` is not a valid index for the corresponding fragment; i.e., not within `0..self.fragments()[fragment_and_inner_index.0].len()`.
///
/// # Examples
///
/// Assume that we create a split vector with a constant growth of N elements.
/// This means that each fraction will have a capacity and max-length of N.
///
/// Then, the fragment and inner index of the element with index `i` can be computed as
/// `(i / N, i % N)`.
///
/// ```
/// use orx_split_vec::*;
///
/// let mut vec = SplitVec::with_linear_growth(2);
///
/// for i in 0..10 {
/// vec.push(i);
/// }
///
/// // layout of the data will be as follows:
/// // fragment-0: [0, 1, 2, 3]
/// // fragment-1: [4, 5, 6, 7]
/// // fragment-2: [8, 9]
///
/// vec[(0, 1)] += 100; // 1 -> 101
/// vec[(1, 3)] += 100; // 7 -> 107
/// vec[(2, 0)] += 100; // 8 -> 108
/// assert_eq!(vec, &[0, 101, 2, 3, 4, 5, 6, 107, 108, 9]);
///
/// // since we know the layout, we can define the index transformer for direct access
/// fn fragment_and_inner_idx(index: usize) -> (usize, usize) {
/// (index / 4, index % 4)
/// }
///
/// for index in 0..vec.len() {
/// let direct_access = &mut vec[fragment_and_inner_idx(index)];
/// if *direct_access < 100 {
/// *direct_access += 100;
/// }
/// }
/// assert_eq!(vec, &[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]);
///
/// ```
fn index_mut(&mut self, fragment_and_inner_index: (usize, usize)) -> &mut Self::Output {
&mut self.fragments[fragment_and_inner_index.0][fragment_and_inner_index.1]
}
}
#[cfg(test)]
mod tests {
use crate::test_all_growth_types;
use crate::*;
use alloc::vec::Vec;
#[test]
fn index() {
fn test<G: Growth>(mut vec: SplitVec<usize, G>) {
vec.extend_from_slice(&(0..42).collect::<Vec<_>>());
vec.extend_from_slice(&(42..63).collect::<Vec<_>>());
vec.extend_from_slice(&(63..100).collect::<Vec<_>>());
assert_eq!(100, vec.len());
for i in 0..100 {
assert_eq!(i, vec[i]);
vec[i] += 100;
}
for i in 0..100 {
assert_eq!(100 + i, vec[i]);
}
}
test_all_growth_types!(test);
}
#[test]
fn double_indices() {
fn test<G: Growth>(mut vec: SplitVec<usize, G>) {
vec.extend_from_slice(&(0..42).collect::<Vec<_>>());
vec.extend_from_slice(&(42..63).collect::<Vec<_>>());
vec.extend_from_slice(&(63..100).collect::<Vec<_>>());
assert_eq!(100, vec.len());
for i in 0..100 {
let (f, j) = vec.get_fragment_and_inner_indices(i).expect("is-some");
assert_eq!(i, vec[(f, j)]);
vec[(f, j)] += 100;
}
for i in 0..100 {
let (f, j) = vec.get_fragment_and_inner_indices(i).expect("is-some");
assert_eq!(100 + i, vec[(f, j)]);
}
}
test_all_growth_types!(test);
}
}