-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/main' into release
- Loading branch information
Showing
69 changed files
with
956 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2020 ONIXLabs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.Buffers; | ||
|
||
namespace OnixLabs.Core.UnitTests.Data; | ||
|
||
public sealed class BufferSegment<T> : ReadOnlySequenceSegment<T> | ||
{ | ||
private BufferSegment(ReadOnlyMemory<T> memory) => Memory = memory; | ||
|
||
private void SetNext(BufferSegment<T> next) | ||
{ | ||
Next = next; | ||
next.RunningIndex = RunningIndex + Memory.Length; | ||
} | ||
|
||
public static ReadOnlySequence<T> FromMemory(ReadOnlyMemory<T>[] segments) | ||
{ | ||
if (segments.Length == 1) | ||
return new ReadOnlySequence<T>(segments[0]); | ||
|
||
BufferSegment<T>[] bufferSegments = segments | ||
.Select(segment => new BufferSegment<T>(segment)).ToArray(); | ||
|
||
for (int index = 0; index < bufferSegments.Length - 1; index++) | ||
bufferSegments[index].SetNext(bufferSegments[index + 1]); | ||
|
||
return new ReadOnlySequence<T>(bufferSegments[0], 0, bufferSegments[^1], bufferSegments[^1].Memory.Length); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2020 ONIXLabs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Buffers; | ||
using OnixLabs.Core.UnitTests.Data; | ||
using Xunit; | ||
|
||
namespace OnixLabs.Core.UnitTests; | ||
|
||
public sealed class ReadOnlySequenceExtensionTests | ||
{ | ||
[Fact(DisplayName = "ReadOnlySequence.CopyTo should copy into a new array with a single element")] | ||
public void ReadOnlySequenceCopyToShouldCopyIntoNewArrayWithSingleElement() | ||
{ | ||
// Given | ||
string[] expected = ["ABC", "XYZ", "123", "789"]; | ||
ReadOnlySequence<string> value = new(expected); | ||
|
||
// When | ||
value.CopyTo(out string[] actual); | ||
|
||
// Then | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Fact(DisplayName = "ReadOnlySequence.CopyTo should copy into a new array with multiple elements")] | ||
public void ReadOnlySequenceCopyToShouldCopyIntoNewArrayWithMultipleElements() | ||
{ | ||
// Given | ||
string[] expected = ["ABC", "XYZ", "123", "789"]; | ||
ReadOnlySequence<string> value = BufferSegment<string>.FromMemory([ | ||
new ReadOnlyMemory<string>(["ABC", "XYZ"]), | ||
new ReadOnlyMemory<string>(["123", "789"]) | ||
]); | ||
|
||
// When | ||
value.CopyTo(out string[] actual); | ||
|
||
// Then | ||
Assert.Equal(expected, actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2020 ONIXLabs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.Buffers; | ||
using System.ComponentModel; | ||
|
||
namespace OnixLabs.Core; | ||
|
||
/// <summary> | ||
/// Provides extension methods for read-only sequences. | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class ReadOnlySequenceExtensions | ||
{ | ||
/// <summary> | ||
/// Copies the current <see cref="ReadOnlySequence{T}"/> to the specified <see cref="T:[]"/> by reference. | ||
/// </summary> | ||
/// <param name="sequence">The current <see cref="ReadOnlySequence{T}"/> to copy.</param> | ||
/// <param name="array">The <see cref="T:[]"/> to copy to.</param> | ||
/// <typeparam name="T">The underlying type of the specified <see cref="ReadOnlySequence{T}"/> and <see cref="T:[]"/>.</typeparam> | ||
public static void CopyTo<T>(this ReadOnlySequence<T> sequence, out T[] array) | ||
{ | ||
if (sequence.IsSingleSegment) | ||
{ | ||
array = sequence.First.Span.ToArray(); | ||
} | ||
else | ||
{ | ||
array = new T[sequence.Length]; | ||
sequence.CopyTo(array); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.