-
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
1c277f2
commit 857cabc
Showing
4 changed files
with
267 additions
and
17 deletions.
There are no files selected for viewing
176 changes: 176 additions & 0 deletions
176
...dGreen/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample/Internal/GreenListBuilder.g.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,176 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// Modified by the Tsu (https://github.com/GGG-KILLER/Tsu) project for embedding into other projects. | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace Tsu.Trees.RedGreen.Sample.Internal | ||
{ | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
|
||
internal class SampleListBuilder | ||
{ | ||
private global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode?[] _nodes; | ||
public int Count { get; private set; } | ||
|
||
public SampleListBuilder(int size) | ||
{ | ||
_nodes = new global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode?[size]; | ||
} | ||
|
||
public static SampleListBuilder Create() => new(8); | ||
|
||
public void Clear() => Count = 0; | ||
|
||
public global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode? this[int index] | ||
{ | ||
get => _nodes[index]; | ||
|
||
set => _nodes[index] = value; | ||
} | ||
|
||
public void Add(global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode? item) | ||
{ | ||
if (item == null) return; | ||
|
||
if (item.IsList) | ||
{ | ||
int slotCount = item.SlotCount; | ||
|
||
// Necessary, but not sufficient (e.g. for nested lists). | ||
EnsureAdditionalCapacity(slotCount); | ||
|
||
for (int i = 0; i < slotCount; i++) | ||
{ | ||
Add(item.GetSlot(i)); | ||
} | ||
} | ||
else | ||
{ | ||
EnsureAdditionalCapacity(1); | ||
|
||
_nodes[Count++] = item; | ||
} | ||
} | ||
|
||
public void AddRange(global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode[] items) => AddRange(items, 0, items.Length); | ||
|
||
public void AddRange(global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode[] items, int offset, int length) | ||
{ | ||
// Necessary, but not sufficient (e.g. for nested lists). | ||
EnsureAdditionalCapacity(length - offset); | ||
|
||
int oldCount = Count; | ||
|
||
for (int i = offset; i < length; i++) | ||
{ | ||
Add(items[i]); | ||
} | ||
|
||
Validate(oldCount, Count); | ||
} | ||
|
||
private void Validate(int start, int end) | ||
{ | ||
for (int i = start; i < end; i++) | ||
{ | ||
global::System.Diagnostics.Debug.Assert(_nodes[i] != null); | ||
} | ||
} | ||
|
||
public void AddRange(SampleList<global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode> list) => AddRange(list, 0, list.Count); | ||
|
||
public void AddRange(SampleList<global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode> list, int offset, int length) | ||
{ | ||
// Necessary, but not sufficient (e.g. for nested lists). | ||
EnsureAdditionalCapacity(length - offset); | ||
|
||
int oldCount = Count; | ||
|
||
for (int i = offset; i < length; i++) | ||
{ | ||
Add(list[i]); | ||
} | ||
|
||
Validate(oldCount, Count); | ||
} | ||
|
||
public void AddRange<TNode>(SampleList<TNode> list) where TNode : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode => | ||
AddRange(list, 0, list.Count); | ||
|
||
public void AddRange<TNode>(SampleList<TNode> list, int offset, int length) where TNode : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode => | ||
AddRange(new SampleList<global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode>(list.Node), offset, length); | ||
|
||
public void RemoveLast() | ||
{ | ||
Count--; | ||
_nodes[Count] = null; | ||
} | ||
|
||
private void EnsureAdditionalCapacity(int additionalCount) | ||
{ | ||
int currentSize = _nodes.Length; | ||
int requiredSize = Count + additionalCount; | ||
|
||
if (requiredSize <= currentSize) return; | ||
|
||
int newSize = | ||
requiredSize < 8 ? 8 : | ||
requiredSize >= (int.MaxValue / 2) ? int.MaxValue : | ||
Math.Max(requiredSize, currentSize * 2); // NB: Size will *at least* double. | ||
global::System.Diagnostics.Debug.Assert(newSize >= requiredSize); | ||
|
||
Array.Resize(ref _nodes, newSize); | ||
} | ||
|
||
public bool Any(global::Tsu.Trees.RedGreen.Sample.SampleKind kind) | ||
{ | ||
for (int i = 0; i < Count; i++) | ||
{ | ||
if (_nodes[i]!.Kind == kind) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode[] ToArray() | ||
{ | ||
var array = new global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode[Count]; | ||
for (int i = 0; i < array.Length; i++) | ||
{ | ||
array[i] = _nodes[i]!; | ||
} | ||
|
||
return array; | ||
} | ||
|
||
internal global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode? ToListNode() | ||
{ | ||
switch (Count) | ||
{ | ||
case 0: | ||
return null; | ||
case 1: | ||
return _nodes[0]; | ||
case 2: | ||
return SampleList.List(_nodes[0]!, _nodes[1]!); | ||
case 3: | ||
return SampleList.List(_nodes[0]!, _nodes[1]!, _nodes[2]!); | ||
default: | ||
var tmp = new global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode[Count]; | ||
Array.Copy(_nodes, tmp, Count); | ||
return SampleList.List(tmp); | ||
} | ||
} | ||
|
||
public SampleList<global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode> ToList() => new(ToListNode()); | ||
|
||
public SampleList<TNode> ToList<TNode>() where TNode : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode => new(ToListNode()); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...reen/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample/Internal/GreenListBuilder`1.g.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,74 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// Modified by the Tsu (https://github.com/GGG-KILLER/Tsu) project for embedding into other projects. | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace Tsu.Trees.RedGreen.Sample.Internal | ||
{ | ||
internal readonly struct SampleListBuilder<TNode> where TNode : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode | ||
{ | ||
private readonly SampleListBuilder _builder; | ||
|
||
public SampleListBuilder(int size) | ||
: this(new SampleListBuilder(size)) | ||
{ | ||
} | ||
|
||
public static SampleListBuilder<TNode> Create() => new(8); | ||
|
||
internal SampleListBuilder(SampleListBuilder builder) | ||
{ | ||
_builder = builder; | ||
} | ||
|
||
public bool IsNull => _builder == null; | ||
|
||
public int Count => _builder.Count; | ||
|
||
public TNode? this[int index] | ||
{ | ||
get => (TNode?) _builder[index]; | ||
|
||
set => _builder[index] = value; | ||
} | ||
|
||
public void Clear() => _builder.Clear(); | ||
|
||
public SampleListBuilder<TNode> Add(TNode node) | ||
{ | ||
_builder.Add(node); | ||
return this; | ||
} | ||
|
||
public void AddRange(TNode[] items, int offset, int length) => | ||
_builder.AddRange(items, offset, length); | ||
|
||
public void AddRange(SampleList<TNode> nodes) => _builder.AddRange(nodes); | ||
|
||
public void AddRange(SampleList<TNode> nodes, int offset, int length) => | ||
_builder.AddRange(nodes, offset, length); | ||
|
||
public bool Any(global::Tsu.Trees.RedGreen.Sample.SampleKind kind) => _builder.Any(kind); | ||
|
||
public SampleList<TNode> ToList() => _builder.ToList(); | ||
|
||
public global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode? ToListNode() => _builder.ToListNode(); | ||
|
||
public static implicit operator SampleListBuilder(SampleListBuilder<TNode> builder) => builder._builder; | ||
|
||
public static implicit operator SampleList<TNode>(SampleListBuilder<TNode> builder) | ||
{ | ||
if (builder._builder != null) | ||
{ | ||
return builder.ToList(); | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public SampleList<TDerived> ToList<TDerived>() where TDerived : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode => | ||
new(ToListNode()); | ||
} | ||
} |
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