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
use crate::SplitVec;
use std::ops::{Index, IndexMut};

impl<T> Index<usize> for SplitVec<T> {
    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::SplitVec;
    ///
    /// let mut vec = SplitVec::default();
    ///
    /// 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
            .fragment_and_inner_index(index)
            .expect("index is out of bounds");
        &self.fragments[f][i]
    }
}

impl<T> IndexMut<usize> for SplitVec<T> {
    /// 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::SplitVec;
    ///
    /// let mut vec = SplitVec::default();
    ///
    /// 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
            .fragment_and_inner_index(index)
            .expect("index is out of bounds");
        &mut self.fragments[f][i]
    }
}

impl<T> Index<(usize, usize)> for SplitVec<T> {
    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::{FragmentGrowth, SplitVec};
    ///
    /// let growth = FragmentGrowth::constant(4);
    /// let mut vec = SplitVec::with_growth(growth);
    ///
    /// 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> IndexMut<(usize, usize)> for SplitVec<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::{FragmentGrowth, SplitVec};
    ///
    /// let growth = FragmentGrowth::constant(4);
    /// let mut vec = SplitVec::with_growth(growth);
    ///
    /// 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]
    }
}