pub struct DimensionShiftableBuffer<T: PartialOrd + Clone> { /* private fields */ }
Implementations§
Source§impl<T: PartialOrd + Clone> DimensionShiftableBuffer<T>
impl<T: PartialOrd + Clone> DimensionShiftableBuffer<T>
Sourcepub fn new(
entity: Vec<T>,
dimension: usize,
) -> Result<DimensionShiftableBuffer<T>, DimensionShiftableBufferError>
pub fn new( entity: Vec<T>, dimension: usize, ) -> Result<DimensionShiftableBuffer<T>, DimensionShiftableBufferError>
Examples found in repository?
examples/example1.rs (line 6)
3fn example_u8()
4{
5 // make a 2d-empty DimensionShiftableBuffer
6 let mut dsb = DimensionShiftableBuffer::<u8>::new(vec![], 2).unwrap();
7 // push a 2d-datum
8 dsb.push(&[0u8, 1]).unwrap();
9 // push a 2d-datum
10 dsb.push(&[2u8, 3]).unwrap();
11 // append a 2d-datum sequence
12 dsb.append(&[4u8, 5, 6, 7, 8, 9, 10, 11]).unwrap();
13 for index in 0..dsb.len().unwrap()
14 {
15 // get a 2d slice
16 assert_eq!(dsb.get(index).unwrap(), &[index as u8 * 2, index as u8 * 2 + 1]);
17 }
18 // shift dimension to 3 from 2
19 dsb.shift_dimension(3).unwrap();
20 // push a 3d-datum
21 dsb.push(&[12u8, 13, 14]).unwrap();
22 // get a 3d-datum
23 assert_eq!(dsb.get(0).unwrap(), &[0u8, 1, 2]);
24 assert_eq!(dsb.get(1).unwrap(), &[3u8, 4, 5]);
25 assert_eq!(dsb.get(2).unwrap(), &[6u8, 7, 8]);
26 assert_eq!(dsb.get(3).unwrap(), &[9u8, 10, 11]);
27 assert_eq!(dsb.get(4).unwrap(), &[12u8, 13, 14]);
28 // get a linear slice
29 let linear_slice = dsb.as_slice();
30 assert_eq!(linear_slice, &[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
31}
More examples
examples/example2.rs (line 12)
3fn example_f32()
4{
5 // Index of 1D => 0 |1 |2 |3 |4 |5 |6 |7 |8 |
6 // Index of 2D => 0,0 0,1 |1,0 1,1 |2,0 2,1 |3,0 3,1 |4,0 4,1 |
7 // Index of 3D => 0,0 0,1 0,2 |1,0 1,1 1,2 |2,0 2,1 2,2 |
8 // Index of 7D => 0,0 0,1 0,2 0,3 0,4 0,5 0,6 |1,0 1,1 1,2 1,3 1,4 1,5 1,6 |
9 let entity = vec![1.1f32, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3];
10 let dimension = 1;
11
12 let mut dsb = DimensionShiftableBuffer::<f32>::new(entity, dimension).unwrap();
13
14 let d1_i3 = dsb.get(3).unwrap();
15 assert_eq!(d1_i3, [2.1f32]);
16
17 dsb.shift_dimension(3).unwrap();
18
19 let d3_i2 = dsb.get(2).unwrap();
20 assert_eq!(d3_i2, [3.1f32, 3.2, 3.3]);
21
22 match dsb.shift_dimension(2)
23 {
24 Ok(_) => panic!("Detect an unexpected false-ok."),
25 Err(_) => eprintln!("Expected err.")
26 }
27
28 dsb.shift_dimension_truncate(2).unwrap();
29
30 let d2_i1 = dsb.get(1).unwrap();
31 assert_eq!(d2_i1, [1.3f32, 2.1]);
32
33 dsb.shift_dimension_padding(7, std::f32::NAN).unwrap();
34 let d7_i1 = dsb.get(1).unwrap();
35 assert_eq!(
36 // rounding, just easily
37 format!("{:?}", d7_i1),
38 format!("{:?}", &[
39 // [0] of 7-d
40 3.2f32,
41 // [1] of 7-d, truncated at .shift_dimension.truncate(2) -> padding at .shift_dimension_padding(7)
42 std::f32::NAN,
43 // [2] of 7-d, padding at .shift_dimension_padding(7)
44 std::f32::NAN,
45 std::f32::NAN,
46 std::f32::NAN,
47 std::f32::NAN,
48 std::f32::NAN
49 ])
50 )
51}
Sourcepub fn shift_dimension(
&mut self,
new_dimension: usize,
) -> Result<(), DimensionShiftableBufferError>
pub fn shift_dimension( &mut self, new_dimension: usize, ) -> Result<(), DimensionShiftableBufferError>
Examples found in repository?
examples/example1.rs (line 19)
3fn example_u8()
4{
5 // make a 2d-empty DimensionShiftableBuffer
6 let mut dsb = DimensionShiftableBuffer::<u8>::new(vec![], 2).unwrap();
7 // push a 2d-datum
8 dsb.push(&[0u8, 1]).unwrap();
9 // push a 2d-datum
10 dsb.push(&[2u8, 3]).unwrap();
11 // append a 2d-datum sequence
12 dsb.append(&[4u8, 5, 6, 7, 8, 9, 10, 11]).unwrap();
13 for index in 0..dsb.len().unwrap()
14 {
15 // get a 2d slice
16 assert_eq!(dsb.get(index).unwrap(), &[index as u8 * 2, index as u8 * 2 + 1]);
17 }
18 // shift dimension to 3 from 2
19 dsb.shift_dimension(3).unwrap();
20 // push a 3d-datum
21 dsb.push(&[12u8, 13, 14]).unwrap();
22 // get a 3d-datum
23 assert_eq!(dsb.get(0).unwrap(), &[0u8, 1, 2]);
24 assert_eq!(dsb.get(1).unwrap(), &[3u8, 4, 5]);
25 assert_eq!(dsb.get(2).unwrap(), &[6u8, 7, 8]);
26 assert_eq!(dsb.get(3).unwrap(), &[9u8, 10, 11]);
27 assert_eq!(dsb.get(4).unwrap(), &[12u8, 13, 14]);
28 // get a linear slice
29 let linear_slice = dsb.as_slice();
30 assert_eq!(linear_slice, &[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
31}
More examples
examples/example2.rs (line 17)
3fn example_f32()
4{
5 // Index of 1D => 0 |1 |2 |3 |4 |5 |6 |7 |8 |
6 // Index of 2D => 0,0 0,1 |1,0 1,1 |2,0 2,1 |3,0 3,1 |4,0 4,1 |
7 // Index of 3D => 0,0 0,1 0,2 |1,0 1,1 1,2 |2,0 2,1 2,2 |
8 // Index of 7D => 0,0 0,1 0,2 0,3 0,4 0,5 0,6 |1,0 1,1 1,2 1,3 1,4 1,5 1,6 |
9 let entity = vec![1.1f32, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3];
10 let dimension = 1;
11
12 let mut dsb = DimensionShiftableBuffer::<f32>::new(entity, dimension).unwrap();
13
14 let d1_i3 = dsb.get(3).unwrap();
15 assert_eq!(d1_i3, [2.1f32]);
16
17 dsb.shift_dimension(3).unwrap();
18
19 let d3_i2 = dsb.get(2).unwrap();
20 assert_eq!(d3_i2, [3.1f32, 3.2, 3.3]);
21
22 match dsb.shift_dimension(2)
23 {
24 Ok(_) => panic!("Detect an unexpected false-ok."),
25 Err(_) => eprintln!("Expected err.")
26 }
27
28 dsb.shift_dimension_truncate(2).unwrap();
29
30 let d2_i1 = dsb.get(1).unwrap();
31 assert_eq!(d2_i1, [1.3f32, 2.1]);
32
33 dsb.shift_dimension_padding(7, std::f32::NAN).unwrap();
34 let d7_i1 = dsb.get(1).unwrap();
35 assert_eq!(
36 // rounding, just easily
37 format!("{:?}", d7_i1),
38 format!("{:?}", &[
39 // [0] of 7-d
40 3.2f32,
41 // [1] of 7-d, truncated at .shift_dimension.truncate(2) -> padding at .shift_dimension_padding(7)
42 std::f32::NAN,
43 // [2] of 7-d, padding at .shift_dimension_padding(7)
44 std::f32::NAN,
45 std::f32::NAN,
46 std::f32::NAN,
47 std::f32::NAN,
48 std::f32::NAN
49 ])
50 )
51}
Sourcepub fn shift_dimension_truncate(
&mut self,
new_dimension: usize,
) -> Result<(), DimensionShiftableBufferError>
pub fn shift_dimension_truncate( &mut self, new_dimension: usize, ) -> Result<(), DimensionShiftableBufferError>
Examples found in repository?
examples/example2.rs (line 28)
3fn example_f32()
4{
5 // Index of 1D => 0 |1 |2 |3 |4 |5 |6 |7 |8 |
6 // Index of 2D => 0,0 0,1 |1,0 1,1 |2,0 2,1 |3,0 3,1 |4,0 4,1 |
7 // Index of 3D => 0,0 0,1 0,2 |1,0 1,1 1,2 |2,0 2,1 2,2 |
8 // Index of 7D => 0,0 0,1 0,2 0,3 0,4 0,5 0,6 |1,0 1,1 1,2 1,3 1,4 1,5 1,6 |
9 let entity = vec![1.1f32, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3];
10 let dimension = 1;
11
12 let mut dsb = DimensionShiftableBuffer::<f32>::new(entity, dimension).unwrap();
13
14 let d1_i3 = dsb.get(3).unwrap();
15 assert_eq!(d1_i3, [2.1f32]);
16
17 dsb.shift_dimension(3).unwrap();
18
19 let d3_i2 = dsb.get(2).unwrap();
20 assert_eq!(d3_i2, [3.1f32, 3.2, 3.3]);
21
22 match dsb.shift_dimension(2)
23 {
24 Ok(_) => panic!("Detect an unexpected false-ok."),
25 Err(_) => eprintln!("Expected err.")
26 }
27
28 dsb.shift_dimension_truncate(2).unwrap();
29
30 let d2_i1 = dsb.get(1).unwrap();
31 assert_eq!(d2_i1, [1.3f32, 2.1]);
32
33 dsb.shift_dimension_padding(7, std::f32::NAN).unwrap();
34 let d7_i1 = dsb.get(1).unwrap();
35 assert_eq!(
36 // rounding, just easily
37 format!("{:?}", d7_i1),
38 format!("{:?}", &[
39 // [0] of 7-d
40 3.2f32,
41 // [1] of 7-d, truncated at .shift_dimension.truncate(2) -> padding at .shift_dimension_padding(7)
42 std::f32::NAN,
43 // [2] of 7-d, padding at .shift_dimension_padding(7)
44 std::f32::NAN,
45 std::f32::NAN,
46 std::f32::NAN,
47 std::f32::NAN,
48 std::f32::NAN
49 ])
50 )
51}
Sourcepub fn shift_dimension_padding(
&mut self,
new_dimension: usize,
padding_value: T,
) -> Result<(), DimensionShiftableBufferError>
pub fn shift_dimension_padding( &mut self, new_dimension: usize, padding_value: T, ) -> Result<(), DimensionShiftableBufferError>
Examples found in repository?
examples/example2.rs (line 33)
3fn example_f32()
4{
5 // Index of 1D => 0 |1 |2 |3 |4 |5 |6 |7 |8 |
6 // Index of 2D => 0,0 0,1 |1,0 1,1 |2,0 2,1 |3,0 3,1 |4,0 4,1 |
7 // Index of 3D => 0,0 0,1 0,2 |1,0 1,1 1,2 |2,0 2,1 2,2 |
8 // Index of 7D => 0,0 0,1 0,2 0,3 0,4 0,5 0,6 |1,0 1,1 1,2 1,3 1,4 1,5 1,6 |
9 let entity = vec![1.1f32, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3];
10 let dimension = 1;
11
12 let mut dsb = DimensionShiftableBuffer::<f32>::new(entity, dimension).unwrap();
13
14 let d1_i3 = dsb.get(3).unwrap();
15 assert_eq!(d1_i3, [2.1f32]);
16
17 dsb.shift_dimension(3).unwrap();
18
19 let d3_i2 = dsb.get(2).unwrap();
20 assert_eq!(d3_i2, [3.1f32, 3.2, 3.3]);
21
22 match dsb.shift_dimension(2)
23 {
24 Ok(_) => panic!("Detect an unexpected false-ok."),
25 Err(_) => eprintln!("Expected err.")
26 }
27
28 dsb.shift_dimension_truncate(2).unwrap();
29
30 let d2_i1 = dsb.get(1).unwrap();
31 assert_eq!(d2_i1, [1.3f32, 2.1]);
32
33 dsb.shift_dimension_padding(7, std::f32::NAN).unwrap();
34 let d7_i1 = dsb.get(1).unwrap();
35 assert_eq!(
36 // rounding, just easily
37 format!("{:?}", d7_i1),
38 format!("{:?}", &[
39 // [0] of 7-d
40 3.2f32,
41 // [1] of 7-d, truncated at .shift_dimension.truncate(2) -> padding at .shift_dimension_padding(7)
42 std::f32::NAN,
43 // [2] of 7-d, padding at .shift_dimension_padding(7)
44 std::f32::NAN,
45 std::f32::NAN,
46 std::f32::NAN,
47 std::f32::NAN,
48 std::f32::NAN
49 ])
50 )
51}
Sourcepub fn for_each<F: FnMut(&mut T)>(&mut self, f: F) -> &Self
pub fn for_each<F: FnMut(&mut T)>(&mut self, f: F) -> &Self
Examples found in repository?
examples/example3.rs (line 15)
7fn example_csv()
8{
9 let delimiter = " ";
10 let source = "0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0";
11 let dimension = 3;
12 // construct
13 let mut dsb = DimensionShiftableBuffer::<f32>::from_csv(source, dimension, delimiter).unwrap();
14 // modify-1
15 dsb.for_each(|v| *v += 0.1);
16 // modify-2
17 for n in 0..dsb.len().unwrap()
18 {
19 for v in dsb.get_mut(n).unwrap()
20 {
21 *v *= *v;
22 }
23 }
24 // enumerate
25 for i in 0..18
26 // maybe, 0..5 will ok, 6..18 will err
27 {
28 match dsb.get(i)
29 {
30 Ok(dimensional_elements) =>
31 {
32 for (i_sub, actual) in dimensional_elements.iter().enumerate()
33 {
34 let i_flatten = i * dimension as usize + i_sub;
35 let expected = (i_flatten as f32 + 0.1) * (i_flatten as f32 + 0.1);
36 assert_eq!(actual, &expected);
37 }
38 },
39 Err(e) =>
40 {
41 match e
42 {
43 DimensionShiftableBufferError::OutOfRange {
44 limit: _,
45 requested: _
46 } => (),
47 _ => panic!("unexpected err: {:?}", e)
48 }
49 },
50 }
51 }
52 println!("<<print CSV, as a sequential slice>>\n{}", dsb.to_csv(", "));
53 println!(
54 "<<print CSV, as a dimmensional slices>>\n{}",
55 dsb.to_csv_dimensional(", ", "\n").unwrap()
56 );
57}
Sourcepub fn move_entity(self) -> Vec<T>
pub fn move_entity(self) -> Vec<T>
WARNING, It’s MOVE and drop self.
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Examples found in repository?
examples/example1.rs (line 29)
3fn example_u8()
4{
5 // make a 2d-empty DimensionShiftableBuffer
6 let mut dsb = DimensionShiftableBuffer::<u8>::new(vec![], 2).unwrap();
7 // push a 2d-datum
8 dsb.push(&[0u8, 1]).unwrap();
9 // push a 2d-datum
10 dsb.push(&[2u8, 3]).unwrap();
11 // append a 2d-datum sequence
12 dsb.append(&[4u8, 5, 6, 7, 8, 9, 10, 11]).unwrap();
13 for index in 0..dsb.len().unwrap()
14 {
15 // get a 2d slice
16 assert_eq!(dsb.get(index).unwrap(), &[index as u8 * 2, index as u8 * 2 + 1]);
17 }
18 // shift dimension to 3 from 2
19 dsb.shift_dimension(3).unwrap();
20 // push a 3d-datum
21 dsb.push(&[12u8, 13, 14]).unwrap();
22 // get a 3d-datum
23 assert_eq!(dsb.get(0).unwrap(), &[0u8, 1, 2]);
24 assert_eq!(dsb.get(1).unwrap(), &[3u8, 4, 5]);
25 assert_eq!(dsb.get(2).unwrap(), &[6u8, 7, 8]);
26 assert_eq!(dsb.get(3).unwrap(), &[9u8, 10, 11]);
27 assert_eq!(dsb.get(4).unwrap(), &[12u8, 13, 14]);
28 // get a linear slice
29 let linear_slice = dsb.as_slice();
30 assert_eq!(linear_slice, &[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
31}
pub fn clear(&mut self)
Sourcepub fn len(&self) -> Result<usize, DimensionShiftableBufferError>
pub fn len(&self) -> Result<usize, DimensionShiftableBufferError>
Examples found in repository?
examples/example1.rs (line 13)
3fn example_u8()
4{
5 // make a 2d-empty DimensionShiftableBuffer
6 let mut dsb = DimensionShiftableBuffer::<u8>::new(vec![], 2).unwrap();
7 // push a 2d-datum
8 dsb.push(&[0u8, 1]).unwrap();
9 // push a 2d-datum
10 dsb.push(&[2u8, 3]).unwrap();
11 // append a 2d-datum sequence
12 dsb.append(&[4u8, 5, 6, 7, 8, 9, 10, 11]).unwrap();
13 for index in 0..dsb.len().unwrap()
14 {
15 // get a 2d slice
16 assert_eq!(dsb.get(index).unwrap(), &[index as u8 * 2, index as u8 * 2 + 1]);
17 }
18 // shift dimension to 3 from 2
19 dsb.shift_dimension(3).unwrap();
20 // push a 3d-datum
21 dsb.push(&[12u8, 13, 14]).unwrap();
22 // get a 3d-datum
23 assert_eq!(dsb.get(0).unwrap(), &[0u8, 1, 2]);
24 assert_eq!(dsb.get(1).unwrap(), &[3u8, 4, 5]);
25 assert_eq!(dsb.get(2).unwrap(), &[6u8, 7, 8]);
26 assert_eq!(dsb.get(3).unwrap(), &[9u8, 10, 11]);
27 assert_eq!(dsb.get(4).unwrap(), &[12u8, 13, 14]);
28 // get a linear slice
29 let linear_slice = dsb.as_slice();
30 assert_eq!(linear_slice, &[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
31}
More examples
examples/example3.rs (line 17)
7fn example_csv()
8{
9 let delimiter = " ";
10 let source = "0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0";
11 let dimension = 3;
12 // construct
13 let mut dsb = DimensionShiftableBuffer::<f32>::from_csv(source, dimension, delimiter).unwrap();
14 // modify-1
15 dsb.for_each(|v| *v += 0.1);
16 // modify-2
17 for n in 0..dsb.len().unwrap()
18 {
19 for v in dsb.get_mut(n).unwrap()
20 {
21 *v *= *v;
22 }
23 }
24 // enumerate
25 for i in 0..18
26 // maybe, 0..5 will ok, 6..18 will err
27 {
28 match dsb.get(i)
29 {
30 Ok(dimensional_elements) =>
31 {
32 for (i_sub, actual) in dimensional_elements.iter().enumerate()
33 {
34 let i_flatten = i * dimension as usize + i_sub;
35 let expected = (i_flatten as f32 + 0.1) * (i_flatten as f32 + 0.1);
36 assert_eq!(actual, &expected);
37 }
38 },
39 Err(e) =>
40 {
41 match e
42 {
43 DimensionShiftableBufferError::OutOfRange {
44 limit: _,
45 requested: _
46 } => (),
47 _ => panic!("unexpected err: {:?}", e)
48 }
49 },
50 }
51 }
52 println!("<<print CSV, as a sequential slice>>\n{}", dsb.to_csv(", "));
53 println!(
54 "<<print CSV, as a dimmensional slices>>\n{}",
55 dsb.to_csv_dimensional(", ", "\n").unwrap()
56 );
57}
Sourcepub fn get(&self, index: usize) -> Result<&[T], DimensionShiftableBufferError>
pub fn get(&self, index: usize) -> Result<&[T], DimensionShiftableBufferError>
Examples found in repository?
examples/example1.rs (line 16)
3fn example_u8()
4{
5 // make a 2d-empty DimensionShiftableBuffer
6 let mut dsb = DimensionShiftableBuffer::<u8>::new(vec![], 2).unwrap();
7 // push a 2d-datum
8 dsb.push(&[0u8, 1]).unwrap();
9 // push a 2d-datum
10 dsb.push(&[2u8, 3]).unwrap();
11 // append a 2d-datum sequence
12 dsb.append(&[4u8, 5, 6, 7, 8, 9, 10, 11]).unwrap();
13 for index in 0..dsb.len().unwrap()
14 {
15 // get a 2d slice
16 assert_eq!(dsb.get(index).unwrap(), &[index as u8 * 2, index as u8 * 2 + 1]);
17 }
18 // shift dimension to 3 from 2
19 dsb.shift_dimension(3).unwrap();
20 // push a 3d-datum
21 dsb.push(&[12u8, 13, 14]).unwrap();
22 // get a 3d-datum
23 assert_eq!(dsb.get(0).unwrap(), &[0u8, 1, 2]);
24 assert_eq!(dsb.get(1).unwrap(), &[3u8, 4, 5]);
25 assert_eq!(dsb.get(2).unwrap(), &[6u8, 7, 8]);
26 assert_eq!(dsb.get(3).unwrap(), &[9u8, 10, 11]);
27 assert_eq!(dsb.get(4).unwrap(), &[12u8, 13, 14]);
28 // get a linear slice
29 let linear_slice = dsb.as_slice();
30 assert_eq!(linear_slice, &[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
31}
More examples
examples/example3.rs (line 28)
7fn example_csv()
8{
9 let delimiter = " ";
10 let source = "0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0";
11 let dimension = 3;
12 // construct
13 let mut dsb = DimensionShiftableBuffer::<f32>::from_csv(source, dimension, delimiter).unwrap();
14 // modify-1
15 dsb.for_each(|v| *v += 0.1);
16 // modify-2
17 for n in 0..dsb.len().unwrap()
18 {
19 for v in dsb.get_mut(n).unwrap()
20 {
21 *v *= *v;
22 }
23 }
24 // enumerate
25 for i in 0..18
26 // maybe, 0..5 will ok, 6..18 will err
27 {
28 match dsb.get(i)
29 {
30 Ok(dimensional_elements) =>
31 {
32 for (i_sub, actual) in dimensional_elements.iter().enumerate()
33 {
34 let i_flatten = i * dimension as usize + i_sub;
35 let expected = (i_flatten as f32 + 0.1) * (i_flatten as f32 + 0.1);
36 assert_eq!(actual, &expected);
37 }
38 },
39 Err(e) =>
40 {
41 match e
42 {
43 DimensionShiftableBufferError::OutOfRange {
44 limit: _,
45 requested: _
46 } => (),
47 _ => panic!("unexpected err: {:?}", e)
48 }
49 },
50 }
51 }
52 println!("<<print CSV, as a sequential slice>>\n{}", dsb.to_csv(", "));
53 println!(
54 "<<print CSV, as a dimmensional slices>>\n{}",
55 dsb.to_csv_dimensional(", ", "\n").unwrap()
56 );
57}
examples/example2.rs (line 14)
3fn example_f32()
4{
5 // Index of 1D => 0 |1 |2 |3 |4 |5 |6 |7 |8 |
6 // Index of 2D => 0,0 0,1 |1,0 1,1 |2,0 2,1 |3,0 3,1 |4,0 4,1 |
7 // Index of 3D => 0,0 0,1 0,2 |1,0 1,1 1,2 |2,0 2,1 2,2 |
8 // Index of 7D => 0,0 0,1 0,2 0,3 0,4 0,5 0,6 |1,0 1,1 1,2 1,3 1,4 1,5 1,6 |
9 let entity = vec![1.1f32, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3];
10 let dimension = 1;
11
12 let mut dsb = DimensionShiftableBuffer::<f32>::new(entity, dimension).unwrap();
13
14 let d1_i3 = dsb.get(3).unwrap();
15 assert_eq!(d1_i3, [2.1f32]);
16
17 dsb.shift_dimension(3).unwrap();
18
19 let d3_i2 = dsb.get(2).unwrap();
20 assert_eq!(d3_i2, [3.1f32, 3.2, 3.3]);
21
22 match dsb.shift_dimension(2)
23 {
24 Ok(_) => panic!("Detect an unexpected false-ok."),
25 Err(_) => eprintln!("Expected err.")
26 }
27
28 dsb.shift_dimension_truncate(2).unwrap();
29
30 let d2_i1 = dsb.get(1).unwrap();
31 assert_eq!(d2_i1, [1.3f32, 2.1]);
32
33 dsb.shift_dimension_padding(7, std::f32::NAN).unwrap();
34 let d7_i1 = dsb.get(1).unwrap();
35 assert_eq!(
36 // rounding, just easily
37 format!("{:?}", d7_i1),
38 format!("{:?}", &[
39 // [0] of 7-d
40 3.2f32,
41 // [1] of 7-d, truncated at .shift_dimension.truncate(2) -> padding at .shift_dimension_padding(7)
42 std::f32::NAN,
43 // [2] of 7-d, padding at .shift_dimension_padding(7)
44 std::f32::NAN,
45 std::f32::NAN,
46 std::f32::NAN,
47 std::f32::NAN,
48 std::f32::NAN
49 ])
50 )
51}
Sourcepub fn get_mut(
&mut self,
index: usize,
) -> Result<&mut [T], DimensionShiftableBufferError>
pub fn get_mut( &mut self, index: usize, ) -> Result<&mut [T], DimensionShiftableBufferError>
Examples found in repository?
examples/example3.rs (line 19)
7fn example_csv()
8{
9 let delimiter = " ";
10 let source = "0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0";
11 let dimension = 3;
12 // construct
13 let mut dsb = DimensionShiftableBuffer::<f32>::from_csv(source, dimension, delimiter).unwrap();
14 // modify-1
15 dsb.for_each(|v| *v += 0.1);
16 // modify-2
17 for n in 0..dsb.len().unwrap()
18 {
19 for v in dsb.get_mut(n).unwrap()
20 {
21 *v *= *v;
22 }
23 }
24 // enumerate
25 for i in 0..18
26 // maybe, 0..5 will ok, 6..18 will err
27 {
28 match dsb.get(i)
29 {
30 Ok(dimensional_elements) =>
31 {
32 for (i_sub, actual) in dimensional_elements.iter().enumerate()
33 {
34 let i_flatten = i * dimension as usize + i_sub;
35 let expected = (i_flatten as f32 + 0.1) * (i_flatten as f32 + 0.1);
36 assert_eq!(actual, &expected);
37 }
38 },
39 Err(e) =>
40 {
41 match e
42 {
43 DimensionShiftableBufferError::OutOfRange {
44 limit: _,
45 requested: _
46 } => (),
47 _ => panic!("unexpected err: {:?}", e)
48 }
49 },
50 }
51 }
52 println!("<<print CSV, as a sequential slice>>\n{}", dsb.to_csv(", "));
53 println!(
54 "<<print CSV, as a dimmensional slices>>\n{}",
55 dsb.to_csv_dimensional(", ", "\n").unwrap()
56 );
57}
pub fn remove( &mut self, index: usize, ) -> Result<Vec<T>, DimensionShiftableBufferError>
pub fn pop(&mut self) -> Result<Vec<T>, DimensionShiftableBufferError>
Sourcepub fn push(&mut self, v: &[T]) -> Result<(), DimensionShiftableBufferError>
pub fn push(&mut self, v: &[T]) -> Result<(), DimensionShiftableBufferError>
Examples found in repository?
examples/example1.rs (line 8)
3fn example_u8()
4{
5 // make a 2d-empty DimensionShiftableBuffer
6 let mut dsb = DimensionShiftableBuffer::<u8>::new(vec![], 2).unwrap();
7 // push a 2d-datum
8 dsb.push(&[0u8, 1]).unwrap();
9 // push a 2d-datum
10 dsb.push(&[2u8, 3]).unwrap();
11 // append a 2d-datum sequence
12 dsb.append(&[4u8, 5, 6, 7, 8, 9, 10, 11]).unwrap();
13 for index in 0..dsb.len().unwrap()
14 {
15 // get a 2d slice
16 assert_eq!(dsb.get(index).unwrap(), &[index as u8 * 2, index as u8 * 2 + 1]);
17 }
18 // shift dimension to 3 from 2
19 dsb.shift_dimension(3).unwrap();
20 // push a 3d-datum
21 dsb.push(&[12u8, 13, 14]).unwrap();
22 // get a 3d-datum
23 assert_eq!(dsb.get(0).unwrap(), &[0u8, 1, 2]);
24 assert_eq!(dsb.get(1).unwrap(), &[3u8, 4, 5]);
25 assert_eq!(dsb.get(2).unwrap(), &[6u8, 7, 8]);
26 assert_eq!(dsb.get(3).unwrap(), &[9u8, 10, 11]);
27 assert_eq!(dsb.get(4).unwrap(), &[12u8, 13, 14]);
28 // get a linear slice
29 let linear_slice = dsb.as_slice();
30 assert_eq!(linear_slice, &[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
31}
Sourcepub fn append(&mut self, vs: &[T]) -> Result<(), DimensionShiftableBufferError>
pub fn append(&mut self, vs: &[T]) -> Result<(), DimensionShiftableBufferError>
Examples found in repository?
examples/example1.rs (line 12)
3fn example_u8()
4{
5 // make a 2d-empty DimensionShiftableBuffer
6 let mut dsb = DimensionShiftableBuffer::<u8>::new(vec![], 2).unwrap();
7 // push a 2d-datum
8 dsb.push(&[0u8, 1]).unwrap();
9 // push a 2d-datum
10 dsb.push(&[2u8, 3]).unwrap();
11 // append a 2d-datum sequence
12 dsb.append(&[4u8, 5, 6, 7, 8, 9, 10, 11]).unwrap();
13 for index in 0..dsb.len().unwrap()
14 {
15 // get a 2d slice
16 assert_eq!(dsb.get(index).unwrap(), &[index as u8 * 2, index as u8 * 2 + 1]);
17 }
18 // shift dimension to 3 from 2
19 dsb.shift_dimension(3).unwrap();
20 // push a 3d-datum
21 dsb.push(&[12u8, 13, 14]).unwrap();
22 // get a 3d-datum
23 assert_eq!(dsb.get(0).unwrap(), &[0u8, 1, 2]);
24 assert_eq!(dsb.get(1).unwrap(), &[3u8, 4, 5]);
25 assert_eq!(dsb.get(2).unwrap(), &[6u8, 7, 8]);
26 assert_eq!(dsb.get(3).unwrap(), &[9u8, 10, 11]);
27 assert_eq!(dsb.get(4).unwrap(), &[12u8, 13, 14]);
28 // get a linear slice
29 let linear_slice = dsb.as_slice();
30 assert_eq!(linear_slice, &[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
31}
Trait Implementations§
Source§impl<T: Display + FromStr + PartialOrd + Clone> Csv<T> for DimensionShiftableBuffer<T>
impl<T: Display + FromStr + PartialOrd + Clone> Csv<T> for DimensionShiftableBuffer<T>
fn from_csv( source: &str, dimension: usize, delimiter: &str, ) -> Result<DimensionShiftableBuffer<T>, DimensionShiftableBufferError>
fn to_csv(&self, delimiter: &str) -> String
fn to_csv_dimensional( &self, element_delimiter: &str, dimension_delimiter: &str, ) -> Result<String, DimensionShiftableBufferError>
Source§impl<T: Debug + PartialOrd + Clone> Debug for DimensionShiftableBuffer<T>
impl<T: Debug + PartialOrd + Clone> Debug for DimensionShiftableBuffer<T>
Source§impl<T: Default + PartialOrd + Clone> Default for DimensionShiftableBuffer<T>
impl<T: Default + PartialOrd + Clone> Default for DimensionShiftableBuffer<T>
Source§fn default() -> DimensionShiftableBuffer<T>
fn default() -> DimensionShiftableBuffer<T>
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl<T> Freeze for DimensionShiftableBuffer<T>
impl<T> RefUnwindSafe for DimensionShiftableBuffer<T>where
T: RefUnwindSafe,
impl<T> Send for DimensionShiftableBuffer<T>where
T: Send,
impl<T> Sync for DimensionShiftableBuffer<T>where
T: Sync,
impl<T> Unpin for DimensionShiftableBuffer<T>where
T: Unpin,
impl<T> UnwindSafe for DimensionShiftableBuffer<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more