Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Dermody committed Dec 16, 2024
1 parent 8ffc803 commit a463213
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 17 deletions.
48 changes: 44 additions & 4 deletions BrightData/BrightData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -755,16 +755,39 @@
<param name="position"></param>
<returns></returns>
</member>
<member name="M:BrightData.Buffer.OffsetAndSize.Intersects(BrightData.Buffer.OffsetAndSize@)">
<member name="M:BrightData.Buffer.OffsetAndSize.Contains(BrightData.Buffer.OffsetAndSize@)">
<summary>
Checks if another range intersects
Checks if another range is contained within this range
</summary>
<param name="other"></param>
<returns></returns>
</member>
<member name="M:BrightData.Buffer.OffsetAndSize.CompareTo(BrightData.Buffer.OffsetAndSize)">
<inheritdoc />
</member>
<member name="M:BrightData.Buffer.OffsetAndSize.AsRange">
<summary>
Converts to a range
</summary>
<returns></returns>
</member>
<member name="F:BrightData.Buffer.OffsetAndSize.Null">
<summary>
Null indicator
</summary>
</member>
<member name="M:BrightData.Buffer.OffsetAndSize.IsNull">
<summary>
Checks if the offset is "null"
</summary>
<returns></returns>
</member>
<member name="M:BrightData.Buffer.OffsetAndSize.IsValid">
<summary>
Checks if the offset is valid - not "null"
</summary>
<returns></returns>
</member>
<member name="T:BrightData.Buffer.Operations.AggregateNotification">
<summary>
Notification from an aggregate operation
Expand Down Expand Up @@ -13520,11 +13543,12 @@
Binary vector - each bit represents an item in the vector
</summary>
</member>
<member name="M:BrightData.Types.BitVector.#ctor(System.UInt64[])">
<member name="M:BrightData.Types.BitVector.#ctor(System.UInt64[],System.UInt32)">
<summary>
Creates a fixed size bit vector from a data block
</summary>
<param name="data"></param>
<param name="size"></param>
</member>
<member name="M:BrightData.Types.BitVector.#ctor(System.UInt32)">
<summary>
Expand All @@ -13547,6 +13571,22 @@
<param name="bitIndex"></param>
<exception cref="T:System.ArgumentException"></exception>
</member>
<member name="M:BrightData.Types.BitVector.SetBits(System.Range)">
<summary>
Sets a range of bits
</summary>
<param name="range"></param>
</member>
<member name="P:BrightData.Types.BitVector.ContiguousRanges">
<summary>
Returns all contiguous ranges of set bits
</summary>
</member>
<member name="M:BrightData.Types.BitVector.Clear">
<summary>
Clears all bits
</summary>
</member>
<member name="M:BrightData.Types.BitVector.CountOfSetBits">
<summary>
Counts the bits that are true
Expand Down Expand Up @@ -13612,7 +13652,7 @@
</summary>
<param name="other"></param>
</member>
<member name="P:BrightData.Types.BitVector.DataAsBytes">
<member name="M:BrightData.Types.BitVector.ToString">
<inheritdoc />
</member>
<member name="T:BrightData.Types.FixedSizeSortedAscending1Array`2">
Expand Down
30 changes: 27 additions & 3 deletions BrightData/Buffer/OffsetAndSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,44 @@ public readonly record struct OffsetAndSize(uint StartOffset, uint Size) : IHave
public bool Intersects(uint position) => position >= StartOffset && position < EndOffset;

/// <summary>
/// Checks if another range intersects
/// Checks if another range is contained within this range
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Intersects(in OffsetAndSize other) => Intersects(other.StartOffset) || Intersects(other.EndOffset);
public bool Contains(in OffsetAndSize other) => Intersects(other.StartOffset) && Intersects(other.EndOffset);

/// <inheritdoc />
public int CompareTo(OffsetAndSize other)
{
var ret = StartOffset.CompareTo(other.StartOffset);
if(ret != 0) return ret;
if(ret != 0)
return ret;
return Size.CompareTo(other.Size);
}

/// <summary>
/// Converts to a range
/// </summary>
/// <returns></returns>
public Range AsRange() => new((int)StartOffset, (int)EndOffset);

/// <summary>
/// Null indicator
/// </summary>
public static readonly OffsetAndSize Null = new(uint.MaxValue, 0);

/// <summary>
/// Checks if the offset is "null"
/// </summary>
/// <returns></returns>
public bool IsNull() => StartOffset == uint.MaxValue && Size is 0 or uint.MaxValue;

/// <summary>
/// Checks if the offset is valid - not "null"
/// </summary>
/// <returns></returns>
public bool IsValid() => !IsNull();

uint IHaveOffset.Offset => StartOffset;
}
}
47 changes: 47 additions & 0 deletions BrightData/Helper/StringTables/FrozenDictionaryStringIndexer.cs
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();
}
}
12 changes: 2 additions & 10 deletions BrightData/Helper/StringTables/StringTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,19 @@ public async Task<IIndexStrings> GetStringIndexer(StringIndexType type = StringI
{
var span = _stringTable.Span;
var dataSpan = _stringData.Span;
using var buffer = SpanOwner<char>.Allocate(maxStringSize);
switch (type) {
case StringIndexType.Dictionary: {
var ret = new DictionaryStringIndexer();
for (var i = 0U; i < Size; i++) {
var utf8 = span[(int)i].GetSpan(dataSpan);
var str = Encoding.UTF8.GetString(utf8);
if (ret.GetIndex(str) != i)
throw new Exception("Indices did not align");
}
return ret;
return new FrozenDictionaryStringIndexer(span, dataSpan, maxStringSize);
}
case StringIndexType.Trie: {
using var buffer = SpanOwner<char>.Allocate(maxStringSize);
var bufferSpan = buffer.Span;
var trieBuilder = new UniqueIndexedStringTrie<char>.Builder();
for (var i = 0U; i < Size; i++) {
var utf8 = span[(int)i].GetSpan(dataSpan);
var bufferSize = Encoding.UTF8.GetChars(utf8, bufferSpan);
trieBuilder.Add(bufferSpan[..bufferSize], i);
}

return new TrieStringIndexer(trieBuilder.Build(), this);
}
default:
Expand Down
2 changes: 2 additions & 0 deletions BrightData/Types/BitVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,13 @@ public uint HammingDistance(BitVector other)
/// </summary>
/// <param name="other"></param>
public void XorInPlace(in BitVector other) => Xor(Data.AsSpan(), other.AsSpan());

/// <summary>
///
/// </summary>
/// <param name="other"></param>
public void UnionInPlace(in BitVector other) => Or(Data.AsSpan(), other.AsSpan());

/// <summary>
///
/// </summary>
Expand Down

0 comments on commit a463213

Please sign in to comment.