blok/types/layer/partial/
mod.rs

1
2pub mod block;
3pub mod row;
4
5pub mod helpers;
6use helpers::*;
7
8
9use crate::{ Block, Layer };
10
11/// Methods for partial data access:
12impl<B: Block> Layer<B> {
13
14    /// Get a vector of references to consecutive blocks.
15    /// Returns None if the range does not exist in the layer.
16    /// Use this for operations on a collection of blocks, not for building layer structure.
17    /// (Adding to this vector will not add blocks to the layer.)
18    pub fn get_range_ref(
19        &self,
20        start: usize,
21        end: usize,
22    ) -> Option<Vec<&B>> {
23
24        // Check for bounds.
25        if !range_boundary_check_helper(self.blocks.len(), start, end) {
26            return None
27        }
28
29        // No need to repeat range checks.
30        let blocks = self.blocks[start..=end].iter().collect();
31        Some(blocks)
32    }
33
34    /// Get a vector of mutable references to consecutive blocks.
35    /// Returns None if the range does not exist in the layer.
36    /// Use this for operations on a collection of blocks, not for building layer structure.
37    /// (Adding to this vector will not add blocks to the layer.)
38    pub fn get_range_mut(
39        &mut self,
40        start: usize,
41        end: usize
42    ) -> Option<Vec<&mut B>> {
43
44        // Check for bounds.
45        if !range_boundary_check_helper(self.blocks.len(), start, end) {
46            return None
47        }
48
49        // No need to repeat range checks.
50        let blocks = self.blocks[start..=end].iter_mut().collect();
51        Some(blocks)
52    }
53
54    /// Get a matrix of references to all blocks.
55    /// Returns an empty Vec if the layer is empty.
56    /// Use this for operations on a collection of blocks, not for building layer structure.
57    /// (Adding to this vector will not add blocks to the layer.)
58    pub fn get_all_ref(&mut self) -> Vec<Vec<&B>> {
59        let blocks_ref: Vec<_> = self.blocks.iter().collect();
60        // Use layout to represent layer structure as nested vectors.
61        collection_organization_helper::<&B>(self.layout(), blocks_ref)
62    }
63
64
65    /// Get a matrix of mutable references to all blocks.
66    /// Returns an empty Vec if the layer is empty.
67    /// Use this for operations on a collection of blocks, not for building layer structure.
68    /// (Adding to this vector will not add blocks to the layer.)
69    pub fn get_all_mut(&mut self) -> Vec<Vec<&mut B>> {
70        let layout = self.layout().clone(); // Clone for borrowing reasons.
71        let blocks_ref: Vec<_> = self.blocks.iter_mut().collect();
72        // Use layout to represent layer structure as nested vectors.
73        collection_organization_helper::<&mut B>(&layout, blocks_ref)
74    }
75
76}
77
78
79
80/*  UNIT TESTS  */
81#[cfg(test)] mod test {
82
83    use super::*;
84    use crate::{ Block, Layer, Layout };
85    use crate::types::layer::test::test_layer;
86
87    ///
88    #[test] fn get_range_test() {
89
90        // Test layer layout is [1, 2]
91        let mut layer = test_layer();
92
93        let range_ref = layer.get_range_ref(0, 2);
94        assert!(range_ref.is_some());
95
96        let range_mut = layer.get_range_mut(1, 1);
97        assert!(range_mut.is_some());
98
99        let bad_range_ref = layer.get_range_ref(0, 3);
100        assert!(bad_range_ref.is_none());
101
102        let bad_range_mut = layer.get_range_mut(3, 2);
103        assert!(bad_range_mut.is_none());
104
105    }
106
107    ///
108    #[test] fn get_all_test() {
109
110        // Test layer layout is [1, 2]
111        let mut layer = test_layer();
112
113        let ref_collection = layer.get_all_ref(); 
114        // Should mirror the structure of the test layer (1, 2):
115        assert_eq!(ref_collection.len(), 2);
116        assert_eq!(ref_collection[0].len(), 1);
117        assert_eq!(ref_collection[1].len(), 2);
118
119        let mut_collection = layer.get_all_mut(); 
120        // Should mirror the structure of the test layer (1, 2):
121        assert_eq!(mut_collection.len(), 2);
122        assert_eq!(mut_collection[0].len(), 1);
123        assert_eq!(mut_collection[1].len(), 2);
124    }
125}
126