Slice
.Slice(offset) creates a memory that represents a slice of the decoree from offset onwards.
// Create list memory
IMemory<byte> memory = new ListMemory<List<byte>, byte>(new());
// Expand
memory.Count = 10;
// Take slice
IMemory<byte> slice = memory.Slice(5);
.Slice(offset, count) creates a slice with count number of elements.
// Create list memory
IMemory<byte> memory = new ListMemory<List<byte>, byte>(new());
// Expand
memory.Count = 10;
// Take slice 2 bytes at 5.
IMemory<byte> slice = memory.Slice(5, 2);
Elements can be added, removed, modified on slice, they reflect changes on parent.
// Insert at [5] on the parent using 'slice'
slice.Insert(0, (byte)4);
// Print on parent
WriteLine(memory.Count); // 11
WriteLine(memory[5]); // 4
Slice<M, T>
new Slice<IMemory<byte>, byte>(decoree, offset?, count?, allocation?) creates a slice on stack.
// Create list on stack
IMemory<byte> memory = new ListMemory<List<byte>, byte>(new());
// Expand
memory.Count = 20;
// Create slice on stack
var slice = new Slice<IMemory<byte>, byte>(memory, offset: 2, count: 7, allocation: 0);
decoree: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
slice: ▒▒▒▒▒▒▒
↑count↑
.Sliced(offset, count?) re-slices on stack.
// Add using slice (at [8] on parent)
slice.Add((byte)101);
// Reslice
slice = slice.Sliced(3, 3);
Full Example
Full example
using Avalanche.Memory;
using static System.Console;
public class slice
{
public static void Run()
{
{
// <01>
// Create list memory
IMemory<byte> memory = new ListMemory<List<byte>, byte>(new());
// Expand
memory.Count = 10;
// Take slice
IMemory<byte> slice = memory.Slice(5);
// </01>
}
{
// <02>
// Create list memory
IMemory<byte> memory = new ListMemory<List<byte>, byte>(new());
// Expand
memory.Count = 10;
// Take slice 2 bytes at 5.
IMemory<byte> slice = memory.Slice(5, 2);
// </02>
// <03>
// Insert at [5] on the parent using 'slice'
slice.Insert(0, (byte)4);
// Print on parent
WriteLine(memory.Count); // 11
WriteLine(memory[5]); // 4
// </03>
}
{
// <04>
// Create list on stack
IMemory<byte> memory = new ListMemory<List<byte>, byte>(new());
// Expand
memory.Count = 20;
// Create slice on stack
var slice = new Slice<IMemory<byte>, byte>(memory, offset: 2, count: 7, allocation: 0);
// </04>
// <05>
// Add using slice (at [8] on parent)
slice.Add((byte)101);
// Reslice
slice = slice.Sliced(3, 3);
// </05>
}
}
}