Crates.io | vec-dimension-shift |
lib.rs | vec-dimension-shift |
version | 1.0.1 |
source | src |
created_at | 2020-08-19 22:01:31.547907 |
updated_at | 2020-08-19 22:05:52.085408 |
description | N-Dimension shift features for Vec |
homepage | |
repository | https://github.com/usagi/vec-dimension-shift |
max_upload_size | |
id | 278422 |
size | 27,261 |
This crate is an extension for Vec<T>
that extends the dimension-shift features.
Note: vec-dimension-shift crate use an unsafe features in the internal details. As an alternative, dimension_shift_buffer is available if you want a more flexible runtime dimension shifting or safe-only implementation.
In basically,
Vec<f64>
--( Dimension shift, to the 2-dimension! )--> Vec<[f64;2]>
f64
≈ [f64;1]
, 2D-shifted datum type is [f64;2]
.f64
, 2D-shifted datum element type is f64
.Vec<[f64;2]>
--( Dimension shift, flatten! )--> Vec<f64>
VecDimensionShift2D
, VecDimensionShift2DFlatten
and the 2..16-dimension trait
s for Vec<T>
.make_vec_dimension_shift_n_dimension!
macro for make your desired N-dimension trait
s.Note: In the default, 2D, 3D, 4D version of VecDimensionShift?D
is enabled. Set default-features=false
if you don't need these.
[dependencies]
vec-dimension-shift = "*"
1D -> 2D -> 1D -> 3D -> 1D, and modify an element:
use vec_dimension_shift::{
VecDimensionShift2D,
VecDimensionShift2DFlatten,
VecDimensionShift3D,
VecDimensionShift3DFlatten
};
fn d2_and_d3()
{
let original = vec![0.0, 1.1, 2.2, 3.3, 4.4, 5.5];
dbg!(&original);
let mut d2_shifted = original.as_2d_array().unwrap();
dbg!(&d2_shifted);
assert_eq!(d2_shifted[0], [0.0, 1.1]);
assert_eq!(d2_shifted[1], [2.2, 3.3]);
assert_eq!(d2_shifted[2], [4.4, 5.5]);
d2_shifted[1][1] = -1.0;
let flatten = d2_shifted.as_flatten();
dbg!(&flatten);
let mut d3_shifted = flatten.as_3d_array().unwrap();
dbg!(&d3_shifted);
assert_eq!(d3_shifted[0], [0.0, 1.1, 2.2]);
assert_eq!(d3_shifted[1], [-1.0, 4.4, 5.5]);
d3_shifted[1][1] = -2.0;
let flatten = d3_shifted.as_flatten();
dbg!(&flatten);
assert_eq!(flatten, vec![0.0, 1.1, 2.2, -1.0, -2.0, 5.5])
}
trait
s just you needs (eg, 2D and 3D)use vec_dimension_shift::make_vec_dimension_shift_n_dimension;
fn n_dimension_macro_generator()
{
make_vec_dimension_shift_n_dimension! { VecDimensionShift2D, VecDimensionShift2DFlatten, as_2d_array_no_check, to_2d_array_no_check, as_2d_array, to_2d_array, as_2d_array_truncate, to_2d_array_truncate, as_2d_array_padding, to_2d_array_padding, 2 }
make_vec_dimension_shift_n_dimension! { VecDimensionShift3D, VecDimensionShift3DFlatten, as_3d_array_no_check, to_3d_array_no_check, as_3d_array, to_3d_array, as_3d_array_truncate, to_3d_array_truncate, as_3d_array_padding, to_3d_array_padding, 3 }
let original = vec![0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10, 11.11];
dbg!(&original);
let d2 = original.as_2d_array().unwrap();
assert_eq!(d2[0], [0.0, 1.1]);
assert_eq!(d2[1], [2.2, 3.3]);
assert_eq!(d2[2], [4.4, 5.5]);
assert_eq!(d2[3], [6.6, 7.7]);
assert_eq!(d2[4], [8.8, 9.9]);
assert_eq!(d2[5], [10.10, 11.11]);
dbg!(&d2);
let d3 = d2.as_3d_array().unwrap();
assert_eq!(d3[0], [[0.0, 1.1], [2.2, 3.3], [4.4, 5.5]]);
assert_eq!(d3[1], [[6.6, 7.7], [8.8, 9.9], [10.10, 11.11]]);
dbg!(&d3);
}