Skip to content

Commit dd1f7a5

Browse files
committed
Convert to span API
1 parent 181e21a commit dd1f7a5

File tree

94 files changed

+738
-668
lines changed

Some content is hidden

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

94 files changed

+738
-668
lines changed

Src/FastData.Cli.Tests/CommandOutputs/cpp_-k UInt8_Files_Integers.input.verified.txt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
{
22
Item1:
33
// This file is auto-generated. Do not edit manually.
4-
// Structure: Auto (BitSet)
4+
// Structure: Auto (Conditional)
55
#pragma once
66
#include <array>
77
#include <cstdint>
88
#include <limits>
99
#include <string_view>
1010

1111
class MyData final {
12-
static constexpr std::array<uint64_t, 1> bitset = {
13-
288230651030667265ull
14-
};
1512
public:
1613
[[nodiscard]]
1714
static constexpr bool contains(const uint8_t key) noexcept {
1815
if (key < 42 || key > 100)
1916
return false;
17+
if (key == 42 || key == 62 || key == 80 || key == 100)
18+
return true;
2019

21-
const uint64_t offset = static_cast<uint64_t>(key - min_key);
22-
const size_t word = static_cast<size_t>(offset >> 6);
23-
return (bitset[word] & (1ULL << (offset & 63))) != 0;
20+
return false;
2421
}
2522

2623
static constexpr size_t item_count = 4;

Src/FastData.Cli.Tests/CommandOutputs/csharp_-k UInt8_Files_Integers.input.verified.txt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,30 @@
22
Item1:
33
// <auto-generated />
44
// This file is auto-generated. Do not edit manually.
5-
// Structure: Auto (BitSet)
5+
// Structure: Auto (Conditional)
66
#nullable enable
77
using System;
88
using System.Runtime.CompilerServices;
99
using System.Runtime.InteropServices;
1010

1111
internal static class MyData
1212
{
13-
private static readonly ulong[] _bitset = new ulong[] {
14-
288230651030667265ul
15-
};
1613

1714
public static bool Contains(byte key)
1815
{
1916
if (key < 42 || key > 100)
2017
return false;
2118

22-
ulong offset = (ulong)(key - MinKey);
23-
int word = (int)(offset >> 6);
24-
return (_bitset[word] & (1UL << (int)(offset & 63))) != 0;
19+
switch (key)
20+
{
21+
case 42:
22+
case 62:
23+
case 80:
24+
case 100:
25+
return true;
26+
default:
27+
return false;
28+
}
2529
}
2630

2731
public const uint ItemCount = 4;

Src/FastData.Cli.Tests/CommandOutputs/rust_-k UInt8_Files_Integers.input.verified.txt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
Item1:
33
//! This file is auto-generated. Do not edit manually.
4-
//! Structure: Auto (BitSet)
4+
//! Structure: Auto (Conditional)
55
#![allow(unused_parens)]
66
#![allow(missing_docs)]
77
#![allow(unused_imports)]
@@ -11,19 +11,17 @@ use std::ptr;
1111
pub struct MyData;
1212

1313
impl MyData {
14-
const BITSET: [u64; 1] = [
15-
288230651030667265
16-
];
1714
#[must_use]
1815
pub fn contains(key: u8) -> bool {
1916
if key < 42 || key > 100 {
2017
return false;
2118
}
2219

23-
let offset = ((key as u64) - (Self::MIN_KEY as u64)) as usize;
24-
let word = offset >> 6;
25-
let mask = 1u64 << ((offset & 63) as u32);
26-
(Self::BITSET[word] & mask) != 0
20+
if key == 42 || key == 62 || key == 80 || key == 100 {
21+
return true;
22+
}
23+
24+
false
2725
}
2826

2927
pub const ITEM_COUNT: usize = 4;

Src/FastData.Generator.CPlusPlus.Tests/Vectors/RangeStructure_Int32_5_range_bitset.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class RangeStructure_Int32_5_range_bitset final {
1010
public:
1111
[[nodiscard]]
1212
static constexpr bool contains(const int32_t key) noexcept {
13-
return key >= min_key && key <= max_key;
13+
return key >= 1 && key <= 5;
1414
}
1515

1616
static constexpr size_t item_count = 5;

Src/FastData.Generator.CPlusPlus/CPlusPlusCodeGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected override void AppendFooter<T>(StringBuilder sb, GeneratorConfig<T> gen
6464
protected override OutputWriter<TKey>? GetOutputWriter<TKey, TValue>(GeneratorConfig<TKey> genCfg, IContext<TValue> context) => context switch
6565
{
6666
SingleValueContext<TKey, TValue> x => new SingleValueCode<TKey, TValue>(x, Shared),
67-
RangeContext<TKey, TValue> x => new RangeCode<TKey, TValue>(x, Shared),
67+
RangeContext<TKey, TValue> x => new RangeCode<TKey, TValue>(x),
6868
BitSetContext<TKey, TValue> x => new BitSetCode<TKey, TValue>(x, Shared),
6969
ArrayContext<TKey, TValue> x => new ArrayCode<TKey, TValue>(x, Shared),
7070
BinarySearchContext<TKey, TValue> x => new BinarySearchCode<TKey, TValue>(x, Shared),
@@ -75,4 +75,4 @@ protected override void AppendFooter<T>(StringBuilder sb, GeneratorConfig<T> gen
7575
KeyLengthContext<TValue> x => new KeyLengthCode<TKey, TValue>(x, Shared),
7676
_ => null
7777
};
78-
}
78+
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,30 @@ public override string Generate()
1111
{
1212
bool customValue = !typeof(TValue).IsPrimitive;
1313
StringBuilder sb = new StringBuilder();
14+
ReadOnlySpan<TKey> keys = ctx.Keys.Span;
1415

15-
if (ctx.Values != null)
16+
if (!ctx.Values.IsEmpty)
1617
{
18+
ReadOnlySpan<TValue> values = ctx.Values.Span;
1719
sb.Append($$"""
18-
{{GetFieldModifier(false)}}std::array<{{GetValueTypeName(customValue)}}, {{ctx.Values.Length.ToStringInvariant()}}> values = {
19-
{{FormatColumns(ctx.Values, ToValueLabel)}}
20+
{{GetFieldModifier(false)}}std::array<{{GetValueTypeName(customValue)}}, {{values.Length.ToStringInvariant()}}> values = {
21+
{{FormatColumns(values, ToValueLabel)}}
2022
};
2123
2224
""");
2325
}
2426

2527
sb.Append($$"""
26-
{{GetFieldModifier(true)}}std::array<{{KeyTypeName}}, {{ctx.Keys.Length.ToStringInvariant()}}> keys = {
27-
{{FormatColumns(ctx.Keys, ToValueLabel)}}
28+
{{GetFieldModifier(true)}}std::array<{{KeyTypeName}}, {{keys.Length.ToStringInvariant()}}> keys = {
29+
{{FormatColumns(keys, ToValueLabel)}}
2830
};
2931
3032
public:
3133
{{MethodAttribute}}
3234
{{GetMethodModifier(true)}}bool contains(const {{KeyTypeName}} key){{PostMethodModifier}} {
3335
{{GetMethodHeader(MethodType.Contains)}}
3436
35-
for ({{ArraySizeType}} i = 0; i < {{ctx.Keys.Length.ToStringInvariant()}}; i++)
37+
for ({{ArraySizeType}} i = 0; i < {{keys.Length.ToStringInvariant()}}; i++)
3638
{
3739
if ({{GetEqualFunction("keys[i]", LookupKeyName)}})
3840
return true;
@@ -41,7 +43,7 @@ public override string Generate()
4143
}
4244
""");
4345

44-
if (ctx.Values != null)
46+
if (!ctx.Values.IsEmpty)
4547
{
4648
string ptr = customValue ? "" : "&";
4749
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
@@ -52,7 +54,7 @@ public override string Generate()
5254
{{GetMethodModifier(false)}}bool try_lookup(const {{KeyTypeName}} key, const {{ValueTypeName}}*& value){{PostMethodModifier}} {
5355
{{GetMethodHeader(MethodType.TryLookup)}}
5456
55-
for ({{ArraySizeType}} i = 0; i < {{ctx.Keys.Length.ToStringInvariant()}}; i++) {
57+
for ({{ArraySizeType}} i = 0; i < {{keys.Length.ToStringInvariant()}}; i++) {
5658
if ({{GetEqualFunction("keys[i]", LookupKeyName)}}) {
5759
value = {{ptr}}values[i];
5860
return true;

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ public override string Generate()
1111
{
1212
bool customValue = !typeof(TValue).IsPrimitive;
1313
StringBuilder sb = new StringBuilder();
14+
ReadOnlySpan<TKey> keys = ctx.Keys.Span;
1415

15-
if (ctx.Values != null)
16+
if (!ctx.Values.IsEmpty)
1617
{
18+
ReadOnlySpan<TValue> values = ctx.Values.Span;
1719
sb.Append($$"""
18-
{{GetFieldModifier(false)}}std::array<{{GetValueTypeName(customValue)}}, {{ctx.Values.Length.ToStringInvariant()}}> values = {
19-
{{FormatColumns(ctx.Values, ToValueLabel)}}
20+
{{GetFieldModifier(false)}}std::array<{{GetValueTypeName(customValue)}}, {{values.Length.ToStringInvariant()}}> values = {
21+
{{FormatColumns(values, ToValueLabel)}}
2022
};
2123
2224
""");
2325
}
2426

2527
sb.Append($$"""
26-
{{GetFieldModifier(true)}}std::array<{{KeyTypeName}}, {{ctx.Keys.Length.ToStringInvariant()}}> keys = {
27-
{{FormatColumns(ctx.Keys, ToValueLabel)}}
28+
{{GetFieldModifier(true)}}std::array<{{KeyTypeName}}, {{keys.Length.ToStringInvariant()}}> keys = {
29+
{{FormatColumns(keys, ToValueLabel)}}
2830
};
2931
3032
public:
@@ -33,7 +35,7 @@ public override string Generate()
3335
{{GetMethodHeader(MethodType.Contains)}}
3436
3537
int32_t lo = 0;
36-
int32_t hi = {{(ctx.Keys.Length - 1).ToStringInvariant()}};
38+
int32_t hi = {{(keys.Length - 1).ToStringInvariant()}};
3739
while (lo <= hi) {
3840
const int32_t mid = lo + ((hi - lo) >> 1);
3941
const {{KeyTypeName}} mid_key = keys[mid];
@@ -51,7 +53,7 @@ public override string Generate()
5153
}
5254
""");
5355

54-
if (ctx.Values != null)
56+
if (!ctx.Values.IsEmpty)
5557
{
5658
string ptr = customValue ? "" : "&";
5759
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
@@ -63,7 +65,7 @@ public override string Generate()
6365
{{GetMethodHeader(MethodType.TryLookup)}}
6466
6567
int32_t lo = 0;
66-
int32_t hi = {{(ctx.Keys.Length - 1).ToStringInvariant()}};
68+
int32_t hi = {{(keys.Length - 1).ToStringInvariant()}};
6769
while (lo <= hi) {
6870
const int32_t mid = lo + ((hi - lo) >> 1);
6971
const {{KeyTypeName}} mid_key = keys[mid];

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ public override string Generate()
1919
2020
""");
2121

22-
if (ctx.Values != null)
22+
if (!ctx.Values.IsEmpty)
2323
{
24+
ReadOnlySpan<TValue> values = ctx.Values.Span;
2425
sb.Append($$"""
25-
{{GetFieldModifier(false)}}std::array<{{GetValueTypeName(customValue)}}, {{ctx.Values.Length.ToStringInvariant()}}> values = {
26-
{{FormatColumns(ctx.Values, ToValueLabel)}}
26+
{{GetFieldModifier(false)}}std::array<{{GetValueTypeName(customValue)}}, {{values.Length.ToStringInvariant()}}> values = {
27+
{{FormatColumns(values, ToValueLabel)}}
2728
};
2829
2930
""");
@@ -41,7 +42,7 @@ public override string Generate()
4142
}
4243
""");
4344

44-
if (ctx.Values != null)
45+
if (!ctx.Values.IsEmpty)
4546
{
4647
string ptr = customValue ? "" : "&";
4748
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
@@ -68,4 +69,4 @@ public override string Generate()
6869

6970
return sb.ToString();
7071
}
71-
}
72+
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ public override string Generate()
1111
{
1212
bool customValue = !typeof(TValue).IsPrimitive;
1313
StringBuilder sb = new StringBuilder();
14+
ReadOnlySpan<TKey> keys = ctx.Keys.Span;
15+
ReadOnlySpan<TValue> values = ctx.Values.Span;
1416

15-
if (ctx.Values != null)
17+
if (!values.IsEmpty)
1618
{
1719
sb.Append($$"""
18-
{{GetFieldModifier(false)}}std::array<{{GetValueTypeName(customValue)}}, {{ctx.Values.Length.ToStringInvariant()}}> values = {
19-
{{FormatColumns(ctx.Values, ToValueLabel)}}
20+
{{GetFieldModifier(false)}}std::array<{{GetValueTypeName(customValue)}}, {{values.Length.ToStringInvariant()}}> values = {
21+
{{FormatColumns(values, ToValueLabel)}}
2022
};
2123
2224
""");
@@ -27,14 +29,14 @@ public override string Generate()
2729
{{MethodAttribute}}
2830
{{GetMethodModifier(true)}}bool contains(const {{KeyTypeName}} key){{PostMethodModifier}} {
2931
{{GetMethodHeader(MethodType.Contains)}}
30-
if ({{FormatList(ctx.Keys, x => GetEqualFunction(LookupKeyName, ToValueLabel(x)), " || ")}})
32+
if ({{FormatList(keys, x => GetEqualFunction(LookupKeyName, ToValueLabel(x)), " || ")}})
3133
return true;
3234
3335
return false;
3436
}
3537
""");
3638

37-
if (ctx.Values != null)
39+
if (!values.IsEmpty)
3840
{
3941
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
4042

@@ -43,21 +45,21 @@ public override string Generate()
4345
{{MethodAttribute}}
4446
{{GetMethodModifier(false)}}bool try_lookup(const {{KeyTypeName}} key, const {{ValueTypeName}}*& value){{PostMethodModifier}} {
4547
{{GetMethodHeader(MethodType.TryLookup)}}
46-
{{GenerateBranches()}}
48+
{{GenerateBranches(keys)}}
4749
value = nullptr;
4850
return false;
4951
}
5052
""");
5153

52-
string GenerateBranches()
54+
string GenerateBranches(ReadOnlySpan<TKey> data)
5355
{
5456
string ptr = customValue ? "" : "&";
5557
StringBuilder temp = new StringBuilder();
5658

57-
for (int i = 0; i < ctx.Keys.Length; i++)
59+
for (int i = 0; i < data.Length; i++)
5860
{
5961
temp.AppendLine($$"""
60-
if ({{GetEqualFunction(LookupKeyName, ToValueLabel(ctx.Keys[i]))}}) {
62+
if ({{GetEqualFunction(LookupKeyName, ToValueLabel(data[i]))}}) {
6163
value = {{ptr}}values[{{i.ToStringInvariant()}}];
6264
return true;
6365
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,24 @@ public override string Generate()
1111
{
1212
bool customValue = !typeof(TValue).IsPrimitive;
1313
StringBuilder sb = new StringBuilder();
14+
ReadOnlyMemory<TValue> values = ctx.Values;
1415

1516
sb.Append($$"""
1617
struct e {
1718
{{KeyTypeName}} key;
1819
{{GetSmallestSignedType(ctx.Buckets.Length)}} next;
1920
{{(ctx.StoreHashCode ? $"{HashSizeType} hash_code;" : "")}}
20-
{{(ctx.Values != null ? $"const {GetValueTypeName(customValue)} value;" : "")}}
21-
e(const {{KeyTypeName}} key, const {{GetSmallestSignedType(ctx.Buckets.Length)}} next{{(ctx.StoreHashCode ? $", const {HashSizeType} hash_code" : "")}}{{(ctx.Values != null ? $", const {GetValueTypeName(customValue)} value" : "")}})
22-
: key(key), next(next){{(ctx.StoreHashCode ? ", hash_code(hash_code)" : "")}}{{(ctx.Values != null ? ", value(value)" : "")}} {}
21+
{{(ctx.Values.IsEmpty ? "" : $"const {GetValueTypeName(customValue)} value;")}}
22+
e(const {{KeyTypeName}} key, const {{GetSmallestSignedType(ctx.Buckets.Length)}} next{{(ctx.StoreHashCode ? $", const {HashSizeType} hash_code" : "")}}{{(!ctx.Values.IsEmpty ? $", const {GetValueTypeName(customValue)} value" : "")}})
23+
: key(key), next(next){{(ctx.StoreHashCode ? ", hash_code(hash_code)" : "")}}{{(ctx.Values.IsEmpty ? "" : ", value(value)")}} {}
2324
};
2425
2526
{{GetFieldModifier(true)}}std::array<{{GetSmallestSignedType(ctx.Buckets.Length)}}, {{ctx.Buckets.Length.ToStringInvariant()}}> buckets = {
2627
{{FormatColumns(ctx.Buckets, static x => x.ToStringInvariant())}}
2728
};
2829
2930
{{GetFieldModifier(false)}}std::array<e, {{ctx.Entries.Length.ToStringInvariant()}}> entries = {
30-
{{FormatColumns(ctx.Entries, (i, x) => $"e({ToValueLabel(x.Key)}, {x.Next.ToStringInvariant()}{(ctx.StoreHashCode ? $", {x.Hash.ToStringInvariant()}" : "")}{(ctx.Values != null ? $", {ToValueLabel(ctx.Values[i])}" : "")})")}}
31+
{{FormatColumns(ctx.Entries, (i, x) => $"e({ToValueLabel(x.Key)}, {x.Next.ToStringInvariant()}{(ctx.StoreHashCode ? $", {x.Hash.ToStringInvariant()}" : "")}{(!ctx.Values.IsEmpty ? $", {ToValueLabel(values.Span[i])}" : "")})")}}
3132
};
3233
3334
{{HashSource}}
@@ -54,7 +55,7 @@ struct e {
5455
}
5556
""");
5657

57-
if (ctx.Values != null)
58+
if (!ctx.Values.IsEmpty)
5859
{
5960
string ptr = customValue ? "" : "&";
6061
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());

0 commit comments

Comments
 (0)