|
| 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 | +} |
0 commit comments