Skip to content

Commit 5c7741d

Browse files
committed
Add support for BitSetStructure
1 parent 4c9f0b0 commit 5c7741d

File tree

20 files changed

+569
-22
lines changed

20 files changed

+569
-22
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
{
22
Item1:
33
// This file is auto-generated. Do not edit manually.
4-
// Structure: Auto (Conditional)
4+
// Structure: Auto (BitSet)
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+
};
1215
public:
1316
[[nodiscard]]
1417
static constexpr bool contains(const uint8_t key) noexcept {
1518
if (key < 42 || key > 100)
1619
return false;
17-
if (key == 42 || key == 62 || key == 80 || key == 100)
18-
return true;
1920

20-
return false;
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;
2124
}
2225

2326
static constexpr size_t item_count = 4;

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,26 @@
22
Item1:
33
// <auto-generated />
44
// This file is auto-generated. Do not edit manually.
5-
// Structure: Auto (Conditional)
5+
// Structure: Auto (BitSet)
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+
};
1316

1417
public static bool Contains(byte key)
1518
{
1619
if (key < 42 || key > 100)
1720
return false;
1821

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-
}
22+
ulong offset = (ulong)(key - MinKey);
23+
int word = (int)(offset >> 6);
24+
return (_bitset[word] & (1UL << (int)(offset & 63))) != 0;
2925
}
3026

3127
public const uint ItemCount = 4;

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

Lines changed: 8 additions & 6 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 (Conditional)
4+
//! Structure: Auto (BitSet)
55
#![allow(unused_parens)]
66
#![allow(missing_docs)]
77
#![allow(unused_imports)]
@@ -11,17 +11,19 @@ use std::ptr;
1111
pub struct MyData;
1212

