Skip to content

Commit 3e52156

Browse files
committed
Add Span<T> support
1 parent 3623062 commit 3e52156

File tree

117 files changed

+777
-680
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+777
-680
lines changed

Misc/PowerShell/FastData.psm1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ function Invoke-FastData
6161
$generator = [Genbox.FastData.Generator.Rust.RustCodeGenerator]::Create($cfg)
6262
}
6363

64-
$source = [ref]''
65-
$ok = [Genbox.FastData.FastDataGenerator]::TryGenerate($data, $config, $generator, [ref]$source)
64+
$source = [Genbox.FastData.FastDataGenerator]::Generate($data, $config, $generator)
6665

6766
if (-not $ok)
6867
{

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The `Dogs` class is generated at compile time and contains a `Contains()` method
6767
### Using the C# library
6868

6969
1. Add the [Genbox.FastData.Generator.CSharp](https://www.nuget.org/packages/Genbox.FastData.Generator.CSharp/) NuGet package to your project.
70-
2. Use the `FastDataGenerator.TryGenerate()` method. Give it your data as an array.
70+
2. Use the `FastDataGenerator.Generate()` method. Give it your data as an array.
7171

7272
```csharp
7373
internal static class Program
@@ -77,9 +77,7 @@ internal static class Program
7777
FastDataConfig config = new FastDataConfig();
7878
CSharpCodeGenerator generator = CSharpCodeGenerator.Create(new CSharpCodeGeneratorConfig("Dogs"));
7979

80-
if (!FastDataGenerator.TryGenerate(["Labrador", "German Shepherd", "Golden Retriever"], config, generator, out string? source))
81-
Console.WriteLine("Failed to generate source code");
82-
80+
string source = FastDataGenerator.Generate(["Labrador", "German Shepherd", "Golden Retriever"], config, generator);
8381
Console.WriteLine(source);
8482
}
8583
}

Src/FastData.Benchmarks/Benchmarks/AnalyzerBenchmarks.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ namespace Genbox.FastData.Benchmarks.Benchmarks;
1010
public class AnalyzerBenchmarks
1111
{
1212
private readonly GPerfAnalyzer _analyzer;
13+
private readonly string[] _data;
1314

1415
public AnalyzerBenchmarks()
1516
{
1617
Random rng = new Random(42);
18+
_data = Enumerable.Range(1, 100).Select(_ => TestHelper.GenerateRandomString(rng, 50)).ToArray();
19+
StringProperties props = DataAnalyzer.GetStringProperties(_data);
1720

18-
string[] data = Enumerable.Range(1, 100).Select(_ => TestHelper.GenerateRandomString(rng, 50)).ToArray();
19-
StringProperties props = DataAnalyzer.GetStringProperties(data);
20-
21-
_analyzer = new GPerfAnalyzer(data, props, new GPerfAnalyzerConfig(), new Simulator(data, new SimulatorConfig()), NullLogger<GPerfAnalyzer>.Instance);
21+
_analyzer = new GPerfAnalyzer(_data.Length, props, new GPerfAnalyzerConfig(), new Simulator(new SimulatorConfig()), NullLogger<GPerfAnalyzer>.Instance);
2222
}
2323

2424
[Benchmark]
25-
public object GPerfAnalyzer() => _analyzer.GetCandidates();
25+
public void GPerfAnalyzer() => _analyzer.GetCandidates(_data, OnCandidateFound);
26+
27+
private static bool OnCandidateFound(Candidate candidate)
28+
{
29+
// Do nothing
30+
return true;
31+
}
2632
}

Src/FastData.Cli/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ private static async Task GenerateAsync(FileInfo inputFile, DataType dataType, S
136136
object[] data = await ReadFileAsync(inputFile.FullName, dataType).ToArrayAsync().ConfigureAwait(false);
137137
FastDataConfig config = new FastDataConfig(structureType);
138138

139-
if (!FastDataGenerator.TryGenerate(data, config, generator, out string? source))
140-
throw new InvalidOperationException("Unable to generate code");
139+
string source = FastDataGenerator.Generate(data, config, generator);
141140

142141
if (outputFile == null)
143142
Console.WriteLine(source);

Src/FastData.Examples/Program.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ private static void Main()
99
FastDataConfig config = new FastDataConfig();
1010
CSharpCodeGenerator generator = CSharpCodeGenerator.Create(new CSharpCodeGeneratorConfig("Dogs"));
1111

12-
if (!FastDataGenerator.TryGenerate(["Labrador", "German Shepherd", "Golden Retriever"], config, generator, out string? source))
13-
Console.WriteLine("Failed to generate source code");
14-
12+
string source = FastDataGenerator.Generate(["Labrador", "German Shepherd", "Golden Retriever"], config, generator);
1513
Console.WriteLine(source);
1614
}
1715
}

Src/FastData.Generator.CPlusPlus.Tests/VectorTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class VectorTests(VectorTests.CPlusPlusContext context) : IClassFixture<V
1616
[ClassData(typeof(TestVectorTheoryData))]
1717
public async Task Test<T>(TestVector<T> data)
1818
{
19-
Assert.True(TryGenerate(id => CPlusPlusCodeGenerator.Create(new CPlusPlusCodeGeneratorConfig(id)), data, out GeneratorSpec spec));
19+
GeneratorSpec spec = Generate(id => CPlusPlusCodeGenerator.Create(new CPlusPlusCodeGeneratorConfig(id)), data);
2020
Assert.NotEmpty(spec.Source);
2121

2222
await Verify(spec.Source)
@@ -36,9 +36,9 @@ await Verify(spec.Source)
3636
int main(int argc, char* argv[])
3737
{
3838
{{FormatList(data.Values, x => $"""
39-
if (!{spec.Identifier}::contains({helper.ToValueLabel(x)}))
40-
return false;
41-
""", "\n")}}
39+
if (!{spec.Identifier}::contains({helper.ToValueLabel(x)}))
40+
return false;
41+
""", "\n")}}
4242
4343
return 1;
4444
}

Src/FastData.Generator.CPlusPlus.Tests/Verify/KeyLengthStructure_String_7.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is auto-generated. Do not edit manually.
2-
// Structure: Auto (KeyLengthContext)
2+
// Structure: Auto (KeyLength)
33
#pragma once
44
#include <array>
55
#include <cstdint>

Src/FastData.Generator.CPlusPlus/CPlusPlusCodeGenerator.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,16 @@ public static CPlusPlusCodeGenerator Create(CPlusPlusCodeGeneratorConfig userCfg
2323
return new CPlusPlusCodeGenerator(userCfg, langDef, new CPlusPlusConstantsDef(), new CPlusPlusEarlyExitDef(helper, userCfg.GeneratorOptions), new CPlusPlusHashDef());
2424
}
2525

26-
public override bool TryGenerate<T>(GeneratorConfig<T> genCfg, IContext context, out string? source)
26+
public override string Generate<T>(ReadOnlySpan<T> data, GeneratorConfig<T> genCfg, IContext<T> context)
2727
{
2828
//C++ generator does not support chars outside ASCII
2929
if (genCfg.DataType == DataType.Char && (char)(object)genCfg.Constants.MaxValue! > 127)
30-
{
31-
source = null;
32-
return false;
33-
}
30+
throw new InvalidOperationException("C++ generator does not support chars outside ASCII. Please use a different data type or reduce the max value to 127 or lower.");
3431

35-
return base.TryGenerate(genCfg, context, out source);
32+
return base.Generate(data, genCfg, context);
3633
}
3734

38-
protected override void AppendHeader<T>(StringBuilder sb, GeneratorConfig<T> genCfg, IContext context)
35+
protected override void AppendHeader<T>(StringBuilder sb, GeneratorConfig<T> genCfg, IContext<T> context)
3936
{
4037
base.AppendHeader(sb, genCfg, context);
4138

@@ -70,7 +67,7 @@ protected override void AppendFooter<T>(StringBuilder sb, GeneratorConfig<T> gen
7067
""");
7168
}
7269

73-
protected override OutputWriter<T>? GetOutputWriter<T>(GeneratorConfig<T> genCfg, IContext context) => context switch
70+
protected override OutputWriter<T>? GetOutputWriter<T>(GeneratorConfig<T> genCfg, IContext<T> context) => context switch
7471
{
7572
SingleValueContext<T> x => new SingleValueCode<T>(x),
7673
ArrayContext<T> x => new ArrayCode<T>(x),
@@ -80,7 +77,7 @@ protected override void AppendFooter<T>(StringBuilder sb, GeneratorConfig<T> gen
8077
HashSetChainContext<T> x => new HashSetChainCode<T>(x),
8178
HashSetLinearContext<T> x => new HashSetLinearCode<T>(x),
8279
HashSetPerfectContext<T> x => new HashSetPerfectCode<T>(x),
83-
KeyLengthContext x => new KeyLengthCode<T>(x),
80+
KeyLengthContext<T> x => new KeyLengthCode<T>(x),
8481
_ => null
8582
};
8683
}

Src/FastData.Generator.CPlusPlus/Internal/Generators/ArrayCode.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace Genbox.FastData.Generator.CPlusPlus.Internal.Generators;
66

77
internal sealed class ArrayCode<T>(ArrayContext<T> ctx) : CPlusPlusOutputWriter<T>
88
{
9-
public override string Generate() =>
9+
public override string Generate(ReadOnlySpan<T> data) =>
1010
$$"""
11-
{{FieldModifier}}std::array<{{TypeName}}, {{ctx.Data.Length.ToStringInvariant()}}> entries = {
12-
{{FormatColumns(ctx.Data, ToValueLabel)}}
11+
{{FieldModifier}}std::array<{{TypeName}}, {{data.Length.ToStringInvariant()}}> entries = {
12+
{{FormatColumns(data, ToValueLabel)}}
1313
};
1414
1515
public:
@@ -18,7 +18,7 @@ public override string Generate() =>
1818
{
1919
{{EarlyExits}}
2020
21-
for ({{ArraySizeType}} i = 0; i < {{ctx.Data.Length.ToStringInvariant()}}; i++)
21+
for ({{ArraySizeType}} i = 0; i < {{data.Length.ToStringInvariant()}}; i++)
2222
{
2323
if ({{GetEqualFunction("entries[i]", "value")}})
2424
return true;

Src/FastData.Generator.CPlusPlus/Internal/Generators/BinarySearchCode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Genbox.FastData.Generator.CPlusPlus.Internal.Generators;
66

77
internal sealed class BinarySearchCode<T>(BinarySearchContext<T> ctx) : CPlusPlusOutputWriter<T>
88
{
9-
public override string Generate() =>
9+
public override string Generate(ReadOnlySpan<T> data) =>
1010
$$"""
1111
{{FieldModifier}}std::array<{{TypeName}}, {{ctx.Data.Length.ToStringInvariant()}}> entries = {
1212
{{FormatColumns(ctx.Data, ToValueLabel)}}

0 commit comments

Comments
 (0)