-
Notifications
You must be signed in to change notification settings - Fork 19
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
Jack Dermody
committed
Dec 16, 2024
1 parent
8ffc803
commit a463213
Showing
5 changed files
with
122 additions
and
17 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
BrightData/Helper/StringTables/FrozenDictionaryStringIndexer.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,47 @@ | ||
using System; | ||
using System.Collections.Frozen; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using BrightData.Buffer; | ||
|
||
namespace BrightData.Helper.StringTables | ||
{ | ||
internal class FrozenDictionaryStringIndexer : IIndexStrings | ||
{ | ||
readonly FrozenDictionary<string, uint> _data; | ||
|
||
[SkipLocalsInit] | ||
public FrozenDictionaryStringIndexer(ReadOnlySpan<OffsetAndSize> stringPointers, ReadOnlySpan<byte> utf8Data, int maxStringSize) | ||
{ | ||
Span<char> stringBuffer = stackalloc char[maxStringSize]; | ||
var buffer = new KeyValuePair<string, uint>[stringPointers.Length]; | ||
var index = 0U; | ||
foreach (ref readonly var str in stringPointers) { | ||
var utf8 = str.GetSpan(utf8Data); | ||
var bufferSize = Encoding.UTF8.GetChars(utf8, stringBuffer); | ||
buffer[index] = new(new(stringBuffer[..bufferSize]), index); | ||
++index; | ||
} | ||
_data = buffer.ToFrozenDictionary(); | ||
} | ||
|
||
public uint Size => (uint)_data.Count; | ||
|
||
[SkipLocalsInit] | ||
public uint GetIndex(ReadOnlySpan<char> str) | ||
{ | ||
Span<char> temp = stackalloc char[str.Length]; | ||
var size = str.Trim().ToLowerInvariant(temp); | ||
var lowerStr = temp[..size].AsReadOnly(); | ||
|
||
var lookup = _data.GetAlternateLookup<ReadOnlySpan<char>>(); | ||
if (lookup.TryGetValue(lowerStr, out var ret)) | ||
return ret; | ||
return uint.MaxValue; | ||
} | ||
|
||
public IEnumerable<string> OrderedStrings => _data.Keys.Order(); | ||
} | ||
} |
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