Struct DimensionShiftableBuffer

Source
pub struct DimensionShiftableBuffer<T: PartialOrd + Clone> { /* private fields */ }

Implementations§

Source§

impl<T: PartialOrd + Clone> DimensionShiftableBuffer<T>

Source

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
Hide additional 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}
Source

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
Hide additional 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}
Source

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}
Source

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}
Source

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}
Source

pub fn move_entity(self) -> Vec<T>

WARNING, It’s MOVE and drop self.

Source

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}
Source

pub fn clear(&mut self)

Source

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
Hide additional 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}
Source

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
Hide additional 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}
Source

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}
Source

pub fn remove( &mut self, index: usize, ) -> Result<Vec<T>, DimensionShiftableBufferError>

Source

pub fn pop(&mut self) -> Result<Vec<T>, DimensionShiftableBufferError>

Source

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}
Source

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>

Source§

fn from_csv( source: &str, dimension: usize, delimiter: &str, ) -> Result<DimensionShiftableBuffer<T>, DimensionShiftableBufferError>

Source§

fn to_csv(&self, delimiter: &str) -> String

Source§

fn to_csv_dimensional( &self, element_delimiter: &str, dimension_delimiter: &str, ) -> Result<String, DimensionShiftableBufferError>

Source§

impl<T: Debug + PartialOrd + Clone> Debug for DimensionShiftableBuffer<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default + PartialOrd + Clone> Default for DimensionShiftableBuffer<T>

Source§

fn default() -> DimensionShiftableBuffer<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.