public void ImageMemoryTest() { SetImageStubImplementation(); CallCount count = NativeK4a.CountCalls(); Assert.AreEqual(0, count.Calls("k4a_image_create")); Assert.AreEqual(0, count.Calls("k4a_image_release")); Task.Run(() => { using (Image image = new Image(ImageFormat.Custom, 640, 480, 640 * 2)) { Memory <byte> memory = image.Memory; System.Span <byte> memorySpan = memory.Span; System.Span <short> shortSpan = MemoryMarshal.Cast <byte, short>(memorySpan); Assert.AreEqual(0, shortSpan[0]); Assert.AreEqual(1, shortSpan[1]); image.Dispose(); } }).Wait(); GC.Collect(0, GCCollectionMode.Forced, true, true); GC.WaitForPendingFinalizers(); Assert.AreEqual(count.Calls("k4a_image_reference") + 1, count.Calls("k4a_image_release"), "References not zero"); }
public unsafe void HeapAllocationShouldFree() { //arrange var blocks = 256; var size = blocks * HeapAllocation.HeapSize; var memory = stackalloc HeapAllocation[blocks]; var span = new System.Span <HeapAllocation>(memory, blocks); *memory = new HeapAllocation(size); //act HeapAllocation.Split(memory, 1); HeapAllocation.Free(memory); //assert span[0].Blocks.ShouldBe((uint)blocks - 1); span[0].SizeInBytes.ShouldBe((uint)(blocks - 1) * HeapAllocation.HeapSize); HeapAllocation.CountFreeBlocks(memory).ShouldBe((uint)blocks - 1u); HeapAllocation.CountUsedBlocks(memory, out var allocations).ShouldBe((uint)1u); allocations.ShouldBe(0); span[2].Blocks.ShouldBe(0u); Assert(span[0].Previous == null); Assert(span[0].Next == null); Assert(span[2].Next == null); Assert(span[2].Previous == null); Assert(span[2].Flags == 0); //Assert(span[2].Previous == null); }
public unsafe void HeapAllocationShouldAllocate() { //arrange var blocks = 256; var size = blocks * HeapAllocation.HeapSize; var memory = stackalloc HeapAllocation[blocks]; var span = new System.Span <HeapAllocation>(memory, blocks); *memory = new HeapAllocation(size); //memory->Blocks = (uint)blocks - 1; //offset the first heap block //act HeapAllocation.Split(memory, 1); //assert span[0].Flags.ShouldBe(1u); span[0].Blocks.ShouldBe(1u); span[0].SizeInBytes.ShouldBe(span[0].Blocks * HeapAllocation.HeapSize); HeapAllocation.CountFreeBlocks(memory).ShouldBe((uint)blocks - 3u); HeapAllocation.CountUsedBlocks(memory, out var allocations).ShouldBe((uint)3u); allocations.ShouldBe(1); span[2].Blocks.ShouldBe((uint)blocks - 3u); Assert(span[2].Next == null); Assert(span[0].Previous == null); Assert(span[0].Next == &memory[2]); Assert(span[2].Previous == &memory[0]); }
public void RawSinglePass() { System.Span <ComponentType> componentTypes = stackalloc ComponentType[] { ComponentType <Position> .Type, ComponentType <Velocity> .Type }; var entityArrays = _entities.EntityArrays; for (var i = 0; i < entityArrays.Count; i++) { var array = entityArrays[i]; if (array.Specification.HasAll(componentTypes)) { var t0i = array.Specification.GetComponentIndex(componentTypes[0]); var t1i = array.Specification.GetComponentIndex(componentTypes[1]); for (var k = 0; k < array.AllChunks.Count; k++) { var chunk = array.AllChunks[k]; var length = chunk.Count; var t0 = chunk.PackedArray.GetComponentData <Position>(t0i, componentTypes[0]); var t1 = chunk.PackedArray.GetComponentData <Velocity>(t1i, componentTypes[1]); for (var j = 0; j < length; j++) { ref var position = ref t0[j]; ref var velocity = ref t1[j]; position.X += velocity.X * dt; position.Y += velocity.Y * dt; velocity.X -= velocity.X * dt; velocity.Y -= velocity.Y * dt; } } }
public static WhereEnumerable <TSource> Where <TSource>(this System.Span <TSource> source, Func <TSource, bool> predicate) { if (predicate is null) { ThrowHelper.ThrowArgumentNullException(nameof(predicate)); } return(new WhereEnumerable <TSource>(source, predicate)); }
//[Benchmark] public void Raw() { System.Span <ComponentType> componentTypes = stackalloc ComponentType[] { ComponentType <Position> .Type, ComponentType <Velocity> .Type }; var entityArrays = _entities.EntityArrays; for (var i = 0; i < entityArrays.Count; i++) { var array = entityArrays[i]; if (array.Specification.HasAll(componentTypes)) { var t0i = array.Specification.GetComponentIndex(componentTypes[0]); var t1i = array.Specification.GetComponentIndex(componentTypes[1]); for (var k = 0; k < array.AllChunks.Count; k++) { var chunk = array.AllChunks[k]; var length = chunk.Count; var t0 = chunk.PackedArray.GetComponentData <Position>(t0i, componentTypes[0]); var t1 = chunk.PackedArray.GetComponentData <Velocity>(t1i, componentTypes[1]); for (var j = 0; j < length; j++) { ref var position = ref t0[j]; ref var velocity = ref t1[j]; position.X += velocity.X * dt; position.Y += velocity.Y * dt; //WTF: Well I learned the hard way today about denormalized floats... //need to reset the data on each iteration velocity.X -= velocity.X * dt; velocity.Y -= velocity.Y * dt; if ((position.X > maxx && velocity.X > 0) || (position.X < 0 && velocity.X < 0)) { velocity.X = -velocity.X; } if ((position.Y > maxy && velocity.Y > 0) || (position.Y < 0 && velocity.Y < 0)) { velocity.Y = -velocity.Y; } } } }
public int Encode(System.Span <byte> destination) { throw null; }
public bool TryReadOctetString(System.Span <byte> destination, out int bytesWritten, System.Formats.Asn1.Asn1Tag?expectedTag = default(System.Formats.Asn1.Asn1Tag?)) { throw null; }
int Utils.Wrappers.Interfaces.ISocket.Receive(System.Span <byte> buffer, SocketFlags socketFlags) { return(InternalSocket.Receive(buffer, socketFlags)); }
private string[] GenerateTableRow(string[] values, ReadOnlySpan <int> alignment, DisplayCells ds) { // select the active columns (skip hidden ones) Span <int> validColumnArray = _si.columnInfo.Length <= OutCommandInner.StackAllocThreshold ? stackalloc int[_si.columnInfo.Length] : new int[_si.columnInfo.Length]; int validColumnCount = 0; for (int k = 0; k < _si.columnInfo.Length; k++) { if (_si.columnInfo[k].width > 0) { validColumnArray[validColumnCount++] = k; } } if (validColumnCount == 0) { return(null); } StringCollection[] scArray = new StringCollection[validColumnCount]; bool addPadding = true; for (int k = 0; k < scArray.Length; k++) { // for the last column, don't pad it with trailing spaces if (k == scArray.Length - 1) { addPadding = false; } // obtain a set of tokens for each field scArray[k] = GenerateMultiLineRowField(values[validColumnArray[k]], validColumnArray[k], alignment[validColumnArray[k]], ds, addPadding); // NOTE: the following padding operations assume that we // pad with a blank (or any character that ALWAYS maps to a single screen cell if (k > 0) { // skipping the first ones, add a separator for concatenation for (int j = 0; j < scArray[k].Count; j++) { scArray[k][j] = StringUtil.Padding(ScreenInfo.separatorCharacterCount) + scArray[k][j]; } } else { // add indentation padding if needed if (_startColumn > 0) { for (int j = 0; j < scArray[k].Count; j++) { scArray[k][j] = StringUtil.Padding(_startColumn) + scArray[k][j]; } } } } // now we processed all the rows columns and we need to find the cell that occupies the most // rows int screenRows = 0; for (int k = 0; k < scArray.Length; k++) { if (scArray[k].Count > screenRows) { screenRows = scArray[k].Count; } } // column headers can span multiple rows if the width of the column is shorter than the header text like: // // Long Header2 Head // Head er3 // er // ---- ------- ---- // 1 2 3 // // To ensure we don't add whitespace to the end, we need to determine the last column in each row with content System.Span <int> lastColWithContent = screenRows <= OutCommandInner.StackAllocThreshold ? stackalloc int[screenRows] : new int[screenRows]; for (int row = 0; row < screenRows; row++) { for (int col = 0; col < scArray.Length; col++) { if (scArray[col].Count > row) { lastColWithContent[row] = col; } } } // add padding for the columns that are shorter for (int col = 0; col < scArray.Length; col++) { int paddingBlanks = 0; // don't pad if last column if (col < scArray.Length - 1) { paddingBlanks = _si.columnInfo[validColumnArray[col]].width; if (col > 0) { paddingBlanks += ScreenInfo.separatorCharacterCount; } else { paddingBlanks += _startColumn; } } int paddingEntries = screenRows - scArray[col].Count; if (paddingEntries > 0) { for (int row = screenRows - paddingEntries; row < screenRows; row++) { // if the column is beyond the last column with content, just use empty string if (col > lastColWithContent[row]) { scArray[col].Add(string.Empty); } else { scArray[col].Add(StringUtil.Padding(paddingBlanks)); } } } } // finally, build an array of strings string[] rows = new string[screenRows]; for (int row = 0; row < screenRows; row++) { StringBuilder sb = new StringBuilder(); // for a given row, walk the columns for (int col = 0; col < scArray.Length; col++) { // if the column is the last column with content, we need to trim trailing whitespace, unless there is only one row if (col == lastColWithContent[row] && screenRows > 1) { sb.Append(scArray[col][row].TrimEnd()); } else { sb.Append(scArray[col][row]); } } rows[row] = sb.ToString(); } return(rows); }
public virtual System.Buffers.OperationStatus EncodeUtf8(System.ReadOnlySpan <byte> utf8Source, System.Span <byte> utf8Destination, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true) { throw null; }
public virtual bool TryExportECPrivateKey(System.Span <byte> destination, out int bytesWritten) => throw new PlatformNotSupportedException();
public static void Process(EntityManager entityManager, CreateCommand *it, Span <uint> lastEntities) { System.Span <ComponentType> componentTypes = new System.Span <ComponentType>(it + 1, it->ComponentCount); entityManager.Create(componentTypes, lastEntities); }
public unsafe void HeapAllocationShouldFillGap() { //arrange var blocks = 256; var size = blocks * HeapAllocation.HeapSize; var memory = stackalloc HeapAllocation[blocks]; var span = new System.Span <HeapAllocation>(memory, blocks); *memory = new HeapAllocation(size); //act HeapAllocation.Split(memory, 1); //255 HeapAllocation.Split(&memory[2], 1); //252 HeapAllocation.Split(&memory[4], 1); //249 HeapAllocation.Free(&memory[2]); //assert span[0].Blocks.ShouldBe(1u); span[2].Blocks.ShouldBe(1u); span[4].Blocks.ShouldBe(1u); span[6].Blocks.ShouldBe((uint)blocks - 7); span[0].SizeInBytes.ShouldBe((uint)HeapAllocation.HeapSize); span[2].SizeInBytes.ShouldBe((uint)HeapAllocation.HeapSize); span[4].SizeInBytes.ShouldBe((uint)HeapAllocation.HeapSize); span[6].SizeInBytes.ShouldBe((uint)(blocks - 7) * HeapAllocation.HeapSize); HeapAllocation.CountFreeBlocks(memory).ShouldBe((uint)blocks - 6u); HeapAllocation.CountUsedBlocks(memory, out var allocations).ShouldBe((uint)6u); allocations.ShouldBe(2); Assert(span[0].Previous == null); Assert(span[2].Previous == &memory[0]); Assert(span[4].Previous == &memory[2]); Assert(span[6].Previous == &memory[4]); Assert(span[0].Next == &memory[2]); Assert(span[2].Next == &memory[4]); Assert(span[4].Next == &memory[6]); Assert(span[6].Next == null); span[0].Flags.ShouldBe(1u); span[2].Flags.ShouldBe(0u); span[4].Flags.ShouldBe(1u); span[6].Flags.ShouldBe(0u); //act2 HeapAllocation.Free(&memory[0]); //assert2 span[0].Blocks.ShouldBe(3u); span[2].Blocks.ShouldBe(0u); span[4].Blocks.ShouldBe(1u); span[6].Blocks.ShouldBe((uint)blocks - 7); span[0].SizeInBytes.ShouldBe((uint)HeapAllocation.HeapSize * 3); span[2].SizeInBytes.ShouldBe(0u); span[4].SizeInBytes.ShouldBe((uint)HeapAllocation.HeapSize); span[6].SizeInBytes.ShouldBe((uint)(blocks - 7) * HeapAllocation.HeapSize); HeapAllocation.CountFreeBlocks(memory).ShouldBe((uint)blocks - 4u); HeapAllocation.CountUsedBlocks(memory, out allocations).ShouldBe((uint)4u); allocations.ShouldBe(1); Assert(span[0].Previous == null); Assert(span[2].Previous == null); Assert(span[4].Previous == &memory[0]); Assert(span[6].Previous == &memory[4]); Assert(span[0].Next == &memory[4]); Assert(span[2].Next == null); Assert(span[4].Next == &memory[6]); Assert(span[6].Next == null); span[0].Flags.ShouldBe(0u); span[2].Flags.ShouldBe(0u); span[4].Flags.ShouldBe(1u); span[6].Flags.ShouldBe(0u); //act3 HeapAllocation.Free(&memory[4]); //assert3 span[0].Blocks.ShouldBe((uint)(blocks - 1)); span[2].Blocks.ShouldBe(0u); span[4].Blocks.ShouldBe(0u); span[6].Blocks.ShouldBe(0u); span[0].SizeInBytes.ShouldBe((uint)(HeapAllocation.HeapSize * (blocks - 1))); span[2].SizeInBytes.ShouldBe(0u); span[4].SizeInBytes.ShouldBe(0u); span[6].SizeInBytes.ShouldBe(0u); HeapAllocation.CountFreeBlocks(memory).ShouldBe((uint)(blocks - 1u)); HeapAllocation.CountUsedBlocks(memory, out allocations).ShouldBe(1u); allocations.ShouldBe(0); Assert(span[0].Previous == null); Assert(span[2].Previous == null); Assert(span[4].Previous == null); Assert(span[6].Previous == null); Assert(span[0].Next == null); Assert(span[2].Next == null); Assert(span[4].Next == null); Assert(span[6].Next == null); span[0].Flags.ShouldBe(0u); span[2].Flags.ShouldBe(0u); span[4].Flags.ShouldBe(0u); span[6].Flags.ShouldBe(0u); }
public unsafe void ShouldSlice() { using var memory = new DynamicAllocator(_logFactory); using var list = new NativeList <Data>(memory); System.Span <Data> data = stackalloc[] { new Data() { b = 1, x = 2, y = 3 }, new Data() { b = 2, x = 1, y = 3 }, new Data() { b = 3, x = 2, y = 1 }, new Data() { b = 2, x = 3, y = 1 }, new Data() { b = 1, x = 3, y = 2 }, new Data() { b = 2, x = 1, y = 2 } }; for (var i = 0; i < data.Length; i++) { list.Add(data[i]); } var slice = list.Slice(); slice.Length.ShouldBe(6); for (var i = 0; i < data.Length; i++) { slice[i].x.ShouldBe(data[i].x); slice[i].b.ShouldBe(data[i].b); slice[i].y.ShouldBe(data[i].y); } slice = list.Slice(2); slice.Length.ShouldBe(4); for (var i = 2; i < data.Length; i++) { slice[i - 2].x.ShouldBe(data[i].x); slice[i - 2].b.ShouldBe(data[i].b); slice[i - 2].y.ShouldBe(data[i].y); } slice = list.Slice(2, 2); slice.Length.ShouldBe(2); for (var i = 2; i < 4; i++) { slice[i - 2].x.ShouldBe(data[i].x); slice[i - 2].b.ShouldBe(data[i].b); slice[i - 2].y.ShouldBe(data[i].y); } } }
public bool TryEncode(System.Span <byte> destination, out int bytesWritten) { throw null; }
public bool TryEncrypt(System.ReadOnlySpan <char> password, System.Security.Cryptography.PbeParameters pbeParameters, System.Span <byte> destination, out int bytesWritten) { throw null; }
public static bool TryReadCharacterString(System.ReadOnlySpan <byte> source, System.Span <char> destination, System.Formats.Asn1.AsnEncodingRules ruleSet, System.Formats.Asn1.UniversalTagNumber encodingType, out int bytesConsumed, out int charsWritten, System.Formats.Asn1.Asn1Tag?expectedTag = default(System.Formats.Asn1.Asn1Tag?)) { throw null; }
public virtual System.Buffers.OperationStatus Encode(System.ReadOnlySpan <char> source, System.Span <char> destination, out int charsConsumed, out int charsWritten, bool isFinalBlock = true) { throw null; }
public static bool TryReadCharacterStringBytes(System.ReadOnlySpan <byte> source, System.Span <byte> destination, System.Formats.Asn1.AsnEncodingRules ruleSet, System.Formats.Asn1.Asn1Tag expectedTag, out int bytesConsumed, out int bytesWritten) { throw null; }
public static bool TryReadOctetString(System.ReadOnlySpan <byte> source, System.Span <byte> destination, System.Formats.Asn1.AsnEncodingRules ruleSet, out int bytesConsumed, out int bytesWritten, System.Formats.Asn1.Asn1Tag?expectedTag = default(System.Formats.Asn1.Asn1Tag?)) { throw null; }
public bool TryReadCharacterString(System.Span <char> destination, System.Formats.Asn1.UniversalTagNumber encodingType, out int charsWritten, System.Formats.Asn1.Asn1Tag?expectedTag = default(System.Formats.Asn1.Asn1Tag?)) { throw null; }
public void CopyTo(System.Span <byte> destination) { }
public bool TryReadCharacterStringBytes(System.Span <byte> destination, System.Formats.Asn1.Asn1Tag expectedTag, out int bytesWritten) { throw null; }
int Utils.Wrappers.Interfaces.ISocket.Receive(System.Span <byte> buffer, SocketFlags socketFlags, out SocketError errorCode) { return(InternalSocket.Receive(buffer, socketFlags, out errorCode)); }
int Utils.Wrappers.Interfaces.ISocket.Receive(System.Span <byte> buffer) { return(base.Receive(buffer)); }