-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78555fa
commit 55acbe7
Showing
6 changed files
with
108 additions
and
39 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
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 |
---|---|---|
@@ -1,9 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<IsAotCompatible>true</IsAotCompatible> | ||
<OutputType>Library</OutputType> | ||
<PackageId>Bundles</PackageId> | ||
<VersionPrefix>1.2.0</VersionPrefix> | ||
<VersionPrefix>2.0.0</VersionPrefix> | ||
</PropertyGroup> | ||
</Project> |
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
18 changes: 18 additions & 0 deletions
18
src/Bundles/ValueCollections/ValueAppendList.Enumerator.cs
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,18 @@ | ||
// This Source Code form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
namespace Bundles.ValueCollections; | ||
|
||
partial record struct ValueAppendList<T> | ||
{ | ||
public struct Enumerator(ValueAppendList<T> list) | ||
{ | ||
private int index; | ||
|
||
public ref readonly T Current => ref list[this.index]; | ||
|
||
public bool MoveNext() | ||
=> ++this.index != list.Count; | ||
} | ||
} |
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,68 @@ | ||
// This Source Code form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
using System.Buffers; | ||
|
||
namespace Bundles.ValueCollections; | ||
|
||
/// <summary> | ||
/// A value-type list type that can only be modified by appending, never by removing. | ||
/// </summary> | ||
/// <typeparam name="T">The element type controlled by this list.</typeparam> | ||
public partial record struct ValueAppendList<T> | ||
{ | ||
private T[] buffer; | ||
private int index; | ||
|
||
/// <summary> | ||
/// Gets a readonly reference to the element at the specified index. | ||
/// </summary> | ||
public readonly ref readonly T this[int index] => ref this.buffer[index]; | ||
|
||
/// <summary> | ||
/// Gets the current capacity of this list. | ||
/// </summary> | ||
public readonly int Capacity => this.buffer.Length; | ||
|
||
/// <summary> | ||
/// Gets the element count of this list. | ||
/// </summary> | ||
public readonly int Count => this.index + 1; | ||
|
||
/// <summary> | ||
/// Adds an element to the list, returning a readonly reference to the newly added element. | ||
/// </summary> | ||
public ref readonly T Add(T value) | ||
{ | ||
if (++this.index == buffer.Length) | ||
{ | ||
this.ResizeBuffer(); | ||
} | ||
|
||
this.buffer[this.index] = value; | ||
return ref this.buffer[this.index]; | ||
} | ||
|
||
/// <summary> | ||
/// Gets an enumerator over the present structure. | ||
/// </summary> | ||
/// <returns></returns> | ||
public readonly Enumerator GetEnumerator() | ||
=> new(this); | ||
|
||
private void ResizeBuffer() | ||
{ | ||
int newLength = this.buffer.Length * 2; | ||
T[] newBuffer = typeof(T).IsPrimitive ? ArrayPool<T>.Shared.Rent(newLength) : new T[newLength]; | ||
|
||
this.buffer.CopyTo(newBuffer, 0); | ||
|
||
if (typeof(T).IsPrimitive) | ||
{ | ||
ArrayPool<T>.Shared.Return(this.buffer); | ||
} | ||
|
||
this.buffer = newBuffer; | ||
} | ||
} |
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