Module building_blocks_storage::access_traits [−][src]
Expand description
Traits defining different ways to access data from generic lattice maps.
Strided Iteration
The fastest way to iterate over data in an Array is with a simple for loop over array indices, we call them “stride“s:
use building_blocks_core::prelude::*; use building_blocks_storage::prelude::*; let extent = Extent3i::from_min_and_shape(Point3i::ZERO, Point3i::fill(100)); let mut map = Array3x1::fill(extent, 0); for i in 0..extent.num_points() { // Use the `GetMut<Stride>` trait impl of the map. *map.get_mut(Stride(i)) = 1; }
But this may require understanding the array layout.
ForEach
over Extent
Often, you only want to iterate over a sub-extent of the map. This can also be done at similar speeds using the ForEach
and ForEachMut
traits:
let subextent = Extent3i::from_min_and_shape(Point3i::fill(1), Point3i::fill(98)); // Use the `ForEachMut<[i32; 3], Stride>` trait. map.for_each_mut(&subextent, |_s: Stride, value| { *value = 2 });
Arrays also implement ForEach*<PointN<N>>
and ForEach*<(PointN<N>, Stride)>
. ChunkMap
only implements
ForEach*<PointN<N>>
, because it’s ambiguous which chunk a Stride
would apply to.
Copy an Extent
If you need to copy data between lattice maps, you should use the copy_extent
function. Copies can be done efficiently
because the ReadExtent
and WriteExtent
traits allow lattice maps to define how they would like to be written to or read
from.
// Create another map to copy to/from. We use a `ChunkHashMap`, but any map that implements // `WriteExtent` can be a copy destination, and any map that implements `ReadExtent` can be a // copy source. let chunk_shape = Point3i::fill(16); let builder = ChunkMapBuilder3x1::new(chunk_shape, 0); let mut other_map = builder.build_with_hash_map_storage(); copy_extent(&subextent, &map, &mut other_map.lod_view_mut(0)); copy_extent(&subextent, &other_map.lod_view(0), &mut map); // You can even copy from a `Fn(Point3i) -> T`. copy_extent(&subextent, &Func(|p: Point3i| p.x()), &mut map);
Traits
FillExtent | |
ForEach | |
ForEachMut | |
Get | |
GetMut | |
GetMutPtr | |
GetRef | |
ReadExtent | A trait to facilitate the generic implementation of |
WriteExtent | A trait to facilitate the generic implementation of |
Functions
copy_extent | Copy all points in |