1313
impl MyData {
14+
const BITSET: [u64; 1] = [
15+
288230651030667265
16+
];
1417
#[must_use]
1518
pub fn contains(key: u8) -> bool {
1619
if key < 42 || key > 100 {
1720
return false;
1821
}
1922

20-
if key == 42 || key == 62 || key == 80 || key == 100 {
21-
return true;
22-
}
23-
24-
false
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
2527
}
2628

2729
pub const ITEM_COUNT: usize = 4;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// This file is auto-generated. Do not edit manually.
2+
// Structure: Auto (BitSet)
3+
#pragma once
4+
#include <array>
5+
#include <cstdint>
6+
#include <limits>
7+
#include <string_view>
8+
9+
struct Person {
10+
int32_t age;
11+
std::u32string_view name;
12+
const Person* other;
13+
14+
constexpr Person(const int32_t age, const std::u32string_view name, const Person* other) noexcept : age(age), name(name), other(other) { }
15+
};
16+
class BitSetStructure_Int32_3_complex final {
17+
static constexpr std::array<uint64_t, 1> bitset = {
18+
7ull
19+
};
20+
inline static const std::array<Person*, 3> values = {
21+
new Person(1, U"Bob", new Person(4, U"Anna", nullptr)), new Person(2, U"Billy", nullptr), new Person(3, U"Bibi", nullptr)
22+
};
23+
public:
24+
[[nodiscard]]
25+
static constexpr bool contains(const int32_t key) noexcept {
26+
if (key < 1 || key > 3)
27+
return false;
28+
29+
const uint64_t offset = static_cast<uint64_t>(key - min_key);
30+
const size_t word = static_cast<size_t>(offset >> 6);
31+
return (bitset[word] & (1ULL << (offset & 63))) != 0;
32+
}
33+
[[nodiscard]]
34+
static bool try_lookup(const int32_t key, const Person*& value) noexcept {
35+
if (key < 1 || key > 3)
36+
return false;
37+
38+
const uint64_t offset = static_cast<uint64_t>(key - min_key);
39+
const size_t word = static_cast<size_t>(offset >> 6);
40+
if ((bitset[word] & (1ULL << (offset & 63))) == 0)
41+
{
42+
value = nullptr;
43+
return false;
44+
}
45+
46+
value = values[static_cast<size_t>(offset)];
47+
return true;
48+
}
49+
50+
static constexpr size_t item_count = 3;
51+
static constexpr int32_t min_key = 1;
52+
static constexpr int32_t max_key = 3;
53+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This file is auto-generated. Do not edit manually.
2+
// Structure: Auto (BitSet)
3+
#pragma once
4+
#include <array>
5+
#include <cstdint>
6+
#include <limits>
7+
#include <string_view>
8+
9+
class BitSetStructure_Int32_5 final {
10+
static constexpr std::array<uint64_t, 1> bitset = {
11+
31ull
12+
};
13+
public:
14+
[[nodiscard]]
15+
static constexpr bool contains(const int32_t key) noexcept {
16+
if (key < 1 || key > 5)
17+
return false;
18+
19+
const uint64_t offset = static_cast<uint64_t>(key - min_key);
20+
const size_t word = static_cast<size_t>(offset >> 6);
21+
return (bitset[word] & (1ULL << (offset & 63))) != 0;
22+
}
23+
24+
static constexpr size_t item_count = 5;
25+
static constexpr int32_t min_key = 1;
26+
static constexpr int32_t max_key = 5;
27+
};

Src/FastData.Generator.CPlusPlus/CPlusPlusCodeGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected override void AppendFooter<T>(StringBuilder sb, GeneratorConfig<T> gen
6565
{
6666
SingleValueContext<TKey, TValue> x => new SingleValueCode<TKey, TValue>(x, Shared),
6767
RangeContext<TKey, TValue> x => new RangeCode<TKey, TValue>(x, Shared),
68+
BitSetContext<TKey, TValue> x => new BitSetCode<TKey, TValue>(x, Shared),
6869
ArrayContext<TKey, TValue> x => new ArrayCode<TKey, TValue>(x, Shared),
6970
BinarySearchContext<TKey, TValue> x => new BinarySearchCode<TKey, TValue>(x, Shared),
7071
ConditionalContext<TKey, TValue> x => new ConditionalCode<TKey, TValue>(x, Shared),
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Genbox.FastData.Generator.CPlusPlus.Internal.Framework;
2+
using Genbox.FastData.Generator.Enums;
3+
using Genbox.FastData.Generator.Extensions;
4+
using Genbox.FastData.Generators.Contexts;
5+
6+
namespace Genbox.FastData.Generator.CPlusPlus.Internal.Generators;
7+
8+
internal sealed class BitSetCode<TKey, TValue>(BitSetContext<TKey, TValue> ctx, SharedCode shared) : CPlusPlusOutputWriter<TKey>
9+
{
10+
public override string Generate()
11+
{
12+
bool customValue = !typeof(TValue).IsPrimitive;
13+
StringBuilder sb = new StringBuilder();
14+
15+
sb.Append($$"""
16+
{{GetFieldModifier(true)}}std::array<uint64_t, {{ctx.BitSet.Length.ToStringInvariant()}}> bitset = {
17+
{{FormatColumns(ctx.BitSet, ToValueLabel)}}
18+
};
19+
20+
""");
21+
22+
if (ctx.Values != null)
23+
{
24+
sb.Append($$"""
25+
{{GetFieldModifier(false)}}std::array<{{GetValueTypeName(customValue)}}, {{ctx.Values.Length.ToStringInvariant()}}> values = {
26+
{{FormatColumns(ctx.Values, ToValueLabel)}}
27+
};
28+
29+
""");
30+
}
31+
32+
sb.Append($$"""
33+
public:
34+
{{MethodAttribute}}
35+
{{GetMethodModifier(true)}}bool contains(const {{KeyTypeName}} key){{PostMethodModifier}} {
36+
{{GetEarlyExits(MethodType.Contains)}}
37+
38+
const uint64_t offset = static_cast<uint64_t>(key - min_key);
39+
const size_t word = static_cast<size_t>(offset >> 6);
40+
return (bitset[word] & (1ULL << (offset & 63))) != 0;
41+
}
42+
""");
43+
44+
if (ctx.Values != null)
45+
{
46+
string ptr = customValue ? "" : "&";
47+
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
48+
49+
sb.Append($$"""
50+
51+
{{MethodAttribute}}
52+
{{GetMethodModifier(false)}}bool try_lookup(const {{KeyTypeName}} key, const {{ValueTypeName}}*& value){{PostMethodModifier}} {
53+
{{GetEarlyExits(MethodType.TryLookup)}}
54+
55+
const uint64_t offset = static_cast<uint64_t>(key - min_key);
56+
const size_t word = static_cast<size_t>(offset >> 6);
57+
if ((bitset[word] & (1ULL << (offset & 63))) == 0)
58+
{
59+
value = nullptr;
60+
return false;
61+
}
62+
63+
value = {{ptr}}values[static_cast<size_t>(offset)];
64+
return true;
65+
}
66+
""");
67+
}
68+
69+
return sb.ToString();
70+
}
71+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// <auto-generated />
2+
// This file is auto-generated. Do not edit manually.
3+
// Structure: Auto (BitSet)
4+
#nullable enable
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
public class Person
9+
{
10+
public Person(int age, string name, Person other)
11+
{
12+
Age = age;
13+
Name = name;
14+
Other = other;
15+
}
16+
int Age { get; set; }
17+
string Name { get; set; }
18+
Person Other { get; set; }
19+
20+
};
21+
22+
internal static class BitSetStructure_Int32_3_complex
23+
{
24+
private static readonly ulong[] _bitset = new ulong[] {
25+
7ul
26+
};
27+
private static readonly Person[] _values = {
28+
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
29+
};
30+
31+
public static bool Contains(int key)
32+
{
33+
if (key < 1 || key > 3)
34+
return false;
35+
36+
ulong offset = (ulong)(key - MinKey);
37+
int word = (int)(offset >> 6);
38+
return (_bitset[word] & (1UL << (int)(offset & 63))) != 0;
39+
}
40+
41+
public static bool TryLookup(int key, out Person value)
42+
{
43+
if (key < 1 || key > 3)
44+
{
45+
value = default;
46+
return false;
47+
}
48+
49+
ulong offset = (ulong)(key - MinKey);
50+
int word = (int)(offset >> 6);
51+
if ((_bitset[word] & (1UL << (int)(offset & 63))) == 0)
52+
{
53+
value = default;
54+
return false;
55+
}
56+
57+
value = _values[(int)offset];
58+
return true;
59+
}
60+
61+
public const uint ItemCount = 3;
62+
public const int MinKey = 1;
63+
public const int MaxKey = 3;
64+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// <auto-generated />
2+
// This file is auto-generated. Do not edit manually.
3+
// Structure: Auto (BitSet)
4+
#nullable enable
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
9+
internal static class BitSetStructure_Int32_5
10+
{
11+
private static readonly ulong[] _bitset = new ulong[] {
12+
31ul
13+
};
14+
15+
public static bool Contains(int key)
16+
{
17+
if (key < 1 || key > 5)
18+
return false;
19+
20+
ulong offset = (ulong)(key - MinKey);
21+
int word = (int)(offset >> 6);
22+
return (_bitset[word] & (1UL << (int)(offset & 63))) != 0;
23+
}
24+
25+
public const uint ItemCount = 5;
26+
public const int MinKey = 1;
27+
public const int MaxKey = 5;
28+
}

Src/FastData.Generator.CSharp/CSharpCodeGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ protected override void AppendFooter<T>(StringBuilder sb, GeneratorConfig<T> gen
7676
{
7777
SingleValueContext<TKey, TValue> x => new SingleValueCode<TKey, TValue>(x, _cfg, Shared),
7878
RangeContext<TKey, TValue> x => new RangeCode<TKey, TValue>(x, _cfg, Shared),
79+
BitSetContext<TKey, TValue> x => new BitSetCode<TKey, TValue>(x, _cfg, Shared),
7980
ArrayContext<TKey, TValue> x => new ArrayCode<TKey, TValue>(x, _cfg, Shared),
8081
BinarySearchContext<TKey, TValue> x => new BinarySearchCode<TKey, TValue>(x, _cfg, Shared),
8182
ConditionalContext<TKey, TValue> x => new ConditionalCode<TKey, TValue>(x, _cfg, Shared),

0 commit comments

Comments
 (0